<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>HTML Archives : vedmant.com : coding blog</title>
	<atom:link href="https://vedmant.com/tag/html/feed/" rel="self" type="application/rss+xml" />
	<link>https://vedmant.com/tag/html/</link>
	<description>Sharing my personal experience in web development</description>
	<lastBuildDate>Mon, 29 Jun 2015 18:59:24 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8</generator>
	<item>
		<title>CSS vertical centering techniques</title>
		<link>https://vedmant.com/css-vertical-centering-techniques/</link>
					<comments>https://vedmant.com/css-vertical-centering-techniques/#respond</comments>
		
		<dc:creator><![CDATA[vedmant]]></dc:creator>
		<pubDate>Mon, 29 Jun 2015 18:57:36 +0000</pubDate>
				<category><![CDATA[CSS3]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<guid isPermaLink="false">https://vedmant.com/?p=49</guid>

					<description><![CDATA[<p>So what is the best way to center blocks vertically using CSS? There are bunch of techniques, lets try each of them! 1. &#8220;line-height&#8221; property HTML code: CSS code: Here is the result: &#160; So it works just good, quite simple, but it doesn&#8217;t support variable height. 2. &#8220;position: absolute&#8221; and negative margins HTML code: CSS code: Here &#8230; <a href="https://vedmant.com/css-vertical-centering-techniques/" class="more-link">Continue reading <span class="screen-reader-text">CSS vertical centering techniques</span></a></p>
<p>The post <a href="https://vedmant.com/css-vertical-centering-techniques/">CSS vertical centering techniques</a> appeared first on <a href="https://vedmant.com">vedmant.com :: coding blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<style>
.outer-block { background-color: gray; }
.inner-block p {margin-bottom: 0; }
.inner-text { display: inline-block; text-align: left; }
</style>

<h3>So what is the best way to center blocks vertically using CSS?</h3>
<p>There are bunch of techniques, lets try each of them!</p>
<h3>1. &#8220;line-height&#8221; property</h3>
<p>HTML code:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;div class=&quot;outer-block&quot;&gt;
   &lt;div class=&quot;inner-block&quot;&gt;
      &lt;p&gt;Some text&lt;/p&gt;
      &lt;p&gt;Some more text&lt;/p&gt;
   &lt;/div&gt;
&lt;/div&gt;
</pre>
<p>CSS code:</p>
<pre class="brush: css; title: ; notranslate">
.outer-block { 
   height: 200px; 
   line-height: 200px; 
   text-align: center; 
}
.inner-block { 
   display: inline-block; 
   line-height: 1.5;
   text-align: left; 
   vertical-align: middle; 
}
</pre>
<p>Here is the result: </p>
 
<style>
.centering-inline-block .outer-block { 
   height: 200px; 
   line-height: 200px; 
   text-align: center; 
}
.centering-inline-block .inner-block { 
   display: inline-block; 
   line-height: 1.5;
   text-align: left; 
   vertical-align: middle;
}
</style>

<div class="centering-inline-block">

<div class="outer-block">
   <div class="inner-block">
      <p>Some text</p>
      <p>Some more text</p>
   </div>
</div>

</div>

<p>&nbsp;<br />
So it works just good, quite simple, but it doesn&#8217;t support variable height.</p>
<h3>2. &#8220;position: absolute&#8221; and negative margins</h3>
<p>HTML code:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;div class=&quot;outer-block&quot;&gt;
   &lt;div class=&quot;inner-block&quot;&gt;
      &lt;div class=&quot;inner-text&quot;&gt;
         &lt;p&gt;Some text&lt;/p&gt;
         &lt;p&gt;Some more text&lt;/p&gt;
      &lt;/div&gt;
   &lt;/div&gt;
&lt;/div&gt;

</pre>
<p>CSS code:</p>
<pre class="brush: css; title: ; notranslate">
.outer-block {
   position: relative;
   height: 200px; 
   text-align: center; 
}
.inner-block { 
   position: absolute;
   top: 50%;
   left: 0;
   margin-top: -30px;
   width: 100%;
   height: 60px;
   text-align: center; 
}
</pre>
<p>Here is the result: </p>
 
<style>
.centering-absolute .outer-block {
   position: relative;
   height: 200px; 
   text-align: center; 
}
.centering-absolute .inner-block { 
   position: absolute;
   top: 50%;
   left: 0;
   margin-top: -30px;
   width: 100%;
   height: 60px;
   text-align: center; 
}
</style>

<div class="centering-absolute">

<div class="outer-block">
   <div class="inner-block">
      <div class="inner-text">
         <p>Some text</p>
         <p>Some more text</p>
      </div>
   </div>
</div>

</div>

<p>&nbsp;<br />
Another good approach, not so simple as line-height though. However it needs inner block to by fixed height so negative margin-top can be set. And it needs additional block to center text block horizontally.</p>
<h3>3. Table cell method</h3>
<p>HTML code:</p>
<pre class="brush: xml; title: ; notranslate">

&lt;div class=&quot;some-container&quot;&gt;
   &lt;div class=&quot;outer-block&quot;&gt;
      &lt;div class=&quot;inner-block&quot;&gt;
         &lt;div class=&quot;inner-text&quot;&gt;
            &lt;p&gt;Some text&lt;/p&gt;
            &lt;p&gt;Some more text&lt;/p&gt;
         &lt;/div&gt;
      &lt;/div&gt;
   &lt;/div&gt;
&lt;/div&gt;
</pre>
<p>CSS code:</p>
<pre class="brush: css; title: ; notranslate">
.some-container {
   height: 200px;
}
.outer-block { 
   width: 100%; 
   height: 100%;
   display: table;
}
.inner-block { 
   width: 100%; 
   height: 100%;
   display: table-cell;
   vertical-align: middle;
   text-align: center;
}
</pre>
<p>Here is the result: </p>
 
<style>
.centering-table-cell .some-container {
   height: 200px;
}
.centering-table-cell .outer-block { 
   width: 100%; 
   height: 100%;
   display: table; 
}
.centering-table-cell .inner-block { 
   width: 100%; 
   height: 100%;
   display: table-cell;
   vertical-align: middle;
   text-align: center;
}
</style>

<div class="centering-table-cell">

<div class="some-container">
   <div class="outer-block">
      <div class="inner-block">
         <div class="inner-text">
            <p>Some text</p>
            <p>Some more text</p>
         </div>
      </div>
   </div>
</div>

</div>

<p>&nbsp;<br />
This is a great approach if you have container with variable height, this method will certainly work in this case! Downside of this method is that it will not work in older versions of IE. Also it needs additional block to center text block horizontally. Another pros is that it will expand if inner-block content height will became bigger than outer block.</p>
<p>If you need it to work in older IE versions you can use explicit table tags instead of div blocks:</p>
<p>HTML code:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;div class=&quot;some-container&quot;&gt;
   &lt;table class=&quot;outer-block&quot;&gt;
      &lt;tr&gt;
         &lt;td class=&quot;inner-block&quot;&gt;
            &lt;div class=&quot;inner-text&quot;&gt;
               &lt;p&gt;Some text&lt;/p&gt;
               &lt;p&gt;Some more text&lt;/p&gt;
            &lt;/div&gt;
         &lt;/td&gt;
      &lt;/tr&gt;
   &lt;/table&gt;
&lt;/div&gt;
</pre>
<p>CSS code:</p>
<pre class="brush: css; title: ; notranslate">
.some-container {
   height: 200px;
}
.outer-block { 
   width: 100%; 
   height: 100%; 
}
.inner-block {
   vertical-align: middle;
   text-align: center;
}
</pre>
<p>Here is the result: </p>
 
<style>
.centering-table .some-container {
   height: 200px;
}
.centering-table .outer-block { 
   width: 100%; 
   height: 100%; 
}
.centering-table .inner-block {
   vertical-align: middle;
   text-align: center;
}
</style>

<div class="centering-table">

<div class="some-container">
   <table class="outer-block">
      <tr>
         <td class="inner-block">
            <div class="inner-text">
               <p>Some text</p>
               <p>Some more text</p>
            </div>
         </td>
      </tr>
   </table>
</div>
</div>

<h3>Summary</h3>
<p>To summary this methods, I would say the best way for responsive designs is table cell method. Despite it doesn&#8217;t support old versions of IE this is virtually the only method to fully center block vertically for responsive design without JavaScript snippets. Two other ways always require outer or inner block to have fixes height value.</p>
<p>The post <a href="https://vedmant.com/css-vertical-centering-techniques/">CSS vertical centering techniques</a> appeared first on <a href="https://vedmant.com">vedmant.com :: coding blog</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://vedmant.com/css-vertical-centering-techniques/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
