Given an html structure that follows the schema in this snippet, how can I fully collapse the empty "col" divs, leaving the content divs equally sized (two columns in this case)?
* {
box-sizing: border-box;
}
.container {
border: 1px dashed rgba(255, 0, 0, .5);
background-color: beige;
padding: 20px 10px;
width: 500px;
}
.row {
border: 1px dashed rgba(0, 0, 255, .5);
display: flex;
}
.col {
flex: 1 1 auto;
min-width: 0;
}
.content {
border: 1px dashed rgba(0, 0, 0, .25);
padding: 0 10px;
width: 100%;
}
<section class="container">
<div class="row">
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
<div class="col">
<p class="content">Foo</p>
</div>
<div class="col"></div>
<div class="col">
<p class="content">Bar</p>
</div>
<div class="col"></div>
</div>
</section>
You can select the empty elements with the .col class by using the :empty pseudo class.
I've chosen to change the flex attribute on the, so they will shrink, but you can also set display: none, or force max-width: 0, etc...
* {
box-sizing: border-box;
}
.container {
border: 1px dashed rgba(255, 0, 0, .5);
background-color: beige;
padding: 20px 10px;
width: 500px;
}
.row {
border: 1px dashed rgba(0, 0, 255, .5);
display: flex;
}
.col {
flex: 1 1 auto;
min-width: 0;
}
.col:empty {
flex: 0 1 0;
}
.content {
border: 1px dashed rgba(0, 0, 0, .25);
padding: 0 10px;
width: 100%;
}
<section class="container">
<div class="row">
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
<div class="col">
<p class="content">Foo</p>
</div>
<div class="col"></div>
<div class="col">
<p class="content">Bar</p>
</div>
<div class="col"></div>
</div>
</section>
I was able to resolve the issue by setting the content to a nuclear width of 100vw with a max-width of 100%. The caveat is that this is not compatible with any version of IE.
* {
box-sizing: border-box;
}
.container {
border: 1px dashed rgba(255, 0, 0, .5);
background-color: beige;
padding: 20px 10px;
width: 500px;
}
.row {
border: 1px dashed rgba(0, 0, 255, .5);
display: flex;
}
.col {
flex: 1 1 auto;
min-width: 0;
}
.content {
border: 1px dashed rgba(0, 0, 0, .25);
padding: 0 10px;
width: 100vw;
max-width: 100%;
}
<section class="container">
<div class="row">
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
<div class="col">
<p class="content">Foo</p>
</div>
<div class="col"></div>
<div class="col">
<p class="content">Bar</p>
</div>
<div class="col"></div>
</div>
</section>
Another solution is to use good-old table display props if IE support is needed.
* {
box-sizing: border-box;
}
.container {
border: 1px dashed rgba(255, 0, 0, .5);
background-color: beige;
padding: 20px 10px;
width: 500px;
display: table;
}
.row {
border: 1px dashed rgba(0, 0, 255, .5);
display: table-row;
}
.col {
display: table-cell;
min-width: 0;
}
.content {
border: 1px dashed rgba(0, 0, 0, .25);
padding: 0 10px;
width: 100%;
}
<section class="container">
<div class="row">
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
<div class="col">
<div class="content">Foo</div>
</div>
<div class="col"></div>
<div class="col">
<div class="content">Bar</div>
</div>
<div class="col"></div>
</div>
</section>
Related
I am trying to create a layout for a virtual table.
A virtual table will be placed inside .table-body element.
Each table cell has the same flex layout styles as cells in .table-header so with the same parent element with they would look the same.
The problem is that the .table-cell elements do not stretch .table-header width, and .table-header element does not stretch the .table-container width.
I'm trying to get the .table-cell's to give the width for the .table-header and .table-container. And the .table-body would take up the remaining space in the .table-container
Here is Codesandbox to play with.
.table-wrapper {
width: 500px;
height: 300px;
overflow-x: scroll;
background: rgba(255, 255, 0, 0.2);
}
.table-container {
display: flex;
flex-flow: column nowrap;
width: 100%;
height: 100%;
/* If you uncomment next line, you see what I'm trying to achieve */
/* min-width: 1100px; */
background: rgba(0, 255, 0, 0.3);
}
.table-header {
display: flex;
flex-flow: row nowrap;
width: 100%;
flex: 0 0 auto;
background: rgba(255, 100, 0, 0.3);
}
.table-cell {
min-width: 100px;
display: flex;
flex: 1 1 auto;
height: 30px;
line-height: 30px;
border: 1px solid blue;
}
.table-body {
display: flex;
flex: 1 1 auto;
border: 1px solid red;
background: rgba(255, 0, 0, 0.3);
}
<div class="table-wrapper">
<div class="table-container">
<div class="table-header">
<div class="table-cell" style="flex: 0 0 180px;">header-cell 1</div>
<div class="table-cell">header-cell 2</div>
<div class="table-cell">header-cell 3</div>
<div class="table-cell">header-cell 4</div>
<div class="table-cell" style="flex: 0 0 200px;">header-cell 5</div>
<div class="table-cell">header-cell 6</div>
<div class="table-cell">header-cell 7</div>
</div>
<div class="table-body">virtual table here</div>
</div>
</div>
Consider the use of inline-flex instead of flex and define the width using width and not flex-basis
.table-wrapper {
width: 500px;
height: 300px;
overflow-x: scroll;
background: rgba(255, 255, 0, 0.2);
}
.table-container {
display: inline-flex; /* UPDATED */
flex-flow: column nowrap;
min-width: 100%; /* UPDATED */
height: 100%;
background: rgba(0, 255, 0, 0.3);
}
.table-header {
display: flex;
flex-flow: row nowrap;
width: 100%;
flex: 0 0 auto;
background: rgba(255, 100, 0, 0.3);
}
.table-cell {
min-width: 100px;
display: flex;
flex: 1 1 auto;
height: 30px;
line-height: 30px;
border: 1px solid blue;
}
.table-body {
display: flex;
flex: 1 1 auto;
border: 1px solid red;
background: rgba(255, 0, 0, 0.3);
}
<div class="table-wrapper">
<div class="table-container">
<div class="table-header">
<div class="table-cell" style="flex: 0 0 180px;width:180px;">header-cell 1</div>
<div class="table-cell">header-cell 2</div>
<div class="table-cell">header-cell 3</div>
<div class="table-cell">header-cell 4</div>
<div class="table-cell" style="flex: 0 0 200px;width:200px;">header-cell 5</div>
<div class="table-cell">header-cell 6</div>
<div class="table-cell">header-cell 7</div>
</div>
<div class="table-body">virtual table here</div>
</div>
</div>
Here's a workaround I found, use width: min-content; for both table-header and table-container.
This only work when you get rid of the inline style in the table-cell.
.table-wrapper {
width: 500px;
height: 300px;
overflow-x: scroll;
background: rgba(255, 255, 0, 0.2);
}
.table-container {
display: flex;
flex-flow: column nowrap;
width: min-content;
height: 100%;
/* If you uncomment next line, you see what I'm trying to achieve */
/* min-width: 1100px; */
background: rgba(0, 255, 0, 0.3);
}
.table-header {
display: flex;
flex-flow: row nowrap;
width: min-content;
flex: 0 0 auto;
background: rgba(255, 100, 0, 0.3);
}
.table-cell {
min-width: 100px;
display: flex;
flex: 1 1 auto;
height: 30px;
line-height: 30px;
border: 1px solid blue;
}
.table-body {
display: flex;
flex: 1 1 auto;
border: 1px solid red;
background: rgba(255, 0, 0, 0.3);
}
<div class="table-wrapper">
<div class="table-container">
<div class="table-header">
<div class="table-cell">header-cell 1</div>
<div class="table-cell">header-cell 2</div>
<div class="table-cell">header-cell 3</div>
<div class="table-cell">header-cell 4</div>
<div class="table-cell" >header-cell 5</div>
<div class="table-cell">header-cell 6</div>
<div class="table-cell">header-cell 7</div>
</div>
<div class="table-body">virtual table here</div>
</div>
</div>
How can I use css to make sure the following code can display as 1, 2, or 4 columns but never as 3 columns?
.container {
display: flex;
flex-wrap: wrap;
border: 3px solid rgb(0, 0, 0);
}
.panel {
height: 100px;
width: 100px;
border: 3px solid rgb(255, 0, 0);
margin: 10px 0px 0px 10px;
}
<div class="container">
<div class="panel"></div>
<div class="panel"></div>
<div class="panel"></div>
<div class="panel"></div>
</div>
So it's all about the width since you are using flex-box. if you want an item to take the entire row give the container width of 100% and if you want 2 items next to each other give it width:50% if you want 4 items then make sure to give it 25% of the width.
I made this responsive example I used the names bootstrap uses to make it easier to understand in case you are using bootstrap.
Note that you should work inside the div with class wrap.
.container {
display: flex;
flex-wrap: wrap;
outline: 3px solid rgb(0, 0, 0);
}
.panel {
height: 100px;
outline: 2px solid rgb(255, 0, 0);
background-color: green;
}
.wrap {
margin: 0 1rem;
height: 100%;
background-color: blueviolet;
color: white;
}
.col-12 {
width: 100%;
}
#media (min-width: 768px) {
.col-mid-6 {
width: 50%;
}
}
#media (min-width: 1200px) {
.col-xl-4 {
width: 25%;
}
}
<div class="container">
<div class="panel col-12 col-mid-6 col-xl-4">
<div class="wrap">div1</div>
</div>
<div class="panel col-12 col-mid-6 col-xl-4">
<div class="wrap">div2</div>
</div>
<div class="panel col-12 col-mid-6 col-xl-4">
<div class="wrap">div3</div>
</div>
<div class="panel col-12 col-mid-6 col-xl-4">
<div class="wrap">div4</div>
</div>
</div>
if that didn't help let me know.
I want the max width of the "circlecontainer" to be 300px.
but when the browser exceeds 1200px in width, I want my 4 child divs (with max width 300px) to stay centered in the browser.
They are currently positioned on the left side.
Here is my html and css.
any tips would be appreciated :)
.getstartedcirclescontainer {
height: 650px;
width: 100%;
display: flex;
text-align: center;
background-image: linear-gradient(#3329ff, white);
}
.circlecontainer {
height: 100%;
width: 24.9%;
padding-top: 175px;
background-color: rgba(0, 0, 0, 0.1);
max-width: 300px;
}
.circle1 {
height: 170px;
width: 170px;
border-radius: 50%;
background-image: linear-gradient(#8680ff, #5a52ff);
margin: 0 auto;
border-style: solid;
border-width: 2px;
border-color: #fff;
box-shadow: 0 10px 20px 0 rgba(0, 0, 0, .66);
}
<div class="getstartedcirclescontainer">
<div class="circlecontainer">
<div class="circle1">
</div>
</div>
<div class="circlecontainer">
<div class="circle1">
</div>
</div>
<div class="circlecontainer">
<div class="circle1">
</div>
</div>
<div class="circlecontainer">
<div class="circle1">
</div>
</div>
</div>
Just replace text-align: center; with justify-content: center; for .getstartedcirclescontainer.
HTML:
<div class="getstartedcirclescontainer">
<div class="circlecontainer">
<div class="circle1">
</div>
</div>
<div class="circlecontainer">
<div class="circle1">
</div>
</div>
<div class="circlecontainer">
<div class="circle1">
</div>
</div>
<div class="circlecontainer">
<div class="circle1">
</div>
</div>
</div>
CSS:
.getstartedcirclescontainer {
height: 650px;
width: 100%;
display: flex;
justify-content: center;
background-image: linear-gradient(#3329ff, white);
}
.circlecontainer {
height: 100%;
width: 24.9%;
padding-top: 175px;
background-color: rgba(0, 0, 0, 0.1);
max-width: 300px;
}
.circle1 {
height: 170px;
width: 170px;
border-radius: 50%;
background-image: linear-gradient(#8680ff, #5a52ff);
margin: 0 auto;
border-style: solid;
border-width: 2px;
border-color: #fff;
box-shadow: 0 10px 20px 0 rgba(0,0,0,.66);
}
Hello I'm pretty new to this & I'm trying to figure out how to scale and clip these images to fit into each grid square without having a border...
I also don't know if this is an effective way to set up my images. I'd like to have a different image for each square, but how it's set up now I'd have to make a new .box .inner# for each one. If there is a better way to structure this that'd be really helpful.
.grid {
margin: 0 auto;
width: 240vw;
max-width: 200vh;
height: 240vw;
max-height: 200vh;
font-size: 2rem;
}
.row {
display: flex;
}
.box {
background: red;
margin: 5px;
color: white;
font-weight: bold;
flex: 1 0 auto;
position: relative;
}
.box:after {
content: "";
float: left;
display: block;
padding-top: 100%;
}
.box .inner1 {
background-image: url("https://c1.staticflickr.com/2/1763/29556413048_164120ccb5_b.jpg");
position: absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
display: flex;
align-items: center;
justify-content: center;
text-shadow: 0px 0px 8px rgb(36, 36, 36), 0px 0px 20px rgba(0, 0, 0, 0.9);
}
.box .inner2 {
background-image: url("https://c1.staticflickr.com/1/922/43509246041_043aff0334_h.jpg");
position: absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
display: flex;
align-items: center;
justify-content: center;
text-shadow: 0px 0px 8px rgb(36, 36, 36), 0px 0px 20px rgba(0, 0, 0, 0.9);
}
<div class="grid">
<div class="row">
<div class="box">
<div class="inner1">1</div>
</div>
<div class="box">
<div class="inner1">2</div>
</div>
<div class="box">
<div class="inner1">3</div>
</div>
<div class="box">
<div class="inner1">4</div>
</div>
</div>
<div class="row">
<div class="box">
<div class="inner2">5</div>
</div>
<div class="box">
<div class="inner2">6</div>
</div>
<div class="box">
<div class="inner2">7</div>
</div>
<div class="box">
<div class="inner2">8</div>
</div>
</div>
</div>
You might do it like this:
.grid {
margin: 0 auto;
width: 240vw;
max-width: 200vh;
height: 240vw;
max-height: 200vh;
font-size: 2rem;
}
.row {
display: flex;
}
.box {
background: red;
margin: 5px;
color: white;
font-weight: bold;
flex: 1 0 auto;
position: relative;
}
.box:after {
content: "";
float: left;
display: block;
padding-top: 100%;
}
.box > div {
background-size:cover;
position: absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
display: flex;
align-items: center;
justify-content: center;
text-shadow: 0px 0px 8px rgb(36, 36, 36), 0px 0px 20px rgba(0, 0, 0, 0.9);
}
.inner1 {
background-image: url("https://c1.staticflickr.com/2/1763/29556413048_164120ccb5_b.jpg");
}
.inner2 {
background-image: url("https://c1.staticflickr.com/1/922/43509246041_043aff0334_h.jpg");
}
.inner3 {
background-image: url("https://picsum.photos/200/200?3");
}
.inner4 {
background-image: url("https://picsum.photos/200/200?4");
}
.inner5 {
background-image: url("https://picsum.photos/200/300?5");
}
.inner6 {
background-image: url("https://picsum.photos/200/300?6");
}
.inner7 {
background-image: url("https://picsum.photos/200/200?7");
}
.inner8 {
background-image: url("https://picsum.photos/200/300?8");
}
<div class="grid">
<div class="row">
<div class="box">
<div class="inner1">1</div>
</div>
<div class="box">
<div class="inner2">2</div>
</div>
<div class="box">
<div class="inner3">3</div>
</div>
<div class="box">
<div class="inner4">4</div>
</div>
</div>
<div class="row">
<div class="box">
<div class="inner5">5</div>
</div>
<div class="box">
<div class="inner6">6</div>
</div>
<div class="box">
<div class="inner7">7</div>
</div>
<div class="box">
<div class="inner8">8</div>
</div>
</div>
</div>
I am not fully sure if this will fix your problem but maybe take off the margin in your .box class in your css file.
not enough reputation yet so click this link
Instead of having your margin at 5px change it to 0px and see if that helps.
As for the different images you just need to create a new class for each image an link a new image to that class, go back and link it up like you have with the two previous.
How can I horizontally and vertically center all the contents of a jumbotron in bootstrap 3? I have just started learning it.
The following is my code:
HTML
<div class="site-wrapper">
<div class="jumbotron">
<h1>Header</h1>
<p id="subHeader"><small>sub header</small></p>
<div class="container">
<div class="row">
<div class="col-sm-5 col-sm-offset-2">
</div>
<div class="col-sm-3 lefty">
</div>
</div>
<div class="row">
<div class="col-sm-4 col-sm-offset-4">
</div>
</div>
</div>
</div>
</div>
CSS
html, body {
height: 100%;
background-color: #333;
}
.site-wrapper {
width: 100%;
height: 100%;
min-height: 100%;
-webkit-box-shadow: inset 0 0 100px rgba(0,0,0,.5);
box-shadow: inset 0 0 100px rgba(0,0,0,.5);
border-top: 5px solid #5A332B;
}
.jumbotron {
background-color: #ccbb99 !important;
/*background: linear-gradient(rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.9));*/
height: 100%;
margin-bottom: 0;
}
.lefty {
margin: 0;
}
You can use transform: translateY to vertical align the content like this..
.centerme {
position: relative;
top: 50%;
transform: translateY(-50%);
}
http://www.bootply.com/MlRejPA7m2
Use text-center to horizontal align
use the CSS code to achieve this:
.jumbotron{disply:flex;
align-content:center;
justify-items:center;
align-items:center;
}