显示flexbox居中但对齐左侧文本对齐左侧

时间:2017-10-04 16:22:01

标签: html css css3 flexbox centering

我正在尝试将这些div设置为显示在中心,但保持其项目显示为左侧,就像text-align: left一样。

以下是我的示例:https://jsfiddle.net/gr5Lmos1/

#donateList {
  display: flex;
  justify-content: center;
  align-items: center;
  align-self: center;
  flex-direction: column;
  flex-wrap: wrap;
}

.donateItem {
  flex: 0 1 auto;
  align-items: flex-start;
  justify-content: flex-start;
  align-self: center;
}

.donateItem * {
  display: inline-block;
}

.donateItem p {
  vertical-align: bottom;
}

.donateItem img {
  height: 64px;
  width: 64px;
}
<div id="donateList">
  <div class="donateItem">
    <img src="/images/icons/bitcoin.png">
    <p>Bitcoin:</p>
    <p>fkewjhf;eiwhf;iewfhwehfewifhew</p>
  </div>
  <div class="donateItem">
    <img src="/images/icons/paypal.png">
    <p>Paypal:</p>
    <p>eijfhewfwifhefefewf</p>
  </div>
</div>

我正在尝试将donateItem的内容留给所有左边的显示,但保留所有donateItem的div中心,就像现在一样。

4 个答案:

答案 0 :(得分:4)

如果您愿意在标记中包含其他包装,则很容易:

  1. align-items: flex-start

  2. 使用stretch(或让它采用默认#donateList值)
  3. 居中对齐新的包装div。

  4. 请参阅下面的演示(还删除了一些冗余样式):

    &#13;
    &#13;
    main { /* ADDED */
      display: flex;
      align-items: center;
      justify-content: center;
    }
    #donateList {
      display: flex;
      justify-content: center;
      align-items: flex-start; /* CHANGED */
      /*align-self: center;*/
      flex-direction: column;
      flex-wrap: wrap;
    }
    
    .donateItem {
      flex: 0 1 auto;
      /*align-items: flex-start;
      justify-content: flex-start;
      align-self: center;*/
    }
    
    .donateItem * {
      display: inline-block;
    }
    
    .donateItem p {
      vertical-align: bottom;
    }
    
    .donateItem img{
      height: 64px;
      width: 64px;
    }
    &#13;
    <main>
      <div id="donateList">
        <div class="donateItem">
          <img src="http://placehold.it/100x100">
          <p>Bitcoin:</p>
          <p>fkewjhf;eiwhf;iewfhwehfewifhew</p>
        </div>
        <div class="donateItem">
          <img src="http://placehold.it/100x100">
          <p>Paypal:</p>
          <p>eijfhewfwifhefefewf</p>
        </div>
      </div>
    </main>
    &#13;
    &#13;
    &#13;

答案 1 :(得分:1)

你必须这样做:

#donateList
{ 
    margin: 0px auto;
    width: 50%;
    padding: 20px;
}

display:flex;.donateItem

中添加.donateItem p

&#13;
&#13;
#donateList

{
    margin: 0px auto;
    width: 50%;
    padding: 20px;
    
}

.donateItem
{
  flex: 0 1 auto;
  align-items: flex-start;
  justify-content: flex-start;
  align-self: center;
  display:flex;
}

.donateItem p
{
  vertical-align: bottom;
 display:flex;
}


.donateItem *
{
  display: inline-block;
}

.donateItem img
{
  height: 64px;
  width: 64px;
}
&#13;
<div id="donateList">

<div class="donateItem">

  <img src="http://icons.iconarchive.com/icons/froyoshark/enkel/96/Bitcoin-icon.png">

  <p>Bitcoin:</p>

  <p>fkewjhf;eiwhf;iewfhwehfewifhew</p>

</div>

<div class="donateItem">

  <img src="http://axisj.com/assets/images/donate-how-paypal.png">

  <p>Paypal:</p>

  <p>eijfhewfwifhefefewf</p>

</div>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

这是一个解决方案,但它有点hacky,容器宽度需要根据具体情况进行调整。容器获取这些设置以在体内居中:

width: 50%;
margin: 0 auto;
overflow: visible;
white-space: nowrap;

...并且flex项目获得align-self: flex-start;以便在容器内进行左对齐:

&#13;
&#13;
#donateList {
  display: flex;
  justify-content: center;
  align-items: center;
  align-self: center;
  flex-direction: column;
  flex-wrap: wrap;
  width: 50%;
  margin: 0 auto;
  overflow: visible;
  white-space: nowrap;
}

.donateItem {
  flex: 0 1 auto;
  align-items: flex-start;
  justify-content: flex-start;
  align-self: flex-start;
}

.donateItem * {
  display: inline-block;
}

.donateItem p {
  vertical-align: bottom;
}

.donateItem img {
  height: 64px;
  width: 64px;
}
&#13;
<div id="donateList">
  <div class="donateItem">
    <img src="/images/icons/bitcoin.png">
    <p>Bitcoin:</p>
    <p>fkewjhf;eiwhf;iewfhwehfewifhew</p>
  </div>
  <div class="donateItem">
    <img src="/images/icons/paypal.png">
    <p>Paypal:</p>
    <p>eijfhewfwifhefefewf</p>
  </div>
</div>
&#13;
&#13;
&#13;

答案 3 :(得分:1)

对于现代浏览器,将项目对齐方式更改为flex-start并通过width: max-content将容器设置为与最长项目一样宽,可以将其与常规margin:auto对齐:

&#13;
&#13;
#donateList {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  flex-direction: column;
  width: max-content;
  margin: auto;
}

.donateItem {
  flex: 0 1 auto;
  align-items: flex-start;
  justify-content: flex-start;
}

.donateItem * {
  display: inline-block;
}

.donateItem p {
  vertical-align: bottom;
}

.donateItem img {
  height: 64px;
  width: 64px;
}
&#13;
<div id="donateList">
  <div class="donateItem">
    <img src="/images/icons/bitcoin.png">
    <p>Bitcoin:</p>
    <p>fkewjhf;eiwhf;iewfhwehfewifhew</p>
  </div>
  <div class="donateItem">
    <img src="/images/icons/paypal.png">
    <p>Paypal:</p>
    <p>eijfhewfwifhefefewf</p>
  </div>
</div>
&#13;
&#13;
&#13;

不幸的是,max-content的浏览器支持是far from ideal,因此带有额外包装的@ kukkuz解决方案可能更实用(除非容器的左对齐是可接受的优雅降级你)。