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

學無先后,達者為師

網站首頁 編程語言 正文

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

作者:heiyay 更新時間: 2023-07-03 編程語言

1、flex布局方法:當前div的父級添加flex css樣式

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

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

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

3、絕對定位方法:確定了當前div的寬度,margin值為當前div寬度一半的負值

 .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、絕對定位方法:絕對定位下top left right bottom 都設置0 ,margin設置為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實現水平垂直居中: table-cell middle center組合使用(子元素不能是塊級元素) 子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

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