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>

结果:

相关推荐
邹小邹-AI2 分钟前
Rust + 前端:下一个十年的“王炸组合”
开发语言·前端·rust
行走在顶尖4 分钟前
vue3+ant-design-vue
前端
华仔啊39 分钟前
图片标签用 img 还是 picture?很多人彻底弄混了!
前端·html
lichong9511 小时前
XLog debug 开启打印日志,release 关闭打印日志
android·java·前端
烟袅1 小时前
作用域链 × 闭包:三段代码,看懂 JavaScript 的套娃人生
前端·javascript
风止何安啊1 小时前
收到字节的短信:Trae SOLO上线了?尝尝鲜,浅浅做个音乐播放器
前端·html·trae
抱琴_2 小时前
大屏性能优化终极方案:请求合并+智能缓存双剑合璧
前端·javascript
用户463989754322 小时前
Harmony os——长时任务(Continuous Task,ArkTS)
前端
fruge2 小时前
低版本浏览器兼容方案:IE11 适配 ES6 语法与 CSS 新特性
前端·css·es6
颜酱2 小时前
开发工具链-构建、测试、代码质量校验常用包的比较
前端·javascript·node.js