This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
How can I horizontally center an element?
(133 answers)
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 2 years ago.
I have tried to center my divs like described on this website but something is wrong and I don't get it. Maybe you know what the problem is, because I can't find a proper solution to center the divs in the exact middle. I have added just one css input description because the others are the same.
.registerBox {
position: relative;
height: 80%;
justify-content: center;
align-items: center;
}
.center {
position: relative;
height: auto;
justify-content: center;
align-items: center;
}
.centergui {
justify-content: center;
align-items: center;
}
//text styles
.h1 {
margin: auto;
justify-self: center;
text-align: center;
}
input[id="usernametext"] {
z-index: 2;
border-radius: $textinputradius;
background-color: $textinputbackground;
font-size: $textinputfontsize;
#include respond-to(medium-devices) {width: 125px;}
#include respond-to(wide-screens) {float: none;}
}
input[id="usernametext"],input[type="password"],input[id="emailtext"],input[id="phonenumbertext"], textarea, select {
outline: #272727;
border: none;
color: #ececec;
}
<div className={"h1"}> <h1>Register</h1> </div>
<div className={"flexCenter"}>
<div className={"registerBox"}>
<div className={"center"}><input type={"text"} id={"usernametext"} placeholder={"Username"}/></div>
<div className={"center"}><input type={"text"} id={"emailtext"} placeholder={"Email-Address"}/></div>
<div className={"center"}><input type={"password"} id={"passwordtext"} placeholder={"Password"}/></div>
<div className={"center"}><input type={"text"} id={"phonenumbertext"} placeholder={"Phonenumber(optional)"}/></div>
</div>
I want to offer two options - flex and table. Wrap in two parent divs (an additional parent div is needed so that the top label is left-aligned and centered at the same time), and add flex rules to the topmost parent div. Did you need it?
flex:
.center_flex {
display: flex;
justify-content: center;
}
.registerBox {
position: relative;
height: 80%;
justify-content: center;
align-items: center;
}
.center {
position: relative;
height: auto;
justify-content: center;
align-items: center;
}
.centergui {
justify-content: center;
align-items: center;
}
//text styles
.h1 {
margin: auto;
justify-self: center;
text-align: center;
}
input[id="usernametext"] {
z-index: 2;
border-radius: $textinputradius;
background-color: $textinputbackground;
font-size: $textinputfontsize;
#include respond-to(medium-devices) {width: 125px;}
#include respond-to(wide-screens) {float: none;}
}
input[id="usernametext"],input[type="password"],input[id="emailtext"],input[id="phonenumbertext"], textarea, select {
outline: #272727;
border: none;
color: #ececec;
}
<div class="center_flex">
<div class="center_flex_container">
<div className={"h1"}> <h1>Register</h1> </div>
<div className={"flexCenter"}>
<div className={"registerBox"}>
<div className={"center"}><input type={"text"} id={"usernametext"} placeholder={"Username"}/></div>
<div className={"center"}><input type={"text"} id={"emailtext"} placeholder={"Email-Address"}/></div>
<div className={"center"}><input type={"password"} id={"passwordtext"} placeholder={"Password"}/></div>
<div className={"center"}><input type={"text"} id={"phonenumbertext"} placeholder={"Phonenumber(optional)"}/></div>
</div>
</div>
</div>
For a table, it is enough to wrap it in only one parental div, setting the margin rules:
.center_table {
display: table;
margin-left: auto;
margin-right: auto;
}
.registerBox {
position: relative;
height: 80%;
justify-content: center;
align-items: center;
}
.center {
position: relative;
height: auto;
justify-content: center;
align-items: center;
}
.centergui {
justify-content: center;
align-items: center;
}
//text styles
.h1 {
margin: auto;
justify-self: center;
text-align: center;
}
input[id="usernametext"] {
z-index: 2;
border-radius: $textinputradius;
background-color: $textinputbackground;
font-size: $textinputfontsize;
#include respond-to(medium-devices) {width: 125px;}
#include respond-to(wide-screens) {float: none;}
}
input[id="usernametext"],input[type="password"],input[id="emailtext"],input[id="phonenumbertext"], textarea, select {
outline: #272727;
border: none;
color: #ececec;
}
<div class="center_table">
<div className={"h1"}> <h1>Register</h1> </div>
<div className={"flexCenter"}>
<div className={"registerBox"}>
<div className={"center"}><input type={"text"} id={"usernametext"} placeholder={"Username"}/></div>
<div className={"center"}><input type={"text"} id={"emailtext"} placeholder={"Email-Address"}/></div>
<div className={"center"}><input type={"password"} id={"passwordtext"} placeholder={"Password"}/></div>
<div className={"center"}><input type={"text"} id={"phonenumbertext"} placeholder={"Phonenumber(optional)"}/></div>
</div>
</div>
Related
[![Red: Centered on canvas: Black: Centered between center and top of canvas][1]][1]
Using flex, it is easy to position elements in the center of a div. But how can this be done relative to already centered elements, using flex?
Shown below in the code is the best I've come up so far. I am able to center the red square in the middle but cannot get the blue one above it to be vertically center-aligned between the red square and the top border.
.flex {
border-style: solid;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: white;
height: 100vh
}
.square {
height: 10px;
width: 10px;
}
#square1 {
background-color: blue;
}
#square2 {
background-color: red;
}
.flexdivider {
display: flex;
flex-direction: column
justify-content: center;
align-items: center;
flex: 1;
}
p {
color: white;
}
<body>
<div class="flex">
<div class="flexdivider">
<div class="square" id="square1"></div>
</div>
<div class="flexdivider">
<div class="square" id="square2"></div>
</div>
<div class="flexdivider">
</div>
</div>
</div>
</body>
[1]: https://i.stack.imgur.com/4lUop.png
I have created 2 separated approach.
h2{
color: #333;
}
.box,.fakebox{
width: 30px;
height: 30px;
background-color: red;
}
/* Example #1 */
.container-box-1{
background-color: #eee;
width: 300px;
height: 300px;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
position: relative;
}
.fakebox{
opacity: 0;
}
.box2{
position: absolute;
top: 25%;
left: 50%;
transform: translate(-50%,-50%)
}
<h2>Example #1 (Using Display Flex and position absolute)</h2>
<div class="container-box-1">
<div class="box"></div>
<div class="box box2"></div>
</div>
This question already has answers here:
Center and bottom-align flex items
(3 answers)
Closed 2 years ago.
So i've got div looking like this. My question is how can I align h1 so it stays always on top of this div and rest of content is aligned in the middle of div like how it is now?
HTML:
<div class="container">
<h1>INFO</h1>
<div class="info"><h2>Age</h2><p>20</p></div>
<div class="info"><h2>Adress</h2><p>Wolna 23, Warszawa</p></div>
<div class="info"><h2>Email</h2><p>lorem#gmail.com</p></div>
<div class="info"><h2>Phone</h2><p>669 133 777</p></div>
</div>
CSS:
.container
{
align-items: center;
justify-content: center;
display:flex;
flex-direction: column;
width: 35.5%;
height: 550px;
padding: 20px;
margin: 20px;
}
.info
{
align-items: center;
justify-content: center;
display:flex;
flex-direction: column;
margin: 0 0 15px 0;
}
Can you please check the below code link? Hope it will work for you. We can solve this issue with the help of flex, without using position: absolute;.
You need to remove justify-content: center; from the .container.
We have wrapped all info items in one div like .content and give margin:auto; to them.
Please refer to this link:
https://jsfiddle.net/yudizsolutions/z71rbu6o/7/
.container {
align-items: center;
display: flex;
flex-direction: column;
width: 35.5%;
height: 550px;
padding: 20px;
margin: 20px;
background: #000;
}
h1 {
color: #fff;
}
.content {
margin: auto;
}
.info {
align-items: center;
justify-content: center;
display: flex;
flex-direction: column;
margin: 0 0 15px 0;
color: #fff;
}
.info h2,
.info p {
margin: 0;
}
<div class="container">
<h1>INFO</h1>
<div class="content">
<div class="info">
<h2>Age</h2>
<p>20</p>
</div>
<div class="info">
<h2>Adress</h2>
<p>Wolna 23, Warszawa</p>
</div>
<div class="info">
<h2>Email</h2>
<p>lorem#gmail.com</p>
</div>
<div class="info">
<h2>Phone</h2>
<p>669 133 777</p>
</div>
</div>
</div>
Try adding this:
.container h1{
position: absolute;
top: 0vh;
}
if you want to stick it to the top add this:
h1{
position: absolute;
top: 0;
}
Edit: there are a lot of ways, but in all of them you must to take the header out of the flexbox. You can do it like this:
.container {
box-sizing: border-box;
position: relative;
width: 35.5%;
height: 600px;
background-color: coral;
padding: 20px;
}
.info {
align-items: center;
justify-content: center;
display: flex;
flex-direction: column;
margin: 0 0 15px 0;
}
.wrapper {
position: relative;
align-items: center;
justify-content: center;
display: flex;
flex-direction: column;
height: 450px;
top: 50%;
left: 0;
transform: translateY(-50%);
background-color: cadetblue;
width: 100%;
}
.header {
position: absolute;
top: 0;
left: 50%;
margin-top: 0;
transform: translateX(-50%);
background-color: cadetblue;
}
p {
margin: 0.5rem;
}
<div class="container">
<h1 class="header">INFO</h1>
<div class="wrapper">
<div class="info">
<h2>Age</h2>
<p>20</p>
</div>
<div class="info">
<h2>Adress</h2>
<p>Wolna 23, Warszawa</p>
</div>
<div class="info">
<h2>Email</h2>
<p>lorem#gmail.com</p>
</div>
<div class="info">
<h2>Phone</h2>
<p>669 133 777</p>
</div>
</div>
</div>
I have two divs, but they are at the top, I want them in the middle, also each one has a background color that I'd like to fill their half of the screen.
.contenedor {
display: flex;
justify-content: space-around;
align-items: center;
font-size: 50px;
}
.español {
background: red;
}
.english {
background: blue;
}
a {
text-decoration: none;
}
<div class="contenedor">
<div class="español">
Español
</div>
<div class="english">
English
</div>
</div>
How would I go about doing this?
A picture says more than a thousand words
Thanks!
There's quite a lot to add to your code. If you want to use flex (as you did for the container), use the following settings for the elements:
html,
body {
height: 100%;
margin: 0;
}
.contenedor {
display: flex;
justify-content: space-around;
align-items: center;
font-size: 50px;
height: 100%;
}
.contenedor>div {
width: 100%;
height: 100%;
text-align: center;
position: relative;
display: flex;
flex-direction: column;
justify-content: center;
}
.español {
background: red;
}
.english {
background: blue;
}
a {
text-decoration: none;
color: white;
}
<div class="contenedor">
<div class="español">
Español
</div>
<div class="english">
English
</div>
</div>
There are many approaches you could choose.
A simple one is to set the 2 divs with abolute positionning, with each a width of 50%.
This way each div will occupy the whole height of the page and you don't have to worry about body margins or padding.
.contenedor {
font-size: 50px;
}
.espanol,
.english {
position: absolute;
top: 0;
bottom: 0;
width: 50%;
}
.espanol {
background: red;
left: 0;
}
.english {
background: blue;
left: 50%;
}
.contenedor a {
color: #fff;
text-align: center;
text-decoration: none;
display: flex;
justify-content: center;
flex-direction: column;
height: 100%;
}
<body>
<div class="contenedor">
<div class="espanol">
Español
</div>
<div class="english">
English
</div>
</div>
i managed to have vertically aligned column content with flexbox.
However, I need to have headers for the columns as well.
How can i have headers, in which the text is centered to the width of the column and which are at the top of the columns (the content must remain centered vertically)
see my codepen try:
http://codepen.io/anon/pen/QNmrNx
.tothetop{
position: absolute;
top:0;
text-align:center;
background: yellow;
}
put the headers on top, but the width does not match the column width so I can't center the text
If I understand you correctly, you want header text to be center horizontally in respect to columns. This may help you -
header {
display: flex;
justify-content: center;
width: 500px;
margin: auto;
}
.myView {
margin: auto;
display: flex;
flex-direction: row;
justify-content: space-around;
height: 500px;
width: 500px;
}
.wpType {
display: -webkit-flex;
display: flex;
-webkit-flex-direction: column;
flex-direction: column;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
padding: 0;
margin: 0;
list-style: none;
border: 1px solid silver;
}
.wpType:nth-child(even){
background: blue;
}
.wpType:nth-child(odd){
background: red;
}
.wp{
flex: 0 1 auto;
padding: 5px;
width: 100px;
height: 100px;
margin: 10px;
background: white;
line-height: 100px;
color: white;
font-weight: bold;
font-size: 2em;
text-align: center;
}
<header>header</header>
<div class="myView">
<div class="wpType">
<div class="wp"></div>
</div>
<div class="wpType">
<div class="wp"></div>
<div class="wp"></div>
<div class="wp"></div>
</div>
<div class="wpType">
<div class="wp"></div>
<div class="wp"></div>
<div class="wp"></div>
</div>
<div class="wpType"><div class="wp"></div>
<div class="wp"></div></div>
<div class="wpType"><div class="wp"></div></div>
</div>
I'm trying to horizontally center align group1 and right align group2 with flex.
<div class="holder">
<div class="group1">
...
</div>
<div class="group2">
...
</div>
</div>
CSS:
.holder{
display: flex;
justify-content:center;
}
.group2{
align-self: flex-end;
}
But everything is just centered, what's the fix?
As flexbox is built on manipulation of margins there is no method of moving one item out of the flow other than using position:absolute.
Any other method leaves the center item affected by other elements.
.holder {
display: flex;
justify-content: center;
border: 1px solid grey;
position: relative;
}
.group1 {
background: plum;
}
.group2 {
position: absolute;
right: 0;
top: 50%;
transform: translateY(-50%);
border: 1px solid red;
}
<div class="holder">
<div class="group1">
Group 1
</div>
<div class="group2">
Group 2
</div>
</div>
Immagine you have a button with a text and a icon and you want the text on the center and the icon on the right. Don't forget to give a width to the button.
With flexbox is really simple to do it.
button {
display: flex;
justify-content: center;
align-items: center;
flex: 1;
width: 400px;
order: 1;
}
span {
flex: 1;
}
<button>
<span>
Click me
</span>
<i>></i>
</button>
We can achieve it using flex, grid and position.
Check out the solution for flex, grid as position has already been submitted above 👆🏻
Flex Solution:
.holder {
display: flex;
}
.group1 {
flex: 1;
text-align: center;
}
<div class="holder">
<div class="group1">...</div>
<div class="group2">.....</div>
</div>
Grid Solution:
.holder {
display: grid;
grid-template-columns: 1fr 0fr;
}
.group1 {
flex: 1;
text-align: center;
border: 1px solid green;
}
.group2 {
border: 1px solid pink;
}
<div class="holder">
<div class="group1">...</div>
<div class="group2">.....</div>
</div>
This is the best what you can do with just Flexbox
.holder{
display: flex;
justify-content: center;
}
.group1 {
flex: 1;
text-align: center;
}
.group2{
margin-left: auto;
}
<div class="holder">
<div class="group1">
Center
</div>
<div class="group2">
Right
</div>
</div>
Better option is to use relative/absolute position with Flexbox
.holder{
display: flex;
justify-content: center;
color: white;
position: relative;
}
.group1 {
flex: 1;
text-align: center;
background: black;
}
.group2{
position: absolute;
top: 0;
right: 0;
}
<div class="holder">
<div class="group1">
Center
</div>
<div class="group2">
Right
</div>
</div>
You can set a height in the .holderand the play with align-self and text-align:
CSS
.holder{
display: flex;
justify-content:center;
align-content: center;
height: 200px;
background: #ccc;
}
.group2{
align-self: center;
text-align: right;
width: 50%;
}
.group1{
align-self: center;
width: 50%;
text-align: center;
}
DEMO HERE