HTML CSS第七次笔记

1 弹性盒子换行

1.1 实现重点

·使用justify-content: space-between;实现主轴对齐
·使用margin-bottom: 14px;实现小li上下间隙
·使用flex-wrap: wrap;实现弹性盒子换行

html 复制代码
<!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: 0 和 padding: 0 用于清除所有元素的默认外边距和内边距 */
    /* box-sizing: border-box 设置盒模型的计算方式,使得元素的宽度和高度包含内边距和边框 */
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    .mi {
      /* 父控子(布局) */
      display: flex;
      /* 主轴分布(两个子盒子贴两边) */
      justify-content: space-between;
      width: 1226px;
      height: 614px;
      /* background-color: pink; */
      margin: 100px auto;
    }

    .left {
      width: 234px;
      height: 614px;
      background-color: skyblue;
    }

    .right {
      width: 978px;
      height: 614px;
      background-color: purple;
    }

    .right ul {
      display: flex;
      /* 弹性盒子换行 */
      flex-wrap: wrap;
      justify-content: space-between;
      
    }

    .right li {
      width: 234px;
      height: 300px;
      background-color: orange;
      /* 去除小li的小点 */
      list-style: none;
      margin-bottom: 14px;
    }
  </style>
</head>

<body>
  <div class="mi">
    <div class="left"></div>
    <div class="right">
      <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
        <li>7</li>
        <li>8</li>
      </ul>
    </div>
  </div>
</body>

</html>

结果:

2 多行主轴对齐(align-content)

align-content与align-items(测轴对齐)的区别是前者是多行对齐,后者是单行对齐

2.1 实现重点

·使用justify-content: space-between;实现左右没缝隙(主轴对齐)
·使用align-content: space-between;实现上下没缝隙(测轴对齐)

html 复制代码
<!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>
    .box {
      display: flex;
      flex-wrap: wrap;
      /* 左右没缝隙(主轴对齐) */
      justify-content: space-between;
      /* 上下没缝隙(测轴对齐) */
      align-content: space-between;
      width: 1000px;
      height: 500px;
      border: 1px solid #000;
      margin: 100px auto;
    }

    .box div {
      width: 300px;
      height: 200px;
      background-color: pink;
    }
  </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>

结果:

3 某个弹性盒子测轴对齐方式(align-self)

3.1 实现重点

·使用align-self实现只移动一个子盒子
·再在css中使用.box:nth-child(n)选中某个盒子给他添加样式

html 复制代码
<!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>
    .box {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      /* 只让其中一个移动 */

      width: 600px;
      height: 600px;
      border: 1px solid #000;
      margin: 100px auto;
    }

    .box div {
      width: 100px;
      height: 100px;
      background-color: pink;
      border-radius: 50%;
    }

    .box div:nth-child(2) {
      /* 只移动某一个盒子 */
      align-self: center;
    }

    .box div:nth-child(3) {
      align-self: flex-end;
    }
  </style>
</head>

<body>
  <div class="box">
    <div></div>
    <div></div>
    <div></div>
  </div>
</body>

</html>

结果:

4 修改主轴方向(flex-direction)

html 复制代码
<!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>
    .box {
      display: flex;
      /* 默认主轴是横向排列(row),此时要更改主轴方向 */
      flex-direction: column;
      width: 300px;
      height: 300px;
      border: 1px solid #000;
      /* margin: 100px auto; */
      justify-content: center;
      align-items: center;

    }

    .box div {
      width: 100px;
      height: 100px;
      background-color: pink;
    }
  </style>
</head>

<body>
  <div class="box">
    <div>1</div>
    <div>2</div>
  </div>
</body>

</html>

结果:

5 弹性伸缩比(align-content)

html 复制代码
<!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>
    .box {
      display: flex;
      /* 宽度占浏览器的百分之八十 */
      width: 80%;
      height: 150px;
      border: 1px solid #000;
      margin: 50px auto;
    }

    .box div {
      background-color: pink;
      /* 给子盒子添加 */
      /* 子盒子平分父盒子的宽度  写∏就代表分几份*/
      flex: 1;
    }

    .box div:nth-child(2) {
      background-color: purple;
      flex: 2;
      margin: 0 10px;
    }
  </style>
</head>

<body>
  <div class="box">
    <div>1</div>
    <div>2</div>
    <div>3</div>
  </div>

</body>

</html>

结果:

相关推荐
kyriewen6 小时前
我手写了一个 EventEmitter,面试官追问了 6 个问题——第 4 个我没答上来
前端·javascript·面试
IT_陈寒7 小时前
Java的Date类又坑了我一次,改用时间戳真香
前端·人工智能·后端
小林攻城狮7 小时前
使用 Transport 节流解决 Vercel AI SDK 流式渲染卡死问题
前端·react.js
前端缘梦7 小时前
告别 TS 运行时类型漏洞!Zod 完整入门实战教程(前端 / 全栈必备)
前端·react.js·全栈
the_answer8 小时前
Webpack vs Vite 深度对比分析
前端·webpack
转转技术团队8 小时前
验证码识别实战:前端不写页面,改训模型了?
前端
MomentYY8 小时前
Temperature:AI 的“脑洞旋钮”
前端·llm·ai编程
远航_8 小时前
OpenSpec 完整详细介绍
前端·后端
召钱熏8 小时前
状态枚举正确≠渲染正确:一个语音按钮的状态机边界修复实录
android·前端
SkyWalking中文站9 小时前
认识 Horizon UI · 1/17:SkyWalking 新一代可观测性控制台
运维·前端·监控