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>
相关推荐
liuyang___几秒前
vue管理布局左侧菜单栏NavMenu
前端·javascript·vue.js
@业精于勤荒于嬉10 分钟前
将图片存储至阿里云 OSS
前端·阿里云·云计算·oss
前端Hardy25 分钟前
HTML&CSS&JS:必学!用粒子爆炸效果,让按钮点击 “告别枯燥”
javascript·css·html
前端Hardy28 分钟前
HTML&CSS&JS:必看!主题“自动换装”,10+风格随机切换超惊艳
javascript·css·html
打野赵怀真35 分钟前
render函数中return如果没有使用()会有什么问题?
前端·javascript
Scraper002436 分钟前
如何使用API和Node.js抓取Google新闻?
javascript
Riesenzahn37 分钟前
写一个左中右的满屏布局,左右固定220px,中间自适应并且要优先加载
前端·javascript
Riesenzahn37 分钟前
css在页面上画一个正方形,边长为页面宽度的一半
前端·javascript
tommyrunner39 分钟前
Cursor rule文件测试 一秒了解AI行为规则文件
前端·cursor
北京_宏哥44 分钟前
《手把手教你》系列基础篇(九十三)-java+ selenium自动化测试-框架设计基础-POM设计模式实现-上篇(详解教程)
java·前端·selenium