CSS

195 阅读3分钟

为何CSS不支持父选择器?

浏览器解析HTML文档,是从前往后,有外及里的。所以时常会看到页面先出现头部然后主体内容再出现的加载情况。

但是,如果CSS支持了父选择器,那就必须要页面所有子元素加载完毕才能渲染HTML文档,因为所谓“父选择器”,就是后代元素影响祖先元素,如果后代元素还没加载处理,如何影响祖先元素的样式?于是,网页渲染呈现速度会大大减慢,浏览器会出现长时间的白板。加载多少HTML就可以渲染多少HTML,在网速不是很快的时候,就显得尤为的必要。

实现垂直居中的几种方式

  1. 使用line-height等于height实现行内文字垂直居中

  2. 使用top:0; bottom:0; margin:auto; position: absolute;实现绝对定位元素的垂直居中(块需要指定高度)

  3. 使用top:50%; transform:translateY(-50%); position:absolute实现绝对定位元素的垂直居中(块无需指定高度)

  4. 使用display:flex;align-items:center; 实现块的垂直居中(块无需指定高度)

  5. 使用display:table-cell; vertial-align:middle 实现块的垂直居中(块无需指定高度)

<div style="height: 50px;line-height: 50px;border: 1px solid red;margin-bottom: 10px;">
        使用line-height等于height实现行内文字垂直居中
    </div>


    <div style="background: blue; height: 100px; position: relative;">
        <div style="background: burlywood; height: 40px;margin: auto;top: 0;bottom: 0;position: absolute; width: 100%;">
            使用top:0; bottom:0; margin:auto; position: absolute;实现块的垂直居中
            (块需要指定高度)
        </div>
    </div>


    <div style="height: 100px;background: red ;position: relative;">
        <div style=" background:yellowgreen;top: 50%;transform:translateY(-50%); position: absolute; width: 100%;">
            使用top:50%; transform:translateY(-50%); position:absolute实现块的垂直居中
            (块无需指定高度)
        </div>
    </div>

    <div style=" background: green;height: 100px; display: flex; align-items: center; ">
        <div style="background: gold;">
            使用display:flex;align-items:center; 实现块的垂直居中
            (块无需指定高度)
        </div>
    </div>

    <div style="background: cadetblue;height: 100px;display: table-cell; vertical-align: middle;">
        <div style="background: blue;">
        使用display:table-cell; vertial-align:middle 实现块的垂直居中
        (块无需指定高度)
        </div>
    </div>

清除浮动的几种方式

浮动的元素存在于正常的文档布局之外,在某些方面的行为相当奇怪:

首先,他们的

  1. 浮动元素的父元素下面新增一个空的div,并设置样式为:clear:both

  2. 浮动元素的父元素添加伪元素:after,并设置样式为:clear:both;content:'XX';display:block;height:0;visibility: hidden;

  3. 浮动元素的父元素触发BFC,如添加样式:overflow: auto/ display: flow-root

  4. 浮动元素的父元素设置高度 (父元素中默认所占的面积的有效高度为0)

    <style>
        .float-box-father{
            /**方式3 创建一个会包含这个浮动的BFC,通常的做法是设置父元素 overflow: auto **/
            /* overflow: auto; */

            /* display: flow-root */

            /** 方式4 浮动元素的父元素添加高度**/
            /* height: 250px; */

           

        }
        .float-box-father:after{
            /**方式2 浮动元素的父元素添加伪元素:after,并设置如下样式**/
            /* clear: both;
            content: '111';
            display: block;
            height: 0;
            visibility: hidden; */
        }
        .float-box{
            float: left;
            box-sizing: border-box;
            width: 50%;
            height: 50px;
            border: 1px solid beige;
            background-color: green;
            opacity: 0.5;
        }
        
        .box{
            width: 100%;
            height: 100px;
            background-color: blue;
        }
       
        
    </style>      
    
    <body>
        <div class="float-box-father">
            <div class="float-box"></div>
            <div class="float-box"></div>
            <div class="float-box"></div>
            <div class="float-box"></div>
            <div class="float-box"></div>
            <div class="float-box"></div>
            <div class="float-box"></div>
            <div class="float-box"></div>
            <div class="float-box last-float-box"></div>
        <!--方式1:在box-2前面新增一个div,添加样式clear:both;-->
        <!-- <div style="clear: both;"></div> -->
        </div>
        <div class="box"></div>
</body>

响应式布局

响应式布局指的是允许Web页面适应不同屏幕宽度元素等,进行布局和外观的调整的一系列实践。

  • 媒介查询 (@media)
  • 百分比
  • 多栏布局 column-count
  • 伸缩盒 display:flex
  • 网格 display:grid
  • 响应式图像 使用<picture>元素和<img> srcset和sizes特性
  • rem
  • 视口单位 vw、vh
  • 视口元标签<meta name="viewport" content="width=device-width,initial-scale=1">

css兼容、JS兼容等问题

张鑫旭 鑫空间CSS阅读记录

flex布局

www.zhangxinxu.com/wordpress/2…

vertical-align和line-height的关系

www.zhangxinxu.com/wordpress/2…

zoom和transform:scale的区别

www.zhangxinxu.com/wordpress/2…

自适应表格连续字符换行及单行溢出点点点显示

www.zhangxinxu.com/wordpress/2…

margin:auto实现绝对定位元素的水平垂直居中

www.zhangxinxu.com/wordpress/2…

贝塞尔曲线与CSS3动画、SVG和canvas

www.zhangxinxu.com/wordpress/2…

伪元素表单控件默认样式重置与自定义大全

www.zhangxinxu.com/wordpress/2…

前端性能优化经验分享

www.zhangxinxu.com/wordpress/2…

div模拟textarea文本域轻松实现高度自适应

www.zhangxinxu.com/wordpress/2…