css面试题(总结版)

css3新特性

  1. 伪类选择器、伪元素选择器
  2. 圆角 border-radius
  3. 盒阴影 box-shadow
  4. 渐变:线性渐变和径向渐变
  5. 过渡 transition
  6. 动画
  7. . 媒体查询
  8. flex布局
  9. grid布局

盒模型

在这里插入图片描述

定位的方式

  • 绝对定位:相对于最近的已开启定位的父元素偏移
  • 相对定位:相对于自身偏移,如果元素没有已定位的父元素,那么它的位置相对于
  • 固定定位:相对于视口偏移
  • 静态定位 : 元素的默认定位方式,无定位的意思
  • 粘性定位 : 行为就像 position:relative; 而当页面滚动超出目标区域时,它的表现就像 position:fixed;,它会固定在目标位置。

响应式布局

媒体查询
px、rem、em、vh、vw
flex弹性布局
grid布局

什么是BFC

BFC直译为块级上下文、是一块独立渲染的区域,区域内的元素不会影响区域外的元素

开启BFC的方法
  1. 开启浮动
  2. 绝对定位、固定定位
  3. overflow除了visible以外的属性值
  4. 根元素就是一个BFC
  5. display为flex、inline-flex、inline-block、table

伪类和伪元素

伪类:伪类选择的是处于某种状态的元素,如:hover 、:active、:visited这种
伪元素:伪元素是对元素中的特定内容操作,如:first-child 、:before这种

怎样清除浮动

高度塌陷:子元素脱离文档流,父元素无子元素支撑高度,造成高度塌陷

  1. 给父元素添加高度
  2. 在父元素结尾添加空div,并设置clear:both
  3. 给父元素clearfix类添加伪元素(推荐)

.clearfix::after{
    content: "";
    display: block;
    clear: both;

引入css link 和@import引入的区别

  • link是HTML方式, @import是CSS方式
  • link最大限度支持并行下载,@import过多嵌套导致串行下载,出现FOUC(文档样式短暂失效)
  • link可以通过rel="alternate stylesheet"指定候选样式
  • 浏览器对link支持早于@import,可以使用@import对老浏览器隐藏样式
  • @import必须在样式规则之前,可以在css文件中引用其他文件

less和sass的区别

  • 最主要的区别就是实现方式。less是基于javsscript的,是在客户端处理的;sass是基于Ruby的,是在服务端处理的
  • less的变量是@开头,sass是“$”开头

三栏布局

双飞翼布局
<style>
    .box {
        background-color: green;
        /* 父盒子清除浮动*/
        overflow: hidden;
    }

    /* 三个盒子都浮动 */
    .left {
        width: 100px;
        height: 100px;
        background-color: aqua;
        float: left;
       margin-left: -100%;
    }

    .center {
        height: 100px;
        width: 100%;
        float: left;
        background-color: black;
    }
    .dom{
        background-color: white;
        margin-left: 100px;
        margin-right: 100px;

    }

    .right {
        width: 100px;
        height: 100px;
        background-color: red;
        float: left;
       margin-left: -100px;


    }
</style>

<body>
    <div class="box">
        <div class="center">
            <div class="dom">
                dom
            </div>
        </div>
        <div class="left"></div>
        <div class="right"></div>
    </div>
</body>
圣杯布局

缺点:当center部分的宽小于left的自身宽度就会发生布局混乱

<style>
    .box {
        padding-left: 100px;
        padding-right: 100px;
        background-color: green;
        /* 父盒子清除浮动*/
        overflow: hidden;
    }

    /* 三个盒子都浮动 */
    .left {
        width: 100px;
        height: 100px;
        background-color: aqua;
        float: left;
        /* 左边盒子左边距-100% */
        margin-left: -100%;
        /* 相对定位搬移下位置 */
        position: relative;
        right: 100px;

    }

    .center {
        height: 100px;
        width: 100%;
        float: left;
        background-color: black;
    }

    .right {
        width: 100px;
        height: 100px;
        background-color: red;
        float: left;
        /* 右边盒子左边距-自身宽度 */
        margin-left: -100px;
        position: relative;
        left: 100px;

    }
</style>

<body>
    <div class="box">
        <div class="center"></div>
        <div class="left"></div>
        <div class="right"></div>
    </div>
</body>

flex三栏
<style>
    .box {
       display: flex;
    }
    .left {
        width: 100px;
        height: 100px;
        background-color: aqua;
    }

    .center {
        height: 100px;
        flex: 1;
        background-color: black;
    }

    .right {
        width: 100px;
        height: 100px;
        background-color: red;
    }
</style>

<body>
    <div class="box">
        <div class="left"></div>

        <div class="center">    
        </div>
        <div class="right"></div>
    </div>
</body>

css居中的方法

水平居中

规定width的元素:margin:auto
文本元素:text-align: center;

水平垂直居中

  • flex布局
<style>
    .box {
        display: flex;
        width: 200px;
        height: 300px;
        background-color: black;
        align-items: center;
        justify-content: center;
    }

    .inner {
        background-color: aliceblue;
        width: 100px;
        height: 100px;
    }
</style>

<body>
    <div class="box">
        <div class="inner"></div>
    </div>
</body>
  • 定位 + margin
<style>
    .box {
        position: relative;
        width: 200px;
        height: 300px;
        background-color: black;

    }

    .inner {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        margin: auto;
        background-color: aliceblue;
        width: 100px;
        height: 100px;
    }
</style>

<body>
    <div class="box">
        <div class="inner"></div>
    </div>
</body>
  • 定位 + translate
<style>
    .box {
        position: relative;
        width: 200px;
        height: 300px;
        background-color: black;

    }

    .inner {
        position: absolute;
        top: 50%;
        left: 50%;
       transform: translate(-50%,-50%);
        background-color: aliceblue;
        width: 100px;
        height: 100px;
    }
</style>

<body>
    <div class="box">
        <div class="inner"></div>
    </div>
</body>
  • table-cell布局
<style>
    .box {
        display: table-cell;
        width: 200px;
        height: 300px;
        background-color: black;
        vertical-align: middle;
        text-align: center;
    }

    .inner {
        display: inline-block;
        background-color: aliceblue;
        width: 100px;
        height: 100px;
    }
</style>

<body>
    <div class="box">
        <div class="inner"></div>
    </div>
</body>
  • grid布局
<style>
    .box {
        display: grid;
        width: 200px;
        height: 300px;
        background-color: black;
       align-items: center;
       justify-content: center;
    }

    .inner {
        background-color: aliceblue;
        width: 100px;
        height: 100px;
    }
</style>

<body>
    <div class="box">
        <div class="inner"></div>
    </div>
</body>

画一个三角形

<style>
    .box {
        width: 0px;
        height: 0px;
        border-top: 100px solid transparent; 
        border-right: 100px solid transparent;
        border-left: 100px solid transparent;
        border-bottom: 100px solid orangered;
        margin: 100px auto;   
    }  
</style>

<body>
    <div class="box">        
    </div>
</body>

文本溢出显示省略号

<style>
    .box {
        width: 100px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
    }
</style>

<body>
    <div class="box">
        暴富暴富
        暴富暴富
        暴富暴富
        暴富暴富
        暴富暴富
        暴富暴富
        暴富暴富
        暴富暴富
        暴富暴富
        暴富暴富
        暴富暴富
        暴富暴富
        暴富暴富
        暴富暴富
        暴富暴富
        暴富暴富
        暴富暴富
        暴富暴富
        暴富暴富
        暴富暴富
        暴富暴富
    </div>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值