How to put a div in the middle of the page [duplicate] - css

This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 9 years ago.
Not in the middle of the page, as in margin: 0 auto;
I'm talking about in the center as in top to bottom.
Thank you in advance!

With html5 this is pretty easy using flexbox:
This article discribes how to do it
http://coding.smashingmagazine.com/2013/05/22/centering-elements-with-flexbox/
and here is the demo (also from the article)
http://jsfiddle.net/pnNqd/
HTML:
<h1>OMG, I’m centered</h1>
CSS:
html {
height: 100%;
}
body {
display: -webkit-box; /* OLD: Safari, iOS, Android browser,
older WebKit browsers. */
display: -moz-box; /* OLD: Firefox (buggy) */
display: -ms-flexbox; /* MID: IE 10 */
display: -webkit-flex; /* NEW, Chrome 21+ */
display: flex; /* NEW: Opera 12.1, Firefox 22+ */
-webkit-box-align: center; -moz-box-align: center; /* OLD… */
-ms-flex-align: center; /* you know the drill now… */
-webkit-align-items: center;
align-items: center;
-webkit-box-pack: center; -moz-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
margin: 0;
height: 100%;
width: 100%; /* needed for Firefox */
}
h1 {
display: -webkit-box; display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-align: center; -moz-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
height: 10rem;
}

There are multiple ways:
position: absolute;
left: 50%;
top: 50%;
margin-left: <50% of width from element>;
margin-top: <50% of height from element>;
or:
HTML
<div id="content-wrapper">
<div class="wrapper">
Some text
</div>
</div>
CSS
#content-wrapper {
display: table;
width: 100%;
height: 100%;
text-align: center;
}
.wrapper {
display: table-cell;
vertical-align: middle;
position: relative;
}
note: This won't work every time.. I did this on my own site.

The easiest way if your div is absolutely positioned. Conditions. Your div must have a fixed width and height
#myBox{width:50px; height: 50px; position: absolute; margin: auto; top: 0; left: 0; bottom: 0; right: 0; border:solid 1px #000;}
See http://jsfiddle.net/zM2J2/
Put a border to show you an outline.

Related

How can I expand the empty margins on flex items?

The dark gray area is clickable but the blue margin has no action. How can I expand the rest of the smaller sized items to fill in the space?
div.scrollmenu {
display:flex;
justify-content: flex-start;
align-items: center;
background-color: #333;
overflow: auto;
flex-wrap: nowrap;
}
div.scrollmenu a {
display: flex;
color: white;
text-align: left;
padding: 14px;
text-decoration: none;
margin: auto;
min-width: 25%;
max-width: 25%;
}
Add the align-items: stretch; property to your div.scrollmenu and tweak your code a bit.
div.scrollmenu {
display:flex;
/* align-items: stretch; */ /* redundant */
background-color: #333;
flex-wrap: nowrap;
}
div.scrollmenu a {
display: flex;
align-items: center;
/* uncomment this if you want them to be centered horizontally */
/*justify-content: center;*/
padding: 1 14px;
color: white;
text-align: left;
text-decoration: none;
flex: 1;
}
Hope this helps.
EDIT You don't even need align-items: stretch because I believe that's the default property since the result is the same even if you leave it out.

Why doesn't css work in IE when it works as expected in Firefox and Chrome?

input[type=text], input[type=password], select {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
input[type=submit] {
width: 100%;
background-color: #808080;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type=submit]:hover {
background-color: #A9A9A9;
}
html,body {
height:100%;
width:100%;
margin:0;
}
body {
display:flex;
background-color:#D3D3D3;
}
form {
margin:auto;
}
The above CSS code makes the form center aligned in Chrome and Firefox, but the same doesn't work in Internet Explorer. In IE, the form is center aligned horizontally, but not vertically. What is wrong and what can be done to make it align properly in IE as well?
The above problem is because IE has only partial support for flex.
Including align-items: center; after display: flex;
made the css get properly applied in IE.
Thanks to Rachel Gallen for pointing it out to me that the problem was with flex support in IE.
Reference: https://stackoverflow.com/a/17156937/5293838
Solution provided by cimmanon:
You need to read the notes very closely on caniuse. "Partial support"
refers to supporting one of two older drafts, and they don't make a
note of which browser supports which draft. IE10 supports the March
2012 draft, and it's the only one that's known to do so.
http://codepen.io/cimmanon/pen/ApHEy
.box {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-pack: center;
-moz-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
-webkit-box-align: center;
-moz-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
/* fix for old FF */
width: 100%;
}

Stacking Divs in Reverse order Responsively [duplicate]

This question already has answers here:
CSS responsive float left and right (change which div goes on top)?
(2 answers)
Closed 7 years ago.
I have two div's that are side by side in a wider screen (say 1000 px)
Box 1 is the left of Box 2
When I change the screen size to say 330px, I want Box 2 ABOVE Box 1.
I have no issues getting BOX 1 ABOVE Box 2, but not the reverse.
I'm hoping this can be done with css - I can make it happen with javascript.
As you are saying about responsiveness, #media queries come into play. I don't fishing care about Old IE versions, so I am using FlexBox for your solution. We can have reverse and orders in flexbox.
Snippet
.flex-container {
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row;
-ms-flex-direction: row;
flex-direction: row;
-webkit-flex-wrap: nowrap;
-ms-flex-wrap: nowrap;
flex-wrap: nowrap;
-webkit-justify-content: flex-start;
-ms-flex-pack: start;
justify-content: flex-start;
-webkit-align-content: stretch;
-ms-flex-line-pack: stretch;
align-content: stretch;
-webkit-align-items: flex-start;
-ms-flex-align: start;
align-items: flex-start;
}
.flex-item {
-webkit-order: 0;
-ms-flex-order: 0;
order: 0;
-webkit-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 0 1 auto;
-webkit-align-self: auto;
-ms-flex-item-align: auto;
align-self: auto;
}
div {border: 1px solid #ccc; max-width: 100px;}
div div {width: 50px; height: 50px; line-height: 50px; text-align: center;}
#media (max-width: 330px) {
.flex-container {
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-direction: column-reverse;
-ms-flex-direction: column-reverse;
flex-direction: column-reverse;
-webkit-flex-wrap: nowrap;
-ms-flex-wrap: nowrap;
flex-wrap: nowrap;
-webkit-justify-content: flex-start;
-ms-flex-pack: start;
justify-content: flex-start;
-webkit-align-content: stretch;
-ms-flex-line-pack: stretch;
align-content: stretch;
-webkit-align-items: flex-start;
-ms-flex-align: start;
align-items: flex-start;
}
.flex-item {
-webkit-order: 0;
-ms-flex-order: 0;
order: 0;
-webkit-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 0 1 auto;
-webkit-align-self: auto;
-ms-flex-item-align: auto;
align-self: auto;
}
}
<div class="flex-container">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
</div>
Another option for older browsers with just using float in a cunning way.
Snippet with Floats
div {border: 1px solid #ccc; max-width: 104px;}
div div {width: 50px; height: 50px; line-height: 50px; text-align: center;}
div {overflow: hidden;}
div div {float: left;}
div div:first-child {float: right;}
#media (max-width: 330px) {
div div {float: none;}
}
<div class="container">
<div class="item">2</div>
<div class="item">1</div>
</div>
You can use a media query to modify CSS rules under certain screen width:
div {
width: 130px;
background-color: #7A9;
margin: 10px;
display: inline-block;
}
#media screen and (min-width: 331px) {
#box1 {
float: left;
}
#box2 {
float: right;
}
}
<div id="box2">BOX 2</div>
<div id="box1">BOX 1</div>

make flex item 90% wide

This is a simple question. I must be just missing the answer. I have a flex container with one flex item in it. I want the flex item to be 90% wide and centered vertically and horizontally. I just can't get it to be 90% wide. I am puzzled. Here's my code:
<div class="" id="popupContainer">
<div class="flex-item-popup" id="popup">
</div>
</div>
#popupContainer {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
display:flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-content: flex-start;
align-items: center;
}
#popup {
width: 90%;
height: 90%;
flex-grow: 1;
flex-shrink: 0;
flex-basis: 200px;
justify-content: center;
display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: flex;
-webkit-flex-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
background-color: black;
justify-content: center; /* align horizontal */
align-items: center; /* align vertical */
}
just delete these two and it will work proprely :
flex-grow: 1; flex-basis: 200px;
Live Demo

How to position inside flex-box?

I am using flex box and want to align button to the bottom.
I am using position absolute and bottom: 0, but browser is ignoring it.
<ul class="box">
<li><div>this has <br> more <br> <button>one</button></div></li>
<li><div>this has <br> more <br> content <br> <button>one</button></div></li>
<li>d<div><button>one</button></div></li>
</ul>
ul {
/* basic styling */
width: 100%;
height: 100px;
border: 1px solid #555;
/* flexbox setup */
display: -webkit-box;
-webkit-box-orient: horizontal;
display: -moz-box;
-moz-box-orient: horizontal;
display: box;
box-orient: horizontal;
}
.box > li {
-webkit-box-flex: 1;
-moz-box-flex: 1;
box-flex: 1;
margin: 0 1px;
padding-bottom: 20px;
border-bottom: 20px solid red;
position: relative;
}
button {
position: absolute;
bottom: 0;
}
/* our colors */
.box > li:nth-child(1){ background : #FCC; }
.box > li:nth-child(2){ background : #CFC; }
.box > li:nth-child(3){ background : #CCF; }
I can use float and not use flex-box, but I want to see if there is a solution for this using flex-box.
demo here:
http://jsfiddle.net/NJAAa/
Working WebKit version: http://jsfiddle.net/LLtmE/
In short: you need to put your text context in a separate div; make each li a flexbox and set box-orient to vertical. And remove all that absolute/relative positioning - it's no longer necessary.
The reason is the flex container has no specific width or height,since its flexible with its contents.
To ensure my statement, try with left or top, it will react for your data. If you try with right or bottom, you cant see the reaction. Since the flex container box original width / height is very less.
Check this on codepen. I hope this help and that is not late:)
ul {
margin: 0;
padding: 0;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
width: 80%;
margin: 10% auto;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
color: #b2b2b2;
box-shadow: 2px 2px 3px #666666;
}
ul > li {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
-ms-flex-preferred-size: 33.33%;
flex-basis: 33.33%;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
text-align: center;
}
ul > li > button {
margin: 20px;
padding: 5px 15px;
cursor: pointer;
}
ul > li > button:hover {
color: whitesmoke;
background-color: maroon;
}
/* our colors */
ul > li:nth-child(1) {
background: #000000;
}
ul > li:nth-child(2) {
background: #191919;
}
ul > li:nth-child(3) {
background: #323232;
}
And this can be helpfull....

Resources