I have three elements, two buttons and one bolded text element that I want to display at the same height. I also have text in a tag that I want centered. Here is what I see right now:
How can I center this <b>
tag and center the text inside of it?
Here is my HTML/React code:
<div className="ContainerDiv">
<button className="MinusButton"></button>
<b className="Counter">100</b>
<button className="PlusButton"></button>
</div>
and my css:
.PlusButton {
background-color: blue;
width: 100px;
height: 100px;
display: inline-block;
}
.Counter {
display: inline-block;
background-color: white;
width: 200px;
height: 100px;
text-align: center;
color: #282c34;
display: inline-block;
}
.MinusButton {
background-color: blue;
width: 100px;
height: 100px;
display: inline-block;
}
.ContainerDiv {
display: inline-block;
vertical-align: center;
text-align: center;
}
2
Answers
My proposed solution is to use Flexbox to align the elements at the same level and center the text vertically.
We can make a few adjustments to your CSS.
For the div container: changed display to flex to use flexbox layout, for the counter changed display to flex. Added
align-items: center
andjustify-content: center
to center the text both vertically and horizontally. for the buttons removeddisplay: inline-block
as it’s not needed in a flex container.HTML
CSS
Add
vertical-align: top;
to all inline-blocks to align them equally. Otherwise the default vertical alignment ofbaseline
(for inline-blocks) is applied, which can lead to confusion when there is text versus no text inside the different elements (i.e. in your instance the first line of text inside the middle block is aligned to the bottom of the empty inline-blocks at the right and left).