CSS feels very simple when you first learn it, but as you learn more, you will realize how deep the water of CSS is. You will always encounter various pitfalls. Let’s first summarize some pitfalls that are often encountered. .
Recommended tutorial: CSS video tutorial
Case insensitive
Although we usually write CSS Use lowercase, but 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 Xhtml standard relationship, but even if it is not When two rules are applied to the same HTML element, if the defined attributes conflict, whose value should be used? CSS has a set of priority definitions.
Different levels
#Using !important after an attribute will override the element style defined anywhere within the page.
The above level is still easy to see I understand, but sometimes some rules are a combination of multiple levels, like this <!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
div.test{
background-COLOR:#a00;
width:100px;
height: 100px;
}
.test.test2{
background-COLOR:#0e0;
width:100px;
height: 100px;
}
</style>
</head>
<body>
<div class="test test2"></div>
</body>
</html>
The weight of the inline style sheet is 1000
Some attributes of inline elements
The width attribute will not be applied to inline elements, and their length It is stretched by the content
The height attribute will not be applied to the inline element, and its height is also stretched by the content, but the height can be adjusted through line-height
<div style="background-color: #a44;"> <span style="padding:4px; margin:8px; height: 500px; width:1000px; background-color:#0e0;"> 123456789123456789</span> </div> <div style="background-color: #a44;"> <span style="padding:4px; margin:8px; height: 500px; width:1000px; background-color:#0a0;"> 123456789</span> </div>
Some mutually exclusive elements
For absolute and fixed positioned (fixed size, width and height attributes are set) elements, if If the top and left attributes are set, then setting the bottom and right values has no effect. Top and left should have higher priority. Otherwise, how will the browser know who to position according to who is written at the same time?
For absolute and fixed positioned elements, if the values of top, left, bottom, and right are set, the margin attribute will not work
pt
1. Px is the abbreviation of pixel, which is a unit based on pixels. During the process of browsing the web, the text, pictures, etc. on the screen will change with the resolution of the screen. A picture with a width of 100px will At 800×600 resolution, it occupies 1/8 of the screen width, but at 1024×768, it only occupies about 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是相对于根元素(r:root),使用rem我们只需要在根元素确定一个参考值,然后就可以控制整个html页面所有字体了。
:checked 选择器范围
我们知道:checked会选择被选中的checkbox和radio,看个例子
<!doctype html> <html> <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> <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> <div></div> <div></div> <div style="visibility:hidden;"> <div></div> </div> <div style="display:none;"> <div></div> </div> <div style="display:none"> <img src="images/5.jpg" alt="Understand the easily overlooked features of CSS and avoid some common pitfalls" ></div> </body> </html>
看一下网络监视情况(怎么柳岩的照片变小后感觉怪怪的。。。)
我们可以发现图片0和4没有被下载,0是没有用到的CSS,4是父容器的display被设为none的情况,这两种情况下的CSS引用的图片是不会被加载的,而父容器设置visibility属性为hidden仍然会加载图片,不要搞混了
更多编程相关知识,请访问:编程入门!!
The above is the detailed content of Understand the easily overlooked features of CSS and avoid some common pitfalls. For more information, please follow other related articles on the PHP Chinese website!