CSS常用布局整理

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

固定寬度布局

1-2-1布局(浮動)

http://www.w3.org/1999/xhtml">
<head>
<title>固定寬度-float</title>
<style type="text/css">

#header,#footer,#container{
    width: 760px;
    margin: 0 auto;
}
#content {
    float: left;
    width: 500px;   
    background: #d5ddf3;
}
#side {
    float: left;
    width: 260px;
    background: #d2d8de;
}
#footer {
    clear: both;
    background: #d5ddf3;
}
</style>
</head>
<body>

    <h2>Page Header</h2>
</div>
<div id="container">
    <div id="content">
        <h2>Page Content 1</h2>
        <p>This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.</p>
    </div>
    <div id="side">
        <h2>Side Bar 1</h2>
        <p>This is side.This is side.This is side.This is side.This is side.This is side.This is side.This is side.</p>
    </div>
</div>
<div id="footer">
        <h2>Page Footer</h2>
</div>

</body>
</html>

 

:

 

正中間的兩個div都浮動起來,且包含塊的寬度是固定值。Footer清除浮動防止浮動塊覆蓋。

1-2-1布局(絕對位置)

http://www.w3.org/1999/xhtml">
<head>
<title>固定寬度-absolute</title>
<style type="text/css">

#header,#footer,#container{
    width: 760px;
    margin: 0 auto;
}
#container{
    position:relative;
}
#content {
    width: 500px;
    margin-right:260px;  
    background: #d5ddf3;
}
#side {
    position:absolute;
    top:0;
    right:0;
    width: 260px;
    background: #d2d8de;
}
#footer {
    background: #d5ddf3;
}
</style>
</head>
<body>

    <h2>Page Header</h2>
</div>
<div id="container">
    <div id="content">
        <h2>Page Content 1</h2>
        <p>This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.</p>
    </div>
    <div id="side">
        <h2>Side Bar 1</h2>
        <p>This is side.This is side.This is side.This is side.This is side.This is side.This is side.This is side.</p>
    </div>
</div>
<div id="footer">
        <h2>Page Footer</h2>
</div>

</body>
</html>

:

 

但是有一點要注意,由於用絕對位置的塊脫離的文檔流,當絕對位置塊高度小於旁邊塊高度的時候,會與其他塊重疊。

流式布局

<html xmlns="http://www.w3.org/1999/xhtml"><head><title>float</title><style type="text/css">#header,#footer,#container{    width: 85%;    margin:0 auto;} #content {    float:left;    width: 66%;       background: #d5ddf3;}#side {    float:right;    width: 33%;    background: #d2d8de;}#footer {    cleat:both;    background: #d5ddf3;}</style></head><body><div id="header">    <h2>Page Header</h2></div><div id="container">    <div id="content">        <h2>Page Content 1</h2>        <p>This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.This is content.</p>    </div>    <div id="side">        <h2>Side Bar 1</h2>        <p>This is side.This is side.This is side.This is side.This is side.This is side.This is side.This is side.</p>    </div></div><div id="footer">        <h2>Page Footer</h2></div></body></html>

:

注意:確保不要使得某列的寬度太大。

另外,百分比布局經常會結合min-width和max-width使用。

 

CSS常用布局整理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.