W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
所需知識:
<style type="text/css" media="screen">
article {
width: 800px;
margin: 0 auto;
}
</style>
<body>
<article>
<h1>Title</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. A natus repellendus, modi culpa, dolor ducimus debitis, facere dolorum cum aspernatur, soluta molestias est illo vel iusto esse ex ullam amet!</p>
</article>
</body>
NOTE:設(shè)置 auto 會根據(jù)瀏覽器寬度自動設(shè)置外邊距尺寸。在設(shè)置浮動或絕對定位則會使自動居中失效,因為其會脫離文檔流。
(瀏覽器寬度 - 外包含層的寬度)/ 2 = 外邊距。
此方法也同時可以實現(xiàn)橫向多列布局(原理與兩列布局相同)。
所需知識:
<style type="text/css" media="screen">
.clearfix:after {
content: "."; /* Older browser do not support empty content */
visibility: hidden;
display: block;
height: 0;
clear: both;
}
.clearfix {zoom: 1;} /* 針對 IE 不支持 :after */
body {
width: 930px;
margin: 0 auto; /* 橫向居中 */
}
article {
width: 800px;
float: left;
margin-right: 10px;
}
aside {
width: 120px;
float: right;
}
</style>
<body class="clearfix">
<article>
<h1>Title</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Explicabo, quam, fugit. Accusamus voluptates nesciunt in, autem ipsa assumenda a iusto, reiciendis earum repudiandae, nulla natus blanditiis, aliquam asperiores commodi qui.</p>
</article>
<aside>
<h3>Aside Title</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Expedita, molestiae!</p>
</aside>
</body>
應用場合較少,常用與一列定寬,另一列自適應。
需要知識:
注意:固定寬度列的高度需大于自適應的列(原因是絕對定位會脫離文檔流,不能撐開父元素)。
<style type="text/css" media="screen">
body {
position: relative;
width: 100%;
}
article {
position: absolute;
top: 0;
right: 0;
width: 800px;
}
aside {
position: absolute;
top: 0;
right:800px;
left: 0;
}
</style>
<body>
<article>
<h1>Title</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Error obcaecati sint minima totam fuga, tempora, id quia soluta officia iure eligendi sequi non dicta, doloribus accusamus odit fugiat quam quibusdam.</p>
</article>
<aside>
<h3>Aside Title</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Atque, doloremque.</p>
</aside>
</body>
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: