前端 CSS 经典:flex + margin 布局

前言:如今我们布局大多时候都是用的 flex 布局,但是有时我们也可以使用 margin 小技巧去完成布局。在弹性盒中当我们把 margin 某一个方向上设置为 auto,他的含义是用 margin 吃掉这个方向的剩余空间。

1. 元素垂直和水平居中

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta
      name="viewport"
      content="initial-scale=1.0, user-scalable=no, width=device-width"
    />
    <title>document</title>
    <style>
      .box {
        width: 500px;
        height: 500px;
        border: 1px solid;
        display: flex;
      }
      .item {
        width: 100px;
        height: 100px;
        background: blue;
        margin: auto;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <div class="item"></div>
    </div>
    <script></script>
  </body>
</html>

2. 三个子元素,两个居左一个居右

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta
      name="viewport"
      content="initial-scale=1.0, user-scalable=no, width=device-width"
    />
    <title>document</title>
    <style>
      .box {
        width: 500px;
        height: 500px;
        border: 1px solid;
        display: flex;
      }
      .item {
        width: 100px;
        height: 100px;
        background: blue;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <div class="item"></div>
      <div class="item"></div>
      <div class="item" style="margin-left: auto"></div>
    </div>
    <script></script>
  </body>
</html>
相关推荐
不像程序员的程序媛21 小时前
Nginx日志切分
服务器·前端·nginx
北原_春希21 小时前
如何在Vue3项目中引入并使用Echarts图表
前端·javascript·echarts
尽意啊21 小时前
echarts树图动态添加子节点
前端·javascript·echarts
吃面必吃蒜21 小时前
echarts 极坐标柱状图 如何定义柱子颜色
前端·javascript·echarts
O_oStayPositive21 小时前
Vue3使用ECharts
前端·javascript·echarts
竹秋…21 小时前
echarts自定义tooltip中的内容
前端·javascript·echarts
宝贝露.21 小时前
Axure引入Echarts图无法正常显示问题
前端·javascript·echarts
shmily麻瓜小菜鸡21 小时前
前端文字转语音
前端
人良爱编程21 小时前
Hugo的Stack主题配置记录03-背景虚化-导航栏-Apache ECharts创建地图
前端·javascript·apache·echarts·css3·html5
来颗仙人掌吃吃21 小时前
解决Echarts设置宽度为100%发现宽度变为100px的问题(Echarts图标宽度自适应问题)
前端·javascript·echarts