日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

前端面試中遇到的垂直居中問題

作者:heiyay 更新時(shí)間: 2023-07-03 編程語(yǔ)言

1、flex布局方法:當(dāng)前div的父級(jí)添加flex css樣式

            display: flex;
            -webkit-align-items: center;
            align-items: center;
            -webkit-justify-content: center;
            justify-content: center;

2、絕對(duì)定位方法:不確定當(dāng)前div的寬度和高度,采用 transform: translate(-50%,-50%); 當(dāng)前div的父級(jí)添加相對(duì)定位(position: relative;)

 background: green;       
 position: absolute;      
 left: 50%;          
 top: 50%;        
 transform: translate(-50%,-50%);

3、絕對(duì)定位方法:確定了當(dāng)前div的寬度,margin值為當(dāng)前div寬度一半的負(fù)值

 .father {
            background: red;
            position: relative;
            width: 500px;
            height: 500px;
        }
        .son {
            width: 200px;
            height: 200px;
            background: green;
            position: absolute;
            left: 50%;
            top: 50%;
            margin-left: -100px;
            margin-top: -100px;
        }

4、絕對(duì)定位方法:絕對(duì)定位下top left right bottom 都設(shè)置0 ,margin設(shè)置為auto

.father {
            background: red;
            position: relative;
            width: 500px;
            height: 500px;
        }
        .son {
            width: 200px;
            height: 200px;
            background: green;
            position: absolute;
            left: 0;
            top: 0;
            bottom: 0;
            right: 0;
            margin: auto;
        }

5、table-cell實(shí)現(xiàn)水平垂直居中: table-cell middle center組合使用(子元素不能是塊級(jí)元素) 子div只能不能是塊狀元素,否則只能垂直居中,無法水平居中

/*table-cell、vertical-align、text-align組合使用*/
      
        .father {
            width: 500px;
            height: 500px;
            display: table-cell;
            vertical-align: middle;
            text-align: center;
            background-color: red;
        }
        .son {
            width: 200px;
            height: 200px;
            background-color: green;
            display: inline-block;

原文鏈接:https://blog.csdn.net/study_way/article/details/109004761

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新