currently I have this header bar
#header {
display: flex;
align-items: center;
height: 60px;
background: gray;
}
.box {
width: 32px;
height: 32px;
margin: 0 5px;
background: red;
}
<div id="header">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>
and I want the containers align themselves in the center of the parent.
As you can see I always want the bar being centered when adding more divs or remove some of them.
Add the justify-content: space-around rule:
#header {
display: flex;
align-items: center;
height: 60px;
background: gray;
justify-content: space-around;
}
.box {
width: 32px;
height: 32px;
margin: 0 5px;
background: red;
}
<div id="header">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>
You could also use justify-content: space-evenly;. The difference between space-around and space-evenly is that space around has less space towards the (left and right) edges of the parent container, whereas the space in space-evenly is similar everywhere.
#header {
justify-content: space-evenly;
display: flex;
align-items: center;
height: 60px;
background: gray;
}
.box {
width: 32px;
height: 32px;
margin: 0 20px;
background: red;
}
<div id="header">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>
Here is a good resource for flexbox.
Related
I searched over internet and couldn't find this kind of grid layout.
"grid-template-columns" doesnt do the thing, cant do one box bigger then the others. I want 4 equal boxes and 1 box with equal height and width = square box * 2 + gridgap.
here is the image I've illustrated to make you understand what i ment.
I also tried to use display flex but I didnt get the Idea of it. Please, help me. Thanks!
Illustation of my idea
The answer to the question ("How can I make responsive grid layout in html css") is this:
.wrapper {
display: grid;
justify-content: center;
}
.box {
border: 2px solid #000;
width: 128px;
min-height: 128px;
justify-content: center;
margin: 10px;
display: grid;
align-items: center;
}
.box5 {
grid-column: 2/5;
max-width: 280px;
width: 100%;
}
/*for this to be visible, the screen-width has to be under 600px*/
#media (max-width: 600px) {
.box5 {
grid-column: 2/1;
}
}
<div class="wrapper">
<div class="box1 box">128x128</div>
<div class="box2 box">128x128</div>
<div class="box3 box">128x128</div>
<div class="box4 box">128x128</div>
<div class="box5 box">280x128</div>
</div>
Like in the image
.container {
display: flex;
flex-direction: column;
justify-content: space-between;
width: 100%;
height: 180px;
}
.row {
display: flex;
flex-direction: row;
justify-content: center;
}
.box {
width: 10vw;
height: 10vw;
margin: 12px;
border: 1px solid black;
}
.box.big {
width: calc(20vw + 24px);
}
<div class="container">
<div class="row">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
<div class="row">
<div class="box big"></div>
</div>
</div>
Fields 1-5 directly below each other are centered to the middle at the current width
.container {
display: flex;
flex-direction: column;
align-items: center;
width: 100vw;
height: 100vw;
}
.box {
width: 10vw;
height: 10vw;
margin: 12px;
border: 1px solid black;
}
.box.big {
width: calc(20vw + 24px);
}
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box big"></div>
</div>
I want to move the title to green area. If I carry to the container it puts blue area. If I decrease container height title getting closer to the squares. But I want the boxes in the center and the title little above of them. How can I do it?
#container {
width: 1200;
height: 600px;
margin: auto;
border: 1px solid grey;
display: flex;
align-items: center;
justify-content: center;
}
.box {
height: 250px;
width: 250px;
margin-left: 25px;
margin-right: 25px;
background-color: red;
}
h1 {
text-align: center;
}
<h1>TITLE!!!</h1>
<div id="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
my page
It's the display: flex; that's causing the issue. Here's a working model:
#container {
width: 1200;
height: 600px;
margin: auto;
border: 1px solid grey;
}
.box-wrapper {
display: flex;
align-items: center;
justify-content: center;
}
.box {
height: 250px;
width: 250px;
margin-left: 25px;
margin-right: 25px;
background-color: red;
}
h1 {
text-align: center;
}
<div id="container">
<h1>TITLE!!!</h1>
<div class="box-wrapper">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
</div>
I added a extra wrapper to wrap all the elements and added a display flex to it. Also, You can make use of a gap css property in wrapper class to add extra space between them.
.wrapper {
display: flex;
flex-direction: column;
border: 1px solid grey;
width: 1200px;
height: 600px;
}
#container {
margin: auto;
//border: 1px solid grey;
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
}
.box {
height: 250px;
width: 250px;
margin-left: 25px;
margin-right: 25px;
background-color: red;
}
h1 {
text-align: center;
}
<div class="wrapper">
<h1>TITLE!!!</h1>
<div id="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
</div>
Also, the other way to solve this would be to use absolute positioning and adding a top padding to allow space for the div header if extra div is really not required.
I'm trying to create a Slider Layout with flex boxes, like this photo :
Here I've big photo at right side and thumbnail items on the left. I want to align left side and thumbnails wrapper with big photo height. But unfortunately It's not possible only with flex boxes and I should check big photo height with JavaScript and align left side with that.
For example check this code:
main{
position: absolute;
top: 0px;
left: 0px;
right: 0px;
bottom: 0px;
display: flex;
align-items: center;
justify-content: center;
}
.wrapper{
width: 500px;
overflow: hidden;
background: gray;
display: flex;
align-items: flex-start;
justify-content: center;
}
.right{
width: calc(100% - 150px);
height: 450px;
background: red;
}
.left{
width: 150px;
display: flex;
align-items: center;
justify-content: space-between;
flex-direction: column;
}
.item{
width: 100%;
height: 50px;
color: white;
background: green;
display: flex;
align-items: center;
justify-content: center;
}
<main>
<div class="wrapper">
<div class="left">
<div class="item">
<strong>Item</strong>
</div>
<div class="item">
<strong>Item</strong>
</div>
<div class="item">
<strong>Item</strong>
</div>
<div class="item">
<strong>Item</strong>
</div>
<div class="item">
<strong>Item</strong>
</div>
</div>
<div class="right"></div>
</div>
</main>
In sample code I've not image and I handled this issue with 450px height in CSS.
So how can I align the left side with out JS and only with CSS? I want to use space-between mode to show all items in this height. Consider it, height:100% didn't work for this issue.
As per my comment, in order for your left column to extend to the height of your right column, all you need to do is remove the align items from your wrapper:
main{
position: absolute;
top: 0px;
left: 0px;
right: 0px;
bottom: 0px;
display: flex;
align-items: center;
justify-content: center;
}
.wrapper{
width: 500px;
overflow: hidden;
background: gray;
display: flex;
/* align-items: flex-start; -- remove this */
justify-content: center;
}
.right{
/* width: calc(100% - 150px); I would swap this for flex grow, then you don't need hard values */
flex-grow: 1;
height: 450px;
background: red;
}
.left{
width: 150px;
display: flex;
align-items: center;
justify-content: space-between;
flex-direction: column;
}
.item{
width: 100%;
height: 50px;
color: white;
background: green;
display: flex;
align-items: center;
justify-content: center;
}
<main>
<div class="wrapper">
<div class="left">
<div class="item">
<strong>Item</strong>
</div>
<div class="item">
<strong>Item</strong>
</div>
<div class="item">
<strong>Item</strong>
</div>
<div class="item">
<strong>Item</strong>
</div>
<div class="item">
<strong>Item</strong>
</div>
</div>
<div class="right"></div>
</div>
</main>
This solution uses CSS Grid layouts - use display: grid on your wrapper that lays out your left and right sections using grid-template-columns: 150px 1fr (remove the width definitions in right and left elements.).
Now add height: 100% to left and then for the items flex: 1 for the flexbox items to occupy the dynamic height coming from the right section - see demo below:
main {
position: absolute;
top: 0px;
left: 0px;
right: 0px;
bottom: 0px;
display: flex;
align-items: center;
justify-content: center;
}
.wrapper {
width: 500px;
overflow: hidden;
background: gray;
display: grid; /* make this a grid container */
grid-template-columns: 150px 1fr; /* 150px for left and rest for right*/
align-items: flex-start;
justify-content: center;
}
.right {
/* width: calc(100% - 150px);*/
height: 450px;
background: red;
}
.left {
/*width: 150px;*/
display: flex;
align-items: center;
justify-content: space-between;
flex-direction: column;
height: 100%; /* ADDED */
}
.item {
width: 100%;
height: 50px;
color: white;
background: green;
display: flex;
align-items: center;
justify-content: center;
flex: 1; /* ADDED */
}
<main>
<div class="wrapper">
<div class="left">
<div class="item">
<strong>Item</strong>
</div>
<div class="item">
<strong>Item</strong>
</div>
<div class="item">
<strong>Item</strong>
</div>
<div class="item">
<strong>Item</strong>
</div>
<div class="item">
<strong>Item</strong>
</div>
</div>
<div class="right"></div>
</div>
</main>
Something like this can be done with flexbox, if you set the height of the container to be the height you want all your sliders to be:
.container {
display: flex;
flex-flow: column wrap;
height: 250px;
width: 250px;
}
.container > * {
display: flex;
flex-flow: row wrap;
flex: 1 100%;
}
.thumbs {
flex-direction: row;
margin-right: 5px;
}
.thumb {
background: red;
flex: 1 100%;
margin: 5px 0;
}
.large {
background: blue;
margin: 5px;
}
<div class="container">
<div class="thumbs">
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
</div>
<div class="large"></div>
</div>
I am using flexbox to center content with justify-content: center which works as intended but flexbox also moves divs to be side by side which I don't want.
Here is an example
.flex {
display: flex;
justify-content: center;
}
.content {
width: 100px;
height: 100px;
background-color: #000;
margin: 10px;
}
<div class="flex">
<div class="content">
</div>
<div class="content">
</div>
</div>
How can I use flexbox while retaining the default one div on top of the other positioning.
You can set flex-direction: column and then you have to use align-items: center. Default flex-direction is row.
.flex {
display: flex;
align-items: center;
flex-direction: column;
}
.content {
width: 100px;
height 100px;
color: #000;
border: 1px solid black;
padding: 5px;
margin: 5px;
}
<div class="flex">
<div class="content"></div>
<div class="content"></div>
</div>
Try following code,
.flex {
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;
}
.content {
width: 100px;
height: 100px;
background-color: #000;
margin: 10px;
}
<div class="flex">
<div class="content">
</div>
<div class="content">
</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>