This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 1 year ago.
Flexbox align-items: center; is not working.
I want to position .container at the center
Here is the code for it, I don't understand why
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: black;
display: flex;
justify-content: center;
align-content: center;
}
.container {
position: relative;
background-color: white;
width: 100%;
max-width: 1000px;
min-height: 100px;
}
<div class="container">
<div class="peronal"></div>
<div class="profess"></div>
</div>
To center your .container, presumably inside a body as big as the browser window, size the body element.
Right now your body element has the default height: auto, which means it will be as tall as the content inside of it. You set min-height: 100px on your .container, so that’s the size of the auto-sized body.
So you’ll want to make your body as big as the browser window, and then center your .container element inside:
body{
margin: 0;
box-sizing: border-box;
min-height: 100vh;
background-color:black;
display:flex;
justify-content:center;
align-items: center;
}
.container{
background-color: white;
max-width: 1000px;
min-height: 100px;
}
.personal {
padding: 24px;
background-color: tomato;
}
.professional {
padding: 24px;
background-color: seagreen;
}
<div class="container">
<div class="personal">a</div>
<div class="professional">b</div>
</div>
if you want to vertically center the parent you need a height
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
background-color:black;
display:flex;
justify-content:center;
align-content:center;
align-items:center;
min-height: 100vh;
}
.container{
position:relative;
background-color: white;
width: 100%;
max-width: 1000px;
min-height: 100px;
}
<div class="container">
<div class="peronal">one</div>
<div class="profess">two</div>
</div>
Related
This question already has answers here:
Can't scroll to top of flex item that is overflowing container
(12 answers)
Closed 10 months ago.
I like to have a div that keeps all it's children in the center (vertical and horizontal). I can easily achieve this by using flexbox. But when width of my children get bigger than the parent, a part of children is not visible.
How can I fix this?
Codepen
* {
margin: 0;
padding: 0;
}
.container {
height: 500px;
width: 500px;
background: red;
display: flex;
justify-content: center;
align-items: center;
overflow: scroll;
}
.children {
min-width: 1200px;
height: 50px;
background: blue;
}
<div class='container'>
<div class="children">
<h1>Welcome to my city, california</h1>
</div>
</div>
You just have to change the justify-content to be flex-start
See below.
And if you want the H1 to be centered, just use text-align: center
* {
margin: 0;
padding: 0;
}
.container {
height: 500px;
width: 500px;
background: red;
display: flex;
justify-content: flex-start;
align-items: center;
overflow: scroll;
}
.children {
min-width: 1200px;
height: 50px;
background: blue;
}
<div class='container'>
<div class="children">
<h1>Welcome to my city, california</h1>
</div>
</div>
Change the .container{
min-width: 100%}
* {
margin: 0;
padding: 0;
}
.container {
height: 500px;
width: 500px;
background: red;
display: flex;
justify-content: center;
align-items: center;
overflow: scroll;
}
.children {
min-width: 100%;
height: 50px;
background: blue;
}
<div class='container'>
<div class="children">
<h1>Welcome to my city, california</h1>
</div>
</div>
This question already has answers here:
Fill remaining vertical space with CSS using display:flex
(6 answers)
Make a div fill the height of the remaining screen space
(42 answers)
Closed 1 year ago.
I am trying to vertically center text inside a child div that is full height, but when I do that it has extra spacing at the bottom. How can I do this without the extra spacing? I'd like to have a nav along with some vertically centered text, but without the extra spacing.
html {
height: 100%;
}
body {
margin: 0;
min-height: 100vh;
}
h1 {
margin: 0;
padding: 0;
}
.landing {
background: url(bg.svg);
height: 100vh;
}
.landing-content {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.landing-content h1 {
color: white;
margin: 0;
}
.app {
background-color: black;
height: 100vh;
color: white;
}
<div class="app">
<div class="logo">
<h1>logo</h1>
</div>
<div class="landing">
<div class="landing-content">
<h1>hi, i'm jordan</h1>
</div>
</div>
</div>
The use of height: 100vh was not suitable, what it does is recognize the view-width as its height and that limit limits the height and recreate a small crack at the bottom. You can try using height: 100% instead
html {
height: 100%;
}
body {
margin: 0;
min-height: 100vh;
}
h1 {
margin: 0;
padding: 0;
}
.landing {
background: url(bg.svg);
height: 100vh;
}
.landing-content {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.landing-content h1 {
color: white;
margin: 0;
}
.app {
background-color: black;
height: 100%;
color: white;
}
<div class="app">
<div class="logo">
<h1>logo</h1>
</div>
<div class="landing">
<div class="landing-content">
<h1>hi, i'm jordan</h1>
</div>
</div>
</div>
Remove landing class height and add landing-content class height to 100%. When you add landing class height to 100vh, It covers 100vh height. But logo class already covers some height. so it overflows.
.landing {
background: url(bg.svg);
}
.landing-content {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
working fiddle - https://jsfiddle.net/alimurrazi/36svho08/1/
Since .app has the default display: block, .logo takes up however much space it needs, then .landing goes underneath, taking up an additional 100vh, so that you need to scroll to view its full content.
The solution here is either to simply add overflow-y: hidden to .app, but that still leaves your text off center by a bit. Another solution would be to give .app display: flex, and using flexbox to distribute the space as needed.
html {
height: 100%;
}
body {
margin: 0;
min-height: 100vh;
}
h1 {
margin: 0;
padding: 0;
}
.landing {
background: url(bg.svg);
flex-grow: 1;
}
.landing-content {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
.landing-content h1 {
color: white;
margin: 0;
}
.app {
background-color: black;
height: 100vh;
color: white;
display: flex;
flex-direction: column;
}
<div class="app">
<div class="logo">
<h1>logo</h1>
</div>
<div class="landing">
<div class="landing-content">
<h1>hi, i'm jordan</h1>
</div>
</div>
</div>
Change the .app class as follow.
.app {
background-color: black;
height: auto;
color: white;
}
This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 5 years ago.
I've been learning CSS and am now practicing by trying to replicate basic websites, but I've stumbled across a problem!
I'm trying to vertically align a box so that it is always in the middle, and will automatically scale if I make the browser vertically smaller. So far I've tried to replicate what I've done horizontally (normally margin: 0 auto;) but it isn't working.
My relevant HTML and CSS so far look like this:
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Global Values */
html{
font-family: "Arial";
font-size: 16px;
line-height: 1.2;
height: 100%;
}
body{
background-color: #f9f9f9;
height: 100%;
}
.container{
display: block;
min-width: 240px;
max-width: 768px;
width: 100%;
/*----------------------------------------------*/
/* This is to make sure that the container height is always the same size as the browser. */
min-height: 100px;
height: 100%;
/*----------------------------------------------*/
margin: 0 auto;
text-align: center;
border-style: solid;
}
header{
border: solid;
min-height: 100px;
margin: auto 5%;
}
<body>
<div class="container">
<header>
<h1>Jon Phillips</h1>
<p>User Interface Designer</p>
<nav class="contact">
<p>Contact</p>
</nav>
</header>
</div>
</body>
I'm showing the borders so I can see what's going on, and am sure that (like horizontal centering) my margins need to be automatic. When I've done this horizontally, it's worked fine...
Does anyone have a recommendation on what to do??
Thanks in advance!
Flexbox is your friend! You need to add the following to your container styles
display: flex;
align-items: center;
justify-content: center;
We're setting the display to flex/flexbox with display: flex; and aligning everything to the center with align-items: center; and justify-content: center;
With all the vendor-prefixes it looks like:
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Global Values */
html{
font-family: "Arial";
font-size: 16px;
line-height: 1.2;
height: 100%;
}
body{
background-color: #f9f9f9;
height: 100%;
}
.container{
display: block;
min-width: 240px;
max-width: 768px;
width: 100%;
/*----------------------------------------------*/
/* This is to make sure that the container height is always the same size as the browser. */
min-height: 100px;
height: 100%;
/*----------------------------------------------*/
margin: 0 auto;
text-align: center;
border-style: solid;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}
header{
border: solid;
min-height: 100px;
margin: auto 5%;
}
<body>
<div class="container">
<header>
<h1>Jon Phillips</h1>
<p>User Interface Designer</p>
<nav class="contact">
<p>Contact</p>
</nav>
</header>
</div>
</body>
Check this: https://codepen.io/danieldd/pen/pdooVN
<div class="container">
<div class="item-centered"></div>
</div>
.container {
height: 400px;
width: 100%;
background-color: cyan;
display: flex; /*this is needed for centering*/
align-items: center; /*this center vertically childred elements*/
justify-content: center; /*this center horizontally childred elements*/
}
.item-centered {
height: 200px;
width: 200px;
background-color: red;
}
Sadly the auto margins trick only works horizontally. In CSS3, you can use another trick. You start with top: 50%; and then subtract half the height of your container using transform: translateY(-50%);. The perspective(1px) is just to correct the calculation to a whole pixel and prevent issues in some browsers. See the three lines I've added to your CSS:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Global Values */
html {
font-family: "Arial";
font-size: 16px;
line-height: 1.2;
height: 100%;
}
body {
background-color: #f9f9f9;
height: 100%;
}
.container {
display: block;
min-width: 240px;
max-width: 768px;
width: 100%;
/*----------------------------------------------*/
/* This is to make sure that the container height is always the same size as the browser. */
min-height: 100px;
height: 100%;
/*----------------------------------------------*/
margin: 0 auto;
text-align: center;
border-style: solid;
}
header {
border: solid;
min-height: 100px;
margin: auto 5%;
position: relative;
top: 50%;
transform: perspective(1px) translateY(-50%);
}
<body>
<div class="container">
<header>
<h1>Jon Phillips</h1>
<p>User Interface Designer</p>
<nav class="contact">
<a href="mailto:jon#jonphillips.ca" target="_blank">
<p>Contact</p>
</a>
</nav>
</header>
</div>
</body>
On the parent of the item that you're trying to center, you can just use display: flex; and the child should center inside (with the rest of the styles that you already have set up).
This could also be achieved using absolute positioning, though it would be a few more lines:
.parent {
position: relative;
}
.child {
/* Vertically center - will still need left / right & width adjustment otherwise */
position: absolute;
top: 50%;
transform: translateY(-50%);
}
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Global Values */
html{
font-family: "Arial";
font-size: 16px;
line-height: 1.2;
height: 100%;
}
body{
background-color: #f9f9f9;
height: 100%;
}
.container{
display: block;
min-width: 240px;
max-width: 768px;
width: 100%;
display: flex;
/*----------------------------------------------*/
/* This is to make sure that the container height is always the same size as the browser. */
min-height: 100px;
height: 100%;
/*----------------------------------------------*/
margin: 0 auto;
text-align: center;
border-style: solid;
}
header{
border: solid;
min-height: 100px;
margin: auto 5%;
width: 100%;
}
<body>
<div class="container">
<header>
<h1>Jon Phillips</h1>
<p>User Interface Designer</p>
<nav class="contact">
<p>Contact</p>
</nav>
</header>
</div>
</body>
There are different approuces to solve this problem, the easyest and with a good compatibility is the use of the "table-cell display", so in your case adding this to your CSS:
.container {
display:table-cell;
height:100%;
vertical-align:middle
}
The point is that a correct implementation of a table-cell should consider the parent element as a table, your parent element is the body itself, so do change your CSS into:
body{
background-color: #f9f9f9;
height: 100%;
width:100%;
display:table
}
http://jsfiddle.net/9tghnydg/
On a second though the height propriety on the .container is not strictly necessary, acting it as the unique cell of the body table:
.container {
display:table-cell;
vertical-align:middle
}
http://jsfiddle.net/9tghnydg/1/
body {
max-width: 1000px !important;
margin-right: 5%;
margin-left: 5%;
height: 100%;
}
Trying to proportionally center my content, while keeping a max-width at 1000px seem to not work.. is there a work around this? It's important to keep the 5% margins for this project..
It aligns all my content to the left..
If you have to center the content of your page with a 5% margin left and right on resolution under 1000px you can use this code:
.container{
width:90%;
max-width:1000px;
margin:0 auto;
background-color: red;
height:500px;
}
<body>
<div class="container"></div>
</body>
Over 1000px the content result centered in your page.
Try setting the following to your element to align items center.
div{
display: flex;
justify-content: center;
align-items: center;
}
body {
margin: 0;
padding: 0;
}
div {
display: flex;
justify-content: center;
align-items: center;
}
<div> Loreum Ipsum</div>
Instead of this you can create a div and add CSS on that
body {
margin: 0px;
padding: 0px;
}
.wrapper {
width: 100%;
max-width: 1000px;
margin: 0px auto;
height: 100%;
text-align: center;
}
<div class="wrapper"> Your html code placed here </div>
You can do it without touching the body tag. Div is just enough.
.is-centered {
max-width: 1000px;
margin: 0 auto;
display: block;
padding: 2px;
width: 100%;
text-align: center;
height: 100%;
}
.grey {
background: grey;
color: white;
}
<div class="is-centered grey">
<p>this is centered!</p>
</div>
Ok thanks for the suggestions.. I found that removing the max-width in the body, and adding it to the divs inside the body helped to keep the centered look, while using the 5% side margins..
body {
margin-right: 5%;
margin-left: 5%;
height: 100%;}
<div style="max-width:1920px">content</div>
I have the following layout.
<div class="container">
<div class="content">
<div class="post">post</div>
<div class="image">image</div>
</div>
<div class="footer">footer</div>
</div>
http://jsbin.com/xicatoq/4/edit?html,css,output
The thing I want to achieve is to make the footer stick to the bottom (I don't want to use absolute positioning) and make the .content stretch from the top to the footer, like in the image below.
Can someone explain how I can achieve this?
In your code, the div with class content is a flex container. That makes the child elements (.post and .image) flex items.
However, your div with class container is not a flex container. So .content and .footer are not flex items, and cannot accept flex properties.
So, first step, add this:
.container {
display: flex;
flex-direction: column;
}
Then use flex auto margins to stick the footer to the bottom of the container:
.footer {
margin-top: auto;
}
Here's the full code:
body {
font-family: monospace;
color: #fff;
text-align:center;
}
.container {
width: 100%;
height: 800px;
background: red;
display: flex; /* NEW */
flex-direction: column; /* NEW */
}
.content {
/* float: left; */
width: 100%;
display: flex;
align-items: center;
}
.post {
width: 70%;
background: pink;
line-height: 300px;
}
.image {
width: 30%;
height: 500px;
background: green;
}
.footer {
background: blue;
line-height: 70px;
text-align: center;
/* float: left; */
width: 100%;
margin-top: auto; /* NEW */
}
<div class="container">
<div class="content">
<div class="post">post</div>
<div class="image">image</div>
</div>
<div class="footer">footer</div>
</div>
Revised Demo
Note that I commented out the floats. They aren't working. In a flex container floats are ignored.
Learn more about auto margins here: https://stackoverflow.com/a/33856609/3597276
Check this : http://jsbin.com/dojitevoye/edit?html,css,output
body {
font-family: monospace;
color: #fff;
text-align:center;
}
.container {
width: 100%;
height: 800px;
background: red;
}
.content {
float: left;
width: 100%;
height: 100%;
display: flex;
align-items: center;
}
.post {
width: 70%;
background: pink;
line-height: 300px;
align-self: flex-start;
}
.image {
width: 30%;
height: 500px;
background: green;
align-self: flex-start;
}
.footer {
background: blue;
line-height: 70px;
text-align: center;
float: left;
width: 100%;
align-self:flex-end;
}
Set the height for the .content class to 100%, which will take the height of it's parent ( which is .container ), which will be 800px in this case.
Now align both .post and .image to the top of the parent flexbox with align-self: flex-start;
Now, similarly set the .footer to the bottom of flexbox using align-self:flex-end;
Just use height: 100%; to .content will make footer stick to bottom.
Working JSBin