/    Sign up×
Community /Pin to ProfileBookmark

Keep the cat in the box

I am trying to create a css box that shrinks the image within the box.
So far I can control either the height OR the width, but not both.
The question is simple. How do I keep the cat within the box when resizing the box?
Without destroying the cat πŸ™‚
http://jsfiddle.net/478pdnrj/6/

Here is the box less than the cat. And the cat is outside the box:
http://jsfiddle.net/e42d0uyb/

to post a comment
CSS

8 Comments(s) ↴

Copy linkTweet thisAlerts:
@KeverJun 07.2019 β€”Β This should do the trick..box img {
width: 100%;
height: 100%;
object-fit: scale-down;
}
Copy linkTweet thisAlerts:
@cootheadJun 07.2019 β€”Β Hi there sibert,

if you want the image to fit the box,

whatever it's width, then look at this...

http://jsfiddle.net/j17cpdka/

_coothead_
Copy linkTweet thisAlerts:
@codyhillJun 07.2019 β€”Β @sibert#1604431 Not sure what you ask but those this help you ?

https://codepen.io/raul-rogojan/pen/BeXjbK?editors=0110

I made 2 versions and I also let some comment for you.
Copy linkTweet thisAlerts:
@cootheadJun 07.2019 β€”Β @RaulRogojan#1604439
>**[color=#069]Not sure why you have the yellow line under the img..... [/color]**

That is because the "_img element_ " is an inline element.

To ensure that it exactly fits it's container requires this..

``<i>
</i>.box img {
display: block;
width: 100%;
height: auto;
}<i>
</i>
``


**https://codepen.io/anon/pen/mYNPvm**

_coothead_
Copy linkTweet thisAlerts:
@sibertauthorJun 08.2019 β€”Β > @Kever#1604433 This should do the trick.

Yes. Sort of. But the goal was to fit regardless of size. I e it should also "expand" to fill the box.
Copy linkTweet thisAlerts:
@KeverJun 08.2019 β€”Β If you want the image to shrink and expand to can use "object-fit: contain".
Copy linkTweet thisAlerts:
@codyhillJun 08.2019 β€”Β @sibert#1604442 isn't this the effect you want ?

https://codepen.io/raul-rogojan/pen/BeXjbK?editors=0110
Copy linkTweet thisAlerts:
@sibertauthorJun 09.2019 β€”Β > @RaulRogojan#1604447 isn't this the effect you want ?

.box {<br/>
width: 10px;<br/>
height: 300px;



The cat did not survive :-)
Γ—

Success!

Help @sibert 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 4.20,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...