/    Sign up×
Community /Pin to ProfileBookmark

resize image to fit div

I’ve been searching for a javascript to resize an image, if it is larger than the surrounding DIV. The DIV has a relative size, so it depends on the screen resolution of the user, how big the DIV is. Do some of you know a javascript to fix that problem?

Best Regards Dr. Jones

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisApr 09.2007 — How do you intend to code the image?
Copy linkTweet thisAlerts:
@dr__jonesauthorApr 09.2007 — I was thinking of javascript could read the width of the DIV and the image and if the image was wider than the div, it was scaled down to fit the DIV. it is only a to wide image that brokes the layout, the height doesn't matter.

It is perhaps better to use scale than resize, I'm coding in ASP and my host doesn't support components, so a real resizing is not able afaik.
Copy linkTweet thisAlerts:
@gil_davisApr 09.2007 — Assuming you specify ID for the DIV and the IMG. After the page loads, you can detect the width of the DIV and set the width of the IMG as necessary. Be aware that if you set width on the IMG without setting height, the IMG will keep the aspect ratio. You also need to be aware of padding and borders to get the size right.
Copy linkTweet thisAlerts:
@dr__jonesauthorApr 09.2007 — ID for DIV and IMG set, so for so done.
×

Success!

Help @dr__jones 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.22,
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,
)...