/    Sign up×
Community /Pin to ProfileBookmark

Image fit into a container

How can I make this img fit inside the red container ?
https://codepen.io/raul-rogojan/pen/Zdmgmr?editors=1100

to post a comment
CSS

6 Comments(s)

Copy linkTweet thisAlerts:
@shakilapervinJul 09.2019 — .card-img img {

width: 100%;

}

.card-img {

width: 200px;

height: 200px;

background: red;

overflow:hidden;

}
Copy linkTweet thisAlerts:
@codyhillauthorJul 09.2019 — @shakilapervin#1606051 yeah I tried with overflow hidden but I don't this is a good idea. Because the imgs I have don;t have the same sizes.
Copy linkTweet thisAlerts:
@SempervivumJul 09.2019 — You need to set both, width and height, for the image in order to make object-fit work. This works for me:
/* * {
margin: 5px;
padding: 0;
}
*/
.card-img img {
width: 100%;
height: 100%;
object-fit: cover;
}

<i> </i> .card-img {
<i> </i> width: 200px;
<i> </i> height: 200px;
<i> </i> background: red;
<i> </i> }
Copy linkTweet thisAlerts:
@SempervivumJul 09.2019 — PS: No container necessary, this works either:
/* * {
margin: 5px;
padding: 0;
} */
.card-img img {
width: 200px;
height: 200px;
object-fit: cover;
}

<i> </i> /* .card-img {
<i> </i> width: 200px;
<i> </i> height: 200px;
<i> </i> background: red;
<i> </i> } */
Copy linkTweet thisAlerts:
@codyhillauthorJul 09.2019 — @Sempervivum#1606068 ohh, Ok thx ! works for me too
Copy linkTweet thisAlerts:
@siddhi_patelJul 24.2019 — Hi.. try like this its work for me

``<i>
</i>.card-img img{
width: 200px;
height: 200px;
object-fit: cover;
}<i>
</i>
``
×

Success!

Help @codyhill spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...