在HTML中對于元素居中有哪一些需要注意的事項
發表時間:2024-05-01 來源:明輝站整理相關軟件相關文章人氣:
[摘要]這次給大家帶來在HTML中關于元素居中有哪些需要注意的事項,在HTML中關于元素居中需要注意的事項有哪些,下面就是實戰案例,一起來看一下。不使用定位 水平居中:text-align = center;(可繼承) 豎直居中:margin:0 auto;(塊級元素) 其他居中:1.文...
這次給大家帶來在HTML中關于元素居中有哪些需要注意的事項,在HTML中關于元素居中需要注意的事項有哪些,下面就是實戰案例,一起來看一下。
不使用定位
水平居中:text-align = center;(可繼承)
豎直居中:margin:0 auto;(塊級元素)
其他居中:1.文字居中:父元素設置高 子元素設置高 line-height=height(父元素)
2.圖片居中: vertical-aign:middle ; <-- 必須放在圖片元素中
.first{
width: 300px;
height: 100px;
background-color: black;
color: white;
text-align: center;
margin: 0 auto; //針對塊級元素
}
<div class="first">
不使用定位(1)
</div>
.second{
width: 300px;
height: 100px;
background-color: green;
}
.s_child{
width: 150px;
line-height: 100px;
}
<div class="second">
<div class="s_child">
不使用定位(2)
</div>
</div>
2.定位居中
a.父元素高度固定
父元素:相對定位
子元素:絕對定位
top:50%(父元素高度的一半)
left:50%
margin-top:自己的高度一半;(加負號)
margin-left:自己寬度的一半;(加負號)
.dw_one{
width: 600px;
height: 300px;
position: absolute;
background: black;
}
.dw_one_child{
background: white;
position: relative;
width: 50px;
height: 50px;
top: 50%;
left: 50%;
margin-top: -25px;
margin-left: -25px;
}
<div class="dw_one">
<div class="dw_one_child">
a
</div>
</div>
b.父元素高度不固定
.wrapper{
width: 600px;
height: 600px;
}
.dw_two{
width: 100%;
height: 100%;
position: absolute;
background: black;
}
.dw_two_child{
background: white;
position: relative;
top: 50%;
left: 50%;
width: 100px;
height: 100px;
transform:translate(-50%, -50%);
-moz-transform:translate(-50%, -50%);
-ms-transform:translate(-50%, -50%);
-o-transform:translate(-50%, -50%);
-webkit-transform:translate(-50%, -50%);
}
<div class="wrapper">
<div class="dw_two">
<div class="dw_two_child">
a
</div>
</div>
</div>
3.關于多行文本的居中
使用display:table; display:table-cell;
vertical-align:middle; 居中
#outer{
width: 200px;
height: 200px;
background: #cccccc;
display: table;
_position: relative; // "_"為了兼容IE6
}
#inner{
display: table-cell;
vertical-align: middle;
_position: absolute;
_top: 50%;
}
#content{
_position: relative;
_top: -50%;
}
<div id="outer">
<div id="inner">
<div id="content">
Paradise_追逐者所寫的居中問題總結之我見
</div>
</div>
</div>
相信看了這些案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!
相關閱讀:
怎樣實現HTML的仿命令行界面
meta name="" content="應如何使用
以上就是在HTML中關于元素居中有哪些需要注意的事項的詳細內容,更多請關注php中文網其它相關文章!
網站建設是一個廣義的術語,涵蓋了許多不同的技能和學科中所使用的生產和維護的網站。