How to center flexbox container? [duplicate] - html

This question already has answers here:
Make a div fill the height of the remaining screen space
(41 answers)
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Flexbox: center horizontally and vertically
(14 answers)
How can I vertically align elements in a div?
(28 answers)
Closed 2 years ago.
I'd like to have a horizontally and vertically centered cards. I've done it like this.
*{
margin: 0;
padding: 0;
}
.wrapper{
background-color: darkkhaki;
width: 700px;
height: 700px;
}
.cards{
display: flex;
justify-content: center;
align-items: center;
}
.card{
width: 100px;
height: 250px;
background-color: chartreuse;
border: crimson 2px solid;
}
<div class="wrapper">
<div class="text">
<p>Lorem ipsum</p>
</div>
<div class="cards">
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
</div>
</div>
However, the vertical center doesn't occur (just the horizontal).
The same happens when I try it with the "text" class.
Thank you for your help :)

In order to center align content, you need at least 1 defined height so the container knows where the "center" is. With flexbox it's as easy then setting justify-content: center and align-items: center to make sure any flex item goes to the center of the container.
What you are missing in your code, is a height for the cards container. You could set a number for it, or just heigth: 100% so it takes the full height of its own container (wrapper) which is 700px
body {
margin: 0
}
.flex-item {
border: 1px solid black;
height: 100px;
width: 150px
}
.flex-container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
<div class="flex-container">
<div class="flex-item"></div>
</div>
Here's your code
* {
margin: 0;
padding: 0;
}
.wrapper {
background-color: darkkhaki;
width: 700px;
height: 700px;
}
.cards {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
.card {
width: 100px;
height: 250px;
background-color: chartreuse;
border: crimson 2px solid;
}
<div class="wrapper">
<div class="text">
<p>Lorem ipsum</p>
</div>
<div class="cards">
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
</div>
</div>

Simple, set margin: 0 auto; on the main container that holds everything together. In your case, that appears be the main <div> with the wrapper class:
.wrapper {
background-color: darkkhaki;
width: 700px;
height: 700px;
margin: 0 auto;
}
That should do the trick.

Simply add the total height (100% or 100h) to the cards class.
.cards{
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}

Related

How To cover remaining space between two division vertically [duplicate]

This question already has answers here:
Make a div fill the height of the remaining screen space
(41 answers)
Closed 3 months ago.
Here is the CSS I have applied CSS to wrapper, header and footer, and I tried to cover the remaining part of the screen with the between container but I am not able to do it.
.wrapper {
height: 100vh;
}
.header {
width: 100vw;
display: flex;
position: absolute;
top: 0;
}
.between {}
.footer {
position: absolute;
bottom: 0;
width: 100vw;
}
<div class="wrapper">
<div class="header"></div>
<div class="between"></div>
<div class="footer"></div>
</div>
you can use display: flex for this, use flex:1 to the child you want to take up available space.
.wrapper {
height: 100vh;
display: flex;
flex-direction: column;
}
.header {
background: red;
height: 10px;
}
.between {
background: green;
flex: 1;
}
.footer {
background: yellow;
height: 10px;
}
<div class="wrapper">
<div class="header"></div>
<div class="between"></div>
<div class="footer"></div>
</div>
read more about css flexbox box here.

How do I center the contents of these table rows vertically in css? [duplicate]

This question already has answers here:
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 3 years ago.
.top-row{
min-height: 50vh;
min-width: 100vw;
padding:0px;
}
.top-col{
background: #CCCCCC;
align-content: center;
min-height: 50vh;
min-width: 100vw;
}
.bottom-col{
background: #CCCCCC;
align-content: center;
min-height: 50vh;
min-width: 100vw;
}
.bottom-row{
min-height: 50vh;
min-width: 100vw;
padding:0px;
}
</style>
</head>
<body>
<div class="container">
<div class="row top-row">
<div class="col-12 top-col">
<h2>Hello</h2>
</div>
</div>
<div class ="row bottom-row">
<div class="col-12 bottom-col">
<h2>World</h2>
</div>
</div>
</div>
</body>
</html>
I want to be able to center content vertically from just editing the css portion.
I have done lots of research and tried many different this but I was not able to get a solution that works.
If I understand your question, you want to align the text vertically, so for that just add text-align: center; to your two row class
.top-row{
min-height: 50vh;
min-width: 100vw;
padding:0px;
text-align:center;
}
.top-col{
background: #CCCCCC;
align-content: center;
min-height: 50vh;
min-width: 100vw;
}
.bottom-col{
background: #CCCCCC;
align-content: center;
min-height: 50vh;
min-width: 100vw;
}
.bottom-row{
min-height: 50vh;
min-width: 100vw;
padding:0px;
text-align:center;
}
<div class="container">
<div class="row top-row">
<div class="col-12 top-col">
<h2>Hello</h2>
</div>
</div>
<div class ="row bottom-row">
<div class="col-12 bottom-col">
<h2>World</h2>
</div>
</div>
</div>

Unable to center div with images

I've been working on a website for the past few hours and recently I added an image grid using CSS3 Flexbox. However I've been having trouble centering it for the past few hours.
I've tried using justify-content: center to center it as well as margin: 0 auto but nothing works.
What am I doing wrong?
Here's my code
HTML
<div class="gallery">
<h3>Gallery</h3>
<div class="tiles">
<div class="row">
<div class="column">
<img src="https://upload.wikimedia.org/wikipedia/commons/7/7a/Mahatma-Gandhi%2C_studio%2C_1931.jpg">
<img src="https://www.biography.com/.image/t_share/MTYyNzM4ODIyODY0NDQ2NTA0/gandhi-spiritual-leader-leading-the-salt-march-in-protest-against-the-government-monopoly-on-salt-production-photo-by-central-pressgetty-images.jpg">
</div>
<div class="column">
<img src="https://akm-img-a-in.tosshub.com/indiatoday/images/story/201911/Mahatma_Gandhi-770x433.png?DtfYcyk4yzMDy1GNsIJaQgX7mrBoqTQO">
<img src="https://images.news18.com/ibnlive/uploads/2018/10/Mahatma-Gandhi2.jpg">
</div>
<div class="column">
<img src="https://images.livemint.com/img/2019/10/02/600x338/gandhi_1570037914879.JPG">
<img src="https://m.telegraphindia.com/unsafe/620x350/smart/static.telegraphindia.com/derivative/THE_TELEGRAPH/1671172/16X9/image34ba57f1-21d2-4af6-ad21-b9c036e39194.jpg">
<img src="https://img.etimg.com/thumb/msid-67754218,width-640,resizemode-4,imgsize-184267/he-whom-leaders-looked-up-to.jpg">
</div>
</div>
</div>
</div>
CSS
.row {
display: flex;
flex-wrap: wrap;
padding: 0 4px;
}
.column {
flex: 25%;
max-width: 25%;
padding: 0 4px;
}
.column img {
margin-top: 8px;
vertical-align: middle;
width: 100%;
}
.gallery {
height: auto;
width: 100%;
text-align: center;
}
.gallery h3 {
font-size: 60px;
margin-bottom: 40px;
}
.tiles {
display: block;
width: 100%;
margin: 0 auto;
}
How do I center the tiles div?
if you want to have 3 columns in the center of you screen, you have to:
put the display: flex; justify-content: center in the .tiles class
change the column width property to width: 33% and removing the max-width: 25%
give to your .row class the width you want, or the max-width, like max-width:75%

How can i make my two boxes in the same row? [duplicate]

This question already has answers here:
How to place div side by side
(7 answers)
Closed 4 years ago.
I have two boxes my one box are in the left side and my other box is in the bottom how can I make these two boxes in the same row
I tried to float right
.box2{
width: 280px;
height: 250px;
padding: 10px;
background-color: white;
}
my html
<div class="box2">
</div>
<div class="box2">
</div>
.flex-parent {
display: flex;
justify-content: center;
align-items: center;
}
.box {
background-color: red;
margin: 20px;
padding: 20px;
}
<div class="flex-parent">
<div class="box">Box 1</div>
<div class="box">Box 2</div>
</div>
You could try some flex-box https://codepen.io/krullmizter/pen/xMXWrv with that you add a parent container around the boxes and flex them with it.
Use display:inline; in style for both the div
.box1 {
width: 1005px;
height: 1000px;
padding: 50px;
border: 1px solid red;
margin-left: 162px;
background-color: blue;
}
.box2{
width: 280px;
height: 250px;
padding: 10px;
background-color: red;
}
.box1, .box2
{
display:inline;
text-align:center;
}
<div class="box1">
</div>
<div class="box2">
</div>

Centering a single child div horizontally and vertically on the page

The child is centering horizontally, but not vertically. I think it is because the example-container needs a height set. What if I want it centered on the entire page?
Please see the below code-snippet
.example-container {
display: flex;
align-items: center;
justify-content: center;
}
.example-child {
width: 100px;
height: 100px;
background-color: orange;
}
<div class="example-container">
<div class="example-child">
<p>Sample Content...</p>
</div>
</div>
It's height will only be as big as the child by default. Add height: 100vh or whatever works for your layout.
body { margin: 0; }
.example-container {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.example-child {
width: 100px;
height: 100px;
background-color: orange;
}
<div class="example-container">
<div class="example-child">
<p>Sample Content...</p>
</div>
</div>
Centering a single child div horizontally and vertically on the page
It can be done with less markup and wider browser support by using transform instead of flexbox
.example-container {
position: absolute;
top: 50%; left: 50%;
transform: translate(-50%,-50%);
width: 100px;
height: 100px;
background-color: orange;
}
<div class="example-container">
<p>Sample Content...</p>
</div>