I have the following html code:
<div style="border: 3px coral solid;width:400px;height:400px;background:#D4D0C8;">
<img style="max-width:400px;max-height:400px;" src="myImage.jpg">
</div>
Using these styles images with a width > 400 pixels are resized but remain at the top of the div. Is there some way to vertically center them?
Approach: Create a div tag to place the images. In the <img> tag, provide the path of the images using the src attribute and an alternative text using the alt attribute. Add CSS properties to display the images in a vertical alignment.
To center both vertically and horizontally, use padding and text-align: center : I am vertically and horizontally centered.
Step 1: Wrap the image in a div element. Step 2: Set the display property to "flex," which tells the browser that the div is the parent container and the image is a flex item. Step 3: Set the justify-content property to "center." Step 4: Set the width of the image to a fixed length value.
CSS:
div {
border: 3px solid coral;
width: 400px;
height: 400px;
background: #d4d0c8;
line-height: 400px;
text-align: center;
}
img {
max-width:400px;
max-height:400px;
vertical-align: middle;
}
See demo fiddle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With