Home  >  Article  >  Web Front-end  >  Features that are easily overlooked in CSS

Features that are easily overlooked in CSS

不言
不言Original
2018-06-12 16:08:471249browse

CSS beginners find it very simple and there is no difficulty. In fact, it is because you have not yet fully understood the CSS features. Let me share with you the CSS features that are easily overlooked through this article. This article is a pitfall that the editor encounters every day and is very useful for reference. Reference value, hereby share it for your reference

CSS seems very simple when you first learn it, but as you learn more deeply, you will realize how deep the water of CSS is. You will always encounter various pitfalls. Let’s first summarize some common encounters. The Pit
Case Insensitivity
Although we usually use lowercase when writing CSS, in fact CSS is not case sensitive

.test{    
background-COLOR:#a00;    
width:100px;    
height: 100px;    
}

Although background-color is written as background-COLOR, it will still take effect. The reason why it is written in lowercase is because of the xhtml standard, but even if it is not xhtml, it is better and more beautiful to write it in lowercase , easy to read and can cope with possible conversion needs
Selector priority
When two rules apply to the same html element, if the defined attributes conflict, then you should use Whose value, CSS has a set of priority definitions.
Different levels
1. Using !important after an attribute will override the element style defined anywhere within the page.
2. The style written in the element as the style attribute
3. ID selector
4. Class selector
5. Tag selector
6. Wildcard selector
7 .Browser customization or inheritance
Same level
The style written later in the same level will overwrite the style written first
The above levels are still easy to understand, but sometimes there are some Rules are a combination of multiple levels, like this

<!doctype html>    
<html lang="en">    
<head>    
<meta charset="UTF-8">    
<title>Document</title>    
<style type="text/css">    
p.test{    
background-COLOR:#a00;    
width:100px;    
height: 100px;    
}    
.test.test2{    
background-COLOR:#0e0;    
width:100px;    
height: 100px;    
}    
</style>    
</head>    
<body>    
<p class="test test2"></p>    
</body>    
</html>

Which rule p should apply? There is a simple calculation method (reminders from the garden, the weights are not actually in decimal, but expressed in numbers. To illustrate the idea, ten thousand classes are not as high as the weight of one ID)

•The weight of the inline style sheet is 1000
•The weight of the ID selector is 100
•Class class selection The weight of the selector is 10
•The weight of the HTML tag selector is 1
We can add the rules in the selector and compare the weights. If the weights are the same, the later ones will overwrite the previous ones. The weight of p.class is 1 10=11, and the weight of .test1 .test2 is 10 10=20, so p will use .test1 .test2

to turn green

Some attributes of inline elements
Not all attributes can take effect on inline elements
1. Inline elements do not apply the width attribute, and their length is stretched by the content
2. Inline elements do not apply the height attribute, and their height is also stretched by the content, but the height can be adjusted through line-height
3 .The padding attributes of inline elements only take effect with padding-left and padding-right. padding-top and padding-bottom will change the scope of the element, but will not affect other elements.
4. The margin attributes of inline elements only have margin- left and margin-right are valid, margin-top and margin-bottom are invalid
5. The overflow attribute of the inline element is invalid, needless to say this
6. The vertical-align attribute of the inline element is invalid (the height attribute is invalid)

<p style="">    
<span style="padding:4px; margin:8px; height: 500px; width:1000px; ">123456789123456789</span>    
</p>    
<p style="">    
<span style="padding:4px; margin:8px; height: 500px; width:1000px; ">123456789</span>    
</p>

通过例子可以看出,我们对span设置的width和height属性并没有生效,margin-top和margin-bottom无效,padding-top和padding-bottom会改变元素范围(背景区域变大了),但并没有影响下面元素位置
一些互斥的元素
1.对于absolute和fixed定位的(固定大小,设置了width和height属性)元素,如果设置了top和left属性,那么设置bottom和right值就没有作用了,应该是top和left优先级高,否则同时写了浏览器怎么知道按照谁定位
2.对于absolute和fixed定位的元素,如果设置了top、left、bottom、right的值后margin属性也就不起作用了
3.对于absolute和fixed定位的元素,如果设置了top、left、bottom、right的值后float属性同样会失效
4.块元素如果设置了float属性或者是absolute、fixed定位,那么vertical-align属性不再起作用
font-size单位
我们在写字体的尺寸的时候常用的单位有
•px
•pt
•em
•rem
这些字体分别有什么含义?
1.px是pixel缩写,是基于像素的单位.在浏览网页过程中,屏幕上的文字、图片等会随屏幕的分辨率变化而变化,一个100px宽度大小的图片,在800×600分辨率下,要占屏幕宽度的1/8,但在1024×768下,则只占约1/10。所以如果在定义字体大小时,使用px作为单位,那一旦用户改变显示器分辨率从800到1024,用户实际看到的文字就要变“小”(自然长度单位),甚至会看不清,影响浏览。
2.pt是point(磅)缩写,是一种固定长度的度量单位,大小为1/72英寸。如果在web上使用pt做单位的文字,字体的大小在不同屏幕(同样分辨率)下一样,这样可能会对排版有影响,但在Word中使用pt相当方便。因为使用Word主要目的都不是为了屏幕浏览,而是输出打印。当打印到实体时,pt作为一个自然长度单位就方便实用了:比如Word中普通的文档都用“宋体 9pt”,标题用“黑体 16pt”等等,无论电脑怎么设置,打印出来永远就是这么大。
3.em:是相对单位,是一个相对长度单位,最初是指字母M的宽度,所以叫em,现指的是字符宽度的倍数,用法类似百分比,如:0.8em, 1.2em,2em等。通常1em=16px(浏览器默认字体大小16px),em是指父元素的字体大小。在一个页面上给定了一个父元素的字体大小,这样就可以通过调整一个元素来成比例的改变所有元素大小.它可以自由缩放,比如用来制作可伸缩的样式表。类似还有ex的概念,ex 相对于字符“x”的高度,此高度通常为字体尺寸的一半。
4.rem:rem是CSS新增的,em是相对于其父元素来设置字体大小的,这样就会存在一个问题,进行任何元素设置,都有可能需要知道他父元素的大小,在多次使用时,就会带来无法预知的错误风险。而rem是相对于根元素100db36a723c770d327fc0aef2ce13b1(r:root),使用rem我们只需要在根元素确定一个参考值,然后就可以控制整个html页面所有字体了。
:checked 选择器范围
我们知道:checked会选择被选中的checkbox和radio,看个例子

<!doctype html>    
<html lang="en">    
<head>    
<meta charset="UTF-8">    
<title>Document</title>    
<style type="text/css">    
:checked{    
margin: 10px;    
}    
</style>    
</head>    
<body>    
<input id="t1" type="checkbox" checked/>    
<input id="t3" type="radio" checked/>    
<select>    
<option id="t2">test</option>    
<option id="t4">test2</option>    
</select>    
</body>    
</html>   
对于前两个margin变成10px我们不奇怪,但是当我们看select的option的时候会发现被选中的option的margin业变成了10px,没有被选中的option则没有变化!

是的:checked也会选择被选中的option
并不是所有图片都会被加载
我们知道写在页面上的img标签,无论显示与否,图片都会被加载(所以试图通过对图片display:none来达到节省网络流量的做法就省省吧。。。),我们也经常使用backgroung-image等css属性为页面添加图片,这些图片是不是一定会被加载呢,看个例子

<!doctype html>    
<html lang="en">    
<head>    
<meta charset="UTF-8">    
<title>Document</title>    
<style type="text/css">    
.useless{    
background-image: url(images/0.jpg);    
}    
.hidden{    
background-image: url(images/1.jpg);    
}    
.none{    
background-image: url(images/2.jpg);    
}    
.parentHidden{    
background-image: url(images/3.jpg);    
}    
.parentNone{    
background-image: url(images/4.jpg);    
}    
</style>    
</head>    
<body>    
<p class="hidden"></p>    
<p class="none"></p>    
<p style="visibility:hidden;">    
<p class="parentHidden"></p>    
</p>    
<p style="display:none;">    
<p class="parentNone"></p>    
</p>    
<p style="display:none">    
<img src="images/5.jpg"></p>    
</body>    
</html>

看一下网络监视情况(怎么柳岩的照片变小后感觉怪怪的。。。) 

我们可以发现图片0和4没有被下载,0是没有用到的CSS,4是父容器的display被设为none的情况,这两种情况下的CSS引用的图片是不会被加载的,而父容器设置visibility属性为hidden仍然会加载图片,不要搞混了

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

CSS编写代码时的高性能总结

提高css性能的方法

The above is the detailed content of Features that are easily overlooked 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