How to Remove White Space Under an Image Using CSS
In this tutorial, we are going to see how to Remove White Space Under an Image Using CSS. If you try to place an image in a <div> block, you will see extra white space (approximately 4 or 5 pixels) at the bottom of the image. This happens because the image is an “inline” element, so the browser adds spaces below the line to adjust the other “inline” elements.
The most convenient way to get rid of this problem is to change the display property of the image using the value “block” instead of “inline”, as shown in the example below:
CSS Code:
.box1, .box2 { width: 200px; border: 4px solid black; } .box2 img { display: block; }
HTML Code:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Remove White Space Under an Image Using CSS</title> <style> /* Put the CSS Style Here */ </style> </head> <body> <h3>Default behavior</h3> <div class="box1"> <img src="https://1.bp.blogspot.com/-RAj_WmWtT5A/XSY-zUdWKbI/AAAAAAAAFFM/MzWiRE8G3m8k6aYHr9nsxfOzjiK5ZPzmwCLcBGAs/s1600/test.jpg"> </div> <br> <h3>After changing the 'display' property</h3> <div class="box2"> <img src="https://1.bp.blogspot.com/-RAj_WmWtT5A/XSY-zUdWKbI/AAAAAAAAFFM/MzWiRE8G3m8k6aYHr9nsxfOzjiK5ZPzmwCLcBGAs/s1600/test.jpg"> </div> </body> </html>
Result |
---|
Default behavior![]() After changing the 'display' property![]() |
It’s not working for me….
HTML
CSS
.book_cover{
margin-left: auto;
margin-right: auto;
width: 25%;
height: 71%;
border: 2px solid black;
display: block;
}
.book_cover a{
opacity: .5;
}
.book_cover :hover{
opacity: 1;
}
.book_cover img {
display: block;
}