Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

div container larger than image inside [duplicate]

Tags:

css

once again was hoping you could help me out.

I am a novice at css and trying to build something simple, but even simple is turning out to be really difficult :(. been at this problem for like an hour now.

any ways the problem I have right now is that I have a div that has an image inside, but the div has like 5px space extra at the bottom which I just cannot figure out how to remove.

basically this is what I have

enter image description here

as you can see there is a little "white" space below the image.

this is what I want (edited using paint)

enter image description here

basically I have a gray background for body, with a wrapper inside with a width of 70%,

inside the wrapper there is a menu (the little red thing is part of that menu), then there is a div (id=container) whos background is white, width is 142.8571%, and left is 15% so that it covers the whole page. then inside this div is the image.

so basically the problematic div is the div with ID container.

this is my code for the relevent part

HTML:

<div id="container">

    <img id="banner-img" src="img/banner.jpg">

</div>

CSS:

#container{

    background-color: white;

    padding-bottom:0px;
    margin-bottom: 0px;
    border-bottom-width: 0px;


    width: 142.8571%;

    position:  relative;
    left: -21.428571%;




}

#banner-img{
    position: relative;
    margin: 0px auto 0px auto;
    width: 70%;
    left: 15%;
    height: auto;
    padding: 0px;
    border: 0px;
    position: relative;

}
like image 803
Ahmed-Anas Avatar asked Jul 12 '12 08:07

Ahmed-Anas


People also ask

Why is div bigger than image?

The reason behind your issue is that you did not specify the width of the container but, in the same time, you set a width: 100%; for the image.

How do I resize a div to fit an image?

To auto-resize an image or a video to fit in a div container use object-fit property. It is used to specify how an image or video fits in the container. object-fit property: This property is used to specify how an image or video resize and fit the container.

How do I make images the same size in a container?

Answer: Use the CSS max-width Property You can simply use the CSS max-width property to auto-resize a large image so that it can fit into a smaller width <div> container while maintaining its aspect ratio.

Why are my divs different sizes?

It happens because your <main> have a fixed height: 400px. If you remove it height rule, it goes up, but due to your float rule, the element would be undocked. Try this solution: jsfiddle.net/alexndreazevedo/cqx09mam The problem is a set of rules: float, position, display... Too complex to explain.


Video Answer


3 Answers

now used to this

img, #banner-img{
vertical-align: top;
}
like image 129
Rohit Azad Malik Avatar answered Sep 30 '22 17:09

Rohit Azad Malik


Another solution would we to set the image to block:

img { display: block; }
like image 25
PeeHaa Avatar answered Sep 30 '22 18:09

PeeHaa


The blank space at the bottom is probably down to the CR/LF between your image tag and end div tag. Some browsers don't like this. There's probably a CSS workaround, but I tend to remove the CR/LF's and put it all on one line, like this:

<div id="container"><img id="banner-img" src="img/banner.jpg"></div>
like image 30
AW101 Avatar answered Sep 30 '22 18:09

AW101