首页 web前端 css教程 了解 CSS 继承:一致样式指南

了解 CSS 继承:一致样式指南

Dec 21, 2024 pm 01:05 PM

大家好,欢迎回到我的博客! ?

介绍

让我们深入了解 CSS 继承 的世界。我们将探讨哪些属性会传递下去,如何控制此流程,以及为什么它对您的设计很重要。本指南是为每个人(从初学者到经验丰富的专业人士)精心设计的,可帮助您利用继承来获得更干净、更易于维护的 CSS。

您将在本文中学到什么?

  • 继承基础知识:属性继承意味着什么。

  • 继承哪些属性:深入了解继承和非继承属性。

  • 控制继承:如何使用 CSS 关键字和技术管理继承。

  • 深入示例:展示继承的实际场景,并附有更详细的解释。

什么是 CSS 继承?

CSS 继承是指某些属性自动从父元素传递给子元素。此机制有助于在嵌套元素之间一致地应用样式,而无需重新声明它们。

继承的属性

** ✅ 常见的继承属性:**

  • 文本属性:字体系列、字体大小、颜色、行高、文本对齐。这些旨在在文本内容中保持一致。

  • 可见性:可见性(但不显示)。

  • 光标:交互式提示的光标。

?继承示例:

<div>



<p>Result:</p>

<p><img src="/static/imghw/default1.png"  data-src="https://img.php.cn/upload/article/000/000/000/173475751395222.jpg"  class="lazy" alt="Understanding CSS Inheritance: A Guide to Consistent Styling" /></p>

<p>Here, all child elements inside the div will have the Helvetica font unless overridden.</p>

<h2>
  
  
  <strong>Properties That Don't Inherit</strong>
</h2>

<h3>
  
  
  <strong>✖️ Non-Inherited Properties:</strong>
</h3>

<ul>
<li><p><strong>Box Model Properties</strong>: width, height, margin, border, padding. Each element typically controls its own space.</p></li>
<li><p><strong>Background</strong>: background properties, as backgrounds are often meant to be unique per element.</p></li>
<li><p><strong>Position</strong>: position, top, left, right, bottom.</p></li>
</ul>

<h2>
  
  
  <strong>Controlling Inheritance</strong>
</h2>

<p><strong>Using</strong> inherit: To explicitly make a property inherit from its parent:<br>
</p>

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">/* If the parent has a specific color, the child will adopt it */
.child-element {
    color: inherit;
}

登录后复制
登录后复制

使用 初始 : 将属性重置为其浏览器默认值:

/* Resets the font-size to the default size of the browser */
.reset-font-size {
    font-size: initial;
}

登录后复制

使用 unset : 将属性恢复为其继承值或初始值:

/* Will inherit if a parent has a color set, otherwise, it will be initial */
.unset-color {
    color: unset;
}
登录后复制

实际例子

  1. 简化版式
<article>





<pre class="brush:php;toolbar:false">/* Nothing needed here; inheritance does the job */
登录后复制

结果:文章中的所有文本均使用 Georgia 字体和深灰色,打造统一的外观。

Understanding CSS Inheritance: A Guide to Consistent Styling

  1. 重写继承
<nav>
    <ul>
        <li><a href="#home">Home</a></li>
        <li><a href="#about">About</a></li>
    </ul>
</nav>
登录后复制
nav {
    font-size: 16px; /* Base size for navigation */
    color: #333; /* Base color for text */
}

nav a {
    color: inherit; /* Inherits the color from nav, which is #333 */
    font-size: inherit; /* Also inherits 16px */
    text-decoration: none; /* Default is none, but doesn't inherit */
}

nav a:hover {
    color: #0056b3; /* Changes on hover, overriding inheritance */
}

登录后复制

结果:链接以与其父导航相同的大小和颜色开始,但在悬停时改变颜色,展示对继承的控制。

Understanding CSS Inheritance: A Guide to Consistent Styling

注意:为了更好地检查结果并试验代码,您可以复制粘贴 Codepen.io 上的所有代码块。

  1. 布局的自定义继承
<div>



<p>Result:</p>

<p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173475751395222.jpg" class="lazy" alt="Understanding CSS Inheritance: A Guide to Consistent Styling"></p>

<p>Here, all child elements inside the div will have the Helvetica font unless overridden.</p>

<h2>
  
  
  <strong>Properties That Don't Inherit</strong>
</h2>

<h3>
  
  
  <strong>✖️ Non-Inherited Properties:</strong>
</h3>

登录后复制
  • Box Model Properties: width, height, margin, border, padding. Each element typically controls its own space.

  • Background: background properties, as backgrounds are often meant to be unique per element.

  • Position: position, top, left, right, bottom.

Controlling Inheritance

Using inherit: To explicitly make a property inherit from its parent:

/* If the parent has a specific color, the child will adopt it */
.child-element {
    color: inherit;
}

登录后复制

结果:内容 div 保持与其容器相同的内边距和背景,确保无缝的视觉流。

Understanding CSS Inheritance: A Guide to Consistent Styling

为什么理解继承至关重要

  • 一致性:继承有助于用更少的代码保持整个网站的样式一致性。

  • 性能:通过利用继承,您可以减少 CSS 规则的数量,这有助于解决加载时间和特异性问题。

  • 灵活性:了解如何控制继承可以实现更动态的设计,其中元素可以根据需要共享或覆盖样式。

结论

CSS 继承就像网页设计的家谱,确保样式以逻辑、高效的方式传递下去。通过理解和操纵继承,您可以制作既一致又灵活的设计。

请记住,虽然某些属性会自然继承,但您始终可以使用继承、初始和取消设置等 CSS 关键字进行控制。

编码愉快! ?


?大家好,我是 Eleftheria,社区经理、 开发人员、公共演讲者和内容创建者。

?如果您喜欢这篇文章,请考虑分享。

所有链接 | X | 领英

以上是了解 CSS 继承:一致样式指南的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

VUE 3 VUE 3 Apr 02, 2025 pm 06:32 PM

它的出局!恭喜Vue团队完成了完成,我知道这是一项巨大的努力,而且很长时间。所有新文档也是如此。

使用Redwood.js和Fauna构建以太坊应用 使用Redwood.js和Fauna构建以太坊应用 Mar 28, 2025 am 09:18 AM

随着最近比特币价格超过20k美元的攀升,最近打破了3万美元,我认为值得深入研究创建以太坊

您可以从浏览器获得有效的CSS属性值吗? 您可以从浏览器获得有效的CSS属性值吗? Apr 02, 2025 pm 06:17 PM

我有人写了这个非常合法的问题。 Lea只是在博客上介绍了如何从浏览器中获得有效的CSS属性。那样的是这样。

带有粘性定位的堆叠卡和一点点的杂物 带有粘性定位的堆叠卡和一点点的杂物 Apr 03, 2025 am 10:30 AM

前几天,我发现了科里·金尼文(Corey Ginnivan)网站上的这一点,当您滚动时,彼此之间的卡片堆放集。

在CI/CD上有点 在CI/CD上有点 Apr 02, 2025 pm 06:21 PM

我说的“网站”比“移动应用程序”更合适,但我喜欢Max Lynch的框架:

在WordPress块编辑器中使用Markdown和本地化 在WordPress块编辑器中使用Markdown和本地化 Apr 02, 2025 am 04:27 AM

如果我们需要直接在WordPress编辑器中向用户显示文档,那么最佳方法是什么?

比较浏览器的响应式设计 比较浏览器的响应式设计 Apr 02, 2025 pm 06:25 PM

这些桌面应用程序中有许多目标是同时在不同的维度上显示您的网站。因此,例如,您可以写作

为什么Flex布局中的紫色斜线区域会被误认为是'溢出空间”? 为什么Flex布局中的紫色斜线区域会被误认为是'溢出空间”? Apr 05, 2025 pm 05:51 PM

关于Flex布局中紫色斜线区域的疑问在使用Flex布局时,你可能会遇到一些令人困惑的现象,比如在开发者工具(d...

See all articles