I'm new to HTML/CSS and am making my first web page by myself. Currently working on the last CSS project for codecademy.
I've been fiddling with this for the longest time and just can't figure it out. I'm trying to center all my elements in the .mainContainer
. I'm guessing 'Title' should be in the same container with the boxes, but I don't know how to make it appear above the boxes with everything centered (x and y).
Below is the closest I can get. It seems the bottom of the boxes is centered, but I need all the content centered. What I'm not understanding is why I have inline-block
for .mainContainer, yet flex
in .boxContainer. If I remove one, I lose the centering. I didn't think I could use multiple displays like that...what is going on there?
https://jsfiddle.net/ichristian/ce9mou4w/2/
HTML
<div class='mainContainer'>
<h2>Title</h2>
<div class='boxContainer'>
<div class='box'>
<p>Box 1</p>
</div>
<div class='box'>
<p>Box 2</p>
</div>
<div class='box'>
<p>Box 3</p>
</div>
</div>
</div>
CSS
.mainContainer{
background-color: orange;
height: 200px;
width: 400px;
align-items: center;
text-align: center;
display: inline-block;
}
.boxContainer {
display: flex;
align-content: center;
justify-content: center;
align-items: center;
}
.box {
background-color: black;
color: white;
height: 40px;
width: 40px;
}
.box + .box {
margin-left: 40px;
}
EDIT: Did not like the solution from the recommended question. The answer I chose below is much easier.