Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove unwanted vertical spacing between divs

I've run into a bit of a snag whilst developing the frontend for a website. I'm competent with CSS, but not fantastic. Anyway, I've created a jsFiddle here that illustrates my problem.

On each page of my website, at the top of the content section, I have a banner image. I wish to put a two colour divider seperating this banner from the content. (As is shown in the mockup my designer gave me: https://www.dropbox.com/s/d9opotyiyp0yc9o/menus.jpg)

I'd like to do this in pure CSS+HTML, without just chucking an image in. Anyway, I've done so using the following code:

<img class="banner" src="http://regency.ymindustries.com/static/images/winelist.jpg" style="width: 100%;">
<div>
    <div style="width:30%; height: 10px; display: inline-block; background: #6C210C"></div><div style="width:70%; height:10px; display: inline-block; background: #E5C697;"></div>
</div>

(Please forgive the inline CSS, it's just for demonstration purposes. Also, unfortunately, if I put the second div on a newline and indent it, it creates whitespace)

The issue I'm having is that there is a large gap between the divider and the image. I have tried adding margin: 0px and padding: 0px to all the relevant elements, and the whitespace is still there.

Could someone help me out please?

Thanks, YM

like image 487
Joshua Walsh Avatar asked Jul 21 '13 04:07

Joshua Walsh


People also ask

How do I reduce vertical space in HTML?

HTML allows one to reduce vertical space, using something like p { margin-top: -20px; } or such as shown in number of places on the net. For example how-to-reduce-the-space-between-p-tags.

Why is there extra space in my div?

If you try to put an image inside a <div> element that has borders, you will see an extra white space (around 3px) at the bottom of image. It happens because image is an inline-level element so browser adds some whitespace under the baseline to adjust other inline elements.

How can I remove space between two div tags in bootstrap?

You can add new class to your div elements and remove padding/margin with css. To make it more clear, bootstrap assigns margin-left: -15px and margin-right: -15px for . row selector and padding-left: 15px and padding-right: 15px on all . col-* selectors to get 30px space between all the cols.


2 Answers

To me it's a vertical alignment issue. You can try

.banner {
    display: block;
    width: 100%;
}
div {
    height: 10px;
    vertical-align: top;
}

That way you don't have to use negative margins (which aren't wrong, just controversial practice).

Check it out here

like image 149
Chad Avatar answered Oct 18 '22 00:10

Chad


you can make the position relative and then set the top to something minus. ex:

 position: relative;
 top:-10px;
 left:0px;
like image 20
Daniel Avatar answered Oct 17 '22 23:10

Daniel