了解盒子模型的弹性盒子布局

CSS 盒子模型(Box Model)

所有HTML元素可以看作盒子,在CSS中,"box model"这一术语是用来设计和布局时使用。

CSS盒模型本质上是一个盒子,封装周围的HTML元素,它包括:边距margin,边框border,填充padding,和实际内容(文本、或者图片、或者其它标签)。

盒子模型允许我们在其它元素和周围元素边框之间的空间放置元素。

元素的宽度和高度

重点: 当你指定一个 CSS 元素的宽度和高度属性时,你只是设置内容区域的宽度和高度。要知道,完整元素大小还包括外边距、边框、内边距。

下面的例子中的元素的总宽度为300px:

js 复制代码
<style>
   div {
        background-color: pink;
        width: 300px;
        border: 25px solid skyblue; /* 设置上右下左边框:每个占25px */
        padding: 25px; /* 设置上右下左内边距:每个占25px */
        margin: 25px; /* 设置上右下左外边距:每个占25px */
    }
</style>

我们实际定义为300px,那么此时我们再来计算一下盒子的总宽度:

300px+50px(左右间距)+50px(左右边框)+50px(左右填充) = 450px

flex(弹性盒子布局)

什么是弹性盒子?

display:flex 是一种布局方式。它既可以应用于容器中,也可以应用于行内元素。

容器默认存在两根轴:水平的主轴(main axis)和垂直的交叉轴(cross axis)。 采用Flex布局的元素,称为Flex容器(flex container),简称"容器"。它的所有子元素自动成为容器成员,称为Flex项目(flex item)。

注:设为Flex布局以后,子元素的floatclearvertical-align属性将失效。

容器的几个属性

以下这几个属性都是用在容器上面

  • flex-direction
  • flex-wrap
  • flex-flow
  • justify-content
  • align-items
  • align-content

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

  • column-reverse:主轴为垂直方向,起点在下沿。
  • column:主轴为垂直方向,起点在上沿。
  • row(默认值):主轴为水平方向,起点在左端。
  • row-reverse:主轴为水平方向,起点在右端。

justify-content(定义项目在主轴上的对齐方式)
他的属性值有:

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

align-items(定义项目在竖直方向上对齐方式)
它的属性值有:

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

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

  • flex-start:与交叉轴的起点对齐。
  • flex-end:与交叉轴的终点对齐。
  • center:与交叉轴的中点对齐。
  • space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。
  • space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。
  • stretch(默认值):轴线占满整个交叉轴。
js 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0px;
            padding: 0px;
        }
        .u1{
            width: 200px;
            height: 200px;
            border-radius: 10px;
            background-color: black;
            display: flex;
            /* justify-content: space-between; */
            justify-content: space-between;
            /* justify-content: end; */
            /* justify-content: center; */
            flex-direction: column;
            /* align-items: center; */
            /* align-items: start; */
            /* align-items: end; */
            /* transform: skew(90deg,-90deg); */
        }
        li{
            list-style: none;
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background-color: white;
        }
        .d1{
            width: 200px;
            height: 200px;
            border-radius: 10px;
            background-color: black;
            display: flex;
            /* justify-content: space-between; */
            /* justify-content: space-between; */
            /* justify-content: center; */
            /* justify-content: end; */
            /* flex-direction: column; */
            /* align-items: center; */
            /* align-items: start; */
            /* align-items: end; */
            /* transform: skew(90deg,-90deg); */
        }
        .d2{
            width: 200px;
            height: 200px;
            border-radius: 10px;
            background-color: black;
            display: flex;
            /* justify-content: space-between; */
            /* justify-content: space-between; */
            justify-content: center;
            /* justify-content: end; */
            flex-direction: column;
            align-items: center;
            /* align-items: start; */
            /* align-items: end; */
            /* transform: skew(90deg,-90deg); */
        }
        .d3{
            width: 200px;
            height: 200px;
            border-radius: 10px;
            background-color: black;
            display: flex;
            /* justify-content: space-between; */
            /* justify-content: space-between; */
            /* justify-content: center; */
            justify-content: end;
            flex-direction: column;
            /* align-items: center; */
            /* align-items: start; */
            align-items: end;
            /* transform: skew(90deg,-90deg); */ 
        }
    </style>
</head>
<body>
    <ul class="u1">
        <div class="d1">
<li></li>
        </div>
        <div class="d2">
<li></li>
        </div>
        <div class="d3">
            <li></li>
            </div>
    </ul>
</body>
</html>
相关推荐
码农君莫笑9 小时前
深入理解 CSS Grid 布局:从入门到实战
前端·css
用户0595401744612 小时前
把Agent记忆测试从Mock换到真实Redis,漏测率从30%降到0
前端·css
LIUAWEIO1 天前
CSS 让鼠标呈现手型,鼠标悬浮变小手
css·html·css3·html5
ZC跨境爬虫1 天前
跟着 MDN 学CSS day_51:支持旧浏览器的布局策略
前端·css·html·tensorflow·媒体
Larcher1 天前
从 0 到 1:Node.js 调用 AI API 的完整避坑指南
前端·javascript·css
八目蛛1 天前
八目蛛网络(免费工具网站导航)
css·vue.js·开源·vue3·html5·ai编程
xiangxiongfly9151 天前
CSS @layer总结
css·layer
大家的林语冰1 天前
CSS 新函数上市,一行代码让文本自动变色,无需 JS 也能符合 W3C 无障碍对比度标准
前端·javascript·css
DFT计算杂谈1 天前
VASP 磁性结构可视化:一键生成完美 VESTA / MCIF
java·前端·css·html·css3
ZC跨境爬虫2 天前
跟着 MDN 学CSS day_49:定位实例练习从入门到精通
前端·css·学习