I'm assuming that "global page styling" here refers to things such as fonts, colors and backgrounds.

Personally, I apply global page styling, for the most part, to body and the simple element selectors (p, h1, h2, h3..., input, img, etc). These elements are more closely related to the presentation of content of an HTML page to the user.

My rationale for this is simple: the presentational attributes bgcolor, background, text, topmargin, leftmargin and others were given to the body element, not the html element. These attributes are now converted to their respective CSS rules with extremely low precedence in the cascade:

The UA may choose to honor presentational attributes in an HTML source document. If so, these attributes are translated to the corresponding CSS rules with specificity equal to 0, and are treated as if they were inserted at the start of the author style sheet.

Most if not all implementations I'm aware of will convert these to CSS rules on body, based on their HTML equivalents. Others such as link, alink and vlink will become a:link, a:active and a:visited rules respectively.

Of course, it should be noted that CSS itself doesn't really have any semantics to it per se, as it's a styling language in itself which is completely separate from the content structure of an HTML document. Although the introduction to CSS2.1 covers the basics of styling an HTML document, note that the section calls itself non-normative (or informative); this means it doesn't set any hard and fast rules for CSS implementers to follow. Instead, it simply provides information for readers.

That said, certain styles may be applied to html to modify viewport behavior. For example, to hide the page scrollbars use:

html {
    overflow: hidden;
}

You can also apply rules to both html and body for interesting effects; see the following questions for details and examples:

  • What's the difference in applying CSS to html, body, and *?
  • Applying a background to <html> and/or <body>

Note that html is not the viewport; the viewport establishes an initial containing block in which html is situated. That initial containing block cannot be targeted with CSS, because in HTML, the root element is html.

Note also that, technically, there is no difference between applying properties to html and body that are inherited by default, such as font-family and color.

Last but not least, here is an excellent article that details the differences between html and body in terms of CSS. In summary (quoted from its first section):

  • The html and body elements are distinct block-level entities, in a parent/child relationship.
  • The html element's height and width are controlled by the browser window.
  • It is the html element which has (by default) overflow:auto, causing scrollbars to appear when needed.
  • The body element is (by default) position:static, which means that positioned children of it are positioned relative to the html element's coordinate system.
  • In almost all modern browsers, the built-in offset from the edge of the page is applied through a margin on the body element, not padding on the html element.

As the root element, html is more closely associated with the browser viewport than body (which is why it says html has overflow: auto for scrollbars). Note however that the scrollbars are not necessarily generated by the html element itself. By default, it's the viewport that generates these scrollbars; the values of overflow are simply transferred (or propagated) between body, html, and the viewport, depending on which values you set. The details of all this are covered in the CSS2.1 spec, which says:

UAs must apply the 'overflow' property set on the root element to the viewport. When the root element is an HTML "HTML" element or an XHTML "html" element, and that element has an HTML "BODY" element or an XHTML "body" element as a child, user agents must instead apply the 'overflow' property from the first such child element to the viewport, if the value on the root element is 'visible'. The 'visible' value when used for the viewport must be interpreted as 'auto'. The element from which the value is propagated must have a used value for 'overflow' of 'visible'.

The last bullet point probably has its roots in the aforementioned topmargin and leftmargin attributes of the body element.

Answer from BoltClock on Stack Overflow
🌐
Gatsby
gatsbyjs.com › documentation › how-to guides › styling › standard css
Standard Styling with Global CSS Files | Gatsby
Traditionally, websites are styled using global CSS files. Globally-scoped CSS rules are declared in external .css stylesheets, and CSS…
🌐
Every-layout
every-layout.dev › rudiments › global-and-local-styling
Global and local styling: Every Layout
CSS itself exists to enable the styling of HTML globally, and by category, rather than element-by-element. When used as intended, it is the most efficient way to create any kind of layout or aesthetic on the web.
Discussions

Do you have your own global.css file?
There are a ton of CSS philosophies, like BEM , Cube , utility classes , etc. Try out some approaches and stick with what feels good, also things might change when you work with a larger team. More on reddit.com
🌐 r/webdev
19
4
October 16, 2024
css - What does :global (colon global) do? - Stack Overflow
In some SCSS files, I see the following: :global { /* ... */ } I don't know if it is an SCSS feature or a CSS feature. I tried searching about it but couldn't find any good results at first sight. More on stackoverflow.com
🌐 stackoverflow.com
Creating CSS Global Variables : Stylesheet theme management - Stack Overflow
Is there a way to set global variables in css such as: @Color1 = #fff; @Color2 = #b00; h1 { color:@Color1; background:@Color2; } More on stackoverflow.com
🌐 stackoverflow.com
css - How to Apply global font to whole HTML document - Stack Overflow
I have a HTML page which includes some text and formatting. I want to make it have the same font-family and the same text-size ignoring all inner formatting of text. I want to set a global font fo... More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 2
38

I'm assuming that "global page styling" here refers to things such as fonts, colors and backgrounds.

Personally, I apply global page styling, for the most part, to body and the simple element selectors (p, h1, h2, h3..., input, img, etc). These elements are more closely related to the presentation of content of an HTML page to the user.

My rationale for this is simple: the presentational attributes bgcolor, background, text, topmargin, leftmargin and others were given to the body element, not the html element. These attributes are now converted to their respective CSS rules with extremely low precedence in the cascade:

The UA may choose to honor presentational attributes in an HTML source document. If so, these attributes are translated to the corresponding CSS rules with specificity equal to 0, and are treated as if they were inserted at the start of the author style sheet.

Most if not all implementations I'm aware of will convert these to CSS rules on body, based on their HTML equivalents. Others such as link, alink and vlink will become a:link, a:active and a:visited rules respectively.

Of course, it should be noted that CSS itself doesn't really have any semantics to it per se, as it's a styling language in itself which is completely separate from the content structure of an HTML document. Although the introduction to CSS2.1 covers the basics of styling an HTML document, note that the section calls itself non-normative (or informative); this means it doesn't set any hard and fast rules for CSS implementers to follow. Instead, it simply provides information for readers.

That said, certain styles may be applied to html to modify viewport behavior. For example, to hide the page scrollbars use:

html {
    overflow: hidden;
}

You can also apply rules to both html and body for interesting effects; see the following questions for details and examples:

  • What's the difference in applying CSS to html, body, and *?
  • Applying a background to <html> and/or <body>

Note that html is not the viewport; the viewport establishes an initial containing block in which html is situated. That initial containing block cannot be targeted with CSS, because in HTML, the root element is html.

Note also that, technically, there is no difference between applying properties to html and body that are inherited by default, such as font-family and color.

Last but not least, here is an excellent article that details the differences between html and body in terms of CSS. In summary (quoted from its first section):

  • The html and body elements are distinct block-level entities, in a parent/child relationship.
  • The html element's height and width are controlled by the browser window.
  • It is the html element which has (by default) overflow:auto, causing scrollbars to appear when needed.
  • The body element is (by default) position:static, which means that positioned children of it are positioned relative to the html element's coordinate system.
  • In almost all modern browsers, the built-in offset from the edge of the page is applied through a margin on the body element, not padding on the html element.

As the root element, html is more closely associated with the browser viewport than body (which is why it says html has overflow: auto for scrollbars). Note however that the scrollbars are not necessarily generated by the html element itself. By default, it's the viewport that generates these scrollbars; the values of overflow are simply transferred (or propagated) between body, html, and the viewport, depending on which values you set. The details of all this are covered in the CSS2.1 spec, which says:

UAs must apply the 'overflow' property set on the root element to the viewport. When the root element is an HTML "HTML" element or an XHTML "html" element, and that element has an HTML "BODY" element or an XHTML "body" element as a child, user agents must instead apply the 'overflow' property from the first such child element to the viewport, if the value on the root element is 'visible'. The 'visible' value when used for the viewport must be interpreted as 'auto'. The element from which the value is propagated must have a used value for 'overflow' of 'visible'.

The last bullet point probably has its roots in the aforementioned topmargin and leftmargin attributes of the body element.

2 of 2
3

If you want to style only the content that'll be displayed, targeting the <body> element saves the style rules an unnecessary level of cascading.

Is there a reason you'd want to apply styles to the <title>, <meta>, <script> etc... tags? That would happen by targeting <html>.

🌐
Reddit
reddit.com › r/webdev › do you have your own global.css file?
r/webdev on Reddit: Do you have your own global.css file?
October 16, 2024 -

As someone who's learning about Web Dev (with React), I just noticed how handy a global.css file is for keeping the same look across all of the pages and components.
I'd establish a palette of at least 5 matching colors to use. I'd set responsive font sizes for different devices. I could also set how buttons should look like and behave. And so on...

Question is, is this a common practice? Is that how you or your company keep the styles unified for every single project's frontend?

Example:

.global-title {
  font-size: 0.8rem;
  font-weight: 600;
}
/* Medium screens */
@media (min-width: 768px) {
  .global-title {
    font-size: 1rem; 
  }
}
/* Large screens  */
@media (min-width: 1024px) {
  .global-title {
    font-size: 1.2rem;  
  }
}
🌐
CSS-Tricks
css-tricks.com › defining-global-styles-in-wordpress
Defining Global Styles | CSS-Tricks
March 1, 2023 - We already covered top-level styles in Part 2. By “top-level” we mean styles that are applied globally on the root element (<html>) as well as the <body>. These styles are “top-level” in the sense they are inherited by everything in ...
🌐
meyerweb
meyerweb.com › eric › articles › webrev › 199805.html
Creating a Global Style Sheet
Now that it's been entered into your text editor, you'll want to save the above text to a file called general.css. The general part is the file's name-- I picked 'general' because this is the site's general style sheet-- and the .css indicates it's a CSS style sheet.
🌐
Next.js
nextjs.org › learn › pages-router › assets-metadata-css-global-styles
Pages Router: Global Styles | Next.js
Add the following CSS inside styles/global.css. This code resets some styles and changes the color of the a tag: html, body { padding: 0; margin: 0; font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen, Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif; line-height: 1.6; font-size: 18px; } * { box-sizing: border-box; } a { color: #0070f3; text-decoration: none; } a:hover { text-decoration: underline; } img { max-width: 100%; display: block; }
Find elsewhere
🌐
Next.js
nextjs.org › learn-pages-router › basics › assets-metadata-css › global-styles
Global Styles - Assets, Metadata, and CSS
Add the following CSS inside styles/global.css. This code resets some styles and changes the color of the a tag: html, body { padding: 0; margin: 0; font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen, Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif; line-height: 1.6; font-size: 18px; } * { box-sizing: border-box; } a { color: #0070f3; text-decoration: none; } a:hover { text-decoration: underline; } img { max-width: 100%; display: block; }
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › HTML › Reference › Global_attributes › style
HTML style global attribute - MDN Web Docs
July 9, 2025 - The style global attribute contains CSS styling declarations to be applied to the element. Note that it is recommended for styles to be defined in a separate file or files. This attribute and the element have mainly the purpose of allowing for quick styling, for example for testing purposes.
🌐
The Spicy Web
spicyweb.dev › css-nouveau › 1-vanilla-has-never-tasted-so-hot › 3-how-to-organize-your-(global)-stylesheets
How to Organize Your (Global) Stylesheets | The Spicy Web
August 24, 2025 - After fonts and tokens, I import a stylesheet I typically call global.css. This covers the most basic aspects of styling your HTML, with selectors such as body, a, main, hr, etc. as well as a few “reset” rules such as box-sizing: border-box (which we covered in the previous episode).
🌐
W3Schools
w3schools.com › tags › att_global_style.asp
HTML Global style Attribute
HTML CSS JAVASCRIPT SQL PYTHON ... AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST · HTML by Alphabet HTML by Category HTML Browser Support HTML Attributes HTML Global Attributes HTML Events HTML Colors HTML Canvas HTML Audio/Video HTML Character ...
🌐
Next.js
nextjs.org › docs › app › getting-started › css
Getting Started: CSS | Next.js
2 days ago - Create a app/global.css file and import it in the root layout to apply the styles to every route in your application:
🌐
CSS-Tricks
css-tricks.com › regarding-css-global-scope
Regarding CSS's Global Scope | CSS-Tricks
January 4, 2019 - Total abstraction might come from ... specially named HTML class attributes into a stylesheet with exactly what it needs. Even adhering 100% to BEM across your entire site could be considered total CSS isolation, solving the problems that the global scope may bri...
🌐
GitHub
gist.github.com › jackdomleo7 › 55659bafe581d19cc341ef775d6a9e6b
A set of useful global CSS defaults to add to your site alongside a reset stylesheet (with explanations) · GitHub
July 6, 2021 - A set of useful global CSS defaults to add to your site alongside a reset stylesheet (with explanations) - Useful_global_CSS.css
Top answer
1 of 6
190

Latest Update: 16/01/2020

CSS Custom Properties (Variables) have arrived!

It's 2020 and time to officially roll out this feature in your new applications.


Preprocessor "NOT" required!

There is a lot of repetition in CSS. A single color may be used in several places.

For some CSS declarations, it is possible to declare this higher in the cascade and let CSS inheritance solve this problem naturally.

For non-trivial projects, this is not always possible. By declaring a variable on the :root pseudo-element, a CSS author can halt some instances of repetition by using the variable.

How it works

Set your variable at the top of your stylesheet:

CSS

Create a root class:

:root {
}

Create variables (-- [String] : [value])

:root {
  --red: #b00;
  --blue: #00b;
  --fullwidth: 100%;
}

Set your variables anywhere in your CSS document:

h1 {
  color: var(--red);
}
#MyText {
  color: var(--blue);
  width: var(--fullwidth);
}

BROWSER SUPPORT / COMPATIBILITY

See caniuse.com for current compatability.


Firefox: Version 31+ (Enabled by default)

Supported since 2014 (Leading the way as usual.)

More info from Mozilla


Chrome: Version 49+ (Enabled by default).

Supported since 2016


Safari/IOS Safari: Version 9.1/9.3 (Enabled by default).

Supported since 2016


Opera: Version 39+ (Enabled by default).

Supported since 2016


Android: Version 52+ (Enabled by default).

Supported since 2016


Edge: Version 15+ (Enabled by default).

Supported since 2017

CSS Custom Properties landed in Windows Insider Preview build 14986


IE: When pigs fly.

It's time to finally let this ship sink. No one enjoyed riding her anyway.


W3C SPEC

Full specification for upcoming CSS variables

Read more


TRY IT OUT

A fiddle and snippet are attached below for testing:

(It will only work with supported browsers.)

DEMO FIDDLE

:root {
  --red: #b00;
  --blue: #4679bd;
  --grey: #ddd;
  --W200: 200px;
  --Lft: left;
}
.Bx1,
.Bx2,
.Bx3,
.Bx4 {
  float: var(--Lft);
  width: var(--W200);
  height: var(--W200);
  margin: 10px;
  padding: 10px;
  border: 1px solid var(--red);
}
.Bx1 {
  color: var(--red);
  background: var(--grey);
}
.Bx2 {
  color: var(--grey);
  background: black;
}
.Bx3 {
  color: var(--grey);
  background: var(--blue);
}
.Bx4 {
  color: var(--grey);
  background: var(--red);
}
<p>If you see four square boxes then variables are working as expected.</p>

<div class="Bx1">I should be red text on grey background.</div>
<div class="Bx2">I should be grey text on black background.</div>
<div class="Bx3">I should be grey text on blue background.</div>
<div class="Bx4">I should be grey text on red background.</div>

2 of 6
33

You can't create variables in CSS right now. If you want this sort of functionality you will need to use a CSS preprocessor like SASS or LESS. Here are your styles as they would appear in SASS:

$Color1:#fff;
$Color2:#b00;
$Color3:#050;

h1 {
    color:$Color1;
    background:$Color2;
}

They also allow you to do other (awesome) things like nesting selectors:

#some-id {
    color:red;

    &:hover {
        cursor:pointer;
    }
}

This would compile to:

#some-id { color:red; }
#some-id:hover { cursor:pointer; }

Check out the official SASS tutorial for setup instructions and more on syntax/features. Personally I use a Visual Studio extension called Web Workbench by Mindscape for easy developing, there are a lot of plugins for other IDEs as well.

Update

As of July/August 2014, Firefox has implemented the draft spec for CSS variables, here is the syntax:

:root {
  --main-color: #06c;
  --accent-color: #006;
}
/* The rest of the CSS file */
#foo h1 {
  color: var(--main-color);
}
🌐
CSS-Tricks
css-tricks.com › global-css-options-with-custom-properties
Global CSS options with custom properties | CSS-Tricks
May 29, 2020 - Can we do that in native CSS with custom properties? Mark Otto shows that we can. It’s just a smidge different. html { --component-shadow: 0 .5rem 1rem rgba(0,0,0,.1); } .component { box-shadow: var(--component-shadow); } <!-- override the global anywhere more specific!
🌐
Astro
docs.astro.build › en › guides › styling
Styles and CSS - Astro Docs
You can style HTML elements inline using the style attribute. This can be a CSS string or an object of CSS properties: ... There are two ways to resolve external global stylesheets: an ESM import for files located within your project source, and an absolute URL link for files in your public/ directory, or hosted outside of your project.