日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

让行内元素(如图片)在div中水平垂直居中 (干货)

發布時間:2023/12/13 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 让行内元素(如图片)在div中水平垂直居中 (干货) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

(1)第一種:用vertical-align

<div class="method1"><span class="tiptop"></span><img class="test" src="img/Dota2.jpg" alt="dota2"> </div><style> .method1{text-align:center; } /*vertical-align:middle 是依賴div內子元素最高的行高來實現對某元素居中的,而我們只需要建立一個新元素,給他加上inline-block屬性 再把他高度設置為100%就行了,在下面的<img>設置vertical-align就生效了*/ .tiptop{height:100%;display:inline-block;vertical-align:middle; } img{vertical-align:middle; } </style>

?

(2)第二種:flex布局(注意瀏覽器兼容性)

<div class="method2"><img src="img_p1_title.png"> </div><style> .method2 {display: flex;justify-content: center; //彈性盒子對象在主軸上的對齊方式align-items: center; //定義flex子項在flex容器的當前行的側軸(縱軸)方向上的對齊方式。background-color:#00a0e9;height:200px; } .method2 img {width:20px;height:30px;background-color:#0A58A0; } </style>

?

(3)position:absolute;絕對定位方式

<div class="method3"><span>第三種方法</span> </div><style> .method3 {width:100%;height: 200px;font-size: 18px;position: relative;background-color:#00a2d4; } .method3 span {width:100px;height:100px;background-color:#00ACED;position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);} </style>

?

(4)使用display:table-cell配合vertical-align:center(淘寶也是這樣用的)

<div class="method4"><span>第四種方法</span> </div><style> .method4 {width: 200px;height: 200px;vertical-align: middle;display: table-cell; /*只支持IE8+及現代瀏覽器,與position:absolute;或float:left;屬性盡量不一起用*/text-align: center;background-color:#00ACED; } .method4 span{width:100px;height:100px;background-color:#0A58A0; } </style>

?

?

簡單易懂吧,點個贊

總結

以上是生活随笔為你收集整理的让行内元素(如图片)在div中水平垂直居中 (干货)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。