This question already has answers here:
How to center a flex container but left-align flex items
(9 answers)
Closed 5 years ago.
Hello I have the following bit of code but i'm struggling to sort out a layout issue. Ideally i would like the rows to go from left to right as shown in the picture but I would like to center the entire structure.
Currently have
But I would like this
This is the css and code i'm currently using.
<div class="service_list_container">
<div class="service_tab"></div>
<div class="service_tab"></div>
<div class="service_tab"></div>
<div class="service_tab"></div>
<div class="service_tab"></div>
</div>
.service_list_container {
background: blue;
display: flex; /* or inline-flex */
flex-wrap: wrap;
justify-content: space-evenly;
}
.service_tab {
flex-basis: 300px;
flex-grow: 0;
flex-shrink: 0;
height: 400px;
background: #fff;
margin: 10px 20px;
-webkit-box-shadow: -2px -1px 5px 0px #efefef;
-moz-box-shadow: -2px -1px 5px 0px #efefef;
box-shadow: -2px -1px 5px 0px #efefef;
border: solid 1px #e8e8e8;
}
Is it possible using flexbox to achieve what I am after?
Thanks
You need to use flex-basis: 30%; instead of flex-basis: 300px;
.service_list_container {
background: blue;
display: flex; /* or inline-flex */
flex-wrap: wrap;
justify-content: space-evenly;
}
.service_tab {
flex-basis: 30%;
flex-grow: 0;
flex-shrink: 0;
height: 400px;
background: #fff;
margin: 10px 1.5%;
-webkit-box-shadow: -2px -1px 5px 0px #efefef;
-moz-box-shadow: -2px -1px 5px 0px #efefef;
box-shadow: -2px -1px 5px 0px #efefef;
border: solid 1px #e8e8e8;
}
<div class="service_list_container">
<div class="service_tab"></div>
<div class="service_tab"></div>
<div class="service_tab"></div>
<div class="service_tab"></div>
<div class="service_tab"></div>
</div>
.service_tab { flex-grow: 1; }
instead of
.service_tab { flex-grow: 1; }
.service_list_container {
background: blue;
display: flex; /* or inline-flex */
flex-wrap: wrap;
justify-content: space-evenly;
}
.service_tab {
flex-basis: 300px;
flex-grow: 1;
flex-shrink: 0;
height: 400px;
background: #fff;
margin: 10px 20px;
-webkit-box-shadow: -2px -1px 5px 0px #efefef;
-moz-box-shadow: -2px -1px 5px 0px #efefef;
box-shadow: -2px -1px 5px 0px #efefef;
border: solid 1px #e8e8e8;
}
<div class="service_list_container">
<div class="service_tab"></div>
<div class="service_tab"></div>
<div class="service_tab"></div>
<div class="service_tab"></div>
<div class="service_tab"></div>
</div>
Related
I'm working with CSS display: inline-block.
.container div
{
display: inline-block;
width: calc((100% - 13px) / 2);
margin: 2px;
background: #fff;
border-radius: 10px;
color: #5e5e5e;
cursor: pointer;
margin-bottom: 6px;
box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
transition: all 0.3s cubic-bezier(.25,.8,.25,1);
border-bottom: 2px solid #80D0C7;
position: relative;
}
.container div:last-child
{
width: 100%;
}
<div class="container">
<div class="red">Red</div>
<div class="green">green</div>
<div class="blue">blue</div>
<div class="orange">orange</div>
<div class="black">black</div>
</div>
I put width: 100% on div last-child. But it give an extra padding/margin.
My question, how to make the div last-child fit width 100% without any extra padding/margin?
Use display: flex in .container, then flex-basis: calc(50% - 4px) (where 4px is the sum of the margins in each div) in all divs and in last one set flex-basis: 100%
.container {
display: flex;
flex-wrap: wrap;
}
.container div {
background: #fff;
border-radius: 10px;
color: #5e5e5e;
cursor: pointer;
margin: 2px 2px 6px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
transition: all 0.3s cubic-bezier(.25, .8, .25, 1);
border-bottom: 2px solid #80D0C7;
position: relative;
flex: 0 calc(50% - 4px)
}
.container div:last-child {
flex-basis: 100%;
}
<div class="container">
<div class="red">Red</div>
<div class="green">green</div>
<div class="blue">blue</div>
<div class="orange">orange</div>
<div class="black">black</div>
</div>
I have to align a child div which is inside a div of flex box container to bottom. I tried different options like setting bottom to 0 or margin-top but none of them are working.
<style>
.flex-container {
display: flex;
height: 500px;
background-color: #f1f1f1;
}
.flex-container > div {
color: white;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
.vertical
{
box-shadow: inset 0px 4px 6px #ccc;
}
.progress {
background-color: #f5f5f5;
border-radius: 4px;
-webkit-box-shadow: inset 0 1px 2px rgb(0 0 0 / 10%);
box-shadow: inset 0px 4px 6px rgba(100,100,100,0.6);
}
.progress-bar-info {
background-color: #5bc0de;
}
.progress-bar {
box-shadow: inset 0px 4px 6px rgb(100 100 100 / 60%);
}
</style>
<div class="flex-container">
<div class= "progress vertical" >1
<div class="progress-bar progress-bar-info" style="height: 35%;">
how to align this div to bottom..... ?
</div>
</div>
</div>
You can add flex properties on flex-container>div like below:
.flex-container {
display: flex;
height: 500px;
background-color: #f1f1f1;
}
.flex-container>div {
color: red;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.vertical {
box-shadow: inset 0px 4px 6px #ccc;
}
.progress {
background-color: #f5f5f5;
border-radius: 4px;
-webkit-box-shadow: inset 0 1px 2px rgb(0 0 0 / 10%);
box-shadow: inset 0px 4px 6px rgba(100, 100, 100, 0.6);
}
.progress-bar-info {
background-color: #5bc0de;
}
.progress-bar {
box-shadow: inset 0px 4px 6px rgb(100 100 100 / 60%);
}
<div class="flex-container">
<div class="progress vertical">
<div>1</div>
<div class="progress-bar progress-bar-info" style="height: 35%;">Bottom</div>
</div>
</div>
I'm trying to add a little design flourish to some tabs.
The tabs are contained inside a tab bar, which has a bottom border.
When a tab is active I want it to appear as if it's on the same level of the content, with the other tabs appearing set back.
To do this, I need to make the section of border beneath the active tab disappear - my first thought was to set the tab background white and overlap the parent - but this doesn't work.
Anyone got any ideas as to how to get the effect I'm looking for?
.container {
padding: 30px;
}
.tab-bar {
display: flex;
background-color: #fff;
border-bottom: solid 1px rgba(0,0,0, .12);
height: 48px;
overflow: hidden;
}
.tab {
flex: 1 1 0;
display: flex;
align-items: center;
justify-content: center;
height: 48px;
max-width: 200px;
min-width: 100px;
padding: 0 16px;
}
.tab[data-state="active"] {
box-shadow: 0 2px 4px -1px rgba(0,0,0,0.2), 0 4px 5px 0 rgba(0,0,0,0.14), 0 1px 1px 0 rgba(0,0,0,0.12);
color: red;
}
<div class="container">
<div class="tab-bar">
<div class="tab" data-state="active">Active</div>
<div class="tab">Not Active</div>
</div>
</div>
Link to JsFiddle
from earlier comment
draw the bottom border from the non active tabs
add eventually a pseudo element to fill the room left.
overflow:hidden on the parent hides shadows and also do not allow children to stand on top of the border in order to hide it
What you could do :
.container {
padding: 30px;
}
.tab-bar {
display: flex;
background-color: #fff;
height: 48px;
}
.tab {
flex: 1 1 0;
display: flex;
align-items: center;
justify-content: center;
max-width: 200px;
min-width: 100px;
padding: 0 16px;
border-bottom: solid 1px rgba(0, 0, 0, .12);
}
.tab-bar::after {
content: '';
flex: 1;
border-bottom: solid 1px rgba(0, 0, 0, .12);
}
.tab[data-state="active"] {
box-shadow: 0 -2px 4px -1px rgba(0, 0, 0, 0.2), 0 -4px 5px 0 rgba(0, 0, 0, 0.14), 0 -1px 1px 0 rgba(0, 0, 0, 0.12);
color: red;
border-bottom: none;
}
<div class="container">
<div class="tab-bar">
<div class="tab" data-state="active">Active</div>
<div class="tab">Not Active</div>
</div>
</div>
You should overflow: hidden from .tab-bar.
But after you remove it. It seems not to be as you wanted ("the same level of the content").
So I edited box-shadow of active tab. to make it with no box-shadowfrom button.
border-bottom: 1px solid white is responsible to override tab-bar border
See Full Example here:
.container {
padding: 30px;
}
.tab-bar {
display: flex;
background-color: #fff;
border-bottom: solid 1px rgba(0,0,0, .12);
height: 48px;
}
.tab {
flex: 1 1 0;
display: flex;
align-items: center;
justify-content: center;
height: 48px;
max-width: 200px;
min-width: 100px;
padding: 0 16px;
}
.tab[data-state="active"] {
box-shadow: -4px -2px 4px -1px rgba(0,0,0,0.1), 4px -2px 4px -1px rgba(0,0,0,0.1);
color: red;
border-bottom: 1px solid white;
}
<div class="container">
<div class="tab-bar">
<div class="tab" data-state="active">Active</div>
<div class="tab">Not Active</div>
</div>
</div>
I don't really know how to put words on that but if you look at this fiddle, I'd like the "small value" line to be vertically centered in its space.
What I call its space is what's not occupied by "Title". I tried to set its line-height to 3em but it didn't help.
HTML
<div id="parent">
<div class="child">
<div class="title">
Title
</div>
<div class="value big">
Big value
</div>
</div>
<div class="child">
<div class="title">
Title
</div>
<div class="value">
Small value
</div>
</div>
</div>
CSS
#parent {
box-sizing: border-box;
max-height: 100%;
display: flex;
flex-direction: row;
place-content: stretch center;
align-items: stretch;
}
.child {
margin: 0 10px;
box-sizing: border-box;
padding: 10px;
flex: 0 0 auto;
box-sizing: border-box;
max-width: none;
box-shadow: 0px 2px 1px -1px rgba(0, 0, 0, 0.2), 0px 1px 1px 0px rgba(0, 0, 0, 0.14), 0px 1px 3px 0px rgba(0, 0, 0, 0.12);
}
.child .title {
font-size: 1.5em;
color: grey;
}
.child .value {
font-size: 1.5em;
color: black;
}
.value.big {
font-size: 3em;
}
You can make .child a flex container.
Then to center the value, add margin: auto to .value
fiddle
#parent {
box-sizing: border-box;
max-height: 100%;
display: flex;
flex-direction: row;
place-content: stretch center;
align-items: stretch;
}
.child {
margin: 0 10px;
box-sizing: border-box;
padding: 10px;
flex: 0 0 auto;
box-sizing: border-box;
max-width: none;
box-shadow: 0px 2px 1px -1px rgba(0, 0, 0, 0.2), 0px 1px 1px 0px rgba(0, 0, 0, 0.14), 0px 1px 3px 0px rgba(0, 0, 0, 0.12);
display: flex;
flex-direction: column;
}
.child .title {
font-size: 1.5em;
color: grey;
}
.child .value {
font-size: 1.5em;
color: black;
margin: auto;
}
.value.big {
font-size: 3em;
}
<div id="parent">
<div class="child">
<div class="title">
Title
</div>
<div class="value big">
Big value
</div>
</div>
<div class="child">
<div class="title">
Title
</div>
<div class="value">
Small value
</div>
</div>
</div>
I have a container with three col-md-4 inside. Each column has a card layout. Everything is displaying just fine. However, if the window is resized under 992px and the columns get stacked, they float to left. At first i used:
<div align="center">
but it is not so HTML5. Any other solution did not help.
Here's all i got right now:
<div class="categories">
<div class="container">
<div class="row">
<!-- 1st card -->
<div class="col-md-4">
<div class="card">
<div class="cardoverlay">
<div class="line">
<hr>
<i class="fa lifering"></i>
<hr>
</div>
</div>
</div>
</div>
<!-- 2nd card -->
<div class="col-md-4">
<div class="card">
<div class="cardoverlay">
<div class="line">
<hr>
<i class="fa cogs"></i>
<hr>
</div>
</div>
</div>
</div>
<!-- 3rd card -->
<div class="col-md-4">
<div class="card">
<div class="cardoverlay">
<div class="line">
<hr>
<i class="fa cubes"></i>
<hr>
</div>
</div>
</div>
</div>
</div>`
.categories {
background-color: #e1e1e1;
height: auto;
text-align: center;
}
.card {
max-width: 353px;
height: 662px;
-webkit-border-radius: 5px/7px;
-moz-border-radius: 5px/7px;
border-radius: 8px/10px;
background-color: #fff;
-webkit-box-shadow: 0 2px #b8b3b3;
-moz-box-shadow: 0 2px #b8b3b3;
box-shadow: 3px #b8b3b3;
margin-top: 50px;
margin-bottom: 50px;
}
.line {
display: flex;
width: 100%;
align-items: center;
justify-content: center;
}
hr {
border: 0;
border-top: 1px solid #c2c6c7;
flex: 1;
z-index: 1;
}
.lifering:after {
font-size: 90px;
color: #d67676;
padding: 20px;
content: '\f1cd';
display: inline-block;
text-shadow: 0.7px -1px 0.7px #450c04;
}
.cogs:after {
font-size: 90px;
color: #329fdd;
padding: 20px;
content: '\f085';
display: inline-block;
text-shadow: 0.7px -1px 0.7px #000000;
}
.cubes:after {
font-size: 90px;
color: #36d7b7;
padding: 20px;
content: '\f1b3';
display: inline-block;
text-shadow: 0.7px -1px 0.7px #000000;
}
.cardoverlay {
max-width: 353px;
height: 202px;
-webkit-border-radius: 5px 5px 0 0/10px 10px 0 0;
-moz-border-radius: 5px 5px 0 0/10px 10px 0 0;
border-radius: 5px 5px 0 0/10px 10px 0 0 #f1f1f1;
background-color: #f9f9f9;
-webkit-box-shadow: 0 3px #f1f1f1;
-moz-box-shadow: 0 3px #f1f1f1;
box-shadow: 0 3px #f1f1f1;
}
Fiddle
If you add margin:50px auto; to .card in your CSS it will center align the cards. 50px will remain the margin on the top and bottom of the card and auto works to center align. New card styles would look like this:
.card {
max-width: 353px;
height: 662px;
-webkit-border-radius: 5px/7px;
-moz-border-radius: 5px/7px;
border-radius: 8px/10px;
background-color: #fff;
-webkit-box-shadow: 0 2px #b8b3b3;
-moz-box-shadow: 0 2px #b8b3b3;
box-shadow: 3px #b8b3b3;
margin:50px auto;
}