简体   繁体   English

使用 flexbox 将元素左对齐和居中对齐

[英]Aligning elements left and center with flexbox

I'm using flexbox to align my child elements.我正在使用 flexbox 来对齐我的子元素。 What I'd like to do is center one element and leave the other aligned to the very left.我想做的是将一个元素居中,让另一个元素与最左边对齐。 Normally I would just set the left element using margin-right: auto .通常我只会使用margin-right: auto设置左元素。 The problem is that pushes the center element off center.问题是将中心元素推离中心。 Is this possible without using absolute positioning?这可能使用绝对定位吗?

HTML & CSS HTML 和 CSS

 #parent { align-items: center; border: 1px solid black; display: flex; justify-content: center; margin: 0 auto; width: 500px; } #left { margin-right: auto; } #center { margin: auto; }
 <div id="parent"> <span id="left">Left</span> <span id="center">Center</span> </div>

Add third empty element:添加第三个空元素:

<div class="parent">
  <div class="left">Left</div>
  <div class="center">Center</div>
  <div class="right"></div>
</div>

And the following style:以及以下样式:

.parent {
  display: flex;
}
.left, .right {
  flex: 1;
}

Only left and right are set to grow and thanks to the facts that...只有左派和右派会增长,这要归功于……

  • there are only two growing elements (doesn't matter if empty) and只有两个不断增长的元素(如果为空则无关紧要)和
  • that both get same widths (they'll evenly distribute the available space)两者都具有相同的宽度(它们会均匀分布可用空间)

... center element will always be perfectly centered. ... center 元素将始终完美居中。

This is much better than accepted answer in my opinion because you do not have to copy left content to right and hide it to get same width for both sides, it just magically happens (flexbox is magical).在我看来,这比接受的答案要好得多,因为您不必将左侧内容复制到右侧并隐藏它以获得相同宽度的两侧,它只是神奇地发生了(flexbox 很神奇)。


In action:在行动:

 .parent { display: flex; } .left, .right { flex: 1; } /* Styles for demonstration */ .parent { padding: 5px; border: 2px solid #000; } .left, .right { padding: 3px; border: 2px solid red; } .center { margin: 0 3px; padding: 3px; border: 2px solid blue; }
 <div class="parent"> <div class="left">Left</div> <div class="center">Center</div> <div class="right"></div> </div>

EDIT: See Solo's answer below, it is the better solution.编辑:请参阅下面的Solo 答案,这是更好的解决方案。


The idea behind flexbox is to provide a framework for easily aligning elements with variable dimensions within a container. flexbox 背后的想法是提供一个框架,以便在容器内轻松对齐具有可变尺寸的元素。 As such, it makes little sense to provide a layout where the width of one element is totally ignored.因此,提供一种完全忽略一个元素的宽度的布局是没有意义的。 In essence, that is exactly what absolute positioning is for, as it takes the element out of the normal flow.本质上,这正是绝对定位的用途,因为它将元素从正常流中取出。

As far as I know, there is no nice way of doing this without using position: absolute;据我所知,不使用position: absolute;没有很好的方法来做到这一点position: absolute; , so I would suggest using it... but If you REALLY don't want to, or can't use absolute positioning then I suppose you could use one of the following workarounds. ,所以我建议使用它......但如果你真的不想,或者不能使用绝对定位,那么我想你可以使用以下解决方法之一。


If you know the exact width of the "Left" div , then you could change justify-content to flex-start (left) and then align the "Center" div like this:如果您知道“Left” div 的确切宽度,那么您可以将justify-content更改justify-content flex-start (左),然后像这样对齐“Center” div:

#center {
    position: relative;
    margin: auto;
    left: -{half width of left div}px;
}

If you do not know the width , then you could duplicate "Left" on the right side, use justify-content: space-between;如果您不知道 width ,那么您可以在右侧复制“Left”,使用justify-content: space-between; , and hide the new right element: Just to be clear, this is really, really ugly... better to use absolute positioning than to duplicate content. ,并隐藏新的正确元素:要清楚,这真的非常丑陋......使用绝对定位比复制内容更好。 :-) :-)

 #parent { align-items: center; border: 1px solid black; display: flex; justify-content: space-between; margin: 0 auto; width: 500px; } #right { opacity: 0; }
 <div id="parent"> <span id="left">Left</span> <span id="center">Center</span> <span id="right">Left</span> </div>

 .parent { display: flex; } .left { flex: 1; } .parent::after { flex: 1; content: ''; }
 <div class="parent"> <div class="left">Left</div> <div>Center</div> </div>

I have another solution.我有另一个解决方案。 In my opinion, Adding an empty block to the center element is fine but code-wise it bit ugly.在我看来,向中心元素添加一个空块很好,但代码方面有点难看。

If you don't want to rely on positioning, the only way I've found that makes it truly centered is to use a combination of auto margin and negative margin prevent the centered element to getting pushed over by the left aligned element.如果您不想依赖定位,我发现使其真正居中的唯一方法是使用自动边距和负边距的组合,以防止居中元素被左对齐元素推倒。 This requires that you know the exact width of the left aligned element though.不过,这要求您知道左对齐元素的确切宽度。

 .container { height: 100px; border: solid 10px skyblue; display: flex; justify-content: center; } .block { width: 120px; background: tomato; } .justify-start { margin-right: auto; } .justify-center { margin-right: auto; margin-left: -120px; }
 <div class="container"> <div class="block justify-start"></div> <div class="block justify-center"></div> </div>

As far as I know this is possible with the following code.据我所知,这可以通过以下代码实现。

https://jsfiddle.net/u5gonp0a/ https://jsfiddle.net/u5gonp0a/

 .box { display: flex; justify-content: center; background-color: green; text-align: left; } .left { padding: 10px; background-color: pink; } .center { padding: 10px; background-color: yellow; margin: 0 auto; }
 <div class="box"> <div class="left">left</div> <div class="center">center</div> </div>

Try this no hacks :)试试这个没有黑客:)

CSS CSS

.container{
  width: 500px;
  margin: 0 auto;
}
.box{
  display: flex;
  align-items: center;/* just in case*/
  justify-content: space-between;
}
.box p:nth-child(2){
  text-align: center;
  background-color: lime;
  flex: 1 1 0px;
}

HTML HTML

<div class="container">
  <div class="box">
    <p>One</p>
    <p>Two</p>
  </div>
</div>

http://codepen.io/whisher/pen/XpGaEZ http://codepen.io/whisher/pen/XpGaEZ

Since this is 4 years old I figured I'd update this with a much easier CSS Grid solution.由于这是 4 岁,我想我会用一个更简单的 CSS Grid 解决方案来更新它。

 #parent { display: grid; grid-template-columns: repeat(3, 1fr); border: 1px solid black; margin: 0 auto; width: 500px; } #center { text-align: center; }
 <div id="parent"> <span id="left">Left</span> <span id="center">Center</span> </div>

If you have a grid system you can use it to do what you want without "extra" css.如果你有一个网格系统,你可以用它来做你想做的事,而无需“额外”的 css。

Below with bootstrap (V 4.X)下面带有引导程序(V 4.X)
Note: It uses flex under the hood注意:它在引擎盖下使用 flex

<div class="row">
  <div class="col text-left">left</col>
  <div class="col text-center">center</col>
  <div class="col text-right">right</col>
</div>

Doc bootstrap: https://getbootstrap.com/docs/4.6/layout/grid/文档引导程序: https : //getbootstrap.com/docs/4.6/layout/grid/

Et voilà !等等! :) :)

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

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