search
HomeWeb Front-endCSS TutorialTips to make CSS weights more friendly

In this article, I will show you some CSS tips through some examples, so that you can know how to use CSS cascade to become more friendly and reduce unnecessary requirements. This also reduces the trouble encountered in weighting.

Tip 1: Styles always appear as a combination of specific attributes

A good example is the combination of color and baking-color. Unless you are only making small adjustments, you need to adjust them all together. When adding a background color to an element, it may not contain any text, but may have some child elements. Therefore, by setting the foreground-color and background-color together, we can always be sure that these elements will not encounter any legibility and contrast problems. Next time we change the background color, we don’t have to look around for the text color to change because they all appear together as a group.

Tip 2: Use dynamic values, such as currentColor and em, etc.

Sometimes the text color is also used on other attributes. For example, border, box-shadow or fill in SVG icons. An alternative to defining the same color is to use currentColor directly. By default, color is inheritable, and you only need to modify it in one place to change the color of other properties.

Similarly, using em units for the font-size attribute allows you to change the box model size of the element by just modifying the :root's font-size.

Tip 3: Use the inherit attribute value to inherit the style of its parent element to override the UA's own style.

For form controls such as buttons and inputs, different browsers will have their own styles (UA styles). We can use inherit to inherit the style of its parent element, thereby overriding the browser's UA style.

button,
input,
select,
textarea {
    color: inherit;
    font-family: inherit;
    font-style: inherit;
    font-weight: inherit;
}

The above example code is taken from sanitize.css, normalize.css is also used in this way. If you don't use it this way, it means you have...

You can also try input[type="range"], input[type="radio"] and input[type="checkbox"], etc. Directly use the currentColor attribute introduced earlier on the element to automatically match the color. Maybe you don't need to change anything, you can change a bright color to a dark color.

Tip 4: Whenever you write CSS, you want to get as close to the top of the tree as possible. In other words, go back to :root.

For example, our website has a sidebar and we want to add a short personal introduction to this sidebar. The structure of the HTML may look like this:

<body>
    <main class=“Posts”>
    <aside class=“SideBar”>
        <nav class=“Nav”>
        <p class=“Bio”>

The CSS is written like this:

.Bio {
    font-size: .8em;
    line-height: 1.5;
    color: #888;
}

Written like this, it can work normally and there is no style problem. However, the sidebar also has a navigation nav, and it's likely that they have some styles that are the same. In our example font-size and color are the same. Let's extract these properties from nav and .Bio and add them to its parent element .SideBar:

.SideBar {
    font-size: .8em;
    color: #888;
}

It turns out that line-height:1.5; is already set in .Posts. It seems that the entire page uses the same line height, then we can move the line-height in .Bio and .Posts to the root element:

:root {
    line-height: 1.5;
}

This seems to be a CSS common sense, but he does not would be too focused on sibling elements defining the same thing. This also allows you to find that some code is duplicated. In fact, this is not terrible, because we only need to spend some time to refactor the code, but this maintains the health of CSS code processing.

Tips to make CSS weights more friendly

Write styles on the branches, not on the leaves


The above is the detailed content of Tips to make CSS weights more friendly. 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
css ul标签怎么去掉圆点css ul标签怎么去掉圆点Apr 25, 2022 pm 05:55 PM

在css中,可用list-style-type属性来去掉ul的圆点标记,语法为“ul{list-style-type:none}”;list-style-type属性可设置列表项标记的类型,当值为“none”可不定义标记,也可去除已有标记。

css与xml的区别是什么css与xml的区别是什么Apr 24, 2022 am 11:21 AM

区别是:css是层叠样式表单,是将样式信息与网页内容分离的一种标记语言,主要用来设计网页的样式,还可以对网页各元素进行格式化;xml是可扩展标记语言,是一种数据存储语言,用于使用简单的标记描述数据,将文档分成许多部件并对这些部件加以标识。

css3怎么实现鼠标隐藏效果css3怎么实现鼠标隐藏效果Apr 27, 2022 pm 05:20 PM

在css中,可以利用cursor属性实现鼠标隐藏效果,该属性用于定义鼠标指针放在一个元素边界范围内时所用的光标形状,当属性值设置为none时,就可以实现鼠标隐藏效果,语法为“元素{cursor:none}”。

Go语言开发小技巧:阿里云接口对接实践分享Go语言开发小技巧:阿里云接口对接实践分享Jul 05, 2023 pm 11:49 PM

Go语言开发小技巧:阿里云接口对接实践分享前言:现如今,云计算已经成为了企业信息化建设的核心技术之一,而阿里云作为国内知名的云计算服务提供商,拥有丰富的云产品和服务。本文将分享笔者在使用Go语言对接阿里云接口时的一些实践经验,并以代码示例的形式进行阐述。一、引入阿里云GoSDK在使用Go语言对接阿里云接口之前,首先我们需要引入相应的阿里云GoSDK,以便

css怎么实现英文小写转为大写css怎么实现英文小写转为大写Apr 25, 2022 pm 06:35 PM

转换方法:1、给英文元素添加“text-transform: uppercase;”样式,可将所有的英文字母都变成大写;2、给英文元素添加“text-transform:capitalize;”样式,可将英文文本中每个单词的首字母变为大写。

微博权重是什么意思微博权重是什么意思Dec 11, 2020 pm 02:36 PM

微博权重是指微博官方对微博号的评分,主要体现在搜索和评论时的排序,权重越高排序越靠前,因此微博权重也会影响到微博号的流量数据。提高权重可以通过实名制的方式,还可以成为微博的签约自媒体。

rtl在css是什么意思rtl在css是什么意思Apr 24, 2022 am 11:07 AM

在css中,rtl是“right-to-left”的缩写,是从右往左的意思,指的是内联内容从右往左依次排布,是direction属性的一个属性值;该属性规定了文本的方向和书写方向,语法为“元素{direction:rtl}”。

css怎么设置i不是斜体css怎么设置i不是斜体Apr 20, 2022 am 10:36 AM

在css中,可以利用“font-style”属性设置i元素不是斜体样式,该属性用于指定文本的字体样式,当属性值设置为“normal”时,会显示元素的标准字体样式,语法为“i元素{font-style:normal}”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!