logo

该视频仅会员有权观看

立即开通课程「Web前端页面基础」权限。

¥
199
/ 年

CSS 样式应用规则(层叠)

  • CSS styles for elements can come from various sources and can be layered on top of each other.
  • Styles are applied to elements based on a set of conflict-resolution rules within CSS, with higher-priority styles overriding those of lower priority.
  • Priorities are determined by the specificity of the selector; ID selectors have a higher priority than class selectors, and class selectors are higher than type selectors.
  • If selectors have the same level of specificity, the style defined later in the CSS will prevail.
  • You can increase the priority of a style by increasing the specificity of its selector or by using !important.
  • An example given is changing a paragraph's background to gold and then overriding it with sky blue using the same 'p' selector.
  • Adding a class 'bj green yellow' to the first paragraph element and defining a green-yellow background in CSS will override the 'p' selector style due to the higher priority of class selectors.
  • Combining selectors can further increase specificity and priority, overriding other styles, as in the example where the selector 'content > p' defines a background and supersedes the previous 'bj green yellow' background color.
  • Lastly, using an ID selector '#app' with a background of lavender will override the combined selector 'content p' due to the even higher priority of ID selectors.