Home >Web Front-end >CSS Tutorial >How Do `html`, `body`, and the Universal Selector (*) Differ in CSS Styling?

How Do `html`, `body`, and the Universal Selector (*) Differ in CSS Styling?

Susan Sarandon
Susan SarandonOriginal
2024-12-08 14:13:15509browse

How Do `html`, `body`, and the Universal Selector (*) Differ in CSS Styling?

CSS Selectors: Comparing html, body, and the Universal Selector *

When applying CSS styles to an HTML document, the context in which the selectors are used determines their effects. Let's examine the distinctions between the html, body, and * selectors when applied to the same document.

html Selector

Applying styles to the html element affects only the tag. Its descendant elements, including the tag, inherit the specified colors. However, the background-color property does not inherit, which means the background will be visible across the entire viewport.

body Selector

The body selector styles only the element. Its descendant elements inherit the color. The implied background of the element will propagate to the unless a different background is explicitly set for .

Universal Selector *

The universal selector * applies to every single element in the document. It breaks the inheritance chain, preventing properties from being inherited. This rule is generally discouraged unless there is a compelling reason to completely override inheritance.

Summary

  • html sets the styles for the element, affecting all descendants with color inheritance.
  • body sets the styles for the element, affecting its descendants with color inheritance and inheriting the background of the element unless specified otherwise.
  • * applies to all elements, regardless of context, breaking the inheritance chain, which is generally considered poor practice unless necessary.

The above is the detailed content of How Do `html`, `body`, and the Universal Selector (*) Differ in CSS Styling?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn