CSS Image Max-width Set To Original Image Size? - Stack Overflow
Có thể bạn quan tâm
-
- Home
- Questions
- Tags
- Users
- Companies
- Labs
- Jobs
- Discussions
- Collectives
-
Communities for your favorite technologies. Explore all Collectives
- Teams
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Try Teams for free Explore Teams - Teams
-
Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams
Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about CollectivesTeams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about TeamsGet early access and see previews of new features.
Learn more about Labs CSS image max-width set to original image size? Ask Question Asked 10 years, 6 months ago Modified 4 years, 11 months ago Viewed 143k times 91Can you do this? I'm having users upload images into a container that is width:100%, but I don't want the images to be larger than their original size. Many thanks!
Share Improve this question Follow asked Jun 13, 2014 at 4:27 nikk wongnikk wong 8,6106 gold badges55 silver badges71 bronze badges 2- 3 The image should naturally be the size that it is. Are you setting the image to width:100 also? – Theodore Enderby Commented Jun 13, 2014 at 4:30
- 3 No. You cannot. There is no way for CSS to know the actual size of a random image. You'd need javascript to detect the image size and scale the image. – Koala Yeung Commented Jun 13, 2014 at 4:32
2 Answers
Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 143Just don't set the width of the image, only the max-width.
img {max-width:100%; height:auto}(height:auto is not really necessary, since auto is the default, but I put it in there as a reminder to myself that I want the image to have its natural proportions.)
This snippet has two boxes, one that is smaller than the image and one that is larger. As you can see, the image in the smaller box gets scaled down, while the one in the bigger box has its normal size.
div {border:2px outset green; margin:6px 0} .box1 {width:100px; height:70px;} .box2 {width:200px; height:100px;} img {max-width:100%; height:auto} <div class="box1"> <img src="https://i.sstatic.net/FuQYf.png" alt="" /> </div> <div class="box2"> <img src="https://i.sstatic.net/FuQYf.png" alt="" /> </div>
Share Improve this answer Follow edited Jan 26, 2020 at 8:12 answered Jun 13, 2014 at 6:22 Mr ListerMr Lister 46.5k15 gold badges113 silver badges155 bronze badges 13- 10 If someone is wondering (like me) how to override an inherited width setting, just add width:auto in the new class that you will assign to the image. – grasskode Commented Mar 29, 2016 at 15:10
- 8 This sets the max width to the size of the container, rather than to the original (or natural) size of the image. – Tom Commented Mar 26, 2020 at 22:32
- 3 That’s not how I read the OPs requirements. He’s asking for it to not exceed the original / natural dimensions, rather than to not exceed the container. – Tom Commented Mar 28, 2020 at 11:00
- 2 @MrLister developer.mozilla.org/en-US/docs/Web/CSS/max-width max-width as a percentage defines the max-width as a percentage of the containing block's width – Tom Commented Apr 1, 2020 at 18:00
- 1 What is working here is the absence of setting a width, which typically means an image sizes itself according to native size. What max-width does is set a limit how big the display size can be. At least this is what happens in modern browsers in 2020, but I think this was commonly the case in 2014 as well. I think Mr Lister said as much already, just clarifying, – MiB Commented Dec 1, 2020 at 19:20
You can set the max width of the image in a style tag on the image itself. Then in the style sheet set the display type to "block", the width to whatever percentage of the container you want, and the height to auto. Then add margin: 0px auto to that and it should center perfectly and will never be larger that it's original size.
If these are user-uploaded images and you are using PHP for example, find the image width by using getImageSize() and add the style tag programmatically to the image.
Share Improve this answer Follow answered Apr 13, 2015 at 18:51 Nancy Hastings-TrewNancy Hastings-Trew 1571 bronze badge Add a comment |Your Answer
Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Draft saved Draft discardedSign up or log in
Sign up using Google Sign up using Email and Password SubmitPost as a guest
Name EmailRequired, but never shown
Post Your Answer DiscardBy clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Not the answer you're looking for? Browse other questions tagged
or ask your own question.- The Overflow Blog
- Why do developers love clean code but hate writing documentation?
- This developer tool is 40 years old: can it be improved?
- Featured on Meta
- The December 2024 Community Asks Sprint has been moved to March 2025 (and...
- Stack Overflow Jobs is expanding to more countries
Linked
31 How do I get natural dimensions of an image using JavaScript or jQuery? 6 How do I prevent Mozilla from up-scaling images on HiDPI displays? 2 Responsively resize an image using CSS3, relative to the width of its parent element, without exceeding its original size 0 How to stretch or shrink image to fit min and max size with preserving image ratio?Related
112 How to set max width of an image in CSS 0 Image css max width without distort 0 resize image to fit in div with a max width set 1 HTML: set max width for image within div 3 CSS: Set Max Width of an Image to Its Actual Pixel Width 0 Max-width property on an image 0 Why can't I set max-width of an image without resizing 3 Max-width on img prevents image from shrinking 2 Image with max-width, and set height 0 Image not shrinking when I try max widthHot Network Questions
- YubiKey 5C NFC not recognized on Silicon MacBook with macOS Sonoma (14.6.1)
- More efficient way to color-code cycle permutation list
- Merits of `cd && pwd` versus `dirname`
- Disk galaxy definition
- How was fraud by false representation charged in this case?
- “Through a door into a parallel universe” movie
- PSE Advent Calendar 2024 (Day 21): Wrap-Up
- Should one avoid making a negative double with a weak four card major?
- Is a second, different, claim on the same matter Res Judicata
- When shouldn't I use possessive s?
- What do "messy" weapons do, exactly?
- Copyright on photographic reproductions of old paintings
- Meaning of the diameter of a space-distorting object
- How is the associator defined in the Eilenberg-Moore category of a monoidal monad?
- Permanent night on a portion of a planet
- Are special screws required inside an oven?
- Errors while starting vite + react
- Why were my lead-acid batteries destroyed after operating them in parallel?
- Network activity halting at every 45s
- Single-producer single-consumer queue
- What's wrong with my formal translation of "every positive number has exactly two square roots"?
- Why do some installers insist on not doing a full frame window replacement?
- If you are working remotely as a contractor, can you be allowed to applying as a business vistor to Australia?
- Is 骰子 pronounced "shăi zi" or "tóu zi"?
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
defaultTừ khóa » Html Original Image Size
-
How To Resize An Image In HTML?
-
How To Get Original Image Size (Width & Height) In JavaScript
-
HTML Img Width Attribute - W3Schools
-
Responsive Web Design Images - W3Schools
-
Setting Height And Width On Images Is Important Again
-
How To Resize An Image With HTML - Computer Hope
-
How To Resize Images In HTML
-
HTML Tag: Change The Width Of A Picture In HTML »
-
zes - Web APIs | MDN
-
Responsive Images - Learn Web Development | MDN
-
Best Image Sizes And How To Save Images For The Web (2022)
-
Resize Images In Adobe Photoshop Elements
-
Advanced Cropping, Resizing, And Resampling - Adobe Support
-
Can I Change The Size Of An Image? - Codecademy