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>
相关推荐
小小竹子3 分钟前
前端vue-实现富文本组件
前端·vue.js·富文本
万物得其道者成11 分钟前
React Zustand状态管理库的使用
开发语言·javascript·ecmascript
小白小白从不日白12 分钟前
react hooks--useReducer
前端·javascript·react.js
下雪天的夏风24 分钟前
TS - tsconfig.json 和 tsconfig.node.json 的关系,如何在TS 中使用 JS 不报错
前端·javascript·typescript
diygwcom36 分钟前
electron-updater实现electron全量版本更新
前端·javascript·electron
volodyan39 分钟前
electron react离线使用monaco-editor
javascript·react.js·electron
^^为欢几何^^1 小时前
lodash中_.difference如何过滤数组
javascript·数据结构·算法
Hello-Mr.Wang1 小时前
vue3中开发引导页的方法
开发语言·前端·javascript
程序员凡尘1 小时前
完美解决 Array 方法 (map/filter/reduce) 不按预期工作 的正确解决方法,亲测有效!!!
前端·javascript·vue.js
编程零零七5 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql