display:inline-block not aligning properly? [duplicate] - javascript
I have a div with two images and an h1. All of them need to be vertically aligned within the div, next to each other. One of the images needs to be absolute positioned within the div.
What is the CSS needed for this to work on all common browsers?
<div id="header">
<img src=".." ></img>
<h1>testing...</h1>
<img src="..."></img>
</div>
Wow, this problem is popular. It's based on a misunderstanding in the vertical-align property. This excellent article explains it:
Understanding vertical-align, or "How (Not) To Vertically Center Content" by Gavin Kistner.
“How to center in CSS” is a great web tool which helps to find the necessary CSS centering attributes for different situations.
In a nutshell (and to prevent link rot):
Inline elements (and only inline elements) can be vertically aligned in their context via vertical-align: middle. However, the “context” isn’t the whole parent container height, it’s the height of the text line they’re in. jsfiddle example
For block elements, vertical alignment is harder and strongly depends on the specific situation:
If the inner element can have a fixed height, you can make its position absolute and specify its height, margin-top and top position. jsfiddle example
If the centered element consists of a single line and its parent height is fixed you can simply set the container’s line-height to fill its height. This method is quite versatile in my experience. jsfiddle example
… there are more such special cases.
Now that Flexbox support is increasing, this CSS applied to the containing element would vertically center all contained items (except for those items that specify the alignment themselves, e.g. align-self:start)
.container {
display: flex;
align-items: center;
}
Use the prefixed version if you also need to target Internet Explorer 10, and older (< 4.4 (KitKat)) Android browsers:
.container {
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-ms-flex-align: center;
-webkit-align-items: center;
-webkit-box-align: center;
align-items: center;
}
I used this very simple code:
div.ext-box { display: table; width:100%;}
div.int-box { display: table-cell; vertical-align: middle; }
<div class="ext-box">
<div class="int-box">
<h2>Some txt</h2>
<p>bla bla bla</p>
</div>
</div>
Obviously, whether you use a .class or an #id, the result won't change.
.outer {
display: flex;
align-items: center;
justify-content: center;
}
It worked for me:
.vcontainer {
min-height: 10em;
display: table-cell;
vertical-align: middle;
}
Vertically and horizontally align element
Use either of these. The result would be the same:
Bootstrap 4
CSS3
1. Bootstrap 4.3+
For vertical alignment: d-flex align-items-center
For horizontal alignment: d-flex justify-content-center
For vertical and horizontal alignment: d-flex align-items-center justify-content-center
.container {
height: 180px;
width:100%;
background-color: blueviolet;
}
.container > div {
background-color: white;
padding: 1rem;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
rel="stylesheet"/>
<div class="d-flex align-items-center justify-content-center container">
<div>I am in Center</div>
</div>
2. CSS 3
.container {
height: 180px;
width:100%;
background-color: blueviolet;
}
.container > div {
background-color: white;
padding: 1rem;
}
.center {
display: flex;
align-items: center;
justify-content: center;
}
<div class="container center">
<div>I am in Center</div>
</div>
A technique from a friend of mine:
div:before {content:" "; display:inline-block; height:100%; vertical-align:middle;}
div p {display:inline-block;}
<div style="height:100px; border:1px solid;">
<p style="border:1px dotted;">I'm vertically centered.</p>
</div>
Demo here.
To position block elements to the center (works in Internet Explorer 9 and above), it needs a wrapper div:
.vcontainer {
position: relative;
top: 50%;
transform: translateY(-50%);
-webkit-transform: translateY(-50%);
}
Use this formula, and it will work always without cracks:
#outer {height: 400px; overflow: hidden; position: relative;}
#outer[id] {display: table; position: static;}
#middle {position: absolute; top: 50%;} /* For explorer only*/
#middle[id] {display: table-cell; vertical-align: middle; width: 100%;}
#inner {position: relative; top: -50%} /* For explorer only */
/* Optional: #inner[id] {position: static;} */
<div id="outer">
<div id="middle">
<div id="inner">
any text
any height
any content, for example generated from DB
everything is vertically centered
</div>
</div>
</div>
All of them need to be vertically aligned within the div
Aligned how? Tops of the images aligned with the top of the text?
One of the images needs to be absolute positioned within the div.
Absolutely positioned relative to the DIV? Perhaps you could sketch out what you're looking for...?
fd has described the steps for absolute positioning, as well as adjusting the display of the H1 element such that images will appear inline with it. To that, i'll add that you can align the images by use of the vertical-align style:
#header h1 { display: inline; }
#header img { vertical-align: middle; }
...this would put the header and images together, with top edges aligned. Other alignment options exist; see the documentation. You might also find it beneficial to drop the DIV and move the images inside the H1 element - this provides semantic value to the container, and removes the need to adjust the display of the H1:
<h1 id=header">
<img src=".." ></img>
testing...
<img src="..."></img>
</h1>
Almost all methods needs to specify the height, but often we don't have any heights.
So here is a CSS 3 three-line trick that doesn't require to know the height.
.element {
position: relative;
top: 50%;
transform: translateY(-50%);
}
It's supported even in IE9.
with its vendor prefixes:
.element {
position: relative;
top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
Source: Vertical align anything with just 3 lines of CSS
Three ways to make a center child div in a parent div
Absolute positioning method
Flexbox method
Transform/translate method
Demo
/* Absolute Positioning Method */
.parent1 {
background: darkcyan;
width: 200px;
height: 200px;
position: relative;
}
.child1 {
background: white;
height: 30px;
width: 30px;
position: absolute;
top: 50%;
left: 50%;
margin: -15px;
}
/* Flexbox Method */
.parent2 {
display: flex;
justify-content: center;
align-items: center;
background: darkcyan;
height: 200px;
width: 200px;
}
.child2 {
background: white;
height: 30px;
width: 30px;
}
/* Transform/Translate Method */
.parent3 {
position: relative;
height: 200px;
width: 200px;
background: darkcyan;
}
.child3 {
background: white;
height: 30px;
width: 30px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div class="parent1">
<div class="child1"></div>
</div>
<hr />
<div class="parent2">
<div class="child2"></div>
</div>
<hr />
<div class="parent3">
<div class="child3"></div>
</div>
My trick is to put a table inside the div with one row and one column, set 100% of width and height, and the property vertical-align:middle:
<div>
<table style="width:100%; height:100%;">
<tr>
<td style="vertical-align:middle;">
BUTTON TEXT
</td>
</tr>
</table>
</div>
Fiddle:
http://jsfiddle.net/joan16v/sbqjnn9q/
Using display flex, first you need to wrap the container of the item that you want to align:
<div class="outdiv">
<div class="indiv">
<span>test1</span>
<span>test2</span>
</div>
</div>
Then apply the following CSS content to wrap div or outdiv in my example:
.outdiv {
display: flex;
justify-content: center;
align-items: center;
}
Using CSS to vertical center, you can let the outer containers act like a table, and the content as a table cell. In this format your objects will stay centered. :)
I nested multiple objects in JSFiddle for an example, but the core idea is like this:
HTML
<div class="circle">
<div class="content">
Some text
</div>
</div>
CSS
.circle {
/* Act as a table so we can center vertically its child */
display: table;
/* Set dimensions */
height: 200px;
width: 200px;
/* Horizontal center text */
text-align: center;
/* Create a red circle */
border-radius: 100%;
background: red;
}
.content {
/* Act as a table cell */
display: table-cell;
/* And now we can vertically center! */
vertical-align: middle;
/* Some basic markup */
font-size: 30px;
font-weight: bold;
color: white;
}
The multiple objects example:
HTML
<div class="container">
<div class="content">
<div class="centerhoriz">
<div class="circle">
<div class="content">
Some text
</div><!-- content -->
</div><!-- circle -->
<div class="square">
<div class="content">
<div id="smallcircle"></div>
</div><!-- content -->
</div><!-- square -->
</div><!-- center-horiz -->
</div><!-- content -->
</div><!-- container -->
CSS
.container {
display: table;
height: 500px;
width: 300px;
text-align: center;
background: lightblue;
}
.centerhoriz {
display: inline-block;
}
.circle {
display: table;
height: 200px;
width: 200px;
text-align: center;
background: red;
border-radius: 100%;
margin: 10px;
}
.square {
display: table;
height: 200px;
width: 200px;
text-align: center;
background: blue;
margin: 10px;
}
.content {
display: table-cell;
vertical-align: middle;
font-size: 30px;
font-weight: bold;
color: white;
}
#smallcircle {
display: inline-block;
height: 50px;
width: 50px;
background: green;
border-radius: 100%;
}
Result
https://jsfiddle.net/martjemeyer/ybs032uc/1/
I have found a new workaround to vertically align multiple text-lines in a div using CSS 3 (and I am also using bootstrap v3 grid system to beautify the UI), which is as below:
.immediate-parent-of-text-containing-div {
height: 50px; /* Or any fixed height that suits you. */
}
.text-containing-div {
display: inline-grid;
align-items: center;
text-align: center;
height: 100%;
}
As per my understanding, the immediate parent of text containing element must have some height.
We may use a CSS function calculation to calculate the size of the element and then position the child element accordingly.
Example HTML:
<div class="box">
<span>Some Text</span>
</div>
And CSS:
.box {
display: block;
background: #60D3E8;
position: relative;
width: 300px;
height: 200px;
text-align: center;
}
.box span {
font: bold 20px/20px 'source code pro', sans-serif;
position: absolute;
left: 0;
right: 0;
top: calc(50% - 10px);
}
a {
color: white;
text-decoration: none;
}
Demo created here: https://jsfiddle.net/xnjq1t22/
This solution works well with responsive div height and width as well.
Note: The calc function is not tested for compatiblity with old browsers.
Using only a Bootstrap class:
div: class="container d-flex"
element inside div: class="m-auto"
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.3/css/bootstrap.min.css" crossorigin="anonymous">
<div class="container d-flex mt-5" style="height:110px; background-color: #333;">
<h2 class="m-auto">H➲VER➾M⇡ND</h2>
</div>
By default h1 is a block element and will render on the line after the first img, and will cause the second img to appear on the line following the block.
To stop this from occurring you can set the h1 to have inline flow behaviour:
#header > h1 { display: inline; }
As for absolutely positioning the img inside the div, you need to set the containing div to have a "known size" before this will work properly. In my experience, you also need to change the position attribute away from the default - position: relative works for me:
#header { position: relative; width: 20em; height: 20em; }
#img-for-abs-positioning { position: absolute; top: 0; left: 0; }
If you can get that to work, you might want to try progressively removing the height, width, position attributes from div.header to get the minimal required attributes to get the effect you want.
UPDATE:
Here is a complete example that works on Firefox 3:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Example of vertical positioning inside a div</title>
<style type="text/css">
#header > h1 { display: inline; }
#header { border: solid 1px red;
position: relative; }
#img-for-abs-positioning { position: absolute;
bottom: -1em; right: 2em; }
</style>
</head>
<body>
<div id="header">
<img src="#" alt="Image 1" width="40" height="40" />
<h1>Header</h1>
<img src="#" alt="Image 2" width="40" height="40"
id="img-for-abs-positioning" />
</div>
</body>
</html>
My new favorite way to do it is with a CSS grid:
/* technique */
.wrapper {
display: inline-grid;
grid-auto-flow: column;
align-items: center;
justify-content: center;
}
/* visual emphasis */
.wrapper {
border: 1px solid red;
height: 180px;
width: 400px;
}
img {
width: 100px;
height: 80px;
background: #fafafa;
}
img:nth-child(2) {
height: 120px;
}
<div class="wrapper">
<img src="https://source.unsplash.com/random/100x80/?bear">
<img src="https://source.unsplash.com/random/100x120/?lion">
<img src="https://source.unsplash.com/random/100x80/?tiger">
</div>
Just use a one-cell table inside the div! Just set the cell and table height and with to 100% and you can use the vertical-align.
A one-cell table inside the div handles the vertical-align and is backward compatible back to the Stone Age!
For me, it worked this way:
<div style="width:70px; height:68px; float:right; display: table-cell; line-height: 68px">
Login
</div>
The "a" element converted to a button, using Bootstrap classes, and it is now vertically centered inside an outer "div".
I have been using the following solution (with no positioning and no line height) since over a year, it works with Internet Explorer 7 and Internet Explorer 8 as well.
<style>
.outer {
font-size: 0;
width: 400px;
height: 400px;
background: orange;
text-align: center;
display: inline-block;
}
.outer .emptyDiv {
height: 100%;
background: orange;
visibility: collapse;
}
.outer .inner {
padding: 10px;
background: red;
font: bold 12px Arial;
}
.verticalCenter {
display: inline-block;
*display: inline;
zoom: 1;
vertical-align: middle;
}
</style>
<div class="outer">
<div class="emptyDiv verticalCenter"></div>
<div class="inner verticalCenter">
<p>Line 1</p>
<p>Line 2</p>
</div>
</div>
This is my personal solution for an i element inside a div.
JSFiddle Example
HTML
<div class="circle">
<i class="fa fa-plus icon">
</i></div>
CSS
.circle {
border-radius: 50%;
color: blue;
background-color: red;
height:100px;
width:100px;
text-align: center;
line-height: 100px;
}
.icon {
font-size: 50px;
vertical-align: middle;
}
Just this:
<div>
<table style="width: 100%; height: 100%">
<tr>
<td style="width: 100%; height: 100%; vertical-align: middle;">
What ever you want vertically-aligned
</td>
</tr>
</table>
</div>
A one-cell table inside the div handles the vertical-align and is backward compatible back to the Stone Age!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<style type="text/css">
#style_center { position:relative; top:50%; left:50%; }
#style_center_absolute { position:absolute; top:50px; left:50px; }
<!--#style_center { position:relative; top:50%; left:50%; height:50px; margin-top:-25px; }-->
</style>
</head>
<body>
<div style="height:200px; width:200px; background:#00FF00">
<div id="style_center">+</div>
</div>
</body>
</html>
Here is just another (responsive) approach:
html,
body {
height: 100%;
}
body {
margin: 0;
}
.table {
display: table;
width: auto;
table-layout:auto;
height: 100%;
}
.table:nth-child(even) {
background: #a9edc3;
}
.table:nth-child(odd) {
background: #eda9ce;
}
.tr {
display: table-row;
}
.td {
display: table-cell;
width: 50%;
vertical-align: middle;
}
http://jsfiddle.net/herrfischerhamburg/JcVxz/
<div id="header" style="display: table-cell; vertical-align:middle;">
...
or CSS
.someClass
{
display: table-cell;
vertical-align:middle;
}
Browser Coverage
Related
Why won’t this section stay contained when I change my browser screen size? HTML, CSS
Why won’t this section stay contained when I change my browser screen size? I used HTML and CSS. It looks fine when I made my screen full size but then if I make my browser smaller it doesn’t show the full content. (it only shows half of the image but shows the full text) screenshot of how it looks on smaller browser](https://i.stack.imgur.com/i9qiK.jpg) .whoweare{ padding:80px 0px 50px; background-color: #000000; } .whoweare .content { -webkit-display: flex; display: flex; } .whoweare .content .box{ padding:5px; flex:0 0 100%; max-width: 100%; } .whoweare .content .item-img { width: 50%; -webkit-transform: rotateY(180deg); transform: rotateY(180deg); padding: 30px; } .whoweare .content .item-text h2{ font-size: 50px; font-weight: 500; margin:20px; color: #ffffff; padding: 0px 0px 20px; } .whoweare .content .item-text p{ font-size: 20px; line-height:50px; color:#ffffff; margin:20px; padding:0px 0px 20px; font-family: 'Open Sans', sans-serif; } .whoweare .content .class-items .item{ margin-bottom: 30px; display: flex; } .whoweare .content .class-items .item .item-text{ padding: 15px; display: flex; flex-direction: column; justify-content: center; } ` <section class="whoweare" id="whoweare"> <dic class="container"> <div class="content"> <div class="box text wow slideInRight"> <div class="class-items"> <div class="item wow bounceInUp"> <div class="item-img"> <img src="images/ACD68359-D3EB-4302-99C0-E59A663380AB.jpeg" alt="classes" width="601" height="751" /> </div> <div class="item-text"> <h2>Who we are</h2> <p>Our gym provides a unique experience </p> </div> </div> </section>````
Change the height width of img tags. Use % parameters for example height=50% width=60%. I think 🤔 you need also change the height width of other container tags If design is not look like good
You should add to all importan containers` .container { width:100%: height: auto; margin: auto; } //Also can try .container { width: auto; padding: 0; } that should work...
Problem img has specific width and height, hence it overflows even though its container (in your case .item-img has width:50%;). Answer Give the img of width: 100%; and object-fit: contain or object-fit: cover if you want it cropped.
Horizontally center a div relative to the viewport when parent is small
I have a page layout with a sidebar alongside a main-content div. Partway down the page, I have a div (parent) inside of a div (child). The child div needs to be horizontally centered relative to the screen instead of the parent. <div class="sidebar"></div> <div class="main-content"> <div class="parent"> <div class="child"></div> <div class="other-stuff"></div> </div> </div> <style> .parent{ width: 100px; } .child{ width: 200px; } </style> My first thought was using absolute positioning with something like this: .child{ position: absolute; margin-left: auto; margin-right: auto; left: 0; right: 0; text-align: center; } The problem I faced here was that I had to make up for the extra space due to the child being taken out of document flow. "Other-stuff" wanted to move up to fill the gap left behind. Is there a better way to accomplish this beyond just pushing the div around with absolute positioning and adding extra margin/padding to make up the space so the lower content doesn't come up? I'm open to abandoning absolute positioning — that was just the first thing that came to mind.
You can use a flexbox to position multiple items in one container and knock the children out of the parent div so it's not affected by whatever positioning you do with that. *, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; } body { width: 100vw; } .parent { border: 1px solid black; width: 100px; height: fit-content; } .container { display: flex; flex-direction: column; align-items: center; width: 100%; height: 100%; gap: 2px; } .child { display: block; border: 1px solid black; width: 200px; height: fit-content; } .other-stuff { display: block; border: 1px solid black; width: 200px; height: fit-content; } <div class="sidebar"></div> <div class="main-content"> <div class="parent"> I'm the Parent </div> <div class="container"> <div class="child">I'm the Child</div> <div class="other-stuff">I'm the Other Stuff</div> </div> </div>
how do I vertically center content inside a div? [duplicate]
I want to center a div vertically with CSS. I don't want tables or JavaScript, but only pure CSS. I found some solutions, but all of them are missing Internet Explorer 6 support. <body> <div>Div to be aligned vertically</div> </body> How can I center a div vertically in all major browsers, including Internet Explorer 6?
Below is the best all-around solution I could build to vertically and horizontally center a fixed-width, flexible height content box. It was tested and worked for recent versions of Firefox, Opera, Chrome, and Safari. .outer { display: table; position: absolute; top: 0; left: 0; height: 100%; width: 100%; } .middle { display: table-cell; vertical-align: middle; } .inner { margin-left: auto; margin-right: auto; width: 400px; /* Whatever width you want */ } <div class="outer"> <div class="middle"> <div class="inner"> <h1>The Content</h1> <p>Once upon a midnight dreary...</p> </div> </div> </div> View A Working Example With Dynamic Content I built in some dynamic content to test the flexibility and would love to know if anyone sees any problems with it. It should work well for centered overlays also -- lightbox, pop-up, etc.
The simplest way would be the following three lines of CSS: 1) position: relative; 2) top: 50%; 3) transform: translateY(-50%); Following is an example: div.outer-div { height: 170px; width: 300px; background-color: lightgray; } div.middle-div { position: relative; top: 50%; -webkit-transform: translateY(-50%); -ms-transform: translateY(-50%); transform: translateY(-50%); } <div class='outer-div'> <div class='middle-div'> Test text </div> </div>
One more I can't see on the list: .Center-Container { position: relative; height: 100%; } .Absolute-Center { width: 50%; height: 50%; overflow: auto; margin: auto; position: absolute; top: 0; left: 0; bottom: 0; right: 0; border: solid black; } Cross-browser (including Internet Explorer 8 - Internet Explorer 10 without hacks!) Responsive with percentages and min-/max- Centered regardless of padding (without box-sizing!) height must be declared (see Variable Height) Recommended setting overflow: auto to prevent content spillover (see Overflow) Source: Absolute Horizontal And Vertical Centering In CSS
Now the Flexbox solution is a very easy way for modern browsers, so I recommend this for you: .container { display: flex; align-items: center; justify-content: center; height: 100%; background: green; } body, html { height: 100%; } <div class="container"> <div>Div to be aligned vertically</div> </div>
Actually, you need two div's for vertical centering. The div containing the content must have a width and height. #container { position: absolute; top: 50%; margin-top: -200px; /* Half of #content height */ left: 0; width: 100%; } #content { width: 624px; margin-left: auto; margin-right: auto; height: 395px; border: 1px solid #000000; } <div id="container"> <div id="content"> <h1>Centered div</h1> </div> </div> Here is the result.
Edit 2020: only use this if you need to support old browsers like Internet Explorer 8 (which you should refuse to do 😉). If not, use Flexbox. This is the simplest method I found and I use it all the time (jsFiddle demo here). Thank Chris Coyier from CSS Tricks for this article. html, body{ height: 100%; margin: 0; } .v-wrap{ height: 100%; white-space: nowrap; text-align: center; } .v-wrap:before{ content: ""; display: inline-block; vertical-align: middle; width: 0; /* adjust for white space between pseudo element and next sibling */ margin-right: -.25em; /* stretch line height */ height: 100%; } .v-box{ display: inline-block; vertical-align: middle; white-space: normal; } <div class="v-wrap"> <article class="v-box"> <p>This is how I've been doing it for some time</p> </article> </div> Support starts with Internet Explorer 8.
After a lot of research I finally found the ultimate solution. It works even for floated elements. View Source .element { position: relative; top: 50%; transform: translateY(-50%); /* or try 50% */ }
Use the CSS Flexbox align-items property to achieve this. html, body { height: 100%; } body { display: flex; align-items: center; } <div>This is centered vertically</div>
To center the div on a page, check the fiddle link. #vh { margin: auto; position: absolute; top: 0; left: 0; bottom: 0; right: 0; } .box{ border-radius: 15px; box-shadow: 0 0 8px rgba(0, 0, 0, 0.4); padding: 25px; width: 100px; height: 100px; background: white; } <div id="vh" class="box">Div to be aligned vertically</div> Another option is to use flex box, check the fiddle link. .vh { background-color: #ddd; height: 400px; align-items: center; display: flex; } .vh > div { width: 100%; text-align: center; vertical-align: middle; } <div class="vh"> <div>Div to be aligned vertically</div> </div> Another option is to use a CSS 3 transform: #vh { position: absolute; top: 50%; left: 50%; /*transform: translateX(-50%) translateY(-50%);*/ transform: translate(-50%, -50%); } .box{ border-radius: 15px; box-shadow: 0 0 8px rgba(0, 0, 0, 0.4); padding: 25px; width: 100px; height: 100px; background: white; } <div id="vh" class="box">Div to be aligned vertically</div>
The easiest solution is below: .outer-div{ width: 100%; height: 200px; display: flex; border:1px solid #000; } .inner-div{ margin: auto; text-align: center; border: 1px solid red; } <div class="outer-div"> <div class="inner-div"> Hey there! </div> </div>
There are multiple ways to achieve this. Using flex property of CSS. Solution #1 .parent { width: 400px; height:200px; background: blue; display: flex; align-items: center; justify-content:center; } .child { width: 75px; height: 75px; background: yellow; } <div class="parent"> <div class="child"></div> </div> or by using display: flex; and margin: auto; Solution #2 .parent { width: 400px; height:200px; background: blue; display: flex; } .child { width: 75px; height: 75px; background: yellow; margin:auto; } <div class="parent"> <div class="child"></div> </div> show text center Solution #3 .parent { width: 400px; height: 200px; background: yellow; display: flex; align-items: center; justify-content:center; } <div class="parent">Center</div> Using percentage(%) height and width. Solution #4 .parent { position: absolute; height:100%; width:100%; background: blue; display: flex; align-items: center; justify-content:center; } .child { width: 75px; height: 75px; background: yellow; } <div class="parent"> <div class="child"></div> </div>
Unfortunately — but not surprisingly — the solution is more complicated than one would wish it to be. Also unfortunately, you'll need to use additional divs around the div you want vertically centered. For standards-compliant browsers like Mozilla, Opera, Safari, etc. you need to set the outer div to be displayed as a table and the inner div to be displayed as a table-cell — which can then be vertically centered. For Internet Explorer, you need to position the inner div absolutely within the outer div and then specify the top as 50%. The following pages explain this technique well and provide some code samples too: Vertical Centering in CSS Vertical Centering in CSS with Unknown Height (Internet Explorer 7 compatible) (Archived article courtesy of the Wayback Machine) There is also a technique to do the vertical centering using JavaScript. Vertical alignment of content with JavaScript & CSS demonstrates it.
If someone cares for Internet Explorer 10 (and later) only, use Flexbox: .parent { width: 500px; height: 500px; background: yellow; display: -webkit-flex; display: -ms-flexbox; display: flex; -webkit-justify-content: center; -ms-flex-pack: center; justify-content: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; } .centered { width: 100px; height: 100px; background: blue; } <div class="parent"> <div class="centered"></div> </div> Flexbox support: http://caniuse.com/flexbox
A modern way to center an element vertically would be to use flexbox. You need a parent to decide the height and a child to center. The example below will center a div to the center within your browser. What's important (in my example) is to set height: 100% to body and html and then min-height: 100% to your container. body, html { background: #F5F5F5; box-sizing: border-box; height: 100%; margin: 0; } #center_container { align-items: center; display: flex; min-height: 100%; } #center { background: white; margin: 0 auto; padding: 10px; text-align: center; width: 200px; } <div id='center_container'> <div id='center'>I am center.</div> </div>
.center { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); /* (x, y) => position */ -ms-transform: translate(-50%, -50%); /* IE 9 */ -webkit-transform: translate(-50%, -50%); /* Chrome, Safari, Opera */ } .vertical { position: absolute; top: 50%; //left: 0; transform: translate(0, -50%); /* (x, y) => position */ } .horizontal { position: absolute; //top: 0; left: 50%; transform: translate(-50%, 0); /* (x, y) => position */ } div { padding: 1em; background-color: grey; color: white; } <body> <div class="vertical">Vertically left</div> <div class="horizontal">Horizontal top</div> <div class="center">Vertically Horizontal</div> </body> Related: Center a Image
Centering only vertically If you don't care about Internet Explorer 6 and 7, you can use a technique that involves two containers. The outer container: should have display: table; The inner container: should have display: table-cell; should have vertical-align: middle; The content box: should have display: inline-block; You can add any content you want to the content box without caring about its width or height! Demo: body { margin: 0; } .outer-container { position: absolute; display: table; width: 100%; /* This could be ANY width */ height: 100%; /* This could be ANY height */ background: #ccc; } .inner-container { display: table-cell; vertical-align: middle; } .centered-content { display: inline-block; background: #fff; padding: 20px; border: 1px solid #000; } <div class="outer-container"> <div class="inner-container"> <div class="centered-content"> Malcolm in the Middle </div> </div> </div> See also this Fiddle! Centering horizontally and vertically If you want to center both horizontally and vertically, you also need the following. The inner container: should have text-align: center; The content box: should re-adjust the horizontal text-alignment to for example text-align: left; or text-align: right;, unless you want text to be centered Demo: body { margin: 0; } .outer-container { position: absolute; display: table; width: 100%; /* This could be ANY width */ height: 100%; /* This could be ANY height */ background: #ccc; } .inner-container { display: table-cell; vertical-align: middle; text-align: center; } .centered-content { display: inline-block; text-align: left; background: #fff; padding: 20px; border: 1px solid #000; } <div class="outer-container"> <div class="inner-container"> <div class="centered-content"> Malcolm in the Middle </div> </div> </div> See also this Fiddle!
It can be done in two ways body{ left: 50%; top:50%; transform: translate(-50%, -50%); height: 100%; width: 100%; } OR Using flex body { height:100% width:100% display: flex; justify-content: center; align-items: center; } align-items:center; makes the content vertically center justify-content: center;makes the content horizontally center
This is always where I go when I have to come back to this issue. For those who don't want to make the jump: Specify the parent container as position:relative or position:absolute. Specify a fixed height on the child container. Set position:absolute and top:50% on the child container to move the top down to the middle of the parent. Set margin-top:-yy where yy is half the height of the child container to offset the item up. An example of this in code: <style type="text/css"> #myoutercontainer {position:relative} #myinnercontainer {position:absolute; top:50%; height:10em; margin-top:-5em} </style> ... <div id="myoutercontainer"> <div id="myinnercontainer"> <p>Hey look! I'm vertically centered!</p> <p>How sweet is this?!</p> </div> </div>
I just wrote this CSS and to know more, please go through: This article with vertical align anything with just 3 lines of CSS. .element { position: relative; top: 50%; transform: perspective(1px) translateY(-50%); }
For newcomers, please try: display: flex; align-items: center; justify-content: center;
The three lines of code using transform works practically on modern browsers and Internet Explorer: .element{ position: relative; top: 50%; transform: translateY(-50%); -moz-transform: translateY(-50%); -webkit-transform: translateY(-50%); -ms-transform: translateY(-50%); } I am adding this answer since I found some incompleteness in the previous version of this answer (and Stack Overflow won't allow me to simply comment). 'position' relative messes up the styling if the current div is in the body and has no container div. However 'fixed' seems to work, but it obviously fixes the content in the center of the viewport Also I used this styling for centering some overlay divs and found that in Mozilla all elements inside this transformed div had lost their bottom borders. Possibly a rendering issue. But adding just the minimal padding to some of them rendered it correctly. Chrome and Internet Explorer (surprisingly) rendered the boxes without any need for padding
CSS Grid body, html { margin: 0; } body { display: grid; min-height: 100vh; align-items: center; } <div>Div to be aligned vertically</div>
.center{ display: grid; place-items: center; }
The answer from Billbad only works with a fixed width of the .inner div. This solution works for a dynamic width by adding the attribute text-align: center to the .outer div. .outer { position: absolute; display: table; width: 100%; height: 100%; text-align: center; } .middle { display: table-cell; vertical-align: middle; } .inner { text-align: center; display: inline-block; width: auto; } <div class="outer"> <div class="middle"> <div class="inner"> Content </div> </div> </div>
Just do it: Add the class at your div: .modal { margin: auto; position: absolute; top: 0; right: 0; left: 0; bottom: 0; height: 240px; } And read this article for an explanation. Note: Height is necessary.
I did it with this (change width, height, margin-top and margin-left accordingly): .wrapper { width: 960px; height: 590px; position: absolute; top: 50%; left: 50%; margin-top: -295px; margin-left: -480px; } <div class="wrapper"> -- Content -- </div>
Not answering for browser compatibility but to also mention the new Grid and the not so new Flexbox feature. Grid From: Mozilla - Grid Documentation - Align Div Vertically Browser Support: Grid Browser Support CSS: .wrapper { display: grid; grid-template-columns: repeat(4, 1fr); grid-gap: 10px; grid-auto-rows: 200px; grid-template-areas: ". a a ." ". a a ."; } .item1 { grid-area: a; align-self: center; justify-self: center; } HTML: <div class="wrapper"> <div class="item1">Item 1</div> </div> Flexbox Browser Support: Flexbox Browser Support CSS: display: -webkit-box; display: -moz-box; display: -ms-flexbox; display: -webkit-flex; display: flex; align-items: center; justify-content: center;
I think a solid solution for all browsers without using Flexbox - "align-items: center;" is a combination of display: table and vertical-align: middle;. CSS .vertically-center { display: table; width: 100%; /* Optional */ height: 100%; /* Optional */ } .vertically-center > div { display: table-cell; vertical-align: middle; } HTML <div class="vertically-center"> <div> <div style="border: 1px solid black;">some text</div> </div> </div> ‣Demo: https://jsfiddle.net/6m640rpp/
Especially for parent divs with relative (unknown) height, the centering in the unknown solution works great for me. There are some really nice code examples in the article. It was tested in Chrome, Firefox, Opera, and Internet Explorer. /* This parent can be any width and height */ .block { text-align: center; } /* The ghost, nudged to maintain perfect centering */ .block:before { content: ''; display: inline-block; height: 100%; vertical-align: middle; margin-right: -0.25em; /* Adjusts for spacing */ } /* The element to be centered, can also be of any width and height */ .centered { display: inline-block; vertical-align: middle; width: 300px; } <div style="width: 400px; height: 200px;"> <div class="block" style="height: 90%; width: 100%"> <div class="centered"> <h1>Some text</h1> <p>Any other text..."</p> </div> </div> </div>
There is a trick I found out recently: You need to use top 50%, and then you do a translateY(-50%). .outer-div { position: relative; height: 150px; width: 150px; background-color: red; } .centered-div { position: absolute; top: 50%; -webkit-transform: translateY(-50%); -ms-transform: translateY(-50%); transform: translateY(-50%); background-color: white; } <div class='outer-div'> <div class='centered-div'> Test text </div> </div>
How to position text and Div on top of eachother and align them in center?
So I can't figure this out. I'm trying to get a red vertical box to display in middle of page. I've set the div's margin to auto. And then there's another div that holds a centered text. Setting margin auto on both. They are both stacking on top of eachother fine in middle of page. However I want it to be responsive to all heights. Right now it's just responsive to the x-axis and not the height. HTML & CSS: .parentDiv { position: relative; width: 250px; height: 450px; margin: auto; } #RedBox { width: 250px; height: 450px; background-color: #FF0000; margin: auto; } #CSText { position: absolute; top: 45%; width: 250px; color: black; text-align: center; } <div class="parentDiv" style="margin-top: auto;"> <div id="CSText" class="TextAlignCenter"> </div> <div id="RedBox"> </div> </div>
flexbox would be a great solution to this: .container { height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; } .red-box { background-color: red; padding: 100px; color: white; } <div class="container"> <div class="red-box">text</div> </div>
I did this for you. https://jsfiddle.net/95ssv6q1/ HTML <div class="parentDiv"> <div class="inner"> <div id="RedBox"> </div> </div> </div> CSS .parentDiv { display:table; width: 100%; height: 100%; position: absolute; } .inner{ display: table-cell; vertical-align:middle; } #RedBox { width: 250px; height: 450px; background-color: #FF0000; margin: auto; }
Keep distance between 2 out of 3 elements equal when scaling window (responsive)
There are 2 img div's on top of each-other, next to a fluid header logo (.svg) also in a div. The HTML: <header class="site-header" role="banner" itemscope="itemscope" itemtype="http://schema.org/WPHeader"><div class="wrap"><div id="menu_container"><img src="http://95.85.63.245/wp-content/uploads/dynamik-gen/theme/images/Hamburger_optimized.svg" alt="menu" class="menu-btn" /><div class="menu_spacer"></div><img src="http://95.85.63.245/wp-content/uploads/dynamik-gen/theme/images/searchicon.png" alt="zoek" class="search_icon" /></div> <div class="title-area"><h1 class="site-title" itemprop="headline"></h1></div><div class="vr_menu_logo"><img src="http://95.85.63.245/wp-content/uploads/dynamik-gen/theme/images/logo_VR_font.svg"></div> </div></header> The CSS: .vr_menu_logo{ max-width:95%; float:left; margin-right:20px; } #menu_container { max-width: 5%; float: right; } .menu-btn{ cursor: pointer; max-height: 30px; max-width: 30px; margin-top:2em; } .menu_spacer{height:4em;} .search_icon{ cursor: pointer; max-height: 24px; max-width: 24px; } .site-header .wrap { width: 1260px; } .site-header .wrap { margin: 0 auto; padding: 0; float: none; overflow: hidden; } Goal: Scaling the browser window would keep the small hamburger and the search icon's on level with respectively the top and bottom of the logo. Actually the 3 seperate items should act as one logo. Check the cssdesk here: http://www.cssdesk.com/JDyYQ I was hoping a spacer div with a max-height would do the trick, or display:table-cell; But I can't get it to work, anyone have an idea? (javascript can be an option too, but this must be possible with CSS I would think...)
here is an example using flexbox - note in the fiddle that the two div are exactly the same apart from having a different height. This should help you getting what you are trying to achieve. Obviously check what kind of browser support you need to provide as flexbox is a relatively new technology. http://jsfiddle.net/zn50mmnu/ html: <div class="flexy f1"> <span class="menu">M</span> <span class="search">S</span> </div> <div class="flexy f2"> <span class="menu">M</span> <span class="search">S</span> </div> css: .flexy { float: right; clear: both; display: flex; flex-direction: column; justify-content: space-between; border: 2px solid red; margin: 10px; } .f1 { height: 50px; } .f2 { height: 90px; } .menu { background: red; width: 1em; } .search { background: blue; width: 1em; }