国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Align elements left and center using Flexbox
P粉545682500
P粉545682500 2023-08-27 21:54:23
0
2
723
<p>I'm using Flexbox to align my child elements. What I want to do is center one element and align the other element to the left. Normally I would use <code>margin-right: auto</code> to set the left element. The problem is pushing the center element away from the center. Is this possible without using absolute positioning? </p> <p><strong>HTML and CSS</strong></p> <p><br /></p> <pre class="brush:css;toolbar:false;">#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; }</pre> <pre class="brush:html;toolbar:false;"><div id="parent"> <span id="left">Left</span> <span id="center">Center</span> </div></pre> <p><br /></p>
P粉545682500
P粉545682500

reply all(2)
P粉561323975

Add a third empty element:

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

and the following styles:

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

Only the left and the right will grow, and due to the fact...

  • There are only two growth elements (it doesn’t matter if it is empty) and
  • Both have the same width (they will distribute the available space evenly)

...The center element will always be perfectly centered.

In my opinion this is much better than the accepted answer because you don't have to copy the left content to the right and hide it to get the same width on both sides, it just magically happens (flexbox is amazing).


Actual operation:

.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>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template