I'm implementing code where I dynamically display flex items where each one takes 1/2 the display width, so after two items I need to start a new row.
class="d-flex flex-row" doesn't seem to do that for me. How can I essentially as a after every two items displayed?
<div class="d-flex mb-3">
<div class="card col-6 m-3">
<p>Card 1</p>
</div>
<div class="card col-6 m-3">
<p>Card 2</p>
</div>
<div class="d-flex flex-row"></div>
<div class="card col-6 m-3">
<p>Card 3</p>
</div>
<div class="card col-6 m-3">
<p>Card 4</p>
</div>
</div>
You are very close! I think the issue is flex calculating margins in the width of the element, so you effectively have a (50% + margin) element specified in your col-6 m-3 classes. This pushes the elements to the next row.
The solution is to apply a wrapper containing the layout class col-6 and leave the margin on the children.
You also do not have a row class on the parent of your columns, so be sure to add that as well!
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row d-flex flex-row mb-3">
<div class="col-6">
<div class="card m-3">
<p>Card 1</p>
</div>
</div>
<div class="col-6">
<div class="card m-3">
<p>Card 2</p>
</div>
</div>
<div class="col-6">
<div class="card m-3">
<p>Card 3</p>
</div>
</div>
<div class="col-6">
<div class="card m-3">
<p>Card 4</p>
</div>
</div>
</div>
use row to make row instead of d-flex flex-row your code should be like:
<div class="row">
<div class="card col-6">
<p>Card 1</p>
</div>
<div class="card col-6">
<p>Card 2</p>
</div>
<div></div>
<div class="card col-6">
<p>Card 3</p>
</div>
<div class="card col-6">
<p>Card 4</p>
</div>
</div>
(card 3 and 4 will automatically go to a new row as there's no remaining space for them)
I removed the margin as if you added margins the card space will be more than 50%
if you want to add space around the card you can make the cards inside divs and give the divs col-6 and then give the divs p-3 like that:
<div class="row w-100 mx-auto">
<div class="col-6 p-3">
<div class="card">
<p>Card 1</p>
</div>
</div>
<div class="col-6 p-3">
<div class="card">
<p>Card 2</p>
</div>
</div>
<div></div>
<div class="col-6 p-3">
<div class="card">
<p>Card 3</p>
</div>
</div>
<div class="col-6 p-3">
<div class="card">
<p>Card 4</p>
</div>
</div>
</div>
Related
I am trying to display a list of card width boostrap. I made my html like so :
<div class="card-deck">
<div class="card card-widget col-sm-2 m-2">
<div class="card-body text-center">
TEXT
</div>
</div>
<div>
etc...
</div>
</div>
The problem is, margin seems to expand the cards' width. So it makes someting like that... (it should display 4 cols)
screen 1
If I remove margins, it makes something like...
screen 2
Thanks for your help !
Thanks for your answers. However it does not solve my problem :(
First solution :
I cannot do that easily because I get them from a database so I don't know how many elements will be returned and I do really want to make a new deck card every 4 cards.
The justify-content-center is not really elegant and it shifts the problem
Second solution :
It looks like my second screen : https://i.stack.imgur.com/erkcT.jpg
You must use padding, don't margin if you don't want any cards size change.
change
<div class="card card-widget col-sm-2 m-2">
with this
<div class="card card-widget col-sm-2 p-2">
<div class="card-deck">
<div class="card card-widget col-sm-2 p-2">
<div class="card-body text-center">
TEXT
</div>
</div>
<div>
etc...
</div>
</div>
Margin can not increase the width of card like that. However, it will definitely increase container's width.
APPROACH 1: Check the code below. It will assign equal widths to the cards.
NOTE : It is working like row-column feature of bootstrap. So you have to add n number of card elements in a card-deck for n number of columns.
<div class="card-deck mx-2 mt-2">
<div class="card card-widget col m-2">
<div class="card-body text-center">
TEXT
</div>
</div>
<div class="card card-widget col m-2">
<div class="card-body text-center">
TEXT
</div>
</div>
<div class="card card-widget col m-2">
<div class="card-body text-center">
TEXT
</div>
</div>
<div class="card card-widget col m-2">
<div class="card-body text-center">
TEXT
</div>
</div>
</div>
<div class="card-deck mx-2">
<div class="card card-widget col m-2">
<div class="card-body text-center">
TEXT
</div>
</div>
<div class="card card-widget col m-2">
<div class="card-body text-center">
TEXT
</div>
</div>
<div class="card card-widget col m-2">
<div class="card-body text-center">
TEXT
</div>
</div>
<div class="card card-widget col m-2">
<div class="card-body text-center">
TEXT
</div>
</div>
</div>
APPROACH 2: In case you don't want to change card's width :
<div class="card-deck m-2 mb-0 justify-content-center">
<div class="card card-widget col-sm-2 m-2">
<div class="card-body text-center">
TEXT
</div>
</div>
<div class="card card-widget col-sm-2 m-2">
<div class="card-body text-center">
TEXT
</div>
</div>
<div class="card card-widget col-sm-2 m-2">
<div class="card-body text-center">
TEXT
</div>
</div>
<div class="card card-widget col-sm-2 m-2">
<div class="card-body text-center">
TEXT
</div>
</div>
</div>
(Bootstrap 5)
I was able to solve the problem by wrapping the card in a column first (which is what the documentation recommends). This appears to apply an mx-2 margin as a default, though I can't actually find out why this is the case. In any case it allowed me to get a margin between cards without breaking the grid.
<div class="container">
<div class="row">
<div class="col-lg-4 my-2">
<div class="card">
</div>
</div>
<div class="col-lg-4 my-2">
<div class="card">
</div>
</div>
...etc
</div>
</div>
I am beginner web developer (html and css).
My project is in Bootstrap 4.
I have 5 columns in one line.
I have this code:
<section class="related-product">
<div class="container">
<div class="row">
<div class="col-12 h-100 pt-4">
<h4 class="float-left">Podobne produkty</h4>
<span class="pl-3 related-product-count">(20 produktów)</span>
</div>
</div>
<div class="row">
<div class="col">
<div class="mt-5 w-100">
<div class="w-100 mb-3">
<img src="img/product1.png" class="img-fluid">
</div>
<div class="product-card-name-box">
<a class="product-card-new-info" href="#">Nowość</a><br/>
<div class="product-card-name pt-2">
Szafka mimimo
</div>
</div>
<div class="product-card-category">Stojąca</div>
</div>
</div>
<div class="col">
<div class="mt-5 w-100">
<div class="w-100 mb-3">
<img src="img/product2.png" class="img-fluid">
</div>
<div class="product-card-name-box">
<a class="product-card-new-info" href="#">Nowość</a><br/>
<div class="product-card-name pt-2">
Szafka mimimo
</div>
</div>
<div class="product-card-category">Stojąca</div>
</div>
</div>
<div class="col">
<div class="mt-5 w-100">
<div class="w-100 mb-3">
<img src="img/product3.png" class="img-fluid">
</div>
<div class="product-card-name-box">
<a class="product-card-new-info" href="#">Nowość</a><br/>
<div class="product-card-name pt-2">
Szafka mimimo
</div>
</div>
<div class="product-card-category">Stojąca</div>
</div>
</div>
<div class="col">
<div class="mt-5 w-100">
<div class="w-100 mb-3">
<img src="img/product4.png" class="img-fluid">
</div>
<div class="product-card-name-box">
<a class="product-card-new-info" href="#">Nowość</a><br/>
<div class="product-card-name pt-2">
Szafka mimimo
</div>
</div>
<div class="product-card-category">Stojąca</div>
</div>
</div>
<div class="col">
<div class="mt-5 w-100">
<div class="w-100 mb-3">
<img src="img/product5.png" class="img-fluid">
</div>
<div class="product-card-name-box">
<a class="product-card-new-info" href="#">Nowość</a><br/>
<div class="product-card-name pt-2">
Szafka mimimo
</div>
</div>
<div class="product-card-category">Stojąca</div>
</div>
</div>
</div>
</div>
</section>
How can you make equal spacing between the columns (on LG and XL)?
How can I do at smaller resolutions (xs sm, md) of 1 or 2 columns in a row (responsive)?
How can I make it?
Please help me
In this situation, I would suggest you try Table instead, you may try here
However, for smaller Col you may refer to link here go to Grid Classes, there you are able to chose the best option according to your targeting resolution.
If you leave it as default Col, it will divide the space equally, lets say:
<Row>
<Col></Col>
<Col></Col>
</Row>
In this case, both Col will share 50% of your space that you allocated.
For responsive, you may get the overall css here, under Grid Option and Responsive column resets Try to drag your browser to see the performance :)
I'm struggling on how to level this card equally to each other:
As you can see in the picture below. The card for the top reason for downtime doesn't match what is on the other cards or vice-versa. I want them to on the same height with each other regardless of what is inside on them.
Below is my code:
<div class="container-fluid">
<div class="row">
<div class="col-md-3">
<div class="card shadow mb-4">
<h5 class="card-header">
Top Reason for downtime
</h5>
<div class="card-body">
<ul id="top5Downtime">
</ul>
</div>
</div>
</div>
<div class="col-md-5">
<div class="card shadow mb-4">
<h5 class="card-header">
Top Down Terminals:
</h5>
<div class="card-body">
<ol id="mostDown">
</ol>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card shadow mb-4">
<h5 class="card-header">
Top Up Terminals:
</h5>
<div class="card-body">
<ol id="mostUp">
</ol>
</div>
</div>
</div>
</div>
<!--End of div.row-->
<div class="row">
<div class="col-12">
<div class="card shadow mb-5">
<div class="card-body">
<div id="bar-chart">
</div>
</div>
</div>
</div>
</div>
<!--End of div.row-->
<div class="row">
<div class="col-12">
<div class="card shadow mb-5">
<div class="card-body">
<div id="lineChart">
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="card shadow mb-3">
<h5 class="card-header">
Planned vs Unplanned Event
</h5>
<div class="card-body">
<div id="pieChart"></div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card shadow mb-3">
<h5 class="card-header">
Downtime vs Uptime
</h5>
<div class="card-body">
<div id="pieChart2"></div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="card shadow mb-5">
<div class="card-body">
<div id="barChart2">
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="card shadow mb-5">
<div class="card-body">
<div id="barChart3">
</div>
</div>
</div>
</div>
</div>
<!--END OF div.row-->
<!--END OF div.row-->
</div>
I've tried using the align-self-stretch but it only broke my design. How could I proceed?
Applying display: flex (or .d-flex class) to each of the .cols in same row will make them have equal height:
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid">
<div class="row">
<div class="col-md-3 d-flex">
<div class="card shadow mb-4 w-100">
<h5 class="card-header">
Top Reason for downtime
</h5>
<div class="card-body">
<ul id="top5Downtime">
</ul>
</div>
</div>
</div>
<div class="col-md-5 d-flex">
<div class="card shadow mb-4 w-100">
<h5 class="card-header">
Top Down Terminals:
</h5>
<div class="card-body">
<ol id="mostDown">
</ol>
</div>
</div>
</div>
<div class="col-md-4 d-flex">
<div class="card shadow mb-4 w-100">
<h5 class="card-header">
Top Up Terminals:
</h5>
<div class="card-body">
<ol id="mostUp">
</ol>
</div>
</div>
</div>
</div>
<!--End of div.row-->
<div class="row">
<div class="col-12">
<div class="card shadow mb-5">
<div class="card-body">
<div id="bar-chart">
</div>
</div>
</div>
</div>
</div>
<!--End of div.row-->
<div class="row">
<div class="col-12">
<div class="card shadow mb-5">
<div class="card-body">
<div id="lineChart">
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 d-flex">
<div class="card shadow mb-3 w-100">
<h5 class="card-header">
Planned vs Unplanned Event
</h5>
<div class="card-body">
<div id="pieChart"></div>
</div>
</div>
</div>
<div class="col-md-6 d-flex">
<div class="card shadow mb-3 w-100">
<h5 class="card-header">
Downtime vs Uptime
</h5>
<div class="card-body">
<div id="pieChart2"></div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="card shadow mb-5">
<div class="card-body">
<div id="barChart2">
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="card shadow mb-5">
<div class="card-body">
<div id="barChart3">
</div>
</div>
</div>
</div>
</div>
<!--END OF div.row-->
<!--END OF div.row-->
</div>
You also have to apply .w-100 to cards inside .d-flex cols, to make them stretch to full column width on smaller responsiveness intervals.
Important note: this solution relies on the fact you only have 1 card in each .col. If you have more than one, you'll need to add .flex-column to your .col as well.
But, for your example, .d-flex to .cols is enough.
<div class="container">
<div class="row no-gutters">
<div class="col m-2">
<div />
</div>
<div class="col m-2">
<div />
</div>
<div class="col m-2">
<div />
</div>
...
</div>
</div>
So in this width it looks well aligned
When i go slightly wide i see that the div's in the second row are not close to each other. Is there a way to align the cols in the last column to be close to each other (and aligned with column 2,3,4 of row 1)?
Here is a jsfiddle for the same.
Use col-auto instead of *col, because you use fixed width for the boxes so that they takes as much space as necessary. And use justify-content-center for the row to align the columns in the center. Most importantly, use p-2 instead of m-2.
.box {
width: 200px;
height: 100px;
background: lightblue;
}
<link href="http://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row no-gutters justify-content-center">
<div class="col-auto p-2">
<div class="box mx-auto">
hello
</div>
</div>
<div class="col-auto p-2">
<div class="box mx-auto">
hello
</div>
</div>
<div class="col-auto p-2">
<div class="box mx-auto">
hello
</div>
</div>
<div class="col-auto p-2">
<div class="box mx-auto">
hello
</div>
</div>
<div class="col-auto p-2" >
<div class="box mx-auto">
hello
</div>
</div>
</div>
http://jsfiddle.net/6bx12u5w
You use an older version of Bootstrap. Better to use the latest one since there are a few new classes has been added.
Update
col takes all the available space while col-auto takes as much as space as it needs: it takes as much space as the natural width of it content.
Here are a few different options for last row alignment.
Align last row to center
I see that you want the last row cols aligned under 2,3,4, so in this case you can simply use justify-content-center along with the col-auto columns. The col-auto uses flexbox shrink to make the column width fit the content.
<div class="container">
<div class="row no-gutters justify-content-center">
<div class="col-auto m-2">
<div class="box mx-auto">
hello
</div>
</div>
<div class="col-auto m-2">
<div class="box mx-auto">
hello
</div>
</div>
..
<div class="col-auto m-2">
<div class="box mx-auto">
hello
</div>
</div>
<div class="col-auto m-2">
<div class="box mx-auto">
hello
</div>
</div>
</div>
</div>
https://www.codeply.com/go/iwL7PW77En
Align last row to left
Aligning the last row to the left is a flexbox issue that has been addressed before. If you don't mind having the entire layout not centered, just use col-auto to shrink the width of the columns to fit the boxes and justify-content-start.
<div class="row no-gutters justify-content-start">
<div class="col-auto m-2">
<div class="box mx-auto">
hello
</div>
</div>
<div class="col-auto m-2">
<div class="box mx-auto">
hello
</div>
</div>
....
</div>
https://codeply.com/go/LZkOF9zexh (align start)
If you need the entire layout centered, as you can see from the other answers there are various workarounds to align the last row left. IMO, the simplest, most reliable way in Bootstrap 4 is using a empty spacer elements at the end. You'll need 4 empty spacers since the max in any row is 5 cols.
<div class="row no-gutters justify-content-center">
<div class="col-auto m-2">
<div class="box mx-auto">
hello
</div>
</div>
<div class="col-auto m-2">
<div class="box mx-auto">
hello
</div>
</div>
<div class="col-auto m-2">
<div class="box mx-auto">
hello
</div>
</div>
...
<div class="col-auto m-2">
<div class="box mx-auto">
hello
</div>
</div>
<div class="col-auto m-2">
<div class="box invisible"></div>
</div>
<div class="col-auto m-2">
<div class="box invisible"></div>
</div>
<div class="col-auto m-2">
<div class="box invisible"></div>
</div>
<div class="col-auto m-2">
<div class="box invisible"></div>
</div>
</div>
https://codeply.com/go/F7pGyqIIT6 (spacers at end)
In the following example is there a bootstrap way of making the div with id "second" fit inside its parent container and not overflow when given h-100 class? A hacky way I used was applying overflow:hidden to the parent container of #second div and it does the job for my specific case but would this be called an elegant solution in this scenario?
<div class="container">
<div class="row">
<div class="col-6 bg-dark">
<div id="first">
<h5 class="text-white">some title</h5>
</div>
<div id="second" class="h-100 bg-success">
</div>
</div>
<div class="col-6 bg-danger">
<img src="http://via.placeholder.com/540x400">
</div>
</div>
</div>
https://www.codeply.com/go/pSVIlOETpB
Sure, just make the containing col-6 display:flex, flex-direction:column by using d-flex flex-column classes on it...
https://www.codeply.com/go/5YiBkLo0qO
<div class="container">
<div class="row">
<div class="col-6 bg-dark d-flex flex-column">
<div id="first">
<h5 class="text-white">some title</h5>
</div>
<div id="second" class="h-100 bg-success">
</div>
</div>
<div class="col-6 bg-danger">
<img src="http://via.placeholder.com/540x400">
</div>
</div>
</div>