简体   繁体   English

使用 flexbox 均匀间隔元素

[英]Use flexbox to space elements evenly

I am currently working on a project where I have to create four quarters for a year using flexbox.我目前正在做一个项目,我必须使用 flexbox 在一年中创建四个季度。 I would like all the quarters to be spaced evenly with the year above quarter 2 and quarter 3. I know I have to use justify-content: space-evenly, but the problem is that quarter 2 and quarter 3 are too close to each other because they are in a container along with the year.我希望所有季度与第 2 季度和第 3 季度以上的年份均匀分布。我知道我必须使用 justify-content: space-evenly,但问题是第 2 季度和第 3 季度彼此太接近因为它们与年份一起在一个容器中。 I will leave the HTML and CSS here in case you want to see it in js fiddle.我将把 HTML 和 CSS 留在这里,以防你想在 js fiddle 中看到它。

HTML HTML

<div class="container">
  <div>Q1</div>
    <div>
      <div class="yearContainer">2022</div>
        <div class="q2q3Container">
          <div>Q2</div>
          <div>Q3</div>
        </div>
    </div>
  <div>Q4</div>
</div>

CSS CSS

.container {
  display: flex;
  align-items: flex-end;
  justify-content: space-evenly;
}

.yearContainer {
  display: flex;
  justify-content: center;
}

.q2q3Container {
  display: flex;
}

My problem is that I don't know how to space out q2 and q3 so they have the same space as q1 and q4.我的问题是我不知道如何分隔 q2 和 q3,因此它们与 q1 和 q4 具有相同的空间。 I will leave pictures on how I image it to look like, also a real application of the view.我将留下关于我如何将其成像的图片,这也是该视图的实际应用。 enter image description here.在此处输入图像描述。 在此处输入图像描述

在此处输入图像描述

Thanks for any help.谢谢你的帮助。

I would use grid for that design.我会为那个设计使用grid But if you want to use flex , you can use flex-basis to define但是如果你想使用flex ,你可以使用flex-basis来定义
an area of 25% | 25%的面积| 50% | 50% | 25% space. 25% 的空间。 Then you can use justify-content: space-around;然后你可以使用justify-content: space-around; in the Q2, Q3 to evenly space the content.在Q2、Q3要均匀分布内容。 I also used text-align: center in main container.我还在主容器中使用了text-align: center

 .container { display: flex; align-items: flex-end; text-align: center; }.yearContainer { flex-basis: 50%; display: flex; justify-content: center; }.q2q3Container { display: flex; justify-content: space-around; }.fb-25 { flex-basis: 25%; }.fb-50 { flex-basis: 50%; }
 <div class="container text-center"> <div class="fb-25">Q1</div> <div class="fb-50"> <div class="yearContainer">2022</div> <div class="q2q3Container"> <div>Q2</div> <div>Q3</div> </div> </div> <div class="fb-25">Q4</div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM