So I'm writing a code where I need to evenly place cards next to each other with rows when using flex. However, I'm getting some spacing on the right side and can't work out a reason why is that happening. I tried inspecting the element, but it just shows the entire card extends to the right, but there is no real reason behind why it does that. I've scrolled through CSS code, and there's no additional width on the right.
<section id="jobs">
<!--Employing cards-->
<div class="container-fluid container-fluid-shorter bg-white">
<!--Assistant card-->
<div class="row">
<div class="col-md-6 col-lg-4 my-3">
<div class="card width-37">
<div class="card-body d-flex align-items-center justify-content-between">
<h6 class="card-title font-weight-lighter pb-3">Custom units</h6>
<p class="card-text">
<h4 class="text-grey font-weight-light text-capitalize pb-3 pt-3">home page hero</h4>
</div>
</div>
</div>
<div class="col-md-6 col-lg-4 my-3">
<div class="card width-37">
<div class="card-body d-flex align-items-center justify-content-between">
<h5 class="card-title font-weight-lighter pb-3">Vlastní jednotky</h5>
<p class="card-text">
<h4 class="text-grey font-weight-light text-capitalize pb-3 pt-3">home page hero</h4>
</div>
</div>
</div>
<div class="col-md-6 col-lg-4 my-3">
<div class="card width-37">
<div class="card-body d-flex align-items-center justify-content-between">
<h5 class="card-title font-weight-lighter pb-3">Vlastní jednotky</h5>
<p class="card-text">
<h4 class="text-grey font-weight-light text-capitalize pb-3 pt-3">home page hero</h4>
</div>
</div>
</div>
</div>
</div>
<!--End of employing cards section-->
</section>
Adjustments I've made to the CSS :
.width-37{
max-width: 37.5vh;
}
.card-body{
padding-left: 0; !important;
padding-right: 0; !important;
}
I'm trying to place all the items in the row where there is a small spacing between each item and all items will create rows as you set rows for different screen sizes large, medium, small. For some reason flex item is doing spacing on the right side of each item there is. It is 480 pixels wide and 180 height. I would like it to be 180 x 180 pixels so same height and width. Anyone got an idea on why is this happening?
.width-37{
max-width: 37.5vh;
}
This is causing the spacing on right side. Also, there should be no ; before !important
This is due to how block level elements are rendered and not a Flexbox specific issue. Block level elements always take up the full width of the containing element. If you set a width on a block level element, and it does not fill the containing element, the remaining space will be filled with margin.
Example
div {
min-height: 24px;
}
div:nth-child( 1 ) {
width: 100px;
background-color: rebeccapurple;
}
div:nth-child( 2 ) {
max-width: 200px;
background-color: gold;
}
div:nth-child( 3 ) {
width: 20vw;
background-color: silver;
}
<div></div>
<div></div>
<div></div>
The default flow of content is left to right and why the element is to the left. All the space to the right of each element is margin. This is what is happening inside of your Bootstrap column elements.
This is the why
*Rows are wrappers for columns. Each column has horizontal padding (called a gutter) for controlling the space between them. This padding is then counteracted on the rows with negative margins. This way, all the content in your columns is visually aligned down the left side.
Documentation here
In order to remove the padding on your gutter you can add the class no-gutters to your row.
Related
I'm trying to add an icon to the bootstrap card. I want the icon, title, and text in the same row. for that, I used float left on the card icon but it didn't work. can someone advise how to do this?
thank you :)
.card-icon {
background: rgba(255,255,255,0.9);
border-radius: 50%;
width: 60px;
height: 60px;
float: left;
margin-right: 15px;
text-align: center;
}
.card-icon i {
font-size: 48px;
}
<div class="card m-b-30 card-body">
<div class="card-icon"><i class="mdi mdi-database"></i></div>
<h3 class="card-title font-24">Available Credits: 2</h3>
<p class="card-text">2 Email Verification Credits Left</p>
<div class="d-flex">
Buy More Credits
Track Your Credits
</div>
</div>
A good way to solve this problem is use flexbox.
Bootstrap 4 have a class d-flex to do that. By default, the flex-direction is row, so you will have your i h3 and p in the same row. And to make sure that the elements will fill all the width available, add flex-fill class in each element. The align-items-center class will try to center the vertical axis (but the margin top/bottom of elements can override this behavior).
<div class="card m-b-30 card-body">
<div class="d-flex align-items-center">
<div class="card-icon flex-fill"><i class="mdi mdi-database">a</i></div>
<h3 class="card-title font-24 flex-fill">Available Credits: 2</h3>
<p class="card-text flex-fill">2 Email Verification Credits Left</p>
</div>
<div class="d-flex">
Buy More Credits
Track Your Credits
</div>
</div>
Add your h3 and p elements to the inside of your div element. If this doesn't work make the widths of the elements percentages of the page, making sure they add up to 100% or less.
Edit:
In your css add
display: inline-block;
To ALL those elements.
As you can see, i do have 3 bootstrap cards having 3 images with different heights. I want to make the images having same height compared to others (like the bigger one "takes the lead" and all the other one in the row adapting to this bigger one).
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<img class="card-img-top" src="/images/produits/1" alt=""><hr>
<div class="card-body">
<h6 class="card-title">
slt la mif
</h6>
<small class="text-muted">Puissance : 10 à 100W</small>
</div>
</div>
</div>
I already put the h-100 class in the card div but it only adapts the card's height, and I want the images to be adapted (in order to not have the blank after the text), how to do that pls?
Bootstrap class card-body has property flex: 1, it means, that this element also grows for available space.
You can add some special class, for example to row, or higher, where you use card, to modify that parameter. Something like that.
.special .card-body {
flex-grow: 0;
}
Also wrap link image with div which has class="flex-fill", it makes grow the link images.
Look into the snippet, I;ve added special class to row, and set different height to link images to illustrate the idea.
UPDATE. Made link images center
Play with flexboxes. For example: add to div which wraps <a> class d-flex it will make this div with display: flex. And then you can add to this div classes justify-content-center and align-items-center, but I prefer in your case to add class m-auto to <a>, it gives margin: auto, which make element centralized with flex parent. Look into snippet.
.special .card-body {
flex-grow: 0;
}
.img_mock1 {
padding: 50px;
background-color: red;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="row special">
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<div class="flex-fill d-flex">
<span class="img_mock1"></span>
</div>
<hr>
<div class="card-body">
<h6 class="card-title">
slt la mif
</h6>
<small class="text-muted">Puissance : 10 à 100W</small>
</div>
</div>
</div>
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<div class="flex-fill d-flex">
<a href="/led/catalogue/produit/1" style="height:300px;" class="m-auto">
<img src="" alt="">
</a>
</div>
<hr>
<div class="card-body">
<h6 class="card-title">
slt la mif
</h6>
<small class="text-muted">Puissance : 10 à 100W</small>
</div>
</div>
</div>
</div>
Use the following CSS
.card-img-top {
width: 100%;
height: 15vw;
object-fit: cover;
}
The object-fit: cover; enables zoom instead of image stretching.
Give fix height suppose 150px to the wrapper of image(anchor tag). Then for the image tag use max-width: 100%, height: auto, display: block
give the image div a fixed height, use the height preference of the largest image to put a fix height on the dive of the image, this will allow the all to show the same way.
I am trying to create a layout that would be responsive using Bootstrap 4. The problem I am having is when two divs wrap vertically for a smaller device the top div seems to push the lower div down exceeding the parent container (please see the images below). I want the lower div to fit with in the container, how can I achieve this using Bootstrap 4 or minimal css?
I added a yellow border on the bottom of the second div so we can see the push.
Large device view
Small device view
html,
body {
height: 100%;
margin: 0;
padding: 0;
width: 100%;
overflow-x: hidden;
overflow-y: hidden;
}
.bottom-border {
border-bottom: 50px solid yellow;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<body>
<div class="container-fluid p-0 h-100">
<div class="row h-100">
<div class="col-md-8 bg-primary h-md-100 ">
<div class="d-md-none text-center bg-primary">
<h5>Left Section</h5>
</div>
<div class="d-none d-md-block m-3">
<h1>Left Section</h1>
</div>
</div>
<div class="col-md-4 h-100 text-center bg-danger bottom-border">
<h4>Right Section</h4>
</div>
</div>
</div>
</body>
After you explained the significance of the border height, now I see what you mean, as mentioned previously, your h-100 classes are the issue.
That class tells the element to use 100% of its parents height, since you put that class on the main container and on the row elements, those two are using the full screen's height since the container's parent is the browser window and the row parent is the container.
When you use that same class on the children div, in this case, the red div, it also tries to use the full height of its parent (the row), but it does not take into consideration the other item inside (the blue div), if you remove the h-100 class from the red div, then each of them will use half the available space.
html,
body {
height: 100%;
margin: 0;
padding: 0;
width: 100%;
overflow-x: hidden;
overflow-y: hidden;
}
.bottom-border {
border-bottom: 50px solid yellow;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" />
<body>
<div class="container-fluid h-100">
<div class="row h-100 flex-column flex-md-row">
<div class="col-md-8 bg-primary">
<div class="d-md-none text-center bg-primary">
<h5>Left Section</h5>
</div>
<div class="d-none d-md-block m-3">
<h1>Left Section</h1>
</div>
</div>
<div class="col-md-4 flex-grow-1 text-center bg-danger bottom-border">
<h4>Right Section</h4>
</div>
</div>
</div>
</body>
If I understand correctly, you'd wish to keep the left and right div keep doesn't go on top of eachother?
col-md means everything that the window size is above 768px, keep that number of columns. if you use col-sm, it would be everything above 576px. If you just say col-number, you'll keep it responsive reguardeless of the container width.
html,
body {
height: 100%;
margin: 0;
padding: 0;
width: 100%;
overflow-x: hidden;
overflow-y: hidden;
}
.bottom-border {
background-color: yellow;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" />
<body>
<div class="container-fluid p-0 ">
<div class="d-flex flex-row flex-wrap bd-highlight text-center ">
<div class="col-md-8 ">
<div class=" h-100 bg-primary">left</div>
</div>
<div class="flex-column col-md-4">
<div class=" bg-danger ">Right</div>
<div class=" bottom-border ">Bottom</div>
</div>
</div>
</div>
</body>
Also, since you're using Bootstrap 4, I'd also recommand checking Flex, which you basically put d-flex class on the parent and all the child will become responsive. You can make them even more precices with columns like you've did, but Flexbox is pretty powerful and really braindead to apply when you know how.
Morning,
I am trying to put my bootstrap card element in a row by 3 cards, cards need to be of the same hight and width and have spacing in between them. For example if I have 7 card I will have 3 rows by three cards and the last row with one element will still be the same size as the card above it.
My code is:
<div class="container pt-2">
<div *ngFor="let g of carMultiArray">
<div class="row">
<div class="col-12">
<div class="card border-0 boxShadow no-padding col-lg-4 col-md-12 col-sm-12 col-xs-12" *ngFor="let t of g">
<a data-toggle="modal" href="'#'+ 'car'+t.Id" class="card-link" [attr.data-target]="'#'+ 'car'+t.Id" style="color:black; text-decoration: none;">
<img class="card-img-top rounded-0" src="{{t.MainImage}}">
</a>
<div class="card-body text-center">
<div class="my-arrow">
<img *ngIf="!t.Verified" src="../../icons/error.png">
<img *ngIf="t.Verified" src="../../icons/checked.png">
</div>
<h5 class="card-title">{{t.Title}} {{t.Kubatura}} {{t.BodyType}}</h5>
<h6 class="card-subtitle mb-2 text-muted">{{t.FuelType}} {{t.GearBox}} {{t.Horsepower}}
{{t.OdometerReading}}</h6>
<a [routerLink]="['/car', t.Id]" class="card-link">Daugiau</a>
<a class="card-link">{{t.City}}</a>
<a class="card-link">{{t.FirstRegistration}} </a>
</div>
</div>
</div>
</div>
</div>
</div>
My css (Though it does not influence the problem):
.boxShadow {
display: inline-block;
vertical-align: middle;
box-shadow: 0 0 8px rgba(0, 0, 0, 0.6);
}
.card-body {
position: relative;
}
.my-arrow {
position: absolute;
top: -10%;
right: 10%;
}
.card-img-top {
width: 100%;
height: 17vw;
object-fit: cover;
}
.card{
width: 90%;
}
.no-padding {
padding-left: 0;
padding-right: 0;
}
The problem that I am having is that with the current code the cards do not have any margin or padding in between them when in one row. As you may have seen I have removed all padding in class .no-padding as if there is any padding even by default the images inside the card get smaller however the cards itself do not and they still touch each other, removing padding brings the cards to look the same and the way they suppose to, however they have no space in between them, adding margin, any margin, pushes the 3 card to a new line.
If I put the elements in a card-group instead of putting it in rows and columns, the result is they have no space in between them, adding some breaks the grid again. Putting it in card-deck results the last card which is one in a row to take up the whole space of the last row, trying to set the width of the cards in a card-deck, for some reason when the elements have 33% or 30% have different width, for example the 3 cards above will have the same width however the single card bellow will still be a little bit wider.
Adding the cards to card-columns, will result in having the cards different hights, trying to make it the same will result in images in the cards not aligning with each other.
While adding everything like you see right now in the code, in a row and then in col-12 allows to handle everything and the way I want it, it removes spacing between cards and as I said if adding margin it pushes the last 3rd card to new line if adding padding image gets smaller then the card.
How would I achieve my wanted result?
Hope this makes sense, if not, please ask.
I think you should use .justify-content-between class from bootstrap4 and try remove your display: inline-block attr from .boxShadow
FOR BOOTSTRAP REFER THIS LINK..
W3 bootstrap 4 card deck
FOR WITHOUT BOOTSTRAP REFER THE CODE BELOW..
.parent{
text-align:center;
}
.padding_maker{
display:inline-block;
padding:10px;
}
.children{
display:inline-block;
background-color:black;
height:300px;
width:150px;
color:white;
}
<div class="parent">
<div class="padding_maker">
<div class="children">
<h3>Your Card</h3>
</div>
</div>
<div class="padding_maker">
<div class="children">
<h3>Your Card</h3>
</div>
</div>
<div class="padding_maker">
<div class="children">
<h3>Your Card</h3>
</div>
</div>
<div class="padding_maker">
<div class="children">
<h3>Your Card</h3>
</div>
</div>
<div class="padding_maker">
<div class="children">
<h3>Your Card</h3>
</div>
</div>
<div class="padding_maker">
<div class="children">
<h3>Your Card</h3>
</div>
</div>
<div class="padding_maker">
<div class="children">
<h3>Your Card</h3>
</div>
</div>
<div class="padding_maker">
<div class="children">
<h3>Your Card</h3>
</div>
</div>
<div class="padding_maker">
<div class="children">
<h3>Your Card</h3>
</div>
</div>
</div>
I'm trying to put some extra margin/padding space between columns on my Bootstrap grid layout. I've tried this but I don't like the result. Here is my code:
<div class="row">
<div class="text-center col-md-6">
Widget 1
</div>
<div class="text-center col-md-6">
Widget 2
</div>
</div>
I want to add margin: 10px and padding:10px. Some people suggest to change their classes to col-md-5 with pull-left and pull-right, but the gap between them will be too large.
Simply add a div within col-md-6 that has the extra padding that you need. The col-md-6 is the 'backbone' to keep the column integrity, but you can add additional padding within it.
<div class="row">
<div class="text-center col-md-6">
<div class="classWithPad">Widget 1</div>
</div>
<div class="text-center col-md-6">
<div class="classWithPad">Widget 2</div>
</div>
</div>
CSS
.classWithPad { margin:10px; padding:10px; }
Bootstrap 5 (Update 2021)
Bootstrap 5 has still includes spacing utilities for padding. However, because of new RTL support "left" and "right" have been changed to "start" and "end". For example pl-2 is now ps-2.
pl-* => ps-* (padding-left)
pr-* => pe-* (padding-right)
ml-* => ms-* (margin-left)
mr-* => me-* (margin-right)
Additionally, Bootstrap 5 introduces new grid gutter classes that can be used to adjust the spacing between columns. The guttter is set on the row instead of each col-* inside the row. For example, use g-0 for no spacing between columns.
Bootstrap 5 column spacing demo
Bootstrap 4 (Update 2018)
Bootstrap 4 has spacing utilities that make adding (or substracting) the space (gutter) between columns easier. Extra CSS isn't necessary.
<div class="row">
<div class="text-center col-md-6">
<div class="mr-2">Widget 1</div>
</div>
<div class="text-center col-md-6">
<div class="ml-2">Widget 2</div>
</div>
</div>
You can adjust margins on the column contents using the margin utils such as ml-0 (margin-left:0), mr-0 (margin-right:0), mx-1 (.25rem left & right margins), etc...
Or, you can adjust padding on the columns (col-*) using the padding utils such as pl-0 (padding-left:0), pr-0 (padding-right:0), px-2 (.50rem left & right padding), etc...
Bootstrap 4 Column Spacing Demo
Notes
Changing the left/right margin(s) on col-* will break the grid.
Change the left/right margin(s) on the content of col-* works.
Change the left/right padding on the col-* also works.
I was facing the same issue; and the following worked well for me. Hope this helps someone landing here:
<div class="row">
<div class="col-md-6">
<div class="col-md-12">
Set room heater temperature
</div>
</div>
<div class="col-md-6">
<div class="col-md-12">
Set room heater temperature
</div>
</div>
</div>
This will automatically render some space between the 2 divs.
Just add 'justify-content-around' class. that would automatically add gap between 2 divs.
Documentation:
https://getbootstrap.com/docs/4.1/layout/grid/#horizontal-alignment
Sample:
<div class="row justify-content-around">
<div class="col-4">
One of two columns
</div>
<div class="col-4">
One of two columns
</div>
</div>
You may use the padding and margin shorthand Bootstrap 4 classes as follows:
For extra small devices i.e. xs
{property}{sides}-{size}
For other devices/viewports (small, medium, large and extra large)
{property}{sides}-{breakpoint}-{size}
Where:
property = m for margin and p for padding
Following are sides shorthand meanings:
l = defines the left-margin or left-padding
r = defines the right-margin or right-padding
t = defines the top-margin or top-padding
b = defines the bottom-margin or right-padding
x = For setting left and right padding and margins by the single call
y = For setting top and bottom margins
blank = margin and padding for all sides
The breakpoint = sm, md, lg, and xl.
Combining all the above, the left padding complete code can be (for example):
For left padding in extra small devices
pl-2
or for medium to extra large
pl-md-2
Try This:
<div class="row">
<div class="text-center col-md-6">
<div class="col-md-12">
Widget 1
</div>
</div>
<div class="text-center col-md-6">
<div class="col-md-12">
Widget 2
</div>
</div>
</div>
I would keep an extra column in the middle for larger displays and reset to default when the columns collapse on smaller displays. Something like this:
<div class="row">
<div class="text-center col-md-5 col-sm-6">
Widget 1
</div>
<div class="col-md-2">
<!-- Gap between columns -->
</div>
<div class="text-center col-md-5 col-sm-6">
Widget 2
</div>
</div>
Super easy with flexbox. Leave room for some space by changing the columns to col-md-5
<div class="row widgets">
<div class="text-center col-md-5">
Widget 1
</div>
<div class="text-center col-md-5">
Widget 2
</div>
</div>
CSS
.widgets {
display: flex;
justify-content: space-around;
}
For those looking to control the space between a dynamic number of columns, try:
<div class="row no-gutters">
<div class="col">
<div class="inner">
<!-- content here -->
</div>
</div>
<div class="col">
<div class="inner">
<!-- content here -->
</div>
</div>
<!-- etc. -->
</div>
CSS:
.col:not(:last-child) .inner {
margin: 2px; // Or whatever you want your spacing to be
}
A solution for someone like me when cells got background color
HTML
<div class="row">
<div class="col-6 cssBox">
a<br />ba<br />ba<br />b
</div>
<div class="col-6 cssBox">
a<br />b
</div>
</div>
CSS
.cssBox {
background-color: red;
margin: 0 10px;
flex-basis: calc(50% - 20px);
}
In the otherside if you like to remove double padding between columns just add class "nogap" inside row
<div class="row nogap">
<div class="text-center col-md-6">Widget 1</div>
<div class="text-center col-md-6">Widget 2</div>
</div>
and create additional css class for it
.nogap > .col{ padding-left:7.5px; padding-right: 7.5px}
.nogap > .col:first-child{ padding-left: 15px; }
.nogap > .col:last-child{ padding-right: 15px; }
Thats it, check here: https://codepen.io/michal-lukasik/pen/xXvoYJ
I had the same issue and worked it out by nesting a div inside bootstrap col and adding padding to it. Something like:
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="custom-box">Your content with padding</div>
</div>
<div class="col-md-4">
<div class="custom-box">Your content with padding</div>
</div>
<div class="col-md-4">
<div class="custom-box">Your content with padding</div>
</div>
</div>
</div>
I have just found a solution that works for me, although it doesnt actually create a space between the boxes so may not be exactly what you are looking for.
border border-white
Doesn't actually create a space but gives the effect of space between cols. Only works if you have a bg-color obviously.
Try this:
<div class="row">
<div class="col-md-5">
Set room heater temperature
</div>
<div class="col-md-2"></div>
<div class="col-md-5">
Set room heater temperature
</div>
</div>
For the more curious, I have also found that adding
border: 5px solid white
or any other variant of your liking, to make it blend in, works superbly.