Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 欧美日韩午夜,成人国产在线视频,又黄又爽又色的视频

          整合營(yíng)銷服務(wù)商

          電腦端+手機(jī)端+微信端=數(shù)據(jù)同步管理

          免費(fèi)咨詢熱線:

          CSS 中幾種最常用的水平垂直居中的方法

          SS 是前端里面的基礎(chǔ)之一,也是非常重要的一部分,它往往決定了你所做出來(lái)的網(wǎng)頁(yè)頁(yè)面是否美觀。在設(shè)計(jì)網(wǎng)頁(yè)頁(yè)面的過(guò)程中,總會(huì)有將元素或者文字進(jìn)行水平垂直居中的要求。下面w3cschool編程獅就為大家介紹 CSS 中幾種常用到的水平垂直居中的方法。


          一、使用 margin:auto

          當(dāng)元素有給定的高度以及寬度的時(shí)候,使用 margin: auto; 元素僅會(huì)水平居中,并不會(huì)進(jìn)行垂直居中。此時(shí)就需要設(shè)置元素的 position 為 absolute,父級(jí)元素的 position 為 relative,同時(shí)元素的上下左右都需要設(shè)置為 0。

          HTML 代碼

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

          CSS 代碼

          .box{
            width: 200px;
            height: 200px;
            background-color: #eee;
            position: relative;
            margin-top: 20px;
          }
          .center1{
            width: 50px;
            height: 50px;
            background-color: #00ACED;
            margin: auto;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
          }

          效果展示:



          二、使用 position:absolute

          當(dāng)已經(jīng)知道了要進(jìn)行水平垂直居中的元素的寬高時(shí),就可以通過(guò)設(shè)置 position: absolute 來(lái)實(shí)現(xiàn)。但是,使用的同時(shí)還需要結(jié)合其他屬性才完整實(shí)現(xiàn)。因?yàn)椋瑔问窃O(shè)置 absolute,上左距離均為一半,就會(huì)出現(xiàn)下面這種情況。很顯然可以看到,元素并不是完全居中,僅只有左上角的位置在中心點(diǎn)

          概念圖:

          因此想要實(shí)現(xiàn)元素完全水平垂直居中,在設(shè)置了 absolute 定位后,可以設(shè)置 margin 值為負(fù),或者使用 calc 來(lái)計(jì)算,上左距離在 50% 的基礎(chǔ)上還要減去元素本身一半的寬高。

          margin 值為負(fù)或者 calc 計(jì)算均是在已知元素寬高的情況下,假設(shè)不知道元素的寬高,那么怎么實(shí)現(xiàn)水平垂直居中呢?這里就可以使用 transform 屬性,通過(guò)坐標(biāo)位移來(lái)實(shí)現(xiàn)居中。

          CSS 代碼

          /* 結(jié)合 margin */
          .center2{
            width: 50px;
            height: 50px;
            background-color: #7FFFD4;
            position: absolute;
            left: 50%;
            top: 50%;
            margin-left: -25px;
            margin-top: -25px;
          }
          /* 結(jié)合 calc 計(jì)算*/
          .center2{
            width: 50px;
            height: 50px;
            background-color: #7FFFD4;
            position: absolute;
            left: calc(50% - 25px)
            top: calc(50% - 25px);
          }
          /* 結(jié)合 transform */
          .center2{
          width: 50px;
          height: 50px;
          background-color: #7FFFD4;
          position: absolute;
          left: 50%;
          top: 50%;
          transform: translate(-50%, -50%);
          }

          效果展示



          03

          PART

          三、使用彈性布局

          可以通過(guò)彈性布局來(lái)設(shè)置水平垂直居中,這里需要設(shè)置父級(jí)元素 display:flex; 還需要設(shè)置兩個(gè)屬性,水平布局 justify-content 以及垂直布局 align-items。

          HTML代碼

          <div class="box2">
            <div class="center4"></div>
          </div>

          CSS代碼:

          .box2{
            background-color: #eee;
            width: 200px;
            height: 200px;
            position: relative;
            margin-top: 20px ;
            display: flex;
            justify-content: center;
            align-items: center;
          }
          .center4{
            width: 50px;
            height: 50px;
            background-color: #B39873;
          }

          效果展示:


          四、文本水平對(duì)齊和行高

          前面介紹的是元素如何實(shí)現(xiàn)水平垂直居中,下面介紹的是如何將文字進(jìn)行水平垂直居中。這第一個(gè)方法也是最經(jīng)常用的,使用文本水平對(duì)齊 text-align 和行高 line-height 來(lái)實(shí)現(xiàn)的。

          HTML 代碼

          <div class="box3">
            <div class="center5">文字居中</div>
          </div>

          CSS 代碼

          .box3{
            background-color: #eee;
            width: 200px;
            height: 200px;
            margin-top: 20px;
          }
          .center5{
            text-align: center;
            line-height: 200px;
          }

          效果展示


          05

          PART

          五、使用網(wǎng)格布局

          第二個(gè)方法可以通過(guò)網(wǎng)格布局 grid 來(lái)實(shí)現(xiàn)。而這里通過(guò) grid 有兩種方式實(shí)現(xiàn),一種對(duì)元素本身屬性進(jìn)行設(shè)置,另一種在元素的父級(jí)元素中設(shè)置。兩者看上去內(nèi)容似乎差不多,不同的是在元素中設(shè)置的是 align-self 還要多了一個(gè) margin,父級(jí)元素中是 align-items。

          相關(guān)代碼:

          /* grid 元素中設(shè)置 */
          .box4{
            background-color: #eee;
            width: 200px;
            height: 200px;
            margin-top: 20px;
            display: grid;
          }
          .center6{
            align-self: center;
            justify-content: center;
            margin: auto;
          }
          /* grid 父級(jí)元素中設(shè)置 */
          .box5{
            background-color: #eee;
            width: 200px;
            height: 200px;
            margin-top: 20px;
            display: grid;
            align-items: center;
            justify-content: center;
          }
          
          

          效果展示:


          六、總結(jié)

          以上就是關(guān)于 CSS 如何將元素或者文字進(jìn)行水平垂直居中的幾種常用方法,大家還其他關(guān)于 CSS 實(shí)現(xiàn)水平垂直居中的方法嗎?請(qǐng)?jiān)谠u(píng)論區(qū)留下你的想法。

          關(guān)注w3cschool編程獅訂閱更多IT資訊、技術(shù)干貨~

          始設(shè)置的樣子

          text-align: center;


          、CSS 垂直居中

          1、父元素display:table-cell;vertical-align:center,里面的子元素就會(huì)實(shí)現(xiàn)垂直居中,不需要知道子元素的寬高

          /* HTML */
          <div class='father'>
            <div class='son'></div>
          </div>
          <style>
            .father {
          	display: table-cell;
          	vertical-align: middle;
          	width: 300px;
          	height: 300px;
          	border: 3px solid red;
            }
            .son {
          	width: 50px;
          	height: 50px;
          	background-color: aqua;
            }
          </style>
          復(fù)制代碼
          • 效果展示


          2、absolute+margin:auto,定位為 absolute 的元素垂直居中,不需要知道該元素的寬高

          <!-- HTMl -->
          <div class="father">
          	<div class="son"></div>
          </div>
          <style>
            .father {
          	position: relative;
          	width: 300px;
          	height: 300px;
          	border: 3px solid red;
            }
            .son {
          	position: absolute;
          	background-color: aqua;
          	width: 50px;
          	height: 50px;
          	top: 0;
          	bottom: 0;
          	margin: auto;
            }
          </style>
          復(fù)制代碼
          • 效果展示


          3、absolute+負(fù)margin,定位為 absolute 的元素垂直居中,需要知道該元素的寬高

          <!-- HTMl -->
          <div class="father">
          	<div class="son"></div>
          </div>
          <style>
            .father {
          	position: relative;
          	width: 300px;
          	height: 300px;
          	border: 3px solid red;
            }
            .son {
          	position: absolute;
          	width: 100px;
          	height: 100px;
          	background-color: aqua;
          	top: 50%;
          	/* 負(fù)margin須是高度的一半 */
          	margin-top: -50px;
            }
          </style>
          復(fù)制代碼
          • 效果展示


          4、absolute+calc(css3計(jì)算屬性),定位為 absolute 的元素垂直居中,需要知道該元素的寬高

          <!-- HTMl -->
          <div class="father">
          	<div class="son"></div>
          </div>
          <style>
            .father {
          	position: relative;
          	width: 300px;
          	height: 300px;
          	border: 3px solid red;
            }
            .son {
          	position: absolute;
          	width: 100px;
          	height: 100px;
          	background-color: aqua;
          	/* 注意"-"兩邊要隔開(kāi) 減去的須是高度的一半*/
          	top: calc(50% - 50px);
            }
          </style>
          復(fù)制代碼
          • 效果展示


          5、absolute+transform,定位為 absolute 的元素垂直居中,不需要知道元素的寬高

          <!-- HTMl -->
          <div class="father">
          	<div class="son"></div>
          </div>
          <style>
            .father {
          	position: relative;
          	width: 300px;
          	height: 300px;
          	border: 3px solid red;
            }
            .son {
          	position: absolute;
          	width: 100px;
          	height: 100px;
          	background-color: aqua;
          	top: 50%;
          	transform: translateY(-50%);
            }
          </style>
          復(fù)制代碼
          • 效果展示


          6、line-height,父元素:line-height=height。子元素:display:inline-block。子元素垂直居中,不需要知道子元素的寬高

          <!-- HTMl -->
          <div class="father">
          	<div class="son"></div>
          </div>
          <style>
            .father {
          	width: 300px;
          	height: 300px;
          	border: 3px solid red;
          	line-height: 300px;
            }
            .son {
          	background-color: aqua;
          	width: 100px;
          	height: 100px;
          	display: inline-block;
          	vertical-align: middle;
            }
          </style>
          復(fù)制代碼
          • 效果展示


          7、flex,目前主流的布局方案,父元素為 flex 容器且添加 align-items: center,控制子元素的布局。不需要知道子元素的寬高

          <!-- HTMl -->
          <div class="father">
          	<div class="son"></div>
          </div>
          <style>
            .father {
          	width: 300px;
          	height: 300px;
          	border: 3px solid red;
          	display: flex;
          	align-items: center;
            }
            .son {
          	background-color: aqua;
          	width: 100px;
          	height: 100px;
            }
          </style>
          復(fù)制代碼
          • 效果展示

          8、grid ,目前最強(qiáng)大的布局方案,使用還尚未流行。父元素為 grid,子元素添加 align-self: center。不需要知道子元素的寬高

          <!-- HTMl -->
          <div class="father">
          	<div class="son"></div>
          </div>
          <style>
            .father {
          	width: 300px;
          	height: 300px;
          	border: 3px solid red;
          	display: grid;
            }
            .son {
          	background-color: aqua;
          	width: 100px;
          	height: 100px;
          	align-self: center;
            }
          </style>
          復(fù)制代碼
          • 效果展示


          9、偽元素after或before,這是我搜出來(lái)整理的。CSS 真的太神(s)奇(d)了,毫無(wú)道理。子元素垂直居中不需要知道寬高

          <!-- HTMl -->
          <div class="father">
          	<div class="son"></div>
          </div>
          <style>
            .father {
          	width: 300px;
          	height: 300px;
          	border: 3px solid red;
          	display: block;
            }
            .father::after {
          	content: "";
          	display: inline-block;
          	vertical-align: middle;
          	height: 100%;
            }
            .son {
          	background-color: aqua;
          	width: 50px;
          	height: 50px;
          	display: inline-block;
          	vertical-align: middle;
            }
          </style>
          復(fù)制代碼
          • 效果展示


          10、隱藏節(jié)點(diǎn)(盒子)實(shí)現(xiàn) 該原理就是使用盒子占位置,但不顯示出該盒子。另外的盒子垂直居中,子盒子的寬高需由實(shí)際計(jì)算時(shí)確定

          <!-- HTML -->
          <div class="father">
          	<div class="hide"></div>
          	<div class="son"></div>
          </div>
          <style>
            .father {
          	width: 300px;
          	height: 300px;
          	border: 3px solid red;
            }
            .son {
          	background-color: aqua;
          	width: 50%;
          	height: 50%;
            }
            .hide {
          	width: 50px;
          	height: 25%;
           }
          </style>
          復(fù)制代碼
          • 效果展示


          11、writing-mode,這是搜索整理而來(lái),參考資料見(jiàn)最后。子元素盒子 display: inline-block。子元素垂直居中,不需要知道該盒子的寬高

          <!-- HTML -->
          <div class="father">
          	<div class="son"></div>
          </div>
          <style>
            .father {
          	width: 300px;
          	height: 300px;
          	border: 3px solid red;
          	writing-mode: vertical-lr;
          	text-align: center;
            }
            .son {
          	background-color: aqua;
          	width: 100px;
          	height: 100px;
          	writing-mode: horizontal-tb;
          	display: inline-block;
            }
          </style>
          復(fù)制代碼
          • 效果展示


          三、參考資料


          作者:soloplayer
          鏈接:https://juejin.cn/post/6904138129612439560
          來(lái)源:掘金


          主站蜘蛛池模板: AV天堂午夜精品一区| 精品国产香蕉伊思人在线在线亚洲一区二区 | 色狠狠色噜噜Av天堂一区| 99无码人妻一区二区三区免费| 国产成人无码精品一区不卡| 亚洲一区二区三区免费观看| 久久久无码精品国产一区| 国产综合无码一区二区三区| 精品人妻少妇一区二区三区在线| 亚洲.国产.欧美一区二区三区| 日韩内射美女人妻一区二区三区| 中文字幕日本一区| 精品福利一区二区三| 伊人激情AV一区二区三区| 中文日韩字幕一区在线观看| 中文字幕一区在线| 国产成人精品一区二区三区无码| 国产另类ts人妖一区二区三区| 亚洲午夜日韩高清一区| 一区二区三区高清在线| 一区二区三区免费视频播放器 | 久久国产精品视频一区| 国精品无码一区二区三区左线| 中文字幕日韩人妻不卡一区| 国产成人无码AV一区二区| 日韩视频一区二区三区| 中文字幕一区二区精品区| 精品欧洲av无码一区二区14| 任你躁国语自产一区在| 中文字幕一区在线观看视频 | 一区二区三区日韩| 99久久无码一区人妻a黑| 午夜性色一区二区三区不卡视频| 午夜福利无码一区二区| 免费在线观看一区| 91福利国产在线观看一区二区| 国产爆乳无码一区二区麻豆| 肉色超薄丝袜脚交一区二区| 午夜一区二区免费视频| 国产福利电影一区二区三区,亚洲国模精品一区 | av一区二区三区人妻少妇|