How to center an outer div? [duplicate] - html

This question already has answers here:
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 a container div which has other divs and contents within it and I am trying to center the outer div.
For example:
body {
height: 100vh;
width: 100vw;
}
.test {
width: 20vw;
height: 20vh;
background-color: green;
}
.outer-test {
display: block;
margin: auto;
}
<div class="outer-test">
<p>Hello</p>
<div class="test"></div>
</div>
In the example above, how would I be able to center the div with class of "outer-test"? I have tried to make the display: block and use margin: auto but that doesn't seem to be working.

Option 1: You can use display: flex on the body
body{
height: 100vh;
width: 100vw;
display: flex;
justify-content: center; // Centers in the direction of flex-direction (default is row)
align-items: center; // Centers in the direction normal to flex-direction
}
.test{
width: 20vw;
height: 20vh;
background-color: green;
}
<body>
<div class="outer-test">
<p>Hello</p>
<div class="test"></div>
</div>
</body>
Option 2: You can use a wrapper around your outer div.
body {
height: 100vh;
width: 100vw;
}
.test {
width: 20vw;
height: 20vh;
background-color: green;
}
.center {
display: flex;
justify-content: center;
}
<body>
<div class="center">
<div class="outer-test">
<p>Hello</p>
<div class="test"></div>
</div>
</div>
</body>

Related

Simple Verticle and Horizontal CSS [duplicate]

This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 6 months ago.
I am trying to make DIV verticle and horizontal center. I am using code from here : How can I vertically center a div element for all browsers using CSS?
I tried both
display: grid;
place-items: center;
and
display: flex;
align-items: center;
justify-content: center;
My code :
body {
background: #000000 50% 50%;
overflow-x: hidden;
overflow-y: hidden;
}
.video {
display: grid;
place-items: center;
}
<body> <div class="video">
<img src="https://place-hold.it/25x25" />
</div> </body>
Horizontal center : Success
Vertical center : failed
with above two CSS code.
It isn't working because elements by default have a height to fit-content. So you need to define a height higher then the fit-content height:
body {
background: black;
margin: 0;
}
.video {
width: 100vw;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
<body>
<div class="video">
<img src="https://place-hold.it/25x25" />
</div>
</body>
body {
background: black;
margin: 0;
}
.video {
width: 100vw;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
<body>
<div class="video">
<img src="https://place-hold.it/25x25" />
</div>
</body>
You need to apply height: 100%; to the div and all its ancestors (i.e. body and html in this case) for the flexbox centering method to work, otherwise it will only have the height of its contents (i.e. the image) and not fill the viewport vertically.
html, body {
height: 100%;
margin: 0;
}
body {
background: #000000 50% 50%;
overflow-x: hidden;
overflow-y: hidden;
}
.video {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
<body>
<div class="video">
<img src="https://place-hold.it/25x25" />
</div>
</body>
You can just add height as 100vh in video class.
body {
background: #000000 50% 50%;
overflow-x: hidden;
overflow-y: hidden;
}
.video {
display: grid;
place-items: center;
height: 100vh;
}
<body>
<div class="video">
<img src="https://place-hold.it/25x25" />
</div>
</body>
If you want to center both vertically and horizontally, you can try this code below on your CSS:
body {
display: flex;
justify-content: center;
align-items: center;
height:100vh;
}
<body>
<div class="video">
<img src="https://place-hold.it/25x25" />
</div>
</body>

What am I missing? Flexbox doesn't center the container in this snippet here [duplicate]

This question already has answers here:
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 1 year ago.
I'm trying to center the container and I don't know why it doesnt work.
html,
body {
height: 100%
}
.container {
min-height: 20em;
width: fit-content;
background-color: #ffffff;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
Flexbox only centers its contents.
In other words, adding flexbox to the container will center everything inside it.
To fix it, add flexbox to the body instead:
html, body {
height:100vh;
margin:0;
}
body {
display: flex;
align-items: center;
justify-content: center;
}
.container{
min-height:20em;
width: fit-content;
background-color:#ffffff;
}
Try this!!!
*{
margin: 0;
padding: 0;
}
.parent{
width: 100vw;
height: 100vh;
background: grey;
display: grid;
place-content: center;
}
.child{
background: blue;
width: 10vw;
height: 10vh;
}
<div class="parent">
<div class="child"></div>
</div>

Can't center both vertically and horizontally [duplicate]

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 1 year ago.
I just can't center the div(Horizontal-Container)both vertically and horizontally and I can't figure out why it's not working...
I've try all the methods by w3school, but either it's not horizontally or vertically center, it can't be both achieved...
Below is my code:
body {
background-color: #62306D;
}
.Horizontal-Container {
width: 100%;
height: auto;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
text-align: center;
}
.Yellow {
background-color: #F7EC7D;
width: 90px;
height: 180px;
}
<div class="Horizontal-Container">
<div class="Yellow"></div>
<div class="Yellow"></div>
<div class="Yellow"></div>
</div>
Your issue arises from .Horizontal-Container not being full height so it is technically vertically centered, just it hasn't moved. To fix this, you need to set the height of body and html to 100% which then allows the container to have the height you desire. It may seem off centre now, but that is down to padding and margin on the elements which you can easily remove.
html, body {
background-color: #62306D;
height: 100%;
margin: 0;
padding: 0;
}
.Horizontal-Container {
width: 100%;
height: 100%;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
text-align: center;
}
.Yellow {
background-color: #F7EC7D;
width: 90px;
height: 180px;
}
<div class="Horizontal-Container">
<div class="Yellow"></div>
<div class="Yellow"></div>
<div class="Yellow"></div>
</div>
It's not working because your .Horizontal-Container does not have a specific height. If you set the height to auto it will consume as much space as its children need. Thus you have to add a height either to your container or simply to your body in order to center your elements over the whole page.
body {
background-color: #62306D;
}
.Horizontal-Container {
width: 100%;
height: 100vh; /* <-- set a specific height */
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
text-align: center;
}
.Yellow {
background-color: #F7EC7D;
width: 90px;
height: 180px;
}
<div class="Horizontal-Container">
<div class="Yellow"></div>
<div class="Yellow"></div>
<div class="Yellow"></div>
</div>

CSS Position child div in center [duplicate]

This question already has answers here:
How can I vertically align elements in a div?
(28 answers)
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 3 years ago.
The Height of parent and child will be given at run time, I have tried vertical-align but it didn't work, margin/top property won't be correct because height is dynamic. Please provide proper way to fix this.
Expected: Child should be center of parent. Width should remain same
Update: Solution of #xmastertje is working, but if i change my child height it breaks. This question is part of big problem so I can't use calc here. In real time I have nested elements each should center of its parent.
:root {
--parent-height: 500px;
--child-height: 200px;
}
html,
body {
width: 100%;
height: 100%;
}
.parent {
height: var(--parent-height);
background-color: #bada55;
position: relative;
}
.child {
height: var(--child-height);
background-color: goldenrod;
}
<div class="parent">
Parent Has 500px Height
<div class="child">
child has 200px height
</div>
</div>
You can use flexbox for it like this:
.parent {
height: var(--parent-height);
background-color: #bada55;
position: relative;
display: flex;
justify-content: center;
flex-direction: column;
}
If you just want the child to be centered and the width will be the same, you could use the margin-top with calc() to calculate the top margin of the child with the parent
Something like this
margin-top: calc((var(--parent-height) - var(--child-height)) * 0.5);
Hope this help....
Use margin-top and position:absolute. With this you can fix it easy.
:root{
--parent-height: 500px;
--child-height: 200px;
}
html, body{
width: 100%;
height: 100%;
}
.parent{
height: var(--parent-height);
background-color: #bada55;
position: relative;
}
.child{
height: var(--child-height);
background-color: goldenrod;
position: absolute;
top: 50%;
width: 100%;
margin-top: -6em;
}
<div class="parent">
Parent Has 500px Height
<div class="child">
child has 200px height
</div>
</div>
EDIT
You have to use transform. This solution keeps working even if you change child or parent height.
:root{
--parent-height: 500px;
--child-height: 200px;
}
html, body{
width: 100%;
height: 100%;
}
.parent{
height: var(--parent-height);
background-color: #bada55;
position: relative;
}
.child{
height: var(--child-height);
background-color: goldenrod;
position: absolute;
top: 50%; left: 50%;
transform: translate(-50%,-50%);
width:100%;
}
<div class="parent">
Parent Has 500px Height
<div class="child">
child has 200px height
</div>
</div>
Hi i think the best way to do that is using table cell property.No matter what is the height of parent the child will be always align center.
But if you want the parent to be full width you can use flex properties.I Hope that will work fine.
:root {
--parent-height: 500px;
--child-height: 200px;
}
html,
body {
width: 100%;
height: 100%;
}
.parent {
height: var(--parent-height);
background-color: #bada55;
position: relative;
display: flex;
justify-content: center;
align-items:center;
width:100%;
}
.child {
height: var(--child-height);
background-color: goldenrod;
}
<div class="parent">
Parent Has 500px Height
<div class="child">
child has 200px height
</div>
</div>
I have used the flexbox to make the child become center. Try the below code.
:root {
--parent-height: 500px;
--child-height: 200px;
}
html,
body {
width: 100%;
height: 100%;
}
.parent {
height: var(--parent-height);
background-color: #bada55;
position: relative;
display:flex;
display: -ms-flexbox!important;
flex-wrap:wrap;
-webkit-flex-wrap: wrap;
}
.child {
height: var(--child-height);
background-color: goldenrod;
-ms-flex-align: center!important;
align-items: center!important;
width:100%;
}
<div class="parent">
Parent Has 500px Height
<div class="child">
child has 200px height
</div>
</div>
You can do it easily with flex box
:root {
--parent-height: 500px;
--child-height: 200px;
}
html,
body {
width: 100%;
height: 100%;
}
.parent {
height: var(--parent-height);
background-color: #bada55;
position: relative;
display: flex;
justify-content:center;
flex-direction: column;
align-items: center;
}
.child {
height: var(--child-height);
background-color: goldenrod;
width:100%;
}
<div class="parent">
<p>Parent Has 500px Height</p>
<br>
<div class="child">
<p>child has 200px height</p>
</div>
</div>

div with display: flex is not centering child div [duplicate]

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 5 years ago.
I have a div with a display: flex, and I would like to display the output in a row center. For some reason it is displaying left. I want the imgBoxBot to be centered inside the imgBot div. But text-align: center is only centering the text inside the imgBoxBot, how do I center the actual div?
#imgBot {
position: absolute;
height: 100px;
width: 100%;
bottom: 0;
display: flex;
flex-direction: row;
text-align: center;
}
#imgBoxBot {
height: 100px;
width: 100px;
}
<div id="imgBot">
<div id="imgBoxBot">
test
</div>
<div id="imgBoxBot">
test
</div>
</div>
Use justify-content: center. You can find more information here
#imgBot {
position: absolute;
height: 100px;
width: 100%;
bottom: 0;
display: flex;
flex-direction: row;
justify-content: center;
}
.imgBoxBot {
height: 100px;
width: 100px;
}
<div id="imgBot">
<div class="imgBoxBot">
test
</div>
<div class="imgBoxBot">
test
</div>
</div>