Skip to content

Commit b4f6d9e

Browse files
committed
added example for vertical center alignment
1 parent 229985e commit b4f6d9e

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

css.html

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,22 @@
148148
font-weight: bolder;
149149
color: purple;
150150
}
151+
#alignExample div{
152+
height: 100px;
153+
border: 2px solid gray;
154+
text-align: center;
155+
position: relative;
156+
}
157+
#alignExample div p{
158+
position: absolute;
159+
top: 0;
160+
bottom: 0;
161+
left: 0;
162+
right: 0;
163+
width: 50%;
164+
height: 30%;
165+
margin: auto;
166+
}
151167
</style>
152168

153169
<!-- Just for debugging purposes. Don't actually copy this line! -->
@@ -437,9 +453,22 @@ <h2>16. @import</h2>
437453
<p>ref: be careful while using @import (<a href="http://www.stevesouders.com/blog/2009/04/09/dont-use-import/">don't use @import</a>)</p>
438454
</div>
439455
<div id="something">
440-
<h2>17. vertical alignment</h2>
441-
<p><strong></strong></p>
442-
<p><strong></strong></p>
456+
<h2>17. vertical Center</h2>
457+
<p><strong>Question:</strong> How do you align a p center-center inside a div?</p>
458+
<p><strong>Answer:</strong> <code>text-align:center</code>will do the horizontal alignment but <code>vertical-laign:middle</code> will not work here. there are couple of different ways to solve this problem and one of them are positioning. You make the parent as relative position and child as absolute positioning. And then define all position parameter as sero and width 50% and height 30% (sounds messy look at the example and read ref) </p>
459+
<div>
460+
<button id = "align" type="button" class="toggleExample btn btn-primary">show example</button>
461+
<div id="alignExample" class="hide">
462+
<pre><code>
463+
&lt;style&gt;<br/> .divContainer{<br/> height: 100px;<br/> border: 2px solid gray;<br/> text-align: center;<br/> position: relative;<br/> }<br/> p{ <br/> position: absolute;<br/> top: 0;<br/> bottom: 0;<br/> left: 0;<br/> right: 0;<br/> width: 50%;<br/> height: 30%;<br/> margin: auto;<br/> }<br/>&lt;/style&gt;<br/>&lt;div class = &quot;divContainer&quot;&gt;<br/> &lt;p&gt;content example&lt;/p&gt;<br/>&lt;/div&gt;<br/>
464+
</code></pre>
465+
<strong>result:</strong>
466+
<div>
467+
<p>content example</p>
468+
</div>
469+
</div>
470+
</div>
471+
<p>ref: <a href="http://www.vanseodesign.com/css/vertical-centering/">6 methods for vertical center</a>, <a href="http://coding.smashingmagazine.com/2013/08/09/absolute-horizontal-vertical-centering-css/">Absolute horizontal and vertical centering</a></p>
443472
</div>
444473
<div id="something">
445474
<h2>18. sprite</h2>

0 commit comments

Comments
 (0)