0
<style>
._1{
  height:20%;
  width:100%;
  overflow:hidden;
  white-space:nowrap;
  text-overflow:ellipsis;
}
</style>
<div class="_1">content</div>

how to make content align at the middle of the div? thanks. demo

Edit: Height is % not px. I set line-height: 20% but it's not work.

4 Answers4

0
   div{ 
    height: 20%;
    width: 100%;
    text-align: center;
    display: flex;
    flex-direction: column;
    flex-wrap: nowrap;
    justify-content: space-around;
    align-content: center;    
  }   
xale94
  • 424
  • 5
  • 12
0

An alternative option: Use an inner element that's vertically centred.

To do this:

  • Set ._1 to position: relative;
  • Wrap the text inside a centerPosition class and use the CSS for that class below.

._1 {
  position: relative;
  height: 20%;
  width: 100%;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  
  /* Just to make this example visible */
  border: 2px solid #555;
}

.centerPosition {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

/* Not needed, just making the page bigger */
.examplePage {
  height: 300px;
}
<div class="examplePage">
  <div class="_1">
    <div class="centerPosition">content</div>
  </div>
</div>
DBS
  • 9,110
  • 4
  • 35
  • 53
0

If you do not want to use flexbox, you can try using CSS3 translate. But in order for this to work you need to wrap your text in paragraph or some other tag:

._1 p{
  -webkit-transform: translateY(-50%);
  -ms-transform: translateY(-50%);
  transform: translateY(-50%); 
    position: relative;
  top: 50%;
}
<div class="_1"><p>content</p></div>

With that markup you can use and table-cell aligning technic as well.

Kaloyan Stamatov
  • 3,894
  • 1
  • 20
  • 30
0

Flex-box only solution. Here browser support

._1{
  height:20%;
  width:100%;
  overflow:hidden;
  white-space:nowrap;
  text-overflow:ellipsis;
  
  display: flex;
  align-items: center;
  
  border: 1px dashed deepskyblue;
}

html,body {
 height: 100%;
}
<div class="_1">content</div>
CastenettoA
  • 673
  • 5
  • 17