Learn how to set global styles in CSS to ensure visual consistency across your web page. Explore the importance of base styles, CSS resets, and normalization techniques.
Creating a visually appealing and consistent web page involves more than just adding colors and fonts. It starts with setting a solid foundation through global styles. In this section, we’ll explore how to define default styles that apply to the entire web page, ensuring a cohesive look and feel. We’ll discuss the importance of setting base styles for the body
element, the concept of CSS resets or normalization, and how these practices contribute to the overall design consistency of your site.
Global styles are CSS rules that apply universally across your web page. They provide a consistent starting point for styling by setting default properties such as fonts, colors, and spacing. By defining these styles, you ensure that all elements on your page adhere to a unified design language, reducing the need for repetitive styling and minimizing inconsistencies.
body
ElementThe body
element is the root of your web page’s content. Setting base styles for the body
ensures that all text and elements inherit these properties unless otherwise specified. Let’s start by defining some fundamental styles for the body
element:
body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
background-color: #f5f5f5;
color: #333;
}
margin: 0;
and padding: 0;
, we eliminate any default spacing that browsers might apply, giving us a clean slate to work with.Arial
, ensures text consistency across the page. The sans-serif
fallback provides a similar font if Arial
is unavailable.#f5f5f5
) offers a neutral canvas that enhances readability and aesthetics.#333
) ensures good contrast against the background, improving legibility.Different browsers have varying default styles for HTML elements, which can lead to inconsistencies in how your web page appears. CSS resets and normalization techniques help address these discrepancies.
A CSS reset removes all default styling applied by browsers, providing a blank canvas for your custom styles. One popular reset is Eric Meyer’s reset:
/* CSS Reset by Eric Meyer */
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
Normalization, on the other hand, aims to make built-in browser styles consistent across different browsers. It preserves useful default styles rather than removing them entirely. Normalize.css is a widely used library for this purpose.
/*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */
/* Document
========================================================================== */
/**
* 1. Correct the line height in all browsers.
* 2. Prevent adjustments of font size after orientation changes in iOS.
*/
html {
line-height: 1.15; /* 1 */
-webkit-text-size-adjust: 100%; /* 2 */
}
/* Sections
========================================================================== */
/**
* Remove the margin in all browsers.
*/
body {
margin: 0;
}
/* Grouping content
========================================================================== */
/**
* 1. Add the correct box sizing in Firefox.
* 2. Show the overflow in Edge and IE.
*/
hr {
box-sizing: content-box; /* 1 */
height: 0; /* 1 */
overflow: visible; /* 2 */
}
/* Text-level semantics
========================================================================== */
/**
* Remove the gray background on active links in IE 10.
*/
a {
background-color: transparent;
}
/* Embedded content
========================================================================== */
/**
* 1. Remove the border on images inside links in IE 10.
* 2. Remove the border on images inside links in Edge 12-.
*/
img {
border-style: none; /* 1 */
}
Global styles offer several advantages that enhance the overall quality of your web page:
When setting global styles, consider the overall aesthetic and functionality of your web page. Here are some tips to guide your choices:
Let’s put these concepts into practice. Create a simple HTML file and apply the following global styles. Experiment with different fonts, colors, and spacing to see how they affect the overall appearance of your page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Global Styles Example</title>
<style>
/* Global Styles */
body {
margin: 0;
padding: 0;
font-family: 'Roboto', sans-serif;
background-color: #e0f7fa;
color: #004d40;
}
h1, h2, h3, h4, h5, h6 {
font-weight: normal;
color: #00796b;
}
p {
line-height: 1.6;
margin-bottom: 1em;
}
a {
color: #00796b;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>Welcome to My Web Page</h1>
<p>This is a simple example of setting global styles using CSS. Feel free to modify the styles and see the changes in real-time.</p>
<a href="#">Learn more</a>
</body>
</html>
To better understand how global styles fit into the larger picture of CSS, let’s visualize the CSS cascade using a Mermaid.js diagram:
graph TD; A[CSS Cascade] --> B[Browser Defaults] A --> C[Global Styles] A --> D[Component Styles] A --> E[Inline Styles] B --> F[Final Rendered Style] C --> F D --> F E --> F
Diagram Explanation: The CSS cascade determines which styles are applied to an element. Browser defaults are overridden by global styles, which can be further customized by component-specific styles and inline styles. The final rendered style is a combination of these layers.
For further reading on CSS resets and normalization, consider exploring the following resources:
To reinforce your understanding of global styles, try the following exercises:
font-family
property in the example above to see how different fonts impact the page’s appearance.background-color
and color
properties to create a new color scheme. Consider using a color palette generator for inspiration.By mastering global styles, you’re well on your way to creating visually consistent and professional-looking web pages. Keep experimenting and refining your styles to achieve the desired results.