【问题标题】:Alignment of item using flexbox使用 flexbox 对齐项目
【发布时间】:2021-03-18 04:12:31
【问题描述】:

以下是与position: relative 和position: absolute 一起正常工作的代码。但是我正在尝试使用Flexbox 来实现类似的效果,尽管该项目在中心进行了调整,但它不像使用位置实现的那样重叠。让我知道如何使用Flexbox 实现类似的效果。

代码-

* { box-sizing: border-box; }
.container { width: 300px; height: 300px; border: 1px solid black; }
.green { background: #2a9d8f; }
.blue { background: #333366; }

/* Position CSS */
.position-container { position: relative; }
.box1, .box2 { position: absolute; }
.box1 { width: 60px; height: 60px; left: 120px; top: 120px; }
.box2 { width: 40px; height: 40px; left: 130px; top: 130px; }

/* Flexbox CSS */
.flex-container { 
  display: flex; 
  align-items: center; 
  justify-content: center; 
}
.box3 { width: 60px; height: 60px; }
.box4 { width: 40px; height: 40px; }
<div class="container position-container">
    <div class="box1 green"></div>
    <div class="box2 blue"></div>
  </div>
  <hr />
  <div class="container flex-container">
    <div class="box3 green"></div>
    <div class="box4 blue"></div>
  </div>

【问题讨论】:

标签: javascript html css flexbox css-position


【解决方案1】:

您可以使用负边距来调整重叠。将margin-left: -50px; 添加到box4 以实现所需的输出。

* { box-sizing: border-box; }
.container { width: 300px; height: 300px; border: 1px solid black; }
.green { background: #2a9d8f; }
.blue { background: #333366; }

/* Position CSS */
.position-container { position: relative; }
.box1, .box2 { position: absolute; }
.box1 { width: 60px; height: 60px; left: 120px; top: 120px; }
.box2 { width: 40px; height: 40px; left: 130px; top: 130px; }

/* Flexbox CSS */
.flex-container { 
  display: flex; 
  align-items: center; 
  justify-content: center; 
}
.box3 { width: 60px; height: 60px; }
.box4 { width: 40px; height: 40px; margin-left: -50px; }
<div class="container position-container">
    <div class="box1 green"></div>
    <div class="box2 blue"></div>
  </div>
  <hr />
  <div class="container flex-container">
    <div class="box3 green"></div>
    <div class="box4 blue"></div>
  </div>

【讨论】:

    【解决方案2】:

    如果你想做出同样的效果,但只使用 flexbox,我认为你不能这样做,但你需要像下面的代码一样使用 flexbox 的位置:

    * {
      box-sizing: border-box;
    }
    .container {
      width: 300px;
      height: 300px;
      border: 1px solid black;
    }
    .green {
      background: #2a9d8f;
    }
    .blue {
      background: #333366;
    }
    
    /* Position CSS */
    .position-container {
      position: relative;
    }
    .box1,
    .box2 {
      position: absolute;
    }
    .box1 {
      width: 60px;
      height: 60px;
      left: 120px;
      top: 120px;
    }
    .box2 {
      width: 40px;
      height: 40px;
      left: 130px;
      top: 130px;
    }
    
    /* Flexbox CSS */
    .flex-container {
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .box3 {
      width: 60px;
      height: 60px;
      position: absolute;
    }
    .box4 {
      width: 40px;
      height: 40px;
      position: absolute;
    }
    <div class="container position-container">
      <div class="box1 green"></div>
      <div class="box2 blue"></div>
    </div>
    <hr />
    <div class="container flex-container">
      <div class="box3 green"></div>
      <div class="box4 blue"></div>
    </div>

    【讨论】:

      【解决方案3】:

      您应该更改 HTML 并将蓝色框放在绿色框内。然后, 为绿色框添加css: .box3 { display: flex; align-items: center; justify-content: center; }

      * { box-sizing: border-box; }
      .container { width: 300px; height: 300px; border: 1px solid black; }
      .green { background: #2a9d8f; }
      .blue { background: #333366; }
      
      /* Position CSS */
      .position-container { position: relative; }
      .box1, .box2 { position: absolute; }
      .box1 { width: 60px; height: 60px; left: 120px; top: 120px; }
      .box2 { width: 40px; height: 40px; left: 130px; top: 130px; }
      
      /* Flexbox CSS */
      .flex-container { 
        display: flex; 
        align-items: center; 
        justify-content: center; 
      }
      .box3 { width: 60px; height: 60px; display: flex; align-items: center; justify-content: center; }
      .box4 { width: 40px; height: 40px; }
      <div class="container position-container">
          <div class="box1 green"></div>
          <div class="box2 blue"></div>
        </div>
        <hr />
        <div class="container flex-container">
          <div class="box3 green">
            <div class="box4 blue"></div>      
          </div>
        </div>

      【讨论】:

        猜你喜欢
        • 2016-09-08
        • 2017-03-19
        • 2018-08-21
        • 1970-01-01
        • 2018-02-06
        • 2020-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多