What is the correct way to force text in a div to vertically align to the middle?
I have found a couple 'tricks', but I need something that works with a single line and multiple lines. Hopefully I am missing something stupid simple:
http://jsfiddle.net/rogerguess/DawFy/
.header {
display: inline-block;
text-align: center;
width: 123px;
background: green;
border-radius: 4px 4px 4px 4px;
height: 80px;
vertical-align: middle;
}
<div >
<div class="header">test1 </div>
<div class="header">some text that will wrap</div>
<div class="header">test3</div>
</div>
Change display: inline-block; to display: table-cell;
If your multi-line elements need to wrap for responsive reasons, then your best bet is with Flexbox. Due to the flakiness of Firefox's 2009 Flexbox implementation, it has to be handled slightly differently than you would do it for modern Flexbox implementations.
http://codepen.io/cimmanon/pen/mxuFa
<ul>
<li>One lorem ipsum dolor sit amet</li><!--
--><li>Two</li><!--
--><li>Three</li><!--
--><li>Four</li><!--
--><li>Five</li>
</ul>
li {
text-align: center;
vertical-align: middle;
/* fallback for non-Flexbox browsers */
display: inline-block;
/* Flexbox browsers */
display: -webkit-inline-box;
display: -moz-inline-box;
display: -ms-inline-flexbox;
display: -webkit-inline-flex;
display: inline-flex;
/* vertical centering for legacy, horizontal centering for modern */
-webkit-box-pack: center;
-moz-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
/* modern Flexbox only */
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
/* legacy Flexbox only */
-webkit-box-orient: vertical;
-moz-box-orient: vertical;
}
I've made a quick example for you, using display: table-cell property on the parent div.
CSS:
.outer {
width: 500px;
height: 500px;
display: table-cell;
background-color: red;
vertical-align: middle;
}
.inner {
display: block;
width: 300px;
height: 200px;
margin: 0px auto;
background-color: yellow;
}
HTML:
<div class="outer">
<div class="inner">
</div>
</div>
http://jsfiddle.net/aKT42/
Try this, it will work.
<div class="greenBorder" id="outer">
<div id="middle">
<div class="greenBorder" id="inner">
Your Text Here
</div>
</div>
</div>
Css Class
#outer {
height: 100%;
overflow: hidden;
position: relative;
width: 100%;
display: table;
position: static;
}
div.greenBorder {
background-color: #39B9FF;
text-align: center;
color: white;
}
#middle[id] {
display: table-cell;
position: static;
vertical-align: middle;
width: 100%;
}
#middle {
position: absolute;
top: 50%;
}
http://jsfiddle.net/DawFy/16/
.header {
display: block;
text-align: center;
width: 123px;
background: green;
border-radius: 4px 4px 4px 4px;
height: 80px;
margin:0px auto;
}
li
{
list-style-type:none;
}
<div >
<li>
<div class="header">test1 </div>
<li>
<div class="header">some text that will wrap</div>
<li>
<div class="header">test3</div>
Try this dont know if its "correct" but works
Most of these solutions wouldn't work for me because either the height was manually specified (which I couldn't do) or things like inline-block and float would remove the ability for the an inner div to inherit the parent div's height. I ended up creating a table with two columns each containing my divs and then I had no issues vertically centering text.
Related
How can i center image in any div in HTML & CSS ?
I have a nav bar that uses images as links for other pages, the nav bar is multiple divs each has an image and anther div contains a words, how can I center this img in the div
The CSS:
#Nav
{
height: 150px;
margin-top: 50px;
}
#Nav ul
{
list-style-type: none;
padding: 50px;
padding-top: 0px;
width: 100%;
}
#Nav li
{
float: left;
font-size: 12px;
font-weight: bold;
letter-spacing: 0;
margin: 30px;
}
#Nav a
{
display: inline-block;
padding: 5px;
width: 70px;
height: 100px;
color: black;
text-decoration: none;
}
#Nav a:hover
{
border: 2px solid #ddd;
border-radius: 4px;
box-shadow: 0 0 2px 1px rgba (0, 140, 186, 0.5);
}
#img img
{
align-self: middle;
width: 50px;
height: 50px;
}
#desc
{
margin-top: 15px;
text-align: center;
}
The html:
<li> <a target="_blank" href="#">
<div id="img"> <img src="C:/Users/hp1/Desktop/Website/Pictures/Accessories.jpg" alt="Accessories">
<div id="desc"> Accessories </div>
</div>
</a> </li>
You need to change your CSS for image like below:-
#img img
{
display: block;
margin: 0 auto;
}
You can see Many Demos Here-
Note that align-self is for elements inside a flexbox container which is not your case, you can try with text-align: center; on img.
Or if you wish to go full flexbox, set the img container to:
.containerClass {
display: flex;
flex-flow: column wrap;
justify-content: center;
}
With this setup align-self on the img is not need since justify-content on it's container will do.
Flexbox to the rescue:
.parent {
width:200px; height:200px;
border:2px solid #000;
display: flex; /* Make it flex */
flex-direction: column; /* wrap children elements to columns */
align-items: center; /* Center children horizontally */
justify-content: center; /* Center children vertically */
}
<div class="parent ">
<img src="http://placehold.it/100x100/cf5">
<p>Green-ish</p>
</div>
<div class="parent ">
<img src="http://placehold.it/100x100/5fc">
<p>Blue-ish</p>
</div>
align-self: center; is valid, align-self: middle; is not valid.
However, you can probably achieve what you're wanting with text-align: center;. Despite it being an image, you can still center using text-align as images are, by default, inline elements just like text.
#img img
{
align-self: center;
width: 50px;
height: 50px;
}
OR
#img img
{
text-align: center;
width: 50px;
height: 50px;
}
First you have to change the nature of div to table then make its align to center like this
#img img
{
display:table;
align: center;
width: 50px;
height: 50px;
}
Question:
Is it possible to vertically centre a child element in a floated div in relation to the dynamic height of a floated div along side it?
Desired result
Background:
I'm using a WordPress theme which has shortcodes for creating columns. The columns are created through floated divs. As such, I'd like to stick to using float, instead of using a table layout as has been suggested to similar questions (Vertically center content of floating div).
I'd like the result to be responsive. The image height changes with the screen width, and so I cannot set explicit height or margins on the p element.
The text in p is also more than one line, so I cannot set line-height as a solution (Vertically centre a div).
Example:
http://codepen.io/besiix/pen/rxdOWM
.wrapper {
background-color: #50C5B7;
padding: 5px;
}
.image {
width: 100%;
}
.clear {
clear: both;
}
.one-half {
float: left;
width:48%;
margin-right: 4%;
background-color: lightgrey;
position: relative;
display: inline;
box-sizing: border-box;
}
.last {
clear: right;
margin-right: 0;
}
<section class="wrapper">
<div class="one-half">
<p class="v-center"> This wants to be centered vertically in relation to the image on the right.</p>
</div>
<div class="one-half last">
<img class="image" src="http://www.premiercottages.co.uk/images/regions/Scotland.jpg">
</div>
<div class="clear"></div>
</section>
You could use flexbox, see jsfiddle https://jsfiddle.net/d5Lptwut/2/
<section class="wrapper">
<div class="one-half vertical-align">
<p> This wants to be centered vertically in relation to the image on the right.</p>
</div>
<div class="one-half last">
<img class="image" src="http://www.premiercottages.co.uk/images/regions/Scotland.jpg">
</div>
</section>
.wrapper {
background-color: #50C5B7;
padding: 5px;
display: flex;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
}
.image {
width: 100%;
height: auto;
}
.one-half {
width:48%;
margin-right: 4%;
background-color: lightgrey;
display: inline-block;
box-sizing: border-box;
}
.vertical-align {
flex-item-align: center;
-webkit-align-self: center;
-ms-flex-item-align: center;
align-self: center;
}
.last {
margin-right: 0;
}
you can try this one:
.wrapper {
background-color: #50C5B7;
padding: 5px;
display: flex;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
}
.image {
width: 100%;
height: auto;
}
.one-half {
width:48%;
margin-right: 4%;
background-color: lightgrey;
display: inline-block;
box-sizing: border-box;
}
.vertical-align {
flex-item-align: center;
-webkit-align-self: center;
-ms-flex-item-align: center;
align-self: center;
}
.last {
margin-right: 0;
clear:both;
}
DEMO HERE
you can use display: inline-block, this allow vertical-alignelements:
http://codepen.io/yukulele/pen/bEvYEZ?editors=1100
No matter how I style an element, none of the Flexbox styles I apply work. I have searched everywhere for a solution but could not find any (apologies if this is a duplicate as I could not find an answer to the problem).
I have created a CodePen here.
HTML:
<div class="test">
<div class="test2">
</div>
</div>
CSS:
* {
padding: 0;
margin: 0;
}
.test {
height: 10em;
width: 10em;
background: #333;
}
.test2 {
height: 2.5em;
width: 2.5em;
background: #ff0000;
display: flex;
flex-direction: column;
align-content: center;
justify-content: center;
text-align: center;
}
Thanks in advance for any help provided.
You need to add those CSS rules to the parent element instead.
When you set display: flex on an element, its direct children elements become flexbox items. In your example, the .test2 elements didn't have any children elements, so I assume you were probably wanting to add display: flex on the parent element instead.
.test {
height: 10em;
width: 10em;
background: #333;
display: flex;
align-items: center;
justify-content: center;
}
.test2 {
height: 2.5em;
width: 2.5em;
background: #ff0000;
}
<div class="test">
<div class="test2"></div>
</div>
I have 2 divs next to each other that I center vertically and horizontally using flex and justify-content/align-items.
Example
HTML:
<div class="inner">
<div class="section green">
<img src="http://i.imgur.com/hEOMgVf.png">
</div>
<div class="section red">
<img src="http://i.imgur.com/nEybO1g.png">
</div>
</div>
CSS:
.inner {
float: left;
width: 500px;
display: flex;
justify-content: center;
align-items: center;
background-color: #343434;
text-align: center;
}
.section {
float: left;
flex: 1;
}
.green { background-color: #7dc242; }
.red { background-color: #ed1c24; }
My issue is that I need to set the height of both 'section' divs to the same as well as centering them vertically and horizontally. You can see in the JSFiddle below that the green background isn't the same height as the red. How can I make both divs the full height of the container div?
Here's a simplified JSFiddle of what I have:
http://jsfiddle.net/beK28/1/
To achieve the effect you want, you shouldn't try to do any of the alignment in the container element and instead should set .section to also be display:flex. Then you can justify and center the images correctly within the children elements.
.section {
align-items: center;
display: flex;
flex: 1;
justify-content:center;
text-align: center;
}
http://jsfiddle.net/beK28/8/
You also don't need to use float, that's the whole point of using flexible containers.
Your elements aren't stretching vertically anymore because you've set align-items: center. If you want them to be equal height, it has to be the default value of stretch. If your elements were multi-line, then you could use align-content: center instead, which will give you the effect you're looking for. For single-line flex items, it does not appear that you can have vertical centering + equal height through Flexbox alone.
http://jsfiddle.net/beK28/6/
.inner {
float: left;
width: 400px;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-content: center;
background-color: #343434;
text-align: center;
height: 500px;
}
Note, however, that you can have flex items with the display property of table-cell.
http://jsfiddle.net/beK28/7/
.inner {
float: left;
width: 400px;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
background-color: #343434;
text-align: center;
height: 500px;
}
.section {
display: table-cell;
flex: 1;
}
I've had problems with stretch/centering before, and ended up formatting as display: table-cell:
.inner {
float: left;
width: 500px;
display: table;
background-color: #343434;
text-align: center;
}
.section {
display: table-cell;
width: 50%;
vertical-align: middle;
}
I have seen that while developing websites, vertically centering a container (of fixed height) inside a container of random height always comes as a nightmare for the web developer (at least, me) while when it comes to horizontal centering a container (of fixed width) inside a container of random width, the margin:0px auto; tends to serve an easy way out in the standard model.
When things can be as simple as that why doesn't CSS work out with the margin:auto 0px; when it comes to centering a container of fixed height inside a container of random height? Is there any specific reason to do so?
It's really less of a nightmare than you think, just don't use margins. vertical-align is really what you should rely on for fluid-height vertical centering. I threw together a quick demo to demonstrate my point:
* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
text-align: center;
}
span {
height: 100%;
vertical-align: middle;
display: inline-block;
}
#any-height {
background: #000;
text-align: left;
width: 200px;
height: 200px;
vertical-align: middle;
display: inline-block;
}
<span></span>
<div id="any-height"></div>
See: http://jsfiddle.net/Wexcode/jLXMS/
The right answer for your question is that margin: auto 0 doesn't work the same way that margin: 0 auto works because width: auto doesn't work the same way height: auto does.
Vertical auto margin works for absolutely positioned elements with a known height.
.parent {
position: relative;
}
.child {
position: absolute;
top: 0; right: 0; bottom: 0; left: 0;
width: 150px;
height: 150px;
margin: auto;
}
Use flexbox parent
One of advantages of using margin: auto compared to justify-content: center / align-items: center is ability to scroll child item that is overflowing parent
html, body {
height: 100%;
margin: 0;
}
.parent {
display: flex;
width: 100%;
height: 100%;
}
.inner {
width: 100px;
height: 100px;
background-color: green;
margin: auto;
}
<div class="parent">
<div class="inner">
</div>
</div>
CSS
.aligncenter{
display: -webkit-box;
display: -moz-box;
display: box;
-webkit-box-align: center;
-moz-box-align: center;
flex-align: center;
-webkit-box-pack: center;
-moz-box-pack: center;
flex-pack: center;
}
HTML
<div class="aligncenter">
---your content appear at the middle of the parent div---
</div>
.aligncenter {
display: -webkit-box;
display: -moz-box;
display: box;
-webkit-box-align: center;
-moz-box-align: center;
flex-align: center;
-webkit-box-pack: center;
-moz-box-pack: center;
flex-pack: center;
}
<div class="aligncenter">
---your content appear at the middle of the parent div---
</div>
Note
This CSS class work with almost all browsers