-2

How can I align text in the center of the vertical axis of a div in HTML? I've tried using text-align:center; but it only centers the text on the horizontal axis. Is there an equivalent statement for the Y axis? I'm trying to do this to every div with the .subcol class and the div with the #top id.

body {
  margin: 15px;
}

.subcol {
  background: teal;
  color: white;
  border-radius: 7px;
  text-align: center;
  margin: 0 5px 5px 0;
  height: 50px;
}

#top {
  background: cyan;
  width: 100%;
  height: 150px;
  text-align: center;
  font-style: bold;
}
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

<div id="top">Hello World Page</div>
<div class="row">
  <div class="col-lg-6 col-sm-12">
    <div class="subcol">
      Hello World 1
    </div>
  </div>
  <div class="col-lg-6 col-sm-12">
    <div class="subcol">
      Hello World 2
    </div>
  </div>
  <div class="col-lg-6 col-sm-12">
    <div class="subcol">
      Hello World 3
    </div>
  </div>
  <div class="col-lg-6 col-sm-12">
    <div class="subcol">
      Hello World 4
    </div>
  </div>
</div>
j08691
  • 204,283
  • 31
  • 260
  • 272
Serket
  • 3,785
  • 3
  • 14
  • 45

3 Answers3

1

You can achieve that simply with flex:

display: flex;
justify-content: center;
align-items: center;

justify-content: center -> for horizontally center.

align-items: center -> for vertically center.

Just add this css to classes you want.

Working usage:

        body{
            margin:15px;
        }
        .subcol {
            background:teal;
            color:white;
            border-radius:7px;
            text-align:center;
            margin:0 5px 5px 0;
            height:50px;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        #top {
            background:cyan;
            width:100%;
            height:150px;
            text-align:center;
            font-style:bold;
                        display: flex;
            justify-content: center;
            align-items: center;
        }
    <div id="top">Hello World Page</div>
    <div class="row">
        <div class="col-lg-6 col-sm-12">
            <div class="subcol">
                Hello World 1
            </div>
        </div>
        <div class="col-lg-6 col-sm-12">
            <div class="subcol">
                Hello World 2
            </div>
        </div>
        <div class="col-lg-6 col-sm-12">
            <div class="subcol">
                Hello World 3
            </div>
        </div>
        <div class="col-lg-6 col-sm-12">
            <div class="subcol">
                Hello World 4
            </div>
        </div>
    </div>
BeHappy
  • 3,705
  • 5
  • 18
  • 59
-1

Add line-height same as the height of you div.

 body{
            margin:15px;
        }
        .subcol {
            background:teal;
            color:white;
            border-radius:7px;
            text-align:center;
            margin:0 5px 5px 0;
            height:50px;
            line-height:50px; /* added */
        }
        #top {
            background:cyan;
            width:100%;
            height:150px;
            line-height:150px;  /* added */
            text-align:center;
            font-style:bold;
        }
    <title>Bootstrap Test</title>
    <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

    <div id="top">Hello World Page</div>
    <div class="row">
        <div class="col-lg-6 col-sm-12">
            <div class="subcol">
                Hello World 1
            </div>
        </div>
        <div class="col-lg-6 col-sm-12">
            <div class="subcol">
                Hello World 2
            </div>
        </div>
        <div class="col-lg-6 col-sm-12">
            <div class="subcol">
                Hello World 3
            </div>
        </div>
        <div class="col-lg-6 col-sm-12">
            <div class="subcol">
                Hello World 4
            </div>
        </div>
    </div>
Bilal Ahmed
  • 257
  • 2
  • 7
-2

There ar so many ways in HTML & CSS

I usually use the following method.

I hope it was helpful to you

.p {
  width: 100px;
  height: 20px;
  position: relative;
  border: 1px solid red;
}

.c {
  width: 10px;
  height: 5px;
  border: 1px solid blue;
}

.center {
  position: absolute;
  top: 50%;
  left: 50%;
  transform:translate(-50%, -50%)
}
<div class='p'>
  <div class='c center'></div>
</div>
smPark
  • 1