Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysterious Padding

Tags:

html

css

layout

This is a much-simplified version of a layout I'm working on. I've pared it down for berevity and to make the problem I'm having more apparent.

I cannot determine where the padding between elements wrapped in 'wrapper1' is coming from. Ideally I shouldn't be able to see any of the red from wrapper1's background and everything inside should be snug up against each other.

Any ideas?

CSS

.wrapper1 {
    float:left;
}

.wrapper2 {
    background-color:#F00;
}

.box-outer {
    display:inline-block;
    background-color:#09F;
    width:50px;
    height:108px;
}

.wrapper3 {
    display:inline-block;
    background-color:#390;
    border:2px solid #000;
}

.box-inner {
    background-color:#999;
    position:relative;
    border:2px solid #FF0;
    width:300px;
    height:100px;
}

HTML

<div class="wrapper1">
    <div class="wrapper2">
        <div class="box-outer"></div>
        <div class="wrapper3">
            <div class="box-inner">
            </div>
        </div>
    </div>  
        <div class="wrapper2">
        <div class="box-outer"></div>
        <div class="wrapper3">
            <div class="box-inner">
            </div>
        </div>
    </div>
        <div class="wrapper2">
        <div class="box-outer"></div>
        <div class="wrapper3">
            <div class="box-inner">
            </div>
        </div>
    </div>
</div>
like image 538
RobJSchultz Avatar asked May 18 '26 15:05

RobJSchultz


2 Answers

You turned the inner divs into inline elements, which means that they will use the white space between them.

Write it like ...</div><div ... and the gaps will go away.

Edit: oh, that's just the horizontal space taken care of. The vertical space is caused by the inner divs being positioned on the baseline and the outer div leaving room below the baseline. vertical-align should solve that.

like image 87
Mr Lister Avatar answered May 22 '26 09:05

Mr Lister


Mr. Lister is right that it's the white space giving you issues with inline-block.

Rather than have to fight this by removing white space from your markup...

Unless you have a compelling reason to use inline-block, it seems this layout works fine by replacing those instances with float:left (which in turn, implicitly applies display:block).

Demo: http://jsfiddle.net/yV89U/

like image 43
Wesley Murch Avatar answered May 22 '26 08:05

Wesley Murch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!