-3

I want my h3 element to be vertically centred within the div element. I tried several solutions but any of those didn't work. Could anyone figure it out for me where I am doing wrong?

Below is my html and CSS code snippet.

HTML:

<div id="test">
    <h3 id="test_h3">This is a text</h3>
</div>

CSS:

#test{
    height: 100px;
    background-color: cadetblue;
    text-align: center;
}
#test_h3{
    vertical-align: middle;
}

#test{
    height: 100px;
    background-color: cadetblue;
    text-align: center;
}
#test_h3{
    vertical-align: center;
}
<div id="test">
    <h3 id="test_h3">This is a text</h3>
</div>
  • 1
    99% of the questions are already answered. Please try to find it before posting a repeated question. This is also a proper answer in w3schools resources : https://stackoverflow.com/questions/396145/how-to-vertically-center-a-div-for-all-browsers https://www.w3schools.com/howto/howto_css_center-vertical.asp https://www.geeksforgeeks.org/how-to-vertically-center-text-with-css/ – Sarthak Kuchhal Sep 03 '20 at 06:57
  • 2
    Does this answer your question? [How to vertically center a div for all browsers?](https://stackoverflow.com/questions/396145/how-to-vertically-center-a-div-for-all-browsers) – Sarthak Kuchhal Sep 03 '20 at 06:57
  • I am sorry for asking naive question. I am a beginner level programmer in web development. That's why I could not figure out how to do this. I should have searched more. Thanks for your reply. – Tapashee Tabassum Urmi Sep 03 '20 at 08:16
  • 1
    Don't be sorry. No question is naive nor i have commented to let you down. My sole purpose is to make understand that you should search more.. Search for answers and then finally post a question. **The more you find answer yourself the better developer you become.** – Sarthak Kuchhal Sep 03 '20 at 09:19
  • 1
    This is true Bro: The more one finds answer his/herself the better developer the person would become. Thanks a lot for your great advice! :) :) – Tapashee Tabassum Urmi Sep 03 '20 at 09:25

5 Answers5

2

vertical-align: center is a joke. Make your div a flexbox, this is simple and works everytime

.your-div {
    display: flex;
    justify-content: center;
    align-items: center;
}

Where align-items: center centers the element vertically

Tracer69
  • 719
  • 6
  • 16
0

#test{
    height: 100px;
    background-color: cadetblue;
    text-align: center;
    line-height:100px
}
#test_h3{
    vertical-align: center;
}
<div id="test">
    <h3 id="test_h3">This is a text</h3>
</div>

you can use line-height css to get this. check out my snippet

CodeBug
  • 1,156
  • 1
  • 5
  • 18
0

This is a simple case but you will encounter more complex layouts in which flex-box will shine. I recommend you to use flex-box:

#test{
    display: flex;
    height: 100px;
    background-color: cadetblue;
    justify-content: center;
    align-items: center;
}
#test_h3{
   display: flex;
   background-color: red;
}
<div id="test">
    <h3 id="test_h3">This is a text</h3>
</div>
0

center is not a valid value for CSS property vertical-align. However, middle is

#test{
    height: 100px;
    line-height: 100px;
    background-color: cadetblue;
    text-align: center;
}
#test_h3{
    vertical-align: middle;
}
<div id="test">
    <h3 id="test_h3">This is a text</h3>
</div>
eric
  • 175
  • 2
  • 17
-2

Try using this code

#test {
    height: 100px;
    background-color: cadetblue;
    display: flex;
    align-items: center;
    justify-content: center;
}
Humanoid
  • 47
  • 6