CSS(布局+flex布局)+HTML(二)

目录

一、flex的介绍

1.什么是flex?

2.flex的使用

二、flex属性 

1. flex-direction属性

1)介绍

2)使用方法

2.flex-warp属性

1) 介绍

2)使用方法

3.  justify-content属性 

1)介绍

2)使用方法

4.  align-items属性

 1)介绍

2)使用方法

5.  align-content属性 

1)介绍

2)使用方法

三、结语


一、flex的介绍

1.什么是flex?

Flex是Flexible Box的缩写,意为”弹性布局”,用来为盒状模型提供最大的灵活性。

2.flex的使用

首先我们先来写一个无序列表

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <ul>
        <li>HTML</li>
        <li>CSS</li>
        <li>JavaScript</li>
        <li>VUE</li>
        <li>React</li>
    </ul>
</body>
</html>

然后为其添加css样式

 <style>
        *{
            //取消全局的margin和padding值
            margin: 0;
            padding: 0;
        }
        ul{
            //取消ul的默认样式
            list-style: none;
            //给其宽高
            width: 800px;
            height: 80px;
            //让其居中显示
            margin: 0 auto;
            
        }
        ul li{
            //给li背景颜色
            background-color: pink;
            //让文字垂直水平居中
            text-align: center;
            line-height: 80px;
            
        }
        ul li:hover{
            //添加鼠标划入以后的背景颜色
            background-color:rgba(255, 192, 203, 0.5);

        }
    </style>

然后我们就得到这样的效果,目前所有的文字都是垂直下来的,如果想让li横着过去的话那我们就需要用到flex布局

那么如何添加呢?

我们需要为父级也就是ul添加flex布局属性,其中我们在li上添加了一个flex为1的属性,是为了等分ul,效果如下

 <style>
        * {
            margin: 0;
            padding: 0;
        }

        ul {
            list-style: none;
            width: 800px;
            height: 80px;
            margin: 0 auto;
            display: flex;
        }

        ul li {
            background-color: pink;
            text-align: center;
            line-height: 80px;
            flex: 1;
        }

        ul li:hover {
            background-color: rgba(255, 192, 203, 0.5);

        }
    </style>

 效果就变成这个样子:

二、flex属性 

1. flex-direction属性

1)介绍

flex-direction属性决定主轴的方向(即项目排列方向)

flex-direction: row(水平方向起点在左) | row-reverse(水平方向起点在右) | column(垂直方向起点在上) | column-reverse;(垂直方向起点在下

2)使用方法

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            width: 850px;
            border: 1px solid pink;
            /* 让当前div.box成为flex容器 */
            display: flex;
            /*  flex-direction: column(垂直方向起点在上)这里我设置成垂直方向,大家可以试试                                    其他属性 */
            flex-direction: column;
           
        }
        .box>div{
             width: 200px;
             height: 200px;
             background-color: pink;
             margin: 5px;
        }
    </style>
</head>
<body>
    <div class="box">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
        <div>5</div>
        <div>6</div>
     
    </div>
</body>
</html>

运行结果如下,这里图片太大就只截图一半下面一样

2.flex-warp属性

1) 介绍

  flex-warp 换行 flex-wrap: nowrap (不换行)| wrap(换行,第一行在上方) | wrap-reverse(换行,第一行在下方); 

补充:flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap。

flex-flow: <flex-direction> <flex-wrap>;

2)使用方法

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            width: 850px;
            border: 1px solid pink;
            /* 让当前div.box成为flex容器 */
            display: flex;                   
          
            flex-wrap: wrap;            
            flex-flow:  row wrap;

        }
        .box>div{
             width: 200px;
             height: 200px;
             background-color: pink;
             margin: 5px;
        }
    </style>
</head>
<body>
    <div class="box">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
        <div>5</div>
        <div>6</div>
     
    </div>
</body>
</html>

运行结果(博主使用的是flex-wrap: wrap; 属性大家可以自行尝试其他属性值,这里就不做过多展示)

3.  justify-content属性 

1)介绍

ustify-content属性定义了项目在主轴上的对齐方式。

  • flex-start(默认值):左对齐
  • lex-end:右对齐
  • center: 居中
  • space-between:两端对齐,项目之间的间隔都相等。
  • space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。
  • space-evenly:flex项目之间的间隔以及项目与 flex 容器边缘之间的间隔都会是相等的。

2)使用方法

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            width: 850px;
            border: 1px solid pink;
            /* 让当前div.box成为flex容器 */
            display: flex;   
            /* 这里使用的是center属性其他属性大家可以尝试使用*/
           justify-content: center;

        }
        .box>div{
             width: 200px;
             height: 200px;
             background-color: pink;
             margin: 5px;
        }
    </style>
</head>
<body>
    <div class="box">
        <div>1</div>
        <div>2</div>
       
     
    </div>
</body>
</html>

 运行结果,我们可以看到现在两个盒子是水平居中显示的

4.  align-items属性

 1)介绍

align-items: flex-start | flex-end | center | baseline | stretch;

  • flex-start:交叉轴的起点对齐。
  • flex-end:交叉轴的终点对齐。
  • center:交叉轴的中点对齐。
  • baseline: 项目的第一行文字的基线对齐。
  • stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。

2)使用方法

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            width: 850px;
            height: 300px;
          
            border: 1px solid pink;
            /* 让当前div.box成为flex容器 */
            display: flex;   
            /* 这里使用的是flex-end的属性值,大家可以根据自己需求自行替换其他属性  */
           align-items: flex-end;

        }
        .box>div{
             width: 200px;
             height: 200px;
             background-color: pink;
             margin: 5px;
        }
    </style>
</head>
<body>
    <div class="box">
        <div>1</div>
        <div>2</div>
       
     
    </div>
</body>
</html>

运行结果,这时我们可以看到粉色盒子在父级盒子的下方。

5.  align-content属性 

1)介绍

align-content属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。

  • align-content: flex-start | flex-end | center | space-between | space-around | stretch;
  • flex-start:与交叉轴的起点对齐。
  • flex-end:与交叉轴的终点对齐。
  • center:与交叉轴的中点对齐。
  • space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。
  • space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。
  • stretch(默认值):轴线占满整个交叉轴。

2)使用方法

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            width: 850px;
            height: 600px;
          
            border: 1px solid pink;
            /* 让当前div.box成为flex容器 */
            display: flex; 
            /* 这里我们允许换行 并且使用 align-content:space-between; 交叉轴两端对齐,轴线之间的间隔平均分布*/
            flex-wrap: wrap;  
            align-content:space-between;

        }
        .box>div{
             width: 200px;
             height: 200px;
             background-color: pink;
             margin: 5px;
        }
    </style>
</head>
<body>
    <div class="box">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
        <div>5</div>
        <div>6</div>
        <div>7</div>
       
     
    </div>
</body>
</html>

运行结果

三、结语

 目前这一节的flex布局先到此为止,下一节我们继续来看其他属性及其用法。

注:若作者博客其中有侵权内容,请及时与作者联系,会及时进行撤回更改

我们下一节再见~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值