How make Bootstrap 4 card decks same width each row? - html

I am displaying 4 cards each row using a card deck:
<div class="row">
<div class="card-deck">
<!-- 4 of these -->
<div class="card">
<img class="card-img-top img-adjusted" >
<div class="card-body">...</div>
</div>
</div>
</div>
<div class="row">
<div class="card-deck">
<!-- 4 of these -->
<div class="card">
<img class="card-img-top img-adjusted" >
<div class="card-body">...</div>
</div>
</div>
</div>
...
The cards within each deck are the same width, but the decks are not, i.e. the deck of the 2nd row is wider than the deck of the 1st row. How do I get the decks to be of the same width?
I have tried setting .card-decks{width: 100%;}, which works for full rows, but distorts cards of rows that have just 1-2 cards.
Additional CSS:
My images are of different sizes, that's why I added the following CSS to make them the same. No other CSS should affect the cards:
.img-adjusted{
position: relative;
float: left;
background-position: 50% 50%;
background-repeat: no-repeat;
background-size: cover;
}
Also, I want to keep the style responsive, so would like to avoid hardcoding pixel set pixel widths.

Don't put .card-deck in a .row. Only .col- columns should be placed in rows...
content must be placed within columns and only columns may be
immediate children of rows.
You should use...
<div class="row">
<div class="col-12">
<div class="card-deck">
<!-- 4 of these -->
<div class="card">
<img class="card-img-top img-adjusted" >
<div class="card-body">...</div>
</div>
</div>
</div>
</div>
The 2nd part of the question...
When using card-deck (and card-group), cards will always fill the width of the next visual row. If you have less than 4 cards in each row, the remaining cards will fill the width across. To workaround this, set a max-width for each card..
.card-deck .card {
max-width: calc(25% - 30px);
}
https://www.codeply.com/go/ygvZvjsELs
Alternatively, You can use the grid columns to wrap the cards. This is explained here: Bootstrap 4 card-deck with number of columns based on viewport

Related

how to control content sizing in bootstrap column grid?

I have a bootstrap grid inside the columns, I have a card with an image in it, but my card does not occupy the full width of my column, how do I make it possible?
My column right now
I've tried playing with the width of the card but it seems there's some maximum limit to the grid size or for some grid sizes, other elements on the page are effected.
div class="container2">
<div class="row">
<!-- outer grid first column -->
<div class="col-8">
MODULES
<div class="module-container">
<!-- first column inner grid -->
<div class="row">
<div class="col-6">
<img src="images/GC.png" class="img-top img-responsive fit-image" alt="module 1" >
GC
</div>
CSS
.fit-image{
width: auto;
object-fit: cover;
height: 300px; /* only if you want fixed height */
}
If you want fit your image to the full width of the column then u can use some css
your code goes here
..........
<img src="images/GC.png" class="card-img-top img-responsive fit-image" alt="module 1" >
.........
add css like this
.fit-image{
width: 100%;
object-fit: cover;
height: 300px; /* only if you want fixed height */
}

Why not the two images along with the paragraph being inline?

The bootstrap grid system divides the entire screen into 12 pieces, so I created a row and then divided it into two columns and put an image and some texts on both the columns but they are coming one after another not inline
<div class="row">
<div class="col-lg-6">
<img src=""> (text)
<div class="col-lg-6">
<img src=""> (text)
</div>
I am expecting a result where there would be two images in one row and some texts after each image
You need to ensure that you've closed all your divs correctly. This means you will need to close your first column <div> and then your row <div>.
You should also apply CSS to the images themselves to keep them within the confines of their columns. In the example below I've made it so the images span the entire width and have an automatic height (so it keeps its aspect ratio).
Rows should also be kept within a container. You can either use the container or container-fluid class to specify what sort of container you would like.
Keep in mind that you've used col-lg-*, and so lg means that your columns will collapse when on large screen sizes. Running the snippet below will cause the two columns to stack (as it is a small screen) but you can see the two columns side-by-side in the one row by expanding the code snippet view:
.img {
height: auto;
width: 100%;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<div class="container-fluid">
<div class="row">
<div class="col-lg-6">
<img class="img" src="https://images.unsplash.com/photo-1524293581917-878a6d017c71?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80" />
<p>(text)</p>
</div>
<div class="col-lg-6">
<img class="img" src="https://images.unsplash.com/photo-1500622944204-b135684e99fd?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80" />
<p>(text)</p>
</div>
</div>
</div>

Why does bootstrap handle col spacing differently depending on num of cols?

<br>
<h6>
This row/deck contains two elements. You can see that even with col-md-4 it spans more than 2/3 of the width.
</h6>
<div class="flex-row">
<div class="card-deck">
<div class="card col-12 col-md-4">
</div>
<div class="card col-12 col-md-4">
</div>
</div>
</div> <br><br>
<h6>
This row/deck contains three elements. You can see that each card spaces 1/3 of the width as expected.
</h6>
<div class="flex-row">
<div class="card-deck">
<div class="card col-12 col-md-4">
</div>
<div class="card col-12 col-md-4">
</div>
<div class="card col-12 col-md-4">
</div>
</div>
</div>
</div>
https://jsfiddle.net/9e85Lb0y/
In this example there are two flex-rows each containing a card deck. The first deck has 2 cards and the second deck has 3 cards. All of the cards are identical. Why is it that the top row has wider cards? I would have expected the top row to have the same widths as the bottom row.
Columns aren't meant to be inside card-deck. They're only supposed to be contained in .row. The cards would go inside the columns.
From the Bootstrap docs...
"Rows are wrappers for columns... In a grid layout, content must be
placed within columns and only columns may be immediate children of
rows."
If you're trying to set widths for the cards in card-decks, see:
Bootstrap 4 card-deck with number of columns based on viewport, or
bootstrap 4 card-deck containing cards with different width
TLDR
Use flex-row and col-* to explicitly specify widths. Use card-deck and card to create evenly sized blocks. Avoid mixing the two, they are not designed for use together straight out of the box.
Long Answer
The issue here is most likely caused by the mixture of both the flex-row and card-deck classes. While both offer very similar functionality, there are some key differences which separate how they behave.
The card-deck class simply guarantees that any immediate elements with the card class are all the same width as one another.
<div class="card-deck">
<div class="card">
<!-- Content -->
</div>
<div class="card">
<!-- Content -->
</div>
<div class="card">
<!-- Content -->
</div>
</div>
Every <div class="card"></div> will now become a uniform width. They are not guaranteed to fill the whole of their parent container. The card class also applies left and right margins of 15px to keep them separated.
<div class="flex-row">
<div class="col-4">
<!-- 1/3 Width Column -->
</div>
<div class="col-4">
<!-- 1/3 Width Column -->
</div>
<div class="col-4">
<!-- 1/3 Width Column -->
</div>
</div>
The flex-row and col-* classes on the other hand allow you to create columns of specific size. Each col-* class simply applies a width to the element, the onus is on you to make sure your content will fit appropriately at each breakpoint. col's SHOULD NOT have any margins applied as CSS width declarations do not account for margins.

Twitter Bootstrap/Unify center variable amount of images

I want to create a call-to-action box with centered content
The box is contains two rows
the first row has the title and the second a variable amount of images, with a maximum of 12 (if greater than, then it will be three or more rows).
The text get perfectly centered using .text-center.
However i cannot create a div with the width of the content.
What i have now:
<div class="container">
<div class="row">
<div class="col-md-12" style=" padding: 10px 0 0;">
<div class="row text-center">
<div class="col-md-12 animated fadeInLeft">
<span class="color-green">Title</span>
</div>
</div>
<div class="row text-center">
<div class="col-md-9 center-block">
<!-- Content / Images-->
</div>
</div>
</div>
</div>
</div>
I tried multiple setups but the center-block doesn't seem to place the col-md-9 in the middle. (even though it does apply margin: 0 auto;)
What can i do to create equal width images in the center of their container, with variable amount. I'm using Unify Template which is based on Twitter Bootstrap 3
Since text-center affects on inline-level elements(children) only, you need to make them inline. It's clear. So all you need is to make <div class="col-md-9 center-block"> inline. Do it:
.center-block {
display: inline-block;
float: none;
}
If you want to push not more than 12 images in one line without javascript, I think there's the only way:
.center-block img {
width: 8.2%;
}

How can I center an column on bootstrap when it is an uneven number

I have the following scenario:
I have a <div class="row"> and then I have another <div class="col-lg-3"> where I will be putting the picture of a profile of an user. The problem is that I would like to center that picture to be in the middle of the available space, so I would normally do this:
<div class="row">
<div class="col-lg-3 col-lg-offset-x">
<img src="..." class="..." alt="..." title="..." data-size="...">
</div>
</div>
What would be the x value on the class of the <div class="col-lg-3 col-lg-offset-x"> If I wanted to center that div? Anybody has any idea how to do that?
Duplicate of Center a column using Twitter Bootstrap 3
"You can center any column size by using the proven margin: 0 auto; technique, you just need to take care of the floating that is added by Bootstrap's grid system. I recommend defining a custom CSS class like the following:
.col-centered{
float: none;
margin: 0 auto;
}
Now you can add it to any column size at any screen size and it will work seamlessly with Bootstrap's responsive layout :
<div class="row">
<div class="col-lg-1 col-centered"></div>
</div>
Note: With both techniques you could skip the .row element and have the column centered inside a .container but you would notice a minimal difference in the actual column size because of the padding in the container class."