display:flex布局,最简单的案例

  1. 左右贴边

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> #parent{ width: 800px; background: red; height: 200px; display: flex; justify-content: space-between; } #parent span{ width: 100px; height: 100px; background: yellow; } </style> </head> <body>
    1 2 3
    </body> </html>

2.左右不贴边

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #parent{
            width: 800px;
            background: red;
            height: 200px;
            display: flex;
            justify-content: space-around;
        }
        #parent span{
            width: 100px;
            height: 100px;
            background: yellow;
        }
    </style>
</head>
<body>

  <div id="parent">
      <span>1</span>
      <span>2</span>
      <span>3</span>
  </div>

</body>
</html>

3.元素自动换行

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #parent{
            width: 600px;
            background: red;
            height: 300px;
            display: flex;
            /*flex布局中,默认的子元素是不换行的nowrap, 如果装不开,会缩小子元素的宽度,放到父元素里面*/
            flex-wrap: wrap;/*换行*/
        }
        #parent span{
            width: 100px;
            height: 100px;
            background: yellow;
            margin: 5px;
        }
    </style>
</head>
<body>

  <div id="parent">
      <span>1</span>
      <span>2</span>
      <span>3</span>
      <span>3</span>
      <span>3</span>
      <span>3</span>
      <span>3</span>
      <span>3</span>

  </div>

</body>
</html>

4.垂直居中

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #parent{
            width: 600px;
            background: red;
            height: 300px;
            display: flex;
            /*默认的主轴是 x轴 row */
            justify-content: center;
            /*我们需要一个侧轴居中*/
            align-items: center;
        }
        #parent span{
            width: 100px;
            height: 100px;
            background: yellow;
            margin: 5px;
        }
    </style>
</head>
<body>
<div id="parent">
      <span>1</span>
      <span>2</span>
      <span>3</span>
   </div>
</body>
</html>

5.子元素的高度自适应父元素(拉伸)

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #parent{
            width: 600px;
            background: red;
            height: 300px;
            display: flex;
            /*默认的主轴是 x轴 row */
            justify-content: center;
            /*拉伸,但是子盒子不要给高度*/
            align-items:stretch;
        }
        #parent span{
            width: 100px;
            background: yellow;
            margin: 5px;
        }
    </style>
</head>
<body>
<div id="parent">
      <span>1</span>
      <span>2</span>
      <span>3</span>
   </div>
</body>
</html>
相关推荐
GISer_Jing20 分钟前
前端图形渲染 html+css、canvas、svg和webgl绘制详解,各个应用场景及其区别
前端·html·图形渲染
零凌林22 分钟前
使用exceljs将excel文件转化为html预览最佳实践(完整源码)
前端·html·excel·vue3·最佳实践·文件预览·exceljs
星空寻流年28 分钟前
css3基于伸缩盒模型生成一个小案例
javascript·css·css3
二十雨辰29 分钟前
[CSS3]属性增强1
前端·css·css3
waterHBO2 小时前
直接从图片生成 html
前端·javascript·html
EndingCoder2 小时前
JavaScript 时间转换:从 HH:mm:ss 到十进制小时及反向转换
javascript
互联网搬砖老肖2 小时前
React组件(一):生命周期
前端·javascript·react.js
我科绝伦(Huanhuan Zhou)2 小时前
深入解析Shell脚本编程:从基础到实战的全面指南
前端·chrome
小马哥编程2 小时前
React和Vue在前端开发中, 通常选择哪一个
前端·vue.js·react.js
aklry2 小时前
uniapp实现在线pdf预览以及下载
前端·pdf·uni-app