關於flex佈局

flex佈局:

flex佈局中,一些屬性定義會失效,如float、clear等css

關於flex佈局的屬性:html

主軸和交叉軸

在flex佈局中,能夠想象爲在二維座標系中,主軸爲x軸,交叉軸爲y軸,而且交叉軸和主軸呈90度,flex-direction能夠設置主軸的方向瀏覽器

容器和項目:

在flex佈局中有兩種角色,容器能夠理解爲父元素,項目能夠理解爲子元素bash

容器:六個屬性

  • flex-direction:決定主軸的方向

flex-direction:column佈局

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>test</title>
   <style type="text/css">
       .container{
           display: flex;
           flex-direction: row;
       }
       .container>div{
           background-color: yellow;
           width: 40px;
           height: 40px;
           margin: 10px;
       }
   </style>
</head>
<body>
   <div class="container">
       <div></div>
       <div></div>
       <div></div>
       <div></div>
   </div>
</body>
</html>
複製代碼

flex-direction:row(默認)flex

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style type="text/css">
        .container{
            display: flex;
            flex-direction: column;
        }
        .container>div{
            background-color: yellow;
            width: 40px;
            height: 40px;
            margin: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>
</body>
</html>
複製代碼

  • flex-wrap:決定主軸裏的項目是否換行
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style type="text/css">
        .container{
            display: flex;
            flex-direction: row;
            width: 250px;
            /*默認爲flex-wrap:nowrap*/
            flex-wrap: wrap;
        }
        .container>div{
            background-color: yellow;
            width: 40px;
            height: 40px;
            margin: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>
</body>
</html>
複製代碼

  • flex-flow:flex-direction和flex-wrap的集合
  • align-items:項目的對齊方式(交叉軸上的對齊)

align-items:flex-endui

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style type="text/css">
        .container{
            display: flex;
            flex-direction: row;
            width: 600px;
            height: 300px;
            /*默認爲flex-wrap:nowrap*/
            flex-wrap: wrap;
            align-items: flex-end;
        }
        .container>div{
            background-color: yellow;
            width: 40px;
            height: 40px;
            margin: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>
</body>
</html>
複製代碼

align-items:centerurl

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style type="text/css">
        .container{
            display: flex;
            flex-direction: row;
            width: 600px;
            height: 300px;
            /*默認爲flex-wrap:nowrap*/
            flex-wrap: wrap;
            align-items: center;
        }
        .container>div{
            background-color: yellow;
            width: 40px;
            height: 40px;
            margin: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>
</body>
</html>
複製代碼

  • justify-content:容器中項目的對齊方式(主軸軸上的對齊)

justify-content:flex-endspa

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style type="text/css">
        .container{
            display: flex;
            flex-direction: row;
            width: 600px;
            height: 300px;
            /*默認爲flex-wrap:nowrap*/
            flex-wrap: wrap;
            align-items: center;
            outline: 1px solid black;
            justify-content: flex-end;
        }
        .container>div{
            background-color: yellow;
            width: 40px;
            height: 40px;
            margin: 10px;
        }
    </style>
</head>
<body>
<div class="container">
    <div></div><div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div>
</div>
</body>
</html>
複製代碼

justify-content: center3d

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style type="text/css">
        .container{
            display: flex;
            flex-direction: row;
            width: 600px;
            height: 300px;
            /*默認爲flex-wrap:nowrap*/
            flex-wrap: wrap;
            align-items: center;
            outline: 1px solid black;
            justify-content: center;
        }
        .container>div{
            background-color: yellow;
            width: 40px;
            height: 40px;
            margin: 10px;
        }
    </style>
</head>
<body>
<div class="container">
    <div></div><div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div>
</div>
</body>
</html>
複製代碼

  • align-content:多根軸線的對齊方式(一根軸線無效)

align-content: flex-end

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style type="text/css">
        .container{
            display: flex;
            flex-direction: row;
            width: 600px;
            height: 300px;
            /*默認爲flex-wrap:nowrap*/
            flex-wrap: wrap;
            align-items: center;
            outline: 1px solid black;
            justify-content: center;
            align-content: flex-end;
        }
        .container>div{
            background-color: yellow;
            width: 40px;
            height: 40px;
            margin: 10px;
        }
    </style>
</head>
<body>
<div class="container">
    <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div>
</div>
</body>
</html>
複製代碼

項目:六個屬性

  • order:排列順序

先初始化一個頁面以下:

咱們將偶數的div排到前面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style type="text/css">
        .container{
            display: flex;
            flex-direction: row;
            width: 600px;
            height: 300px;
            /*默認爲flex-wrap:nowrap*/
            flex-wrap: wrap;
            align-items: center;
            outline: 1px solid black;
            justify-content: center;
            /*align-content: flex-end;*/
        }
        .container>div{
            background-color: yellow;
            width: 40px;
            height: 40px;
            margin: 10px;
        }
        .container>div:nth-child(2n+1){
            order: 3;
        }
    </style>
</head>
<body>
<div class="container">
    <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> <div>10</div> <div>11</div> <div>12</div>
</div>
</body>
</html>
複製代碼

運行效果:

  • flex-grow:有剩餘空間,決定是否擴張(默認:0)

未佔滿一個主軸時:

設置flex-frow:1時:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style type="text/css">
        .container{
            display: flex;
            flex-direction: row;
            width: 600px;
            height: 300px;
            /*默認爲flex-wrap:nowrap*/
            flex-wrap: nowrap;
            align-items: center;
            outline: 1px solid black;
            justify-content: center;
            /*align-content: flex-end;*/
        }
        .container>div{
            background-color: yellow;
            flex-basis: 40px;
            margin: 10px;
            flex-grow: 1;
            /*flex-shrink: 0;*/
        }
    </style>
</head>
<body>
<div class="container">
     <div>6</div> <div>7</div> <div>8</div> <div>9</div> <div>10</div> <div>11</div> <div>12</div>
</div>
</body>
</html>
複製代碼

  • flex-shrink:太滿,決定是否縮小(默認:1)

未佔滿一個主軸時:

沾滿並溢出主軸時:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style type="text/css">
        .container{
            display: flex;
            flex-direction: row;
            width: 600px;
            height: 300px;
            /*默認爲flex-wrap:nowrap*/
            flex-wrap: nowrap;
            align-items: center;
            outline: 1px solid black;
            justify-content: center;
            /*align-content: flex-end;*/
        }
        .container>div{
            background-color: yellow;
            flex-basis: 40px;
            margin: 10px;
            /*flex-shrink: 0;*/
        }
    </style>
</head>
<body>
<div class="container">
    <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> <div>10</div> <div>11</div> <div>12</div>
</div>
</body>
</html>
複製代碼

設置flex-shrink:0時:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style type="text/css">
        .container{
            display: flex;
            flex-direction: row;
            width: 600px;
            height: 300px;
            /*默認爲flex-wrap:nowrap*/
            flex-wrap: nowrap;
            align-items: center;
            outline: 1px solid black;
            justify-content: center;
            /*align-content: flex-end;*/
        }
        .container>div{
            background-color: yellow;
            flex-basis: 40px;
            margin: 10px;
            flex-shrink: 0;
        }
    </style>
</head>
<body>
<div class="container">
    <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> <div>10</div> <div>11</div> <div>12</div>
</div>
</body>
</html>
複製代碼

  • flex-basis:項目的寬度
  • flex:flex-grow,flex-shrink,flex-basis
  • align-self:項目在交叉軸上的排列

關於雪碧圖

雪碧圖:background-position 響應式界面下:background-size:定位雪碧圖的大小 給定一張圖片,咱們想象是不少圖片的組合

截取2號圖片做爲背景圖

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style type="text/css">
        .container{
            width: 50px;
            height: 50px;
            background-image: url("./test.png");
            background-position: -60px 0px;
            outline: 1px solid black;
        }
    </style>
</head>
<body>
    <div class="container"></div>
</body>
</html>
複製代碼

運行效果:

關於雪碧圖的兼容: 一、兼容部分老的IE瀏覽器:能夠使用background-position-x & background-position-y進行兼容 二、兼容部分老版瀏覽器:使用css hock技術

相關文章
相關標籤/搜索