This css
took out the responsive layout of the Cards from the Boostrap 4. How can i put a object-fit: cover;
on the image of the Card, and have the responsive layout from the Bootstrap 4?
#imgUNcover {
width: 285px;
height: 145px;
-o-object-fit: cover;
object-fit: cover;
-o-object-position: center;
object-position: center;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="col-12 col-md-6 col-lg-3 mb-3 mb-md-3">
<div class="card">
<div class="img-container">
<a href="index.php?post=<?php echo $UN['title']?>"><img src="<?php echo $UN['capa']?>" alt="<?php echo $UN['alt']?>" class="card-img-top" id="imgUNcover"></a>
</div>
<div class="card-body">
<a href="index.php?post=<?php echo $UN['title']?>" class="card-title cardTitleLink">
<h1 class="cardTitleUN">
<?php echo $UN['title']?>
</h1>
</a>
<p class="card-text text-muted">
<?php echo $UN['text']?>
</p>
<a href="index.php?post=<?php echo $UN['title']?>" class="btn btn-outline-danger btn-sm">Continue Lendo</a>
</div>
</div>
</div>
Without object-fit: cover;
:
With object-fit: cover;
:
I could change the width
of the imagen on the id #imgUNcover
, but it will not solve the problem in different resulution screens.
Edit:
Susi, I am going to go out on a limb here and just guess what you are looking for. Do let me know if you need another outcome, and I will adjust the answer
In order for image to resize with the size of its containing block you place a percentage value on the height or the width. Please find a code example below.
Some Interesting Notes (in case you want to dive a bit deeper):
used value of other dimension / intrinsic image ratio
object-fit
will have no effect, except if you put it tonone
(none
: Image would keep its intrinsic dimensions, while its content box remains what you set it to)object-fit
only applies if intrinsic aspect ratio of image is different than the dimensions (height/width) you set on the element's content boxClosing remarks:
object-fit
can be found here: MDN object-fitCode:
Finally, a pen so you can easily try resizing the browser (to check that it is responsive): https://codepen.io/magnusriga/pen/xazZzg?editors=1100
UPDATE (added functionality as requested in comment):
To make sure all images are same height, you set a specific height on the cards, then distribute that across the image area (top section) and body section of the card using flexbox's
flex-basis
property. To avoid that the flex items grow outside their flex container, just useoverflow: auto
, which tells the block box to add scrollbars if necessary.The tedious part of modifying Bootstrap functionality, is that there are a lot more factors to check vs just building it from scratch yourself. And many times you have to put in
!important
to overrule their descriptors.New code:
And the pen: https://codepen.io/magnusriga/pen/VGdxJy?editors=1100
Use the
object-position
andobject-fit
properties, to tweak how image is placed inside its block box.Final Edit:
Here is an updated pen with some improvements to scrollbar etc.: https://codepen.io/magnusriga/pen/VGdxJy