css single or multiple line vertical align

HtmlCss

Html Problem Overview


I have a title that can have one or more lines.

How can I align the text vertically? If it was always one line I could just set the line-height to the container height.

I can do it using JavaScript, but I don't really like it, I'm searching for a pure CSS way.

Also if the container could expand with the lines it would be perfect, so I can always have the same padding on the top and bottom.

enter image description here

Html Solutions


Solution 1 - Html

For this you can use display:table-cell property:

.inline {
  display: inline-block;
  margin: 1em;
}
.wrap {
  display: table;
  height:  100px;
  width:   160px;
  padding: 10px;
  border:  thin solid darkgray;
}
.wrap p {
  display:        table-cell;
  vertical-align: middle;
}

<div class="inline">
  <div class="wrap">
    <p>Example of single line.</p>
  </div>
</div>

<div class="inline">
  <div class="wrap">
    <p>To look best, text should really be centered inside.</p>
  </div>
</div>

But it works IE8 & above. Read this article for more info: CSS Tricks: Vertically Center Multi-Lined Text.

Solution 2 - Html

If you don't like the display:table trick (I know I don't) here's a solution without it:

.cen {
  min-height:5em; width:12em; background:red; margin:1em 0;
}
.cen p {
  display:inline-block; vertical-align:middle;
  margin:0 0 0 1em; width:10em;
}
.cen::after {
   display:inline-block; vertical-align:middle; line-height:5em;
   width:0; content:"\00A0"; overflow:hidden;
}

with HTML

<div class="cen">
 <p>Text in div 1</p>
</div>

This gives the div a height of 5em, unless the content is heigher, then it grows.
Live example here.

Edit: Oh, which browsers is this supposed to work on? IE8 won't cooperate.

(Later edit: updated CSS to handle issues in Chrome)

Solution 3 - Html

I really like this solution:

<div>
    <span style="display: inline-block; vertical-align: middle; height: --The height of your box here--"></span>
    <span style="display: inline-block; vertical-align: middle;">Put your multi-line content here</span>
</div>

Feel free to use stylesheets, and 100% for the height...

Also might have to comment out spaces between the span tags, since these are inline-blocks

Credit goes to Hades. I got it from Here

<div style="outline:thin solid red;">
  <span style="display: inline-block; vertical-align: middle; height: 60px;"></span>
  <span style="display: inline-block; vertical-align: middle;">Put your multi-line content here.</span>
</div>

<div style="outline:thin solid red;">
  <span style="display: inline-block; vertical-align: middle; height: 60px;"></span>
  <span style="display: inline-block; vertical-align: middle;">Put your multi-line content here. Put your multi-line content here. Put your multi-line content here. Put your multi-line content here. Put your multi-line content here.</span>
</div>

Note: This appears not to work with multiple line content.

Solution 4 - Html

Instead of using vertical-align: center and display: table-cell, you might want to take a look at the newer method called CSS display flex which is MUCH simpler

.box {
    width: 200px;
    height: 50px; 
    padding: 10px;
    margin-bottom: 20px;
    background-color: red;
    
    /* Only add these 2 lines */
    display: flex;
    align-items: center;
  }

<div class="box">Lorem ipsum dolor</div>
<div class="box">Lorem ipsum dolor sit amet ipsum dolor</div>

Solution 5 - Html

somthing like this

HTML

<div>
    <p>
       Lorem Ipsum is simply
    </p>
</div>

CSS

div {
   display: table;
}
p {
   display:table-cell;
   vertical-align: middle;
}

Solution 6 - Html

If You want your text to be responsive, wrapping words from one to multiple lines as width is dynamically changing, mentioned above trick with inline-block helper (which have the best compability here) can be not sufficient, as .inlinehelper can push wrapping text down under itself.
Here's complete solution for such a simpel task:

HTML:

<div id="responsive_wrap">
    <span class="inlinehelper"><span id="content">        
</div>

CSS:

#responsive_wrap {   
   display: block;    
   height: 100%; //dimensions just for   
   width: 100%;  //example's sake
   white-space: nowrap;
}

#content {   
   display: inline-block;        
   white-space: initial;
}

.inlinehelper {
   height: 100%;    
   width: 0;
   vertical-align: middle;
   display: inline-block;
}

Notice the white-space:nowrap property, which prevents .inlinehelper and #content to wrap in relation to each other. white-space:initial on #content resets ability to wrap for span itself;

Another Option: More a matter of personal preference. You can use a pseudo element instead of .inlinehelper. Remove the .inlinehelper css rules and element and add this pseudo-element css selector:

#responsive_wrap:before {
    content: "";
    display: inline-block;
    vertical-align: middle;
    height: 100%;
    width: 0px;
}

P.S.: I figured out that this is really old question too late, therefore let this answer be, maybe it will be helpfull for someone.

Solution 7 - Html

Styling wise, a table would be the best way to layout your content (put the style tags in CSS):

<table style="border:1;collapse;width:300px;padding:5px;background-color:red;">
<tr>
	<td style="width:250px;vertical-align:middle;">Lorem ipsum dolor sit amet ipswum dolor</td>
	<td style="width:50px;vertical-align:top;color:white;">1 Line</td>
</tr>

Line count will require a JS script, have a look here:

http://www.webdeveloper.com/forum/archive/index.php/t-44333.html

Solution 8 - Html

I like that kind of solution. I saw somewhere this trick in stackoverflow but do not remember where exactly. Very usefel! :)

ul {
background: #000; 
  padding-left: 0;
}
ul li {
  height: 130px;
  position: relative;
  -webkit-box-sizing: border-box;
          box-sizing: border-box;
  border-bottom: 3px solid #F7F7F7;
}
ul li:last-of-type {
  border-bottom: none;
}
ul li:after {
  color: #333;
  position: absolute;
  right: 35px;
  font-size: 40px;
  content: ">";
  top: 50%;
  margin-top: -24px;
  color: #FFDA00;
  -webkit-transition: 0.25s all ease;
  transition: 0.25s all ease;
}
ul li a {
  font-size: 35px;
  font-family: Arial;
  color: #fff;
  -webkit-box-sizing: border-box;
          box-sizing: border-box;
  padding-left: 40px;
  height: 100%;
  line-height: 38px;
  display: inline-block;
  width: 100%;
  text-align: left;
  text-decoration: none;
}
ul li a:before {
  display: inline-block;
  vertical-align: middle;
  content: " ";
  height: 100%;
}
ul li a span {
  display: inline-block;
  vertical-align: middle;
}

<ul>
	<li class="dn">
		<a href="kapec.ru.php"><span> Lorem ipsum 1 LINE</span></a>
		</li>
		<li>
			<a href="varianti.ru.php"><span>Lorem ipsum <br> 2 lines </span></a>
		</li>

	</ul>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbraindamageView Question on Stackoverflow
Solution 1 - HtmlsandeepView Answer on Stackoverflow
Solution 2 - HtmlMr ListerView Answer on Stackoverflow
Solution 3 - HtmlRolfView Answer on Stackoverflow
Solution 4 - HtmlHans TionoView Answer on Stackoverflow
Solution 5 - HtmlAram MkrtchyanView Answer on Stackoverflow
Solution 6 - HtmlMax YariView Answer on Stackoverflow
Solution 7 - HtmlRyan BrodieView Answer on Stackoverflow
Solution 8 - HtmlАлексей ВоробьёвView Answer on Stackoverflow