3个盒子,在一个大盒子中,怎么让3个盒子延中线居中

在一个大盒子中有三个子盒子,怎么让3个盒子延中线居中(注意是沿y轴的中线,不是那种用vertical-align:center沿X轴居中的效果)

阅读 5.6k
4 个回答
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    .root {
      width: 200px;
      height: 600px;
      display: flex;
      justify-content: space-around;
      flex-direction: column;
      align-items: center;
      border: 1px solid red;
    }

    .root > div {
      width: 100px;
      height: 80px;
      border: 1px solid #000000;
    }
  </style>
<body>

<div class="root">
  <div>

  </div>
  <div>

  </div>
  <div>

  </div>
</div>
</body>
</html>

在线调试

更新回答,垂直方向水平居中

<style>
    .box {
        height: 100px;
        /* div水平居中直接margin: auto就行 */
        margin-left: auto;
        margin-right: auto;
    }
    .b1 {
        width: 80px;
        background-color: red;
    }
    .b2 {
        width: 40px;
        background-color: green;
    }
    .b3 {
        width: 100px;
        background-color: blue;
    }
</style>

<div class="wrap">
    <div class="box b1"></div>
    <div class="box b2"></div>
    <div class="box b3"></div>
</div>

以下是水平方向垂直居中

<style>
    .box {
        display: inline-block;
        width: 100px;
        vertical-align: middle;
    }
    .b1 {
        height: 80px;
        background-color: red;
    }
    .b2 {
        height: 40px;
        background-color: green;
    }
    .b3 {
        height: 100px;
        background-color: blue;
    }
</style>

<div class="wrap">
    <div class="box b1"></div>
    <div class="box b2"></div>
    <div class="box b3"></div>
</div>

CSS:

        .div-container{
            position: absolute;
            left: 50px;
            top: 50px;
            width: 500px;
            height: 200px;
            line-height: 200px;
            box-sizing: border-box;
            border: 1px solid #cccccc;
            background-color: palegoldenrod;
            font-size: 0;
        }

        .div-inline{
            display: inline-block;
            width: 50px;
            height: 100px;
            vertical-align: middle;
            background-color: red;
            margin-left: 10px;
        }

        .div-inline:first-child{
            margin-left: 0px;
        }

HTML

    <div class="div-container">
        <div class="div-inline"></div>
        <div class="div-inline"></div>
        <div class="div-inline"></div>
        <div class="div-inline"></div>
        <div class="div-inline"></div>
        <div class="div-inline"></div>
        <div class="div-inline"></div>
        <div class="div-inline"></div>
    </div>

flex布局

<div class="big-box">
<div class="child-box"></div>
<div class="child-box"></div>
<div class="child-box"></div>
<div>
.big-box{
    display:flex;
    align-items:center;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题