I have a flex-box (see the below snippet for examples).
I want to set it so that in all cases, the <h2>
is in the center of the flex-box and the other spans will flow around that, based on their markup.
I am basically looking for an align-self
CSS code, but for the main axis, not the cross axis (this might help explain what I am asking).
I am also applying margin: auto;
to my <h2>
, which I learned about after reading this (a fantastic page, but it still leaves me with my following question—unless I didn't fully understand all of it).
Here's the code I got:
.container {
align-items: center;
border: 1px solid red;
display: flex;
justify-content: center;
width: 100%;
}
h2 {
margin: auto;
]
<div class="container">
<h2>I'm an h2</h2>
<span>I'm span 1</span>
<span>I'm span 2</span>
<span>I'm span 3</span>
</div>
<div class="container">
<span>I'm span 1</span>
<span>I'm span 2</span>
<span>I'm span 3</span>
<h2>I'm an h2</h2>
<span>I'm span 4</span>
<span>I'm span 5</span>
<span>I'm span 6</span>
</div>
<div class="container">
<span>I'm span 1</span>
<span>I'm span 2</span>
<h2>I'm an h2</h2>
<span>I'm span 3</span>
</div>
To fully reiterate my question: I want to know how to center the
<h2>
on my page so that where ever the other<span>
s are, the<h2>
will always be in the dead center of the flex-box.
P.S.: I am willing to use JavaScript and jQuery, but I would prefer a pure CSS way of achieving this.
After an answer by Michael Benjamin:
His answer got me thinking. While I have not found a way to do this, I believe the following is a step in the right direction:
HTML
<div class="container">
<div>
<span>I'm span 1</span>
<span>I'm span 2</span>
<span>I'm span 3</span>
</div>
<h2>I'm an h2</h2>
<div>
<span>I'm span 4</span>
<span>I'm span 5</span>
<span>I'm span 6</span>
</div>
</div>
CSS
.container div {
flex: 1 1 auto;
text-align: center;
}
h2 {
flex: 0 0 auto;
margin: auto;
}
.container {
align-items: center;
border: 1px solid red;
display: flex;
justify-content: center;
width: 100%;
}
.container div {
flex: 1 1 auto;
text-align: center;
}
h2 {
flex: 0 0 auto;
margin: auto;
}
<div class="container">
<div>
</div>
<h2>I'm an h2</h2>
<div>
<span>I'm span 1</span>
<span>I'm span 2</span>
<span>I'm span 3</span>
</div>
</div>
<div class="container">
<div>
<span>I'm span 1</span>
<span>I'm span 2</span>
<span>I'm span 3</span>
</div>
<h2>I'm an h2</h2>
<div>
<span>I'm span 4</span>
<span>I'm span 5</span>
<span>I'm span 6</span>
</div>
</div>
<div class="container">
<div>
<span>I'm span 1</span>
<span>I'm span 2</span>
</div>
<h2>I'm an h2</h2>
<div>
<span>I'm span 3</span>
</div>
</div>
Basically, the theory is that while the amount of total <span>
s are unknown, it is known that there will be a total of three elements: <div><h2><div>
As you can see in my above snippet, I have tried (
flex: 0 0 auto
andflex: 1 1 auto
, etc.) to get it to work but have not been successful. Can anyone give me some insight as to if this is a step in the right direction and maybe how to push it through to an actual product?