Center Oversized Image in Div

Mobile Technologies Mobile Computing 2 years ago

2 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating
_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.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

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

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.

Important Mobile Technologies Links