White space above second inline-block div

前端 未结 5 1992
囚心锁ツ
囚心锁ツ 2021-02-02 11:05

I\'m sure I\'ve overlooked something here but cannot work it out. There\'s white space above my second inline-block div, and this only occurs when the \'text here\' length in th

相关标签:
5条回答
  • 2021-02-02 11:34

    This appears to be a better, cleaner solution: (Example)

    <div class="box">
        <hgroup>
            <h2>Title</h2>
            <h3>Subtitle</h3>
        </hgroup>
        <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor
            quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper.
            Aenean.</p>
    </div>
    
    <style type="text/css">
        .box {
            border-top:    1px dashed black;
            display:       inline-block;
            width:         250px;
            margin-bottom: 10px;
            margin-right:  10px;
            margin-top:    0;
        }
    
        .box hgroup {
            height:         50px;
            padding-top:    2px;
            padding-bottom: 2px;
            text-align:     right;
            font-size:      11px;
            border-left:    100px rgb(205, 205, 205) solid;
        }
    
        .box h2 {
            font-size:   21px;
            margin:      0;
            font-weight: normal;
        }
    
        .box h3 {
            font-weight: normal;
        }
    
        .box p {
            background-color: #efefef;
            height:           75px;
            padding:          5px;
            font-size:        12px;
        }
    </style>
    
    0 讨论(0)
  • 2021-02-02 11:37

    I tried pasting html for 1st block in next one and it worked w/o problem.

    <div style=" border-top: 1px dashed black; display: inline-block; width: 250px; margin-bottom: 10px; margin-right: 10px; margin-top: 0;">
    
        <div style="height: 50px; padding-top: 2px; padding-bottom: 2px; text-align:right; font-size: 11px;">
            <div style="display: block; width: 80px; height: 50px; float: left; background-color: #cdcdcd; background-position: left center;">
            </div>
    
            <span class="main_header" style="font-size: 21px; margin: 0;">Title</span>
            <br />
            Subtitle
    
        </div>
    
        <div style="display:block; background-color: #efefef; height: 75px; padding: 5px; font-size: 12px;">
        Text here Text here Text here Text here Text here Text here Text here Text here Text here Text here Text here Text here Text here Text here Text here Text here Text here
        </div>
    </div>
    
    <div style=" border-top: 1px dashed black; display: inline-block; width: 250px; margin-bottom: 10px; margin-right: 10px; margin-top: 0;">
    
        <div style="height: 50px; padding-top: 2px; padding-bottom: 2px; text-align:right; font-size: 11px;">
            <div style="display: block; width: 80px; height: 50px; float: left; background-color: #cdcdcd; background-position: left center;">
            </div>
    
            <span class="main_header" style="font-size: 21px; margin: 0;">Title</span>
            <br />
            Subtitle
    
        </div>
    
        <div style="display:block; background-color: #efefef; height: 75px; padding: 5px; font-size: 12px;">
        Text here Text here Text here Text here Text here Text here Text here Text here Text here Text here Text here Text here Text here Text here Text here Text here Text here
        </div>
    </div>
    

    You can see updated fiddle here: http://jsfiddle.net/B2S4r/6/

    0 讨论(0)
  • 2021-02-02 11:37

    If you add float:left to both divs, your problem will be resolved.

    HTML:

    <div class="article">
        <div class="header">
            <div class="grayBox"></div>
    
            <span class="main_header">Title</span><br />
            Subtitle
        </div>
    
        <div class="content">
            Text here Text here Text here Text here Text here Text here Text here Text here Text here Text here Text here Text here Text here Text here Text here Text here Text here 
        </div>
    </div>
    
    <div class="article">
        <div class="header">
            <div class="grayBox"></div>
    
            <span class="main_header">Title</span><br />
            Subtitle
        </div>
    
        <div class="content">
            Text here Text here Text here Text here Text here Text here Text here Text here Text 
        </div>
    </div>​
    
    <div class="clear"></div>
    

    CSS:

    .article {
        border-top: 1px dashed black; 
        display: inline-block; 
        width: 250px; 
        margin-bottom: 10px; 
        margin-right: 10px; 
        margin-top: 0;
        float:left;        
    }
    
    .header {
        height: 50px; 
        padding-top: 2px; 
        padding-bottom: 2px; 
        text-align:right; 
        font-size: 11px;    
    }
    
    .main_header {
        font-size: 21px; 
        margin: 0;   
    }
    
    .grayBox {
        display: block; 
        width: 80px; 
        height: 50px; 
        float: left; 
        background-color: #cdcdcd; 
        background-position: left center;    
    }
    
    .content {
        display:block; 
        background-color: #efefef; 
        height: 75px; 
        padding: 5px; 
        font-size: 12px;
    }​
    
    .clear {
        clear:both;
    }​
    

    Live DEMO

    0 讨论(0)
  • 2021-02-02 11:43

    Default value of vertical-align is baseline and when applied to blocks of different heights, it's often unwanted.

    Applying a value of top will solve your problem. Here's a working fiddle: http://jsfiddle.net/PhilippeVay/B2S4r/3/ (as there's no stylesheet in your fiddle but only inline CSS, I won't even try to find how to aim for the one on the right)

    0 讨论(0)
  • 2021-02-02 11:49

    a quick solution is adding a float:left to both divs...

    <div style=" border-top: 1px dashed black; display: inline-block; width: 250px; margin-bottom: 10px; margin-right: 10px; margin-top: 0; float: left;">
    ...
    </div>
    

    (and please use css ;) )

    0 讨论(0)
提交回复
热议问题