How to create a layout using flex? - html

I'm trying to create the following layout for one of my web projects. What's the best way to create that using HTML, CSS (Flex)?
I'm struggling with bottom-margin for C & D in Desktop screens, for D in Mobile screens.

As we don't have your code, so based on your question here i provide solution let me know if you have any question..
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-md-6 mb-5">
<div class="card p-5">
A
</div>
</div>
<div class="col-md-6 mb-5">
<div class="card p-5">
B
</div>
</div>
<div class="col-md-6 mb-5">
<div class="card p-5">
C
</div>
</div>
<div class="col-md-6 mb-5">
<div class="card p-5">
D
</div>
</div>
</div>
</div>

You can do it like this.
.main_container {
display: grid;
grid-template-columns: auto auto;
grid-row-gap: 30px;
grid-column-gap: 30px;
}
.sections {
height: 200px;
text-align: center;
background-color: aqua;
}
/*here for mobiles inside mobile media query put this*/
.main_container {
display: grid;
grid-template-columns: auto;
grid-row-gap: 30px;
grid-column-gap: 30px;
}
<div class="main_container">
<div class="sections">A</div>
<div class="sections">B</div>
<div class="sections">C</div>
<div class="sections">D</div>
</div>

<div class="row">
<div class="col-md-6 col-lg-6 col-xl-6 col-sm-12 col-xs-12">
<p>Write text here A</p>
</div>
<div class="col-md-6 col-lg-6 col-xl-6 col-sm-12 col-xs-12">
<p>Write text here B</p>
</div>
</div>
<div class="cs-t20">
<!-- spacer -->
</div>
</div>
<div class="row">
<div class="col-md-6 col-lg-6 col-xl-6 col-sm-12 col-xs-12">
<p>Write text here C</p>
</div>
<div class="col-md-6 col-lg-6 col-xl-6 col-sm-12 col-xs-12">
<p>Write text here D</p>
</div>
</div>
<div class="cs-t20">
<!-- spacer -->
</div>
</div>
<style>
.cs-t20{
width: 100%;
height: 30px;
}
</style>
Hope it will help you!! All the Best !!

here your go..
.cs-t20 {
width: 100%;
height: 30px;
}
.continer {
width: 100%;
height: 100%;
}
.webpage-page-display {
float: none;
display: table-cell;
}
.webpage-page-column {
z-index: 1;
float: left;
position: relative;
min-height: 1px;
}
.webpage-page-half {
margin-left: 6%;
width: 47%;
}
.webpage-page-first {
margin-left: 0px;
clear: left;
}
.webpage-page-half {
width: 100%;
}
.webpage-page-column {
margin: 0;
margin-bottom: 20px;
width: 100%;
}
#media only screen and (min-width: 320px) and (max-width: 767px) {
.webpage-page_column_table_cell {
display: block;
}
.webpage-page-half {
width: 100%;
}
.webpage-page-column {
margin: 0;
margin-bottom: 20px;
width: 100%;
}
}
<div id="webpage_section">
<div class="container">
<div class="webpage-page-column webpage-page_column_table_cell webpage-page-first webpage-page-half webpage-page-display">
<p>Write text here A</p>
</div>
<div class="webpage-page-column webpage-page_column_table_cell webpage-page-half webpage-page-display">
<p>Write text here B</p>
</div>
<div class="cs-t20"></div>
</div>
</div>
<div id="webpage_section">
<div class="container">
<div class="webpage-page-column webpage-page_column_table_cell webpage-page-first webpage-page-half webpage-page-display">
<p>Write text here C</p>
</div>
<div class="webpage-page-column webpage-page_column_table_cell webpage-page-half webpage-page-display">
<p>Write text here D</p>
</div>
<div class="cs-t20"></div>
</div>
</div>

Related

Putting a button in the bottom-right of a page

How can I put this View More button in the bottom-right of the container? I am having a hard time on putting the view more button on the bottom right of the 'back' div.
.back {
border: #6ACEBC 4px solid;
margin: 0 20px;
border-radius: 15px;
max-width: 100%!important;
min-height: 200px;
}
.content {
max-width: 60%;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.1/dist/css/bootstrap.min.css" rel="stylesheet" />
<div class="back">
<div class="content">
<div class="row ps-3 text-center">
<div class="col">Order #</div>
<div class="col">Order Name</div>
<div class="col">Quantity</div>
<div class="col">Price per pcs</div>
<div class="col">Total Price</div>
</div>
<div class="row ps-3 text-center">
<div class="col">3</div>
<div class="col">Pencil</div>
<div class="col">5</div>
<div class="col">10</div>
<div class="col">50</div>
</div>
<div class="float-end">
<button>VIEW MORE</button>
</div>
</div>
</div>
Use absolute positioning to place the button on the bottom right of the container. The container needs to be position: relative; to get the context of the button in the right place.
.back {
border: #6ACEBC 4px solid;
margin: 0 20px;
border-radius: 15px;
max-width: 100%!important;
min-height: 200px;
background-color: darkorange;
position: relative;
}
.content {
max-width: 60%;
}
button {
position: absolute;
bottom: 10px;
right: 10px;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.1/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class = "back">
<div class="content">
<div class="row ps-3 text-center">
<div class="col">Order #</div>
<div class="col">Order Name</div>
<div class="col">Quantity</div>
<div class="col">Price per pcs</div>
<div class="col">Total Price</div>
</div>
<div class="row ps-3 text-center">
<div class="col">3</div>
<div class="col">Pencil</div>
<div class="col">5</div>
<div class="col">10</div>
<div class="col">50</div>
</div>
<div class = "float-end">
<button>VIEW MORE</button>
</div>
</div>
</div>
You can use position: absolute. Create a new containing block with position: relative on the parent however this takes the button outside of the flow so you may end up with other elements clashing with it (e.g. the button appearing over text content).
.back {
border: #6ACEBC 4px solid;
margin: 0 20px;
border-radius: 15px;
max-width: 100%!important;
min-height: 200px;
position: relative; /* create new containing block */
}
.content {
max-width: 60%;
}
.view-more {
position: absolute;
bottom: 1rem;
right: 1rem;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.1/dist/css/bootstrap.min.css" rel="stylesheet" />
<div class="back">
<div class="content">
<div class="row ps-3 text-center">
<div class="col">Order #</div>
<div class="col">Order Name</div>
<div class="col">Quantity</div>
<div class="col">Price per pcs</div>
<div class="col">Total Price</div>
</div>
<div class="row ps-3 text-center">
<div class="col">3</div>
<div class="col">Pencil</div>
<div class="col">5</div>
<div class="col">10</div>
<div class="col">50</div>
</div>
<div class="view-more">
<button>VIEW MORE</button>
</div>
</div>
</div>
The other way is to move your view-more div outside the content div and then use display: flex to move it to the bottom right hand corner. We make sure the div fills the parent div. This will always make sure the table above and the button do not clash.
.back {
border: #6ACEBC 4px solid;
margin: 0 20px;
border-radius: 15px;
max-width: 100%!important;
min-height: 200px;
display: flex; /* make this a flex container */
flex-direction: column; /* make the flex divs stack on top of each other rather than side by side */
}
.content {
max-width: 60%;
}
.view-more {
flex-grow: 1; /* make the bottom div grow to fill the container */
display: flex; /* make this a flex container also */
justify-content:flex-end; /* put the button to the right hand side of the parent div */
align-items: flex-end; /* put the button at the bottom of the div */
padding:0.5rem; /* put a bit of padding round it to put some space between the green border and the button */
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.1/dist/css/bootstrap.min.css" rel="stylesheet" />
<div class="back">
<div class="content">
<div class="row ps-3 text-center">
<div class="col">Order #</div>
<div class="col">Order Name</div>
<div class="col">Quantity</div>
<div class="col">Price per pcs</div>
<div class="col">Total Price</div>
</div>
<div class="row ps-3 text-center">
<div class="col">3</div>
<div class="col">Pencil</div>
<div class="col">5</div>
<div class="col">10</div>
<div class="col">50</div>
</div>
</div>
<div class="view-more">
<button>VIEW MORE</button>
</div>
</div>
Have you tried adding "position: relative" to the container and then positioning the button absolutely?
.back {
border: #6ACEBC 4px solid;
margin: 0 20px;
border-radius: 15px;
max-width: 100%!important;
min-height: 200px;
position: relative;
}
.content {
max-width: 60%;
font-size: 5px;
}
.float-end {
position: absolute;
right: 0;
bottom: 0;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.1/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class ="back">
<div class="content">
<div class="row ps-3 text-center">
<div class="col">Order #</div>
<div class="col">Order Name</div>
<div class="col">Quantity</div>
<div class="col">Price per pcs</div>
<div class="col">Total Price</div>
</div>
<div class="row ps-3 text-center">
<div class="col">3</div>
<div class="col">Pencil</div>
<div class="col">5</div>
<div class="col">10</div>
<div class="col">50</div>
</div>
</div>
<div class="float-end">
<button>VIEW MORE</button>
</div>
</div>

Alignment image

I'm working on a site which needs an image centered in the div on the left side, centered off the right div with a background.
I tried to a common solution, but for some reason, it won't work. It must be responsive as well, so I'm a bit in the dark over here. See my fiddle for the example.
<div class="container-fluid">
<div class="row">
<div class="col-12 col-sm-6">
<div class="leftSide">
<img src="https://images.pexels.com/photos/3755760/pexels-photo-3755760.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260" class="img-fluid imgLeft">
</div>
</div>
<div class="col-12 col-sm-6">
<div class="rightSide">
<h3>
Some text
</h3>
<p>
Some more text
</p>
<button type="button" class="btn btn-primary">Some text</button>
</div>
</div>
https://jsfiddle.net/Onno0297/grvwxby2/1/
If I understand your question correctly then below code will work.
Visit https://jsfiddle.net/alishapatel/n3ap1vjo/4/!
.leftSide {
height: 100%;
display: flex;
align-items: center;
}
.rightSide {
background: #313131;
height: 546px;
color: #fff;
}
.rightSide h3 {
text-align: center;
padding-top: 200px;
}
.rightSide p {
text-align: center;
}
.btn.btn-primary {
margin: 0 auto;
display: block;
}
.imgLeft {
max-height: 400px;
max-width: 300px;
margin: 0 auto;
width: 100%;
}
<div class="container-fluid">
<div class="row">
<div class="col-12 col-sm-6">
<div class="leftSide">
<img src="https://images.pexels.com/photos/3755760/pexels-photo-3755760.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260" class="img-fluid imgLeft">
</div>
</div>
<div class="col-12 col-sm-6">
<div class="rightSide">
<h3>
Some text
</h3>
<p>
Some more text
</p>
<button type="button" class="btn btn-primary">Some text</button>
</div>
</div>
</div>
</div>

boostrap 4 Equal heights not working

What is the way of making div's have equal heights within the row?
it says everywhere that bootstrap4 is flex by default, but it doesn't work for some unknown reason
<div class="container-fluid">
<div class="row">
<div class="col-12">
<div class="content" >
<div class="sub_title"> HISTORY </div>
<div class="desc"> Sometext </div>
</div>
</div>
</div>
<div class="row">
<div class="col-12 col-sm-6 col-md-6 col-lg">
<div class="content">
<div class="sub_title">
<div class="number"> 2012 </div>
</div>
<div class="desc"> some other text </div>
</div>
</div>
<div class="col-12 col-sm-6 col-md-6 col-lg ">
<div class="content">
<div class="sub_title">
<div class="number"> 2014 </div>
</div>
<div class="desc"> More text </div>
</div>
</div>
<div class="col-12 col-sm-6 col-md-6 col-lg ">
<div class="content">
<div class="sub_title">
<div class="number"> 2017 </div>
</div>
<div class="desc"> Text here too </div>
</div>
</div>
</div>
the scss
.content {
#extend .effect8;
background: white;
margin: 10px;
padding: 10px 30px 20px;
span {
display: block;
p {
font-weight: bold;
}
}
}
.sub_title {
font-size: 1.5em;
font-weight: 700;
text-transform: uppercase;
display: block;
color: $blue;
margin: 10px auto;
}
this is the scss for the content and the sub_title class
If you want .content to be full height on each .col, the content should have height:100%;. I don't know the CSS def of .effect8.
.content {
background: white;
margin: 10px;
padding: 10px 30px 20px;
height: 100%;
span {
display: block;
p {
font-weight: bold;
}
}
}
https://www.codeply.com/go/B4g6TlSDkA
Actually it depend of browser, e.g In Safari it may not work, and also in Internet Explorer may have unwanted behaviors.
If you're really want equal and avoid cross browser issue try JS solution e.g http://brm.io/jquery-match-height/

Metro grid style

I'm trying to achieve something like the following page:
https://undsgn.com/uncode/homepages/blog-metro/
I have tried and was able to come as close as this: https://jsfiddle.net/futu7t1c/
But how can I get those 2 small-thumbs at the bottom to move up?
The order is big, small, small, big, small, small
<div id="blog-posts">
<div class="grid big-thumb">
Title
</div>
<div class="grid small-thumb">
Title
</div>
<div class="grid small-thumb">
Title
</div>
<div class="grid big-thumb">
Title
</div>
<div class="grid small-thumb">
Title
</div>
<div class="grid small-thumb">
Title
</div>
</div>
css
#blog-posts {
-moz-column-count: 1;
-webkit-column-count: 1;
column-count: 1;
-moz-column-gap: 0em;
-webkit-column-gap: 0em;
column-gap: 0em;
}
.grid {
background: #eee;
float: left;
position: relative;
color: #fff;
}
.big-thumb {
width: 50%;
height: 600px;
background: #aeaeae;
}
.small-thumb {
width: 25%;
height: 300px;
background: #353535;
}
To replicate that grid, you can make flex rows that have flex children that are also flex columns holding your images.
.flex {
display: flex;
}
.col {
flex-direction: column;
flex: 0 0 50%;
}
img {
max-width: 100%;
vertical-align: top;
}
<div class="flex row">
<div class="flex col">
<div class="big">
<img src="http://1.bp.blogspot.com/-hNC-oT6f-fY/TeXxO26yjvI/AAAAAAAAAOY/qfkOqdKkBi8/s1600/platon-photographer-putin-man-of-the-year-portrait.jpg">
</div>
<div class="flex row">
<div class="small">
<img src="https://i.stack.imgur.com/2C22p.jpg">
</div>
<div class="small">
<img src="https://i.stack.imgur.com/2C22p.jpg">
</div>
</div>
</div>
<div class="flex col">
<div class="flex row">
<div class="small">
<img src="https://i.stack.imgur.com/2C22p.jpg">
</div>
<div class="small">
<img src="https://i.stack.imgur.com/2C22p.jpg">
</div>
</div>
<div class="big">
<img src="http://1.bp.blogspot.com/-hNC-oT6f-fY/TeXxO26yjvI/AAAAAAAAAOY/qfkOqdKkBi8/s1600/platon-photographer-putin-man-of-the-year-portrait.jpg">
</div>
</div>
Well, that's how you'd do it using regular ol' html/css. But since you want to just have a bunch of elements that automatagically lay out like that, use masonry
$('.masonry').masonry({
itemSelector: '.item',
columnWidth: '.small'
});
body {
margin: 0;
}
.item {
float: left;
}
.big {
width: 50%;
}
.small {
width: 25%;
}
img {
width: 100%;
vertical-align: top;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://unpkg.com/masonry-layout#4/dist/masonry.pkgd.min.js"></script>
<div class="masonry">
<div class="item big">
<img src="http://1.bp.blogspot.com/-hNC-oT6f-fY/TeXxO26yjvI/AAAAAAAAAOY/qfkOqdKkBi8/s1600/platon-photographer-putin-man-of-the-year-portrait.jpg">
</div>
<div class="item small">
<img src="https://i.stack.imgur.com/2C22p.jpg">
</div>
<div class="item small">
<img src="https://i.stack.imgur.com/2C22p.jpg">
</div>
<div class="item big">
<img src="http://1.bp.blogspot.com/-hNC-oT6f-fY/TeXxO26yjvI/AAAAAAAAAOY/qfkOqdKkBi8/s1600/platon-photographer-putin-man-of-the-year-portrait.jpg">
</div>
<div class="item small">
<img src="https://i.stack.imgur.com/2C22p.jpg">
</div>
<div class="item small">
<img src="https://i.stack.imgur.com/2C22p.jpg">
</div>
</div>

Bootstrap - 5 column layout

I'm trying to get 5 column full width layout but I can't find the solution that fits on my needs
Here's the code I use
<!-- Content Section -->
<div class="container">
<div class="row">
<div class="col-lg-12" style="border: 1px solid red">
<div class="row">
<div class="col-xs-12">
<div class="col-xs-2 col-xs-offset-1" id="p1">One</div>
<div class="col-xs-2" id="p2">Two</div>
<div class="col-xs-2" id="p3">Three</div>
<div class="col-xs-2" id="p4">Four</div>
<div class="col-xs-2" id="p5">Five</div>
</div>
<!-- //col-lg-12 -->
</div>
<!-- //row -->
lorem
</div>
</div>
<!-- //col-lg-12 -->
</div>
<!-- //row -->
</div>
<!-- //container -->
As a result I get
And here is what I'm trying to achieve. Full width 5 column layout with a space between each column
5 Columns with Bootstrap 4
Here is 5 equal, full-width columns (no extra CSS or SASS) using the auto-layout grid..
<div class="container-fluid">
<div class="row">
<div class="col">1</div>
<div class="col">2</div>
<div class="col">3</div>
<div class="col">4</div>
<div class="col">5</div>
</div>
</div>
http://codeply.com/go/MJTglTsq9h
This solution works because Bootstrap 4 is now flexbox. You can get the 5 colums to wrap within the same .row using a clearfix break such as <div class="col-12"></div> or <div class="w-100"></div> every 5 columns.
Update 2020
As of Bootstrap 4.4, you can also use the row-cols-5 class on the row...
<div class="container">
<div class="row row-cols-5">
<div class="col">
X
</div>
<div class="col">
X
</div>
<div class="col">
X
</div>
<div class="col">
X
</div>
<div class="col">
X
</div>
<div class="col">
X
</div>
</div>
</div>
https://codeply.com/p/psJLGuBuc3
UPDATE, 2021: for modern Bootstrap (4+), I recommend Zim's answer due to it's natural way of using Bootstrap flex classes
.col-xs-2{
background:#00f;
color:#FFF;
}
.col-half-offset{
margin-left:4.166666667%
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row" style="border: 1px solid red">
<div class="col-xs-2" id="p1">One</div>
<div class="col-xs-2 col-half-offset" id="p2">Two</div>
<div class="col-xs-2 col-half-offset" id="p3">Three</div>
<div class="col-xs-2 col-half-offset" id="p4">Four</div>
<div class="col-xs-2 col-half-offset" id="p5">Five</div>
<div>lorem</div>
</div>
</div>
This should be ok.
Default bootstrap(version 4.6) grid allows you to do this.
You can make 7, 7 11 columns in a row.
<div class="container">
<div class="row row-cols-5">
<div class="col">Column</div>
<div class="col">Column</div>
<div class="col">Column</div>
<div class="col">Column</div>
<div class="col">Column</div>
<div class="col">Column</div>
<div class="col">Column</div>
<div class="col">Column</div>
</div>
</div>
Responsive version of this.
<div class="container">
<div class="row row-cols-2 row-cols-sm-3 row-cols-md-4 row-cols-lg-5 row-cols-xl-5">
<div class="col">Column</div>
<div class="col">Column</div>
<div class="col">Column</div>
<div class="col">Column</div>
<div class="col">Column</div>
<div class="col">Column</div>
<div class="col">Column</div>
<div class="col">Column</div>
</div>
</div>
Just add this to your CSS
/* 5 Columns */
.col-xs-15,
.col-sm-15,
.col-md-15,
.col-lg-15 {
position: relative;
min-height: 1px;
padding-right: 10px;
padding-left: 10px;
}
.col-xs-15 {
width: 20%;
float: left;
}
#media (min-width: 768px) {
.col-sm-15 {
width: 20%;
float: left;
}
}
#media (min-width: 992px) {
.col-md-15 {
width: 20%;
float: left;
}
}
#media (min-width: 1200px) {
.col-lg-15 {
width: 20%;
float: left;
}
}
Sometimes I'm asked to use some pretty odd grid layouts in existing bootstrap websites (without messing up the current code). What I have done is create a separate CSS file which i can just drop into current bootstrap projects and use the grid layout styles in there. Here is what the bootstrap-5ths.css file contains along with an example of how it looks for bootstrap version 4.
If you want to use this with bootstrap version 3, just remove the col-xl-* styles and change the min-widths from 576px to 768px, 768px to 992px and 992px to 1200px.
.col-xs-5ths, .col-sm-5ths, .col-md-5ths, .col-lg-5ths, .col-xl-5ths,
.col-xs-two5ths, .col-sm-two5ths, .col-md-two5ths, .col-lg-two5ths, .col-xl-two5ths,
.col-xs-three5ths, .col-sm-three5ths, .col-md-three5ths, .col-lg-three5ths, .col-xl-three5ths,
.col-xs-four5ths, .col-sm-four5ths, .col-md-four5ths, .col-lg-four5ths, .col-xl-four5ths,
.col-xs-five5ths, .col-sm-five5ths, .col-md-five5ths, .col-lg-five5ths, .col-xl-five5ths,
{
position: relative;
min-height: 1px;
padding-right: 15px;
padding-left: 15px;
}
#media (min-width: 576px) {
.col-sm-5ths {width: 20%;float: left;}
.col-sm-two5ths {width: 40%;float: left;}
.col-sm-three5ths {width: 60%;float: left;}
.col-sm-four5ths {width: 80%;float: left;}
}
#media (min-width: 768px) {
.col-md-5ths {width: 20%;float: left;}
.col-md-two5ths {width: 40%;float: left;}
.col-md-three5ths {width: 60%;float: left;}
.col-md-four5ths {width: 80%;float: left;}
}
#media (min-width: 992px) {
.col-lg-5ths {width: 20%;float: left;}
.col-lg-two5ths {width: 40%;float: left;}
.col-lg-three5ths {width: 60%;float: left;}
.col-lg-four5ths {width: 80%;float: left;}
}
#media (min-width: 1200px) {
.col-xl-5ths {width: 20%;float: left;}
.col-xl-two5ths {width: 40%;float: left;}
.col-xl-three5ths {width: 60%;float: left;}
.col-xl-four5ths {width: 80%;float: left;}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-md-5ths">1</div>
<div class="col-md-5ths">1</div>
<div class="col-md-5ths">1</div>
<div class="col-md-5ths">1</div>
<div class="col-md-5ths">1</div>
</div>
<div class="row">
<div class="col-md-5ths">1</div>
<div class="col-md-two5ths">2</div>
<div class="col-md-two5ths">2</div>
</div>
<div class="row">
<div class="col-md-three5ths">3</div>
<div class="col-md-two5ths">2</div>
</div>
<div class="row">
<div class="col-md-four5ths">4</div>
<div class="col-md-5ths">1</div>
</div>
<div class="row">
<div class="col-md-five5ths">5</div>
</div>
</div>
<div class="row">
<div class="col">One</div>
<div class="col">Two</div>
<div class="col">Three</div>
<div class="col">Four</div>
<div class="col">Five</div>
</div>
you can customize however you need by writing media query. So that we can maintain responsive design
#media(max-width: 425px) {
.col {
flex-basis: auto;
-webkit-box-flex: 1;
flex-grow: 1;
max-width: 100%;
}
}
#media(min-width: 426px) and (max-width: 961px){
.col {
flex-basis: auto;
-webkit-box-flex: 1;
flex-grow: 1;
max-width: 50%;
}
}
CSS
.bg{
background-color: #9DC3E6;
border-radius: 10px;
color: #fff;
padding: 15px;
}
.col-xs-5-cols,
.col-sm-5-cols,
.col-md-5-cols,
.col-lg-5-cols {
position: relative;
min-height: 1px;
padding-right: 15px;
padding-left: 15px;
}
.col-xs-5-cols {
width: 20%;
float: left;
}
#media (min-width: 768px) {
.col-sm-5-cols {
width: 20%;
float: left;
}
}
#media (min-width: 992px) {
.col-md-5-cols {
width: 20%;
float: left;
}
}
#media (min-width: 1200px) {
.col-lg-5-cols {
width: 20%;
float: left;
}
}
html
<div className="row">
<div className="col-md-5-cols">1<div className="bg"></div></div>
<div className="col-md-5-cols">2<div className="bg"></div></div>
<div className="col-md-5-cols">3<div className="bg"></div></div>
<div className="col-md-5-cols">4<div className="bg"></div></div>
<div className="col-md-5-cols">5<div className="bg"></div></div>
</div>
If you want to enforce the 20% column width for every row that you have, even if you don't have 5 columns in some rows, you will have to add the following to your css or scss file:
.col-xs-5-cols,
.col-sm-5-cols,
.col-md-5-cols,
.col-lg-5-cols {
position: relative;
width: 100%;
min-height: 1px;
padding-right: 15px;
padding-left: 15px;
}
.col-xs-5-cols {
-ms-flex: 0 0 20%;
flex: 0 0 20%;
max-width: 20%;
}
#media (min-width: 768px) {
.col-sm-5-cols {
-ms-flex: 0 0 20%;
flex: 0 0 20%;
max-width: 20%;
}
}
#media (min-width: 992px) {
.col-md-5-cols {
-ms-flex: 0 0 20%;
flex: 0 0 20%;
max-width: 20%;
}
}
#media (min-width: 1200px) {
.col-lg-5-cols {
-ms-flex: 0 0 20%;
flex: 0 0 20%;
max-width: 20%;
}
}
And in your html, use the added class as following:
<div class="row">
<div class="col-lg-5-cols"></div>
<div class="col-lg-5-cols"></div>
<div class="col-lg-5-cols"></div>
<div class="col-lg-5-cols"></div>
<div class="col-lg-5-cols"></div>
</div>
Why not using grid?
.container {
display: grid;
grid-template-columns: repeat(5, minmax(0, 1fr));
grid-column-gap: 20px
}
Put some margin-left in all green divs but not in the first
Take a look at this page for general Boostrap layout information http://getbootstrap.com/css/#grid-offsetting
Try this:
<div class="col-xs-2" id="p1">One</div>
<div class="col-xs-3">
<div class="col-xs-8 col-xs-offset-2" id="p2">Two</div>
</div>
<div class="col-xs-2" id="p3">Three</div>
<div class="col-xs-3">
<div class="col-xs-8 col-xs-offset-2" id="p4">Four</div>
</div>
<div class="col-xs-2" id="p5">Five</div>
I've split the width into 5 uneven div with col-xs- 2,3,2,3,2 respectively. The two div with col-xs-3 are 1.5x the size of the col-xs-2 div, so the column width needs to be 2/3x (1.5 x 2/3 = 1) the col-xs-3 widths to match the rest.
That's better, you can use any div count.
.col-xs-2{
background:#00f;
color:#FFF;
}
.col_item {
margin-left:4.166666667%
}
.col_item:first-child,
.col_item:nth-child(5n+1) {
margin-left: 0;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row" style="border: 1px solid red">
<div class="col-xs-2 col_item" id="p1">One</div>
<div class="col-xs-2 col_item" id="p2">Two</div>
<div class="col-xs-2 col_item" id="p3">Three</div>
<div class="col-xs-2 col_item" id="p4">Four</div>
<div class="col-xs-2 col_item" id="p5">Five</div>
<div class="col-xs-2 col_item" id="p5">One</div>
</div>
</div>
The best way to honestly do this is to change the number of columns you have instead of trying to bodge 5 into 12.
The best number you could probably use is 20 as it's divisble by 2, 4 & 5.
So in your variables.less look for: #grid-columns: 12 and change this to 20.
Then change your html to:
<div class="row">
<div class="col-xs-20">
<div class="col-xs-4" id="p1">One</div>
<div class="col-xs-4" id="p2">Two</div>
<div class="col-xs-4" id="p3">Three</div>
<div class="col-xs-4" id="p4">Four</div>
<div class="col-xs-4" id="p5">Five</div>
</div>
<!-- //col-lg-20 -->
</div>
I'd personally use display: flex for this as bootstrap isn't designed greatly for a 5 column split.
Copypastable version of wearesicc's 5 col solution with bootstrap variables:
.col-xs-15,
.col-sm-15,
.col-md-15,
.col-lg-15 {
position: relative;
min-height: 1px;
padding-right: ($gutter / 2);
padding-left: ($gutter / 2);
}
.col-xs-15 {
width: 20%;
float: left;
}
#media (min-width: $screen-sm) {
.col-sm-15 {
width: 20%;
float: left;
}
}
#media (min-width: $screen-md) {
.col-md-15 {
width: 20%;
float: left;
}
}
#media (min-width: $screen-lg) {
.col-lg-15 {
width: 20%;
float: left;
}
}
can use as below in bootstrap3:
<div class="row">
<div class="col-md-2 col-md-offset-1">One</div>
<div class="col-md-2">Two</div>
<div class="col-md-2">Three</div>
<div class="col-md-2">Four</div>
<div class="col-md-2">Five</div>
</div>
Instead of adding margin why not add a padding-right of 1px to each col-xs-2 coz padding would be still a part of that div
.col-xs-2 :not(.col-xs-2:nth-child(5))
{padding-right:1px}
.col-xs-2{
background:#00f;
color:#FFF;
}
.col-half-offset{
margin-left:4.166666667%
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row" style="border: 1px solid red">
<div class="col-xs-4" id="p1">One</div>
<div class="col-xs-4 col-half-offset" id="p2">Two</div>
<div class="col-xs-4 col-half-offset" id="p3">Three</div>
<div>Test</div>
</div>
</div>
In bootstrap 4 and latest bootstrap 5 has become more simpler to handle 5 Column layout in big screen it will show 5 item, medium screen it will show 3 item, in small screen it will show 2 item with center alignment.
<div class="row justify-content-center">
<div class="col-sm-6 col-md-4 col-lg">One</div>
<div class="col-sm-6 col-md-4 col-lg">Two</div>
<div class="col-sm-6 col-md-4 col-lg">Three</div>
<div class="col-sm-6 col-md-4 col-lg">Four</div>
<div class="col-sm-6 col-md-4 col-lg">Five</div></div>
Here is another simple way of doing this, by adding width 20% to every col-xs-2:
<div class="col-xs-12">
<div class="col-xs-2" style="width:20%;" id="p1">One</div>
<div class="col-xs-2" style="width:20%;" id="p2">Two</div>
<div class="col-xs-2" style="width:20%;" id="p3">Three</div>
<div class="col-xs-2" style="width:20%;" id="p4">Four</div>
<div class="col-xs-2" style="width:20%;" id="p5">Five</div>
</div>
What about using offset?
<div class="row">
<div class="col-sm-8 offset-sm-2 col-lg-2 offset-lg-1">
1
</div>
<div class="col-sm-8 offset-sm-2 col-lg-2 offset-lg-0">
2
</div>
<div class="col-sm-8 offset-sm-2 col-lg-2 offset-lg-0">
3
</div>
<div class="col-sm-8 offset-sm-2 col-lg-2 offset-lg-0">
4
</div>
<div class="col-sm-8 offset-sm-2 col-lg-2 offset-lg-0">
5
</div>
</div>