CSS經典布局之彈性布局

來源:互聯網
上載者:User

標籤:分享   位置   splay   width   響應式   ems   元素   isp   不同的   

  當我們在瀏覽瀏覽器的時候,經常會放大/縮小瀏覽器的顯示比例,或者在不同的裝置上。所處的解析度也不盡同樣。

因此。我們須要學習一個新的知識:彈性盒模型。

彈性盒模型實現項目對齊,方向,排序(即使項目大小位置動態產生),可以動態改動子項目的寬度和高度。具有良好的適配性。


就是彈性布局一個大概範圍。彈性容器屬性
屬性 說明
flex-direction 設定主軸方向。確定彈性子項目相片順序
flex-wrap 當彈性子項目超出彈性容器範圍是是否換行
flex-flow 複合屬性。flex-direction和flex-wrap雙重屬性
justify-content 主軸上的對齊
align-items 側軸上的對齊
align-content 側軸上有空白,側軸對齊









以下我們用彈性布局的方式實現響應式菜單,先來看代碼:
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Flexbox響應式菜單</title>    <link rel="stylesheet" type="text/css" href="mycss.css"/></head><body><ul class="menu">    <li><a href="#">number1</a></li>    <li><a href="#">number2</a></li>    <li><a href="#">number3</a></li>    <li><a href="#">number4</a></li>    <li><a href="#">number5</a></li>    <li><a href="#">number6</a></li></ul></body></html>
.menu{    list-style-type: none;    padding: 0;    margin: 0;    display: -webkit-flex;    display: -ms-flexbox;    /*display: flex;    //啟用flex方式    flex-flow: row wrap;     //彈性容器的屬性*/}.menu li{    width: auto;    height: 40px;    text-align: center;    line-height: 40px;    flex: 1 1 100%;      //擴充比例為1,收縮比例為1。初始寬度為100%}.menu li:nth-child(1){    background-color: pink;}.menu li:nth-child(2){    background-color: plum;}.menu li:nth-child(3){    background-color: hotpink;}.menu li:nth-child(4){    background-color: palevioletred;}.menu li:nth-child(5){    background-color: deeppink;}.menu li:nth-child(6){    background-color: purple;}.menu li a{    color: black;    text-decoration: none;}@media (max-width: 480px) {    .menu {        flex: 1 1 100%;        flex-flow: row wrap;    }}@media (min-width: 768px){    .menu {        flex-flow: row nowrap;    }}

以上我們就實現了一個彈性布局的一個大概模型。

再說一個我之前看到過的一面試題,也是要求寫一道彈性布局的題,可是我們也能夠另闢蹊徑:實現例如以所看到的的布局要求:sidebar固定寬度200px。content和header寬度自適應,當window寬度小於600px時。變成三列布局
我們實現的思路也跟彈性布局是一樣的,看代碼:
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>flex布局</title>    <style>        *{            margin: 0;            padding: 0;        }       @media (min-width: 600px) {           .header {               width: auto;               background-color: green;           }           .sidebar {               float: left;               width: 200px;               margin-right: -200px;               background-color: gold;           }           .content {               float: left;               width: 100%;               margin-left: 200px;               background-color: red;           }       }        @media (max-width: 600px) {            .header {                width: auto;                background-color: green;            }            .sidebar {                width: auto;                background-color: gold;            }            .content {                width: auto;                background-color: red;            }        }    </style></head><body><div class="header">header</div><div class="sidebar">sidebar</div><div class="content">content</div></body></html>



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.