By using Flexbox, I want to center three elements inside a photo and then put one element on top of those three:
I tried this to center them and obviously all the items get centered, whereas I only want the three boxes to be centered:
display: inline-flex;
justify-content: center;
align-items: center;
These are the HTML and CSS codes for that particular section:
HTML:
<section class="banner-3">
<article>
<h2 id="locations">Locations</h2>
<div class="address">
<h3>Downtown</h3>
<p>384 West 4th St</p>
<p>Suite 108</p>
<p>Portland, Maine</p>
</div>
<div class="address">
<h3>East Bayside</h3>
<p>3433 Phisherman's Avenue</p>
<p>(Northwest Corner)</p>
<p>Portland, Maine</p>
</div>
<div class="address">
<h3>Oakdale</h3>
<p>515 Crescent Avenue</p>
<p>Second Floor</p>
<p>Portland, Maine</p>
</div>
</article>
</section>
CSS:
.banner-3 article {
height: 500px;
width: 1200px;
background-image: url("../images/img-locations-background.jpg");
background-repeat: no-repeat;
background-position-y: center;
background-size: cover;
margin: 0 auto;
margin-top: 2.5%;
display: inline-flex;
justify-content: center;
align-items: center;
}
.banner-3 .address {
width: 300px;
height: 300px;
display: flex;
flex-direction: column;
align-content: center;
justify-content: center;
background-color: black;
opacity: 1;
margin: 20px;
}
.address h3, p {
margin: 25px auto;
}
Any help would be appreciated.
You have to wrap these three elements into a container
with display: flex; flex-direction: row
. This will make the container display horizontally its contents. Also give the article
style as display: flex; flex-direction: column;
This will make the h2
align above thode 3 cards.
.banner-3 article {
height: 500px;
/* width: 100%; */
width: 1200px;
background-image: url("https://www.w3schools.com/howto/photographer.jpg");
background-repeat: no-repeat;
background-position-y: center;
background-size: cover;
margin: 0 auto;
margin-top: 2.5%;
display: inline-flex;
justify-content: center;
align-items: center;
}
.banner-3 .address {
width: 300px;
height: 300px;
display: flex;
flex-direction: column;
align-content: center;
justify-content: center;
background-color: black;
opacity: 1;
margin: 20px;
margin-top: 0;
color: aliceblue;
}
.address h3,
p {
margin: 25px auto;
}
.container {
display: flex;
}
article {
display: flex;
flex-direction: column;
}
h2 {
margin-bottom: 15px;
}
<section class="banner-3">
<article>
<h2 id="locations">Locations</h2>
<div class="container">
<div class="address">
<h3>Downtown</h3>
<p>384 West 4th St</p>
<p>Suite 108</p>
<p>Portland, Maine</p>
</div>
<div class="address">
<h3>East Bayside</h3>
<p>3433 Phisherman's Avenue</p>
<p>(Northwest Corner)</p>
<p>Portland, Maine</p>
</div>
<div class="address">
<h3>Oakdale</h3>
<p>515 Crescent Avenue</p>
<p>Second Floor</p>
<p>Portland, Maine</p>
</div>
</div>
</article>
</section>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments