简体   繁体   中英

Center vertically when the height of element is unknown

I want to center vertically text, when the elements height is unknown?

html

<div class="table">
  <div class="table-resp">

    <div class="second-row">
      <div class="col-md-5">
          <div class="left-col-text">
            Center vertically
          </div>
      </div>
      <div class="col-md-7">
        <div class="right-col-text">
          <div class="example">Ex1</div>
          <div class="example">Ex2</div>
          <div class="example">Ex3</div>
        </div>
      </div>
    </div>

  </div>
</div>

css

/* CSS used here will be applied after bootstrap.css */
.table{
    text-align: center;
    padding-top: 70px;
    padding-left: 0px;
    padding-right: 35px;
}

.table-resp{
    border: 1px solid green;
    overflow-x: hidden;
}

.text1{
    float: left;
    display: inline-block;
}

.second-row{
    line-height: 30px;
    clear: left;
    min-height: 30px;
    overflow: auto;
}

.left-col-text{
    height: 100%;  
}

Elements "Ex1, Ex2" count is unknown, so, if there are more of those, obviously, the table row will get bigger in height. I need some solution, that would be responsive to this also...

https://www.codeply.com/go/bp/4ZEUS7Q7lm

Just add row-ht-eq class to row <div class="second-row">

CSS:

.row-ht-eq {
    display: flex;
    align-items: center;
}
position: absolute;
top: 50%;
transform: translateY(-50%);

Also you can play with:

display: table-cell;
vertical-align: middle;

Note: Use span Element as helper.

Html:

<div class="col-md-5">
          <span class="helper"></span>
          <div class="left-col-text">
            Center vertically
          </div>
</div>

Css:

.helper {
    display: inline-block;
    height: 100%;
    vertical-align: middle;
}

Full Code:

 .table{ text-align: center; padding-top: 70px; padding-left: 0px; padding-right: 35px; } .table-resp{ border: 1px solid green; overflow-x: hidden; } .text1{ float: left; display: inline-block; } .second-row{ line-height: 30px; clear: left; min-height: 30px; overflow: auto; } .left-col-text{ height: 100%; } .helper { display: inline-block; height: 100%; vertical-align: middle; } 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <div class="table"> <div class="table-resp"> <div class="second-row"> <div class="col-md-5"> <span class="helper"></span> <div class="left-col-text"> Center vertically </div> </div> <div class="col-md-7"> <div class="right-col-text"> <div class="example">Ex1</div> <div class="example">Ex2</div> <div class="example">Ex3</div> </div> </div> </div> </div> </div> 

Change your text class to:

.left-col-text {
   margin:0 auto;
}

This will automatically decide equal distance from top to bottom.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM