Home >Web Front-end >CSS Tutorial >How Do `html`, `body`, and `*` Selectors Differently Impact Styling in CSS?

How Do `html`, `body`, and `*` Selectors Differently Impact Styling in CSS?

Susan Sarandon
Susan SarandonOriginal
2024-12-02 10:42:10618browse

How Do `html`, `body`, and `*` Selectors Differently Impact Styling in CSS?

Impact of CSS Rules on html, body, and * Selectors

In CSS, the html, body, and * selectors serve distinct purposes when applied to the same HTML document. Let's explore the differences:

1. html Selector

  • Applies styles to the element.
  • Declared colors (e.g., black) are inherited by its descendants, including body, but not background colors (e.g., white).
  • The html element's background color fills the viewport, but its height does not automatically extend to the full viewport.

2. body Selector

  • Applies styles to the element.
  • Declared colors are inherited by descendants, including the content within the element.
  • While html's background-color is propagated to body, setting a background-color for body overrides the inherited color from html.

3. * Selector (Universal Selector)

  • Applies styles to all elements in the document.
  • Breaks inheritance chains for properties that are normally inherited (e.g., color), so explicitly setting these properties in a * rule is considered bad practice.
  • Overrides any other CSS rules that apply to specific elements.

The above is the detailed content of How Do `html`, `body`, and `*` Selectors Differently Impact Styling in CSS?. 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