Center Oversized Image in Div

Mobile Technologies Mobile Computing 2 years ago

2 2 0 0 0

_x000D_ _x000D_ I have been trying to sort out how to center an oversized image within a div using css only. We are using a fluid layout, so the width of the image containers varies as the page width does (height of div is fixed). The image sits within a div, withvalue an inset boxshadow so as to appear as if you are looking through the page at the image. The image itself has been sized to fill the surrounding div at its widest possible value (the design has a max-width value). It is pretty easy to do if the image is smaller than the surrounding div: margin-left: auto; margin-right: auto; display: block; But when the image is larger than the div it simply starts at the left edge and is off center to the right (we are using overflow: hidden). We could assign a width=100%, but browsers do a lousy job of resizing images and the web design centers around high quality imagery. Any ideas on centering the image so that overflow:hidden cuts off both edges evenly?

Posted on 01 Sep 2022, this text provides information on Mobile Computing related to Mobile Technologies. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ Late to the game, but I found this method is extremely intuitive. https://codepen.io/adamchenwei/pen/BRNxJr CSS .imageContainer { border: 1px black solid; width: 450px; height: 200px; overflow: hidden; } .imageHolder { border: 1px red dotted; height: 100%; display:flex; align-items: center; } .imageItself { height: auto; width: 100%; align-self: center; } HTML
2 views
0 shares

profilepic.png
Pranav Atre 4 months ago
Thanks for the info sir
2 views   0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum


Q

Would Firebase be a good choice as a server for the Flutter mobile game I'm creating?

_x000D_ _x000D_ I am building a turn-based Flutter game. Would Firebase be a good choice as a server...
Q

Implement Bottom Navigation activity along with a VerticalViewPager with nested Cardview in Android...

_x000D_ _x000D_ I want to develop an Android App which will have a Bottom Navigation Bar and above i...
Q

(JS) For , determine if the device offers or not to capture a photo from the camera

_x000D_ _x000D_ When uploading a file, I need to give the user the possibility: upload the file fr...

Important Mobile Technologies Links