Css三欄布局自適應實現幾種方法

來源:互聯網
上載者:User

標籤:

Css三欄布局自適應實現幾種方法

自適應實現方法我們可以從三個方法來做,一個是絕對位置 ,自身浮動法 和margin負值法了,下面我們一起來看看這三個例子吧,希望例子能協助到各位同學。

絕對位置法三欄布局自適應:

 代碼如下

複製代碼

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>絕對位置法</title>
<style>
 .parent{
  margin:auto;
  background:#09F;
  position:relative;
 }
 .left{
  width:200px;
  height:200px;
  background:#369;
  position:absolute;
  left:0;
  top:0;
 }
 .right{
  width:250px;
  height:200px;
  background:#C0C;
  position:absolute;
  right:0;
  top:0;
 }
 .center{
  background:#F00;
  margin:0 250px 0 200px;
  height:200px;
 }
</style>
</head>
<body>
<div class="parent">
    <div class="center"></div>
    <div class="left"></div>
    <div class="right"></div>
</div>
</body>
</html>

說明:三個div元素可以互換位置。

自身浮動法三欄布局自適應:

 代碼如下

複製代碼

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>自身浮動法</title>
<style>
 .parent{
  margin:auto;
  background:#09F;
 }
 .left{
  width:200px;
  height:200px;
  background:#369;
  float:left;
 }
 .right{
  width:250px;
  height:200px;
  background:#C0C;
  float:right;
 }
 .center{
  background:#F00;
  margin:0 250px 0 200px;
  height:200px;
 }
</style>
</head>
<body>
<div class="parent">
    <div class="left"></div>
    <div class="right"></div>
    <div class="center"></div>
</div>
</body>
</html>

說明:.center所在的div必須在.left和.right之後出現,.left和.right可以互換位置。

margin負值法三欄布局自適應:

 代碼如下

複製代碼

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>margin負值法</title>
<style>
 .parent{
  margin:auto;
  background:#09F;
 }
 .left{
  width:200px;
  height:200px;
  background:#369;
  margin-left:-100%;
 }
 .right{
  width:250px;
  height:200px;
  background:#C0C;
  margin-left:-250px;
 }
 .center{
  background:#F00;
  width:100%;
 }
 .main{
  margin:0 250px 0 200px;
  height:200px;
 }
 .fl{
  float:left;
 }
</style>
</head>
<body>
<div class="parent">
    <div class="center fl">
        <div class="main">Main</div>
    </div>
    <div class="left fl"></div>
    <div class="right fl"></div>
</div>
</body>
</html>

說明:.main為中間主題部分,放到最前面,優先載入;.main外面必須有元素包裹,且寬度為100%;

 

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.