爆款云主机2核4G限时秒杀,88元/年起!
查看详情

活动

天翼云最新优惠活动,涵盖免费试用,产品折扣等,助您降本增效!
热门活动
  • 618智算钜惠季 爆款云主机2核4G限时秒杀,88元/年起!
  • 免费体验DeepSeek,上天翼云息壤 NEW 新老用户均可免费体验2500万Tokens,限时两周
  • 云上钜惠 HOT 爆款云主机全场特惠,更有万元锦鲤券等你来领!
  • 算力套餐 HOT 让算力触手可及
  • 天翼云脑AOne NEW 连接、保护、办公,All-in-One!
  • 中小企业应用上云专场 产品组合下单即享折上9折起,助力企业快速上云
  • 息壤高校钜惠活动 NEW 天翼云息壤杯高校AI大赛,数款产品享受线上订购超值特惠
  • 天翼云电脑专场 HOT 移动办公新选择,爆款4核8G畅享1年3.5折起,快来抢购!
  • 天翼云奖励推广计划 加入成为云推官,推荐新用户注册下单得现金奖励
免费活动
  • 免费试用中心 HOT 多款云产品免费试用,快来开启云上之旅
  • 天翼云用户体验官 NEW 您的洞察,重塑科技边界

智算服务

打造统一的产品能力,实现算网调度、训练推理、技术架构、资源管理一体化智算服务
智算云(DeepSeek专区)
科研助手
  • 算力商城
  • 应用商城
  • 开发机
  • 并行计算
算力互联调度平台
  • 应用市场
  • 算力市场
  • 算力调度推荐
一站式智算服务平台
  • 模型广场
  • 体验中心
  • 服务接入
智算一体机
  • 智算一体机
大模型
  • DeepSeek-R1-昇腾版(671B)
  • DeepSeek-R1-英伟达版(671B)
  • DeepSeek-V3-昇腾版(671B)
  • DeepSeek-R1-Distill-Llama-70B
  • DeepSeek-R1-Distill-Qwen-32B
  • Qwen2-72B-Instruct
  • StableDiffusion-V2.1
  • TeleChat-12B

应用商城

天翼云精选行业优秀合作伙伴及千余款商品,提供一站式云上应用服务
进入甄选商城进入云市场创新解决方案
办公协同
  • WPS云文档
  • 安全邮箱
  • EMM手机管家
  • 智能商业平台
财务管理
  • 工资条
  • 税务风控云
企业应用
  • 翼信息化运维服务
  • 翼视频云归档解决方案
工业能源
  • 智慧工厂_生产流程管理解决方案
  • 智慧工地
建站工具
  • SSL证书
  • 新域名服务
网络工具
  • 翼云加速
灾备迁移
  • 云管家2.0
  • 翼备份
资源管理
  • 全栈混合云敏捷版(软件)
  • 全栈混合云敏捷版(一体机)
行业应用
  • 翼电子教室
  • 翼智慧显示一体化解决方案

合作伙伴

天翼云携手合作伙伴,共创云上生态,合作共赢
天翼云生态合作中心
  • 天翼云生态合作中心
天翼云渠道合作伙伴
  • 天翼云代理渠道合作伙伴
天翼云服务合作伙伴
  • 天翼云集成商交付能力认证
天翼云应用合作伙伴
  • 天翼云云市场合作伙伴
  • 天翼云甄选商城合作伙伴
天翼云技术合作伙伴
  • 天翼云OpenAPI中心
  • 天翼云EasyCoding平台
天翼云培训认证
  • 天翼云学堂
  • 天翼云市场商学院
天翼云合作计划
  • 云汇计划
天翼云东升计划
  • 适配中心
  • 东升计划
  • 适配互认证

开发者

开发者相关功能入口汇聚
技术社区
  • 专栏文章
  • 互动问答
  • 技术视频
资源与工具
  • OpenAPI中心
开放能力
  • EasyCoding敏捷开发平台
培训与认证
  • 天翼云学堂
  • 天翼云认证
魔乐社区
  • 魔乐社区

支持与服务

为您提供全方位支持与服务,全流程技术保障,助您轻松上云,安全无忧
文档与工具
  • 文档中心
  • 新手上云
  • 自助服务
  • OpenAPI中心
定价
  • 价格计算器
  • 定价策略
基础服务
  • 售前咨询
  • 在线支持
  • 在线支持
  • 工单服务
  • 建议与反馈
  • 用户体验官
  • 服务保障
  • 客户公告
  • 会员中心
增值服务
  • 红心服务
  • 首保服务
  • 客户支持计划
  • 专家技术服务
  • 备案管家

了解天翼云

天翼云秉承央企使命,致力于成为数字经济主力军,投身科技强国伟大事业,为用户提供安全、普惠云服务
品牌介绍
  • 关于天翼云
  • 智算云
  • 天翼云4.0
  • 新闻资讯
  • 天翼云APP
基础设施
  • 全球基础设施
  • 信任中心
最佳实践
  • 精选案例
  • 超级探访
  • 云杂志
  • 分析师和白皮书
  • 天翼云·创新直播间
市场活动
  • 2025智能云生态大会
  • 2024智算云生态大会
  • 2023云生态大会
  • 2022云生态大会
  • 天翼云中国行
天翼云
  • 活动
  • 智算服务
  • 产品
  • 解决方案
  • 应用商城
  • 合作伙伴
  • 开发者
  • 支持与服务
  • 了解天翼云
      • 文档
      • 控制中心
      • 备案
      • 管理中心

      CSS-伸缩布局

      首页 知识中心 软件开发 文章详情页

      CSS-伸缩布局

      2023-07-17 08:10:17 阅读次数:84

      CSS3,html

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      .one {
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      overflow: hidden;
      }

      .one > li {
      float: left;
      width: 100px;
      height: 100px;
      background-color: pink;
      margin: 20px;
      text-align: center;
      line-height: 100px;
      font-size: 35px;
      }

      .two {
      border: 1px solid #000;
      margin: 0 auto;
      list-style: none;
      display: flex;
      }

      .two > li {
      width: 100px;
      height: 100px;
      background-color: pink;
      margin: 20px;
      text-align: center;
      line-height: 100px;
      font-size: 35px;
      }
      </style>
      </head>
      <body>
      <ul class="one">
      <li>1</li>
      <li>2</li>
      <li>3</li>
      </ul>
      <ul class="two">
      <li>1</li>
      <li>2</li>
      <li>3</li>
      </ul>
      </body>
      </html>

      主轴方向

      在伸缩布局中, 默认伸缩项是从左至右的排版的,主轴的排版的方向默认就是 ​​row​​, 默认就是从左至右,

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-主轴方向</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      flex-direction: row;
      }

      ul > li {
      width: 100px;
      height: 100px;
      background-color: pink;
      text-align: center;
      line-height: 100px;
      font-size: 35px;
      margin: 20px;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      </ul>
      </body>
      </html>

      修改主轴的排版方向为从右至左

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-主轴方向</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      flex-direction: row-reverse;
      }

      ul > li {
      width: 100px;
      height: 100px;
      background-color: pink;
      text-align: center;
      line-height: 100px;
      font-size: 35px;
      margin: 20px;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      </ul>
      </body>
      </html>

      告诉系统把主轴的方向改为垂直方向

      注意点

      • 默认情况下主轴是水平方向的,但是也可以修改为垂直方向
      • 只要看到​​flex-direction: column​​​ /​​column-reverse​​ 就代表主轴被修改为了垂直方向
      • 如果将主轴修改为了垂直方向,那么侧轴就会自动从垂直方向转换为水平方向

      修改主轴的排版方向为从上至下

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-主轴方向</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      flex-direction: column;
      }

      ul > li {
      width: 100px;
      height: 100px;
      background-color: pink;
      text-align: center;
      line-height: 100px;
      font-size: 35px;
      margin: 20px;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      </ul>
      </body>
      </html>

      修改主轴的排版方向为从下至上

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-主轴方向</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      flex-direction: column-reverse;
      }

      ul > li {
      width: 100px;
      height: 100px;
      background-color: pink;
      text-align: center;
      line-height: 100px;
      font-size: 35px;
      margin: 20px;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      </ul>
      </body>
      </html>

      主轴对齐方式

      属性名

      作用

      justify-content

      用于设置伸缩项主轴上的对齐方式

      如果设置为 ​​flex-start​​, 代表告诉系统伸缩项和主轴的起点对齐

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-主轴对齐方式</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      width: 600px;
      height: 600px;
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      justify-content: flex-start;
      }

      ul > li {
      width: 100px;
      height: 100px;
      background-color: pink;
      text-align: center;
      line-height: 100px;
      font-size: 35px;
      }

      ul > li:nth-child(2) {
      background-color: skyblue;
      }

      ul > li:nth-child(3) {
      background-color: yellow;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      </ul>
      </body>
      </html>

      如果设置为 ​​flex-end​​, 代表告诉系统伸缩项和主轴的终点对齐

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-主轴对齐方式</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      width: 600px;
      height: 600px;
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      justify-content: flex-end;
      }

      ul > li {
      width: 100px;
      height: 100px;
      background-color: pink;
      text-align: center;
      line-height: 100px;
      font-size: 35px;
      }

      ul > li:nth-child(2) {
      background-color: skyblue;
      }

      ul > li:nth-child(3) {
      background-color: yellow;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      </ul>
      </body>
      </html>

      居中对齐

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-主轴对齐方式</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      width: 600px;
      height: 600px;
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      justify-content: center;
      }

      ul > li {
      width: 100px;
      height: 100px;
      background-color: pink;
      text-align: center;
      line-height: 100px;
      font-size: 35px;
      }

      ul > li:nth-child(2) {
      background-color: skyblue;
      }

      ul > li:nth-child(3) {
      background-color: yellow;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      </ul>
      </body>
      </html>

      两端对齐

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-主轴对齐方式</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      width: 600px;
      height: 600px;
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      justify-content: space-between;
      }

      ul > li {
      width: 100px;
      height: 100px;
      background-color: pink;
      text-align: center;
      line-height: 100px;
      font-size: 35px;
      }

      ul > li:nth-child(2) {
      background-color: skyblue;
      }

      ul > li:nth-child(3) {
      background-color: yellow;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      </ul>
      </body>
      </html>

      环绕对齐

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-主轴对齐方式</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      width: 600px;
      height: 600px;
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      justify-content: space-around;
      }

      ul > li {
      width: 100px;
      height: 100px;
      background-color: pink;
      text-align: center;
      line-height: 100px;
      font-size: 35px;
      }

      ul > li:nth-child(2) {
      background-color: skyblue;
      }

      ul > li:nth-child(3) {
      background-color: yellow;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      </ul>
      </body>
      </html>

      侧轴对齐方式

      属性名

      作用

      align-items

      修改侧轴的对齐方式

      默认情况下是以侧轴的起点对齐,align-items 属性的取值也就是 ​​flex-start​​

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-侧轴对齐方式</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      width: 600px;
      height: 600px;
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      align-items: flex-start;
      }

      ul > li {
      width: 100px;
      background-color: pink;
      text-align: center;
      line-height: 100px;
      font-size: 35px;
      }

      ul > li:nth-child(2) {
      background-color: skyblue;
      }

      ul > li:nth-child(3) {
      background-color: yellow;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>j</li>
      <li>y</li>
      <li>z</li>
      </ul>
      </body>
      </html>

      align-items 属性的取值是 ​​flex-end​​ 的话也就是相反呗,也就是以侧轴的终点对齐

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-侧轴对齐方式</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      width: 600px;
      height: 600px;
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      align-items: flex-end;
      }

      ul > li {
      width: 100px;
      background-color: pink;
      text-align: center;
      line-height: 100px;
      font-size: 35px;
      }

      ul > li:nth-child(2) {
      background-color: skyblue;
      }

      ul > li:nth-child(3) {
      background-color: yellow;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>j</li>
      <li>y</li>
      <li>z</li>
      </ul>
      </body>
      </html>

      居中对齐

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-侧轴对齐方式</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      width: 600px;
      height: 600px;
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      align-items: center;
      }

      ul > li {
      width: 100px;
      background-color: pink;
      text-align: center;
      line-height: 100px;
      font-size: 35px;
      }

      ul > li:nth-child(2) {
      background-color: skyblue;
      }

      ul > li:nth-child(3) {
      background-color: yellow;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>j</li>
      <li>y</li>
      <li>z</li>
      </ul>
      </body>
      </html>

      注意点

      和主轴不同的是, 侧轴没有 ​​两端对齐​​​ 和 ​​环绕对齐​​

      基线对齐

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-侧轴对齐方式</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      width: 600px;
      height: 600px;
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      align-items: baseline;
      }

      ul > li {
      width: 100px;
      background-color: pink;
      text-align: center;
      line-height: 100px;
      font-size: 35px;
      }

      ul > li:nth-child(2) {
      background-color: skyblue;
      }

      ul > li:nth-child(3) {
      background-color: yellow;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>j</li>
      <li>y</li>
      <li>z</li>
      </ul>
      </body>
      </html>

      拉伸对齐

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-侧轴对齐方式</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      width: 600px;
      height: 600px;
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      align-items: stretch;
      }

      ul > li {
      width: 100px;
      background-color: pink;
      text-align: center;
      line-height: 100px;
      font-size: 35px;
      }

      ul > li:nth-child(2) {
      background-color: skyblue;
      }

      ul > li:nth-child(3) {
      background-color: yellow;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>j</li>
      <li>y</li>
      <li>z</li>
      </ul>
      </body>
      </html>

      侧轴对齐方式 Two

       

      • 如果在伸缩容器中通过​​align-items​​ 修改侧轴的对齐方式, 是修改所有伸缩项侧轴的对齐方式
      • 如果是在伸缩项中通过​​align-self​​ 修改侧轴的对齐方式, 是单独修改当前伸缩项侧轴的对齐方式
      • ​​align-self​​​ 属性的取值和​​align-items​​ 一样

       

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-侧轴对齐方式2</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      width: 600px;
      height: 600px;
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      }

      ul > li {
      width: 100px;
      height: 100px;
      background-color: pink;
      text-align: center;
      line-height: 100px;
      font-size: 35px;
      }

      ul > li:nth-child(1) {
      align-self: flex-start;
      }

      ul > li:nth-child(2) {
      background-color: skyblue;
      align-self: center;
      }

      ul > li:nth-child(3) {
      background-color: yellow;
      align-self: flex-end;
      }

      ul > li:nth-child(4) {
      background-color: green;
      align-self: stretch;
      }

      ul > li:nth-child(5) {
      background-color: indianred;
      align-self: baseline;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>j</li>
      <li>y</li>
      <li>z</li>
      <li>s</li>
      <li>b</li>
      </ul>
      </body>
      </html>

      主轴侧轴方向问题

       

      • 默认情况下主轴是水平方向, 侧轴是垂直方向
      • 默认情况下主轴的起点在伸缩容器的最左边, 主轴的终点在伸缩容器的最右边
      • 默认情况下侧轴的起点在伸缩容器的最顶部, 侧轴的终点在伸缩容器的最底部
      • 我们可以通过​​flex-direction​​ 属性修改主轴的方向
      • 如果​​flex-direction​​​ 属性的取值是​​column​​​ /​​column-reverse​​ 就代表将主轴的方向修改为了垂直方向, 那么侧轴就会立刻变为水平方向

       

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-主轴侧轴方向问题</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      width: 600px;
      height: 600px;
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      justify-content: space-around;
      flex-direction: column;
      align-items: center;
      }

      ul > li {
      width: 100px;
      height: 100px;
      background-color: pink;
      text-align: center;
      line-height: 100px;
      font-size: 35px;
      }

      ul > li:nth-child(1) {

      }

      ul > li:nth-child(2) {
      background-color: skyblue;
      }

      ul > li:nth-child(3) {
      background-color: yellow;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>j</li>
      <li>y</li>
      <li>z</li>
      </ul>
      </body>
      </html>

      换行问题

       

      • 在伸缩布局中, 如果伸缩容器的宽度不够, 系统会自动压缩伸缩项的宽度, 保证所有的伸缩项都能放在伸缩容器中
      • 如果当伸缩容器宽度不够时, 不想让伸缩项被压缩, 也可以让系统进行换行​​flex-wrap: wrap;​​
      • 默认情况下如果伸缩容器的高度比换行之后所有伸缩项的高度还要高, 那么系统会自动将剩余空间平分之后添加给每一行

       

      宽度不够也不换行, 默认取值

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-换行问题</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      width: 400px;
      height: 600px;
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      flex-wrap: nowrap;
      }

      ul > li {
      width: 200px;
      height: 200px;
      background-color: pink;
      text-align: center;
      line-height: 200px;
      font-size: 35px;
      }

      ul > li:nth-child(2) {
      background-color: skyblue;
      }

      ul > li:nth-child(3) {
      background-color: yellow;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      </ul>
      </body>
      </html>

      宽度不够换行

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-换行问题</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      width: 400px;
      height: 600px;
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      flex-wrap: wrap;
      }

      ul > li {
      width: 200px;
      height: 200px;
      background-color: pink;
      text-align: center;
      line-height: 200px;
      font-size: 35px;
      }

      ul > li:nth-child(2) {
      background-color: skyblue;
      }

      ul > li:nth-child(3) {
      background-color: yellow;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      </ul>
      </body>
      </html>

      规定灵活的伸缩项在必要的时候拆行或拆列,但是会以相反的顺序

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-换行问题</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      width: 400px;
      height: 600px;
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      flex-wrap: wrap-reverse;
      }

      ul > li {
      width: 200px;
      height: 200px;
      background-color: pink;
      text-align: center;
      line-height: 200px;
      font-size: 35px;
      }

      ul > li:nth-child(2) {
      background-color: skyblue;
      }

      ul > li:nth-child(3) {
      background-color: yellow;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      </ul>
      </body>
      </html>

      换行对齐方式

      如果伸缩容器中的伸缩项换行了, 那么我们就可以通过 ​​align-content​​ 来设置行与行之间的对齐方式

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-换行对齐方式</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      width: 400px;
      height: 600px;
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      flex-wrap: wrap;
      /*
      以起点对齐
      align-content: flex-start;
      以结束点对齐
      align-content: flex-end;
      以居中对齐
      align-content: center;
      以基线对齐
      align-content: space-between;
      以拉伸对齐
      align-content: space-around;
      */
      }

      ul > li {
      width: 200px;
      height: 200px;
      background-color: pink;
      text-align: center;
      line-height: 200px;
      font-size: 35px;
      }

      ul > li:nth-child(2) {
      background-color: skyblue;
      }

      ul > li:nth-child(3) {
      background-color: yellow;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      </ul>
      </body>
      </html>

      默认情况下换行就是拉伸对齐, 一定要注意, 在换行中的拉伸对齐是指, 所有行的高度总和要和伸缩容器的高度一样,所以会将多余的空间平分之后添加给每一行

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-换行对齐方式</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      width: 400px;
      height: 600px;
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      flex-wrap: wrap;
      /*
      默认值。元素被拉伸以适应容器。
      各行将会伸展以占用剩余的空间。
      如果剩余的空间是负数,该值等效于 'flex-start'。
      在其它情况下,剩余空间被所有行平分,以扩大它们的侧轴尺寸。
      */
      align-content: stretch;
      }

      ul > li {
      width: 200px;
      height: 200px;
      background-color: pink;
      text-align: center;
      line-height: 200px;
      font-size: 35px;
      }

      ul > li:nth-child(2) {
      background-color: skyblue;
      }

      ul > li:nth-child(3) {
      background-color: yellow;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      </ul>
      </body>
      </html>

      伸缩项排序

       

      • 如果想调整伸缩布局中伸缩项的顺序, 那么我们可以通过修改伸缩项的​​order​​ 属性来实现
      • 默认情况下 order 的取值是 0
      • 如果我们设置了 order 属性的值, 那么系统就会按照设置的值从小到大的排序

       

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-伸缩项排序</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      width: 400px;
      height: 400px;
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      }

      ul > li {
      width: 100px;
      height: 100px;
      background-color: pink;
      text-align: center;
      line-height: 100px;
      font-size: 35px;
      }

      ul > li:nth-child(1) {
      order: -1;
      }

      ul > li:nth-child(2) {
      background-color: skyblue;
      order: 2;
      }

      ul > li:nth-child(3) {
      background-color: yellow;
      order: 1;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      </ul>
      </body>
      </html>

      伸缩项放大比例

      属性名

      作用

      flex-grow

      当所有伸缩项宽度的总和没有伸缩容器宽度大的时, 我们可以通过 flex-grow 让系统调整伸缩项的宽度, 以便于让所有伸缩项的宽度的总和等于伸缩容器的宽度

      flex-grow 计算公式

       

      • 计算剩余的空间:伸缩容器宽度 - 所有伸缩项的宽度,400 - 300 = 100
      • 计算每份剩余空间的宽度:剩余空间 / 需要的份数,100 / 6 = 16.66
      • 计算每个伸缩项最终的宽度:伸缩项的宽度 + 需要的份数 * 每份的宽度

       

      注意点

      flex-grow 的默认值是 0

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-伸缩项放大比例</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      width: 400px;
      height: 400px;
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      }

      ul > li {
      width: 100px;
      height: 100px;
      background-color: pink;
      text-align: center;
      line-height: 100px;
      font-size: 35px;
      }

      ul > li:nth-child(1) {
      flex-grow: 1;
      }

      ul > li:nth-child(2) {
      background-color: skyblue;
      flex-grow: 2;
      }

      ul > li:nth-child(3) {
      background-color: yellow;
      flex-grow: 3;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      </ul>
      </body>
      </html>

      伸缩项缩小比例

      属性名

      作用

      flex-shrink

      当所有伸缩项宽度的总和比伸缩容器宽度大的时, 我们可以通过 flex-shrink 让系统调整伸缩项的宽度, 以便于让所有伸缩项的宽度的总和等于伸缩容器的宽度

      flex-shrink 计算公式

       

      • 计算每个伸缩项需要压缩的宽度:计算溢出的宽度,伸缩容器的宽度 - 所有伸缩项的宽度总和,400 - 600 = -200
      • 计算总权重:每个伸缩项需要的份数 * 每个伸缩项的宽度,1 * 200 + 2 * 200 + 3 * 200 = 1200
      • 计算每个伸缩项需要压缩的宽度, 溢出的宽度 * 需要的份数 * 每个伸缩项的宽度 / 总权重,-200 * 1 * 200 / 1200 = -33.33

       

      注意点

      flex-shrink 的默认值是 1

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-伸缩项缩小比例</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      width: 400px;
      height: 400px;
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      }

      ul > li {
      width: 200px;
      height: 200px;
      background-color: pink;
      text-align: center;
      line-height: 200px;
      font-size: 35px;
      }

      ul > li:nth-child(1) {
      flex-shrink: 1;
      }

      ul > li:nth-child(2) {
      background-color: skyblue;
      flex-shrink: 2;
      }

      ul > li:nth-child(3) {
      background-color: yellow;
      flex-shrink: 3;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      </ul>
      </body>
      </html>

      伸缩项放大缩小比例

       

      • 如果有伸缩项没有设置​​flex-grow​​, 那么系统会保持原有的宽度
      • 而会将多余的宽度等分之后, 按照每个伸缩项需要的份数添加给它们
      • 如果想让某个伸缩项不缩小, 那么需要将它的​​flex-shrink​​​ 设置为​​0​​

       

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-伸缩项放大缩小比例</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      width: 400px;
      height: 400px;
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      }

      ul > li {
      width: 200px;
      height: 200px;
      background-color: pink;
      text-align: center;
      line-height: 200px;
      font-size: 35px;
      }

      ul > li:nth-child(1) {
      flex-shrink: 0;
      }

      ul > li:nth-child(2) {
      background-color: skyblue;
      flex-grow: 1;
      }

      ul > li:nth-child(3) {
      background-color: yellow;
      flex-grow: 4;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      </ul>
      </body>
      </html>

      伸缩项宽度

      如果是伸缩布局, 除了可以通过元素的 ​​width​​​ 属性来设置宽度以外, 还可以通过 ​​flex-basis​​ 属性来设置伸缩项的宽度

      注意点

       

      • width 属性可以设置宽度 / flex-basis 也可以设置宽度
      • 那么如果两者同时存在, 系统会听 flex-basis 的
      • flex-basis 属性是专门提供给伸缩布局使用的
      • 如果同时通过这两个属性设置了宽度, 但是其中一个是 auto, 那么系统会按照具体值来设置

       

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-伸缩项宽度</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      width: 800px;
      height: 400px;
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      }

      ul > li {
      height: 200px;
      background-color: pink;
      text-align: center;
      line-height: 200px;
      font-size: 35px;
      }

      ul > li:nth-child(1) {
      flex-basis: 100px;
      width: auto;
      }

      ul > li:nth-child(2) {
      background-color: skyblue;
      flex-basis: 200px;
      }

      ul > li:nth-child(3) {
      background-color: yellow;
      flex-basis: 300px;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      </ul>
      </body>
      </html>

      伸缩项属性简写

      flex: flex-grow flex-shrink flex-basis;

      默认值:flex: 0 1 auto;

      注意点

      在连写格式中, flex-shrink、flex-basis 是可以省略的

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>伸缩布局-伸缩项属性简写</title>
      <style>
      * {
      margin: 0;
      padding: 0;
      }

      ul {
      width: 800px;
      height: 400px;
      border: 1px solid #000;
      margin: 100px auto;
      list-style: none;
      display: flex;
      }

      ul > li {
      width: 200px;
      height: 200px;
      background-color: pink;
      text-align: center;
      line-height: 200px;
      font-size: 35px;
      }

      ul > li:nth-child(1) {
      flex: 1;
      }

      ul > li:nth-child(2) {
      background-color: skyblue;
      flex: 2;
      }

      ul > li:nth-child(3) {
      background-color: yellow;
      flex: 3;
      }
      </style>
      </head>
      <body>
      <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      </ul>
      </body>
      </html>
      版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://blog.51cto.com/u_15652665/5329981,作者:qq628b229e2808e,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。

      上一篇:Stream流式编程详解

      下一篇:JavaScript-属性注意点

      相关文章

      2025-05-19 09:04:53

      读取模版并生成html文件

      读取模版并生成html文件

      2025-05-19 09:04:53
      exception , html , string
      2025-05-14 10:33:25

      webpack5基础--09_处理其他资源

      webpack5基础--09_处理其他资源

      2025-05-14 10:33:25
      html , index , 图标 , 处理 , 资源 , 音视频
      2025-05-12 08:40:18

      jquery tmpl 详解

      jquery tmpl 详解

      2025-05-12 08:40:18
      html , HTML , jquery
      2025-05-09 08:51:09

      RestTemplate请求UnknownContentTypeException:no suitable HttpMessageConverter异常

      RestTemplate请求UnknownContentTypeException:no suitable HttpMessageConverter异常

      2025-05-09 08:51:09
      html , RestTemplate , text , 日志 , 格式
      2025-04-14 08:45:56

      Java实现一个坦克大战的小游戏【附源码】

      Java实现一个坦克大战的小游戏【附源码】

      2025-04-14 08:45:56
      html , i++ , java
      2025-04-14 08:45:36

      一段诡异的JavaScript代码,其实考察的是一些JavaScript的基础知识

      一段诡异的JavaScript代码,其实考察的是一些JavaScript的基础知识

      2025-04-14 08:45:36
      html , javascript
      2025-04-14 08:45:36

      用尾递归(tailRecursive)实现的阶乘-JavaScript版本

      用尾递归(tailRecursive)实现的阶乘-JavaScript版本

      2025-04-14 08:45:36
      html
      2025-04-11 07:15:54

      Javaweb编程中的乱码问题

      程序中的乱码问题,主要出现在我们处理中文数据的过程中出现。从浏览器向服务器请求数据,服务器返回的数据在浏览器中显示为乱码。或者是服务器中的java文件用到中文,也有可能会出现乱码。数据库在处理数据的时候,也会碰到乱码问题。

      2025-04-11 07:15:54
      html , java , UTF , 乱码 , 数据 , 浏览器
      2025-04-11 07:08:33

      JavaScript|数据类型的使用

      JavaScript|数据类型的使用

      2025-04-11 07:08:33
      Boolean , document , html , script , var , write
      2025-04-01 09:21:49

      数据结构14-栈常见操作3

      数据结构14-栈常见操作3

      2025-04-01 09:21:49
      html , javascript , Stack , 前端
      查看更多
      推荐标签

      作者介绍

      天翼云小翼
      天翼云用户

      文章

      33561

      阅读量

      5250016

      查看更多

      最新文章

      读取模版并生成html文件

      2025-05-19 09:04:53

      webpack5基础--09_处理其他资源

      2025-05-14 10:33:25

      jquery tmpl 详解

      2025-05-12 08:40:18

      RestTemplate请求UnknownContentTypeException:no suitable HttpMessageConverter异常

      2025-05-09 08:51:09

      Java实现一个坦克大战的小游戏【附源码】

      2025-04-14 08:45:56

      一段诡异的JavaScript代码,其实考察的是一些JavaScript的基础知识

      2025-04-14 08:45:36

      查看更多

      热门文章

      Python:使用2to3将Python2转Python3

      2023-02-22 07:04:55

      html:canvas画布绘图简单入门

      2023-02-20 10:15:01

      TypeScript-webpack配置

      2023-06-16 06:09:55

      ajax乱码问题和异步同步问题

      2023-06-13 08:32:34

      js中通过正则表达式验证邮箱是否合法

      2023-03-02 02:40:07

      级联查询

      2023-06-13 08:29:57

      查看更多

      热门标签

      java Java python 编程开发 代码 开发语言 算法 线程 Python html 数组 C++ 元素 javascript c++
      查看更多

      相关产品

      弹性云主机

      随时自助获取、弹性伸缩的云服务器资源

      天翼云电脑(公众版)

      便捷、安全、高效的云电脑服务

      对象存储

      高品质、低成本的云上存储服务

      云硬盘

      为云上计算资源提供持久性块存储

      查看更多

      随机文章

      15(系列完结)-HTML-表单其他控件

      React-React的基本使用

      HTML学习-列表标签

      JavaScript中其他数据类型转换为数值类型

      html通过js调用ios或android代码

      前端项目实战72-控制1个按钮不能删除

      • 7*24小时售后
      • 无忧退款
      • 免费备案
      • 专家服务
      售前咨询热线
      400-810-9889转1
      关注天翼云
      • 旗舰店
      • 天翼云APP
      • 天翼云微信公众号
      服务与支持
      • 备案中心
      • 售前咨询
      • 智能客服
      • 自助服务
      • 工单管理
      • 客户公告
      • 涉诈举报
      账户管理
      • 管理中心
      • 订单管理
      • 余额管理
      • 发票管理
      • 充值汇款
      • 续费管理
      快速入口
      • 天翼云旗舰店
      • 文档中心
      • 最新活动
      • 免费试用
      • 信任中心
      • 天翼云学堂
      云网生态
      • 甄选商城
      • 渠道合作
      • 云市场合作
      了解天翼云
      • 关于天翼云
      • 天翼云APP
      • 服务案例
      • 新闻资讯
      • 联系我们
      热门产品
      • 云电脑
      • 弹性云主机
      • 云电脑政企版
      • 天翼云手机
      • 云数据库
      • 对象存储
      • 云硬盘
      • Web应用防火墙
      • 服务器安全卫士
      • CDN加速
      热门推荐
      • 云服务备份
      • 边缘安全加速平台
      • 全站加速
      • 安全加速
      • 云服务器
      • 云主机
      • 智能边缘云
      • 应用编排服务
      • 微服务引擎
      • 共享流量包
      更多推荐
      • web应用防火墙
      • 密钥管理
      • 等保咨询
      • 安全专区
      • 应用运维管理
      • 云日志服务
      • 文档数据库服务
      • 云搜索服务
      • 数据湖探索
      • 数据仓库服务
      友情链接
      • 中国电信集团
      • 189邮箱
      • 天翼企业云盘
      • 天翼云盘
      ©2025 天翼云科技有限公司版权所有 增值电信业务经营许可证A2.B1.B2-20090001
      公司地址:北京市东城区青龙胡同甲1号、3号2幢2层205-32室
      • 用户协议
      • 隐私政策
      • 个人信息保护
      • 法律声明
      备案 京公网安备11010802043424号 京ICP备 2021034386号