Home  >  Article  >  Web Front-end  >  Understand the easily overlooked features of CSS and avoid some common pitfalls

Understand the easily overlooked features of CSS and avoid some common pitfalls

青灯夜游
青灯夜游forward
2020-11-11 18:09:232113browse

Understand the easily overlooked features of CSS and avoid some common pitfalls

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;
}

Understand the easily overlooked features of CSS and avoid some common pitfalls

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 style written within the element as the style attribute
  • id selector
  • Class selection Selector
  • Tag selector
  • Wildcard selector
  • Browser customization or inheritance
  • Same level

  • The style written later in the same level will overwrite the style written first

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>
Which rule does the div apply to? There is a simple calculation method (reminders from the garden, the weight is actually not According to the decimal system, the numerical representation is just 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
  • The weight of the Class selector is 10
  • HTML tag selector The weight of The value is 1 10=11, and the weight of .test1 .test2 is 10 10=20, so the div will turn green using .test1 .test2
  • Some attributes of inline elements

Not all attributes can take effect on inline elements

Understand the easily overlooked features of CSS and avoid some common pitfallsThe 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

    The padding attribute of inline elements only takes 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
  1. The margin attributes of inline elements are only valid margin-left and margin-right, margin-top and margin-bottom are invalid
  2. The overflow attribute of inline elements is invalid, needless to say more about this
  3. The vertical-align attribute of the inline element is invalid (the height attribute is invalid)
  4. <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>
  5. You can use examples It can be seen that the width and height attributes we set for span do not take effect, margin-top and margin-bottom are invalid, padding-top and padding-bottom will change the scope of the element (the background area becomes larger), but do not affect the following elements. Position
  6. Some mutually exclusive elements

Understand the easily overlooked features of CSS and avoid some common pitfallsFor 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

    For absolute and fixed positioned elements, if After setting the values ​​​​of top, left, bottom, and right, the float attribute will also be invalid.
  1. If the float attribute or absolute or fixed positioning of the block element is set, the vertical-align attribute will no longer be activated. Function
  2. font-size unit

  3. The commonly used units when we write the size of fonts are
  4. px

pt

    em
  • rem
  • What do these fonts mean?
  • 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>

    Understand the easily overlooked features of CSS and avoid some common pitfalls

    对于前两个margin变成10px我们不奇怪,但是当我们看select的option的时候会发现被选中的option的margin业变成了10px,没有被选中的option则没有变化!

    Understand the easily overlooked features of CSS and avoid some common pitfalls

    是的: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>

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

    Understand the easily overlooked features of CSS and avoid some common pitfalls 我们可以发现图片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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete