Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying flexbox centered but align items left like text align left

I am trying to setup these divs to display in the centre but keep their items displayed left, like text-align: left would do.

Here's my example: 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>

I am trying to get the donateItem's contents to all display left but keep all the donateItem's divs centre as they are now.

like image 737
Erdss4 Avatar asked Oct 04 '17 16:10

Erdss4


2 Answers

If you are open to include another wrapper in your markup, it is easy:

  1. Use align-items: flex-start (or let it take the default stretch value) for the #donateList

  2. Center align vertically and horizontally the new wrapper div.

See demo below (also removed some redundant styles):

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;
}
<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>
like image 65
kukkuz Avatar answered Oct 07 '22 05:10

kukkuz


Here's a solution, but it's a bit hacky and the container width needs to be adjusted to the particular situation. The container gets these settings for centering inside the body:

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

...and the flex items get align-self: flex-start; for left-alignment inside the container:

#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;
}
<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>
like image 31
Johannes Avatar answered Oct 07 '22 07:10

Johannes