How can I achieve the style below:
********************
* *
* ******** *
* *Text * *
* ******** *
* *
********************
Where the inner box is centered, but the text inside is not.
How can I achieve the style below:
********************
* *
* ******** *
* *Text * *
* ******** *
* *
********************
Where the inner box is centered, but the text inside is not.
.container {
text-align: center;
width: 50%;
height: 25%;
background-color: #00aaff;
padding: 20px;
}
.box {
text-align: left;
width: 35%;
height: 50%;
background-color: #00aa00;
margin: 0 auto;
}
<div class="container">
<div class="box">
Text
</div>
</div>
Start with this HTML:
<div class="outer">
<div class="inner">Test</div>
</div>
Then apply these CSS styles:
.outer {
height: 100px;
width: 200px;
background-color: green;
display: flex;
align-items: center;
}
.inner {
height: 50px;
line-height: 50px;
width: 100px;
background-color: blue;
color: white;
margin: auto;
}
The sizing and coloring styles should be obvious, and you can make them any size and color you want. The other styles are what you use to get the positioning you're looking for, and I'll explain them.
display: flex;
align-items: center;
(This won't work on older browsers. I'll leave you to research that if you need to.) These two center the inner box in the outer one along the Y-axis (vertical).
margin: auto;
This centers the inner box in the outer one along the X-axis.
line-height: 50px;
This value needs to be the same as the height
value for the inner box, if you want the text to be vertically centered in the inner box.