【问题标题】:White space above second inline-block div第二个内联块 div 上方的空白
【发布时间】:2012-06-21 21:11:21
【问题描述】:

我确定我在这里忽略了一些东西,但无法解决。我的第二个内联块 div 上方有空白,只有当右侧 div 中的“文本此处”长度小于左侧时,才会出现这种情况。

jsFiddle:http://jsfiddle.net/B2S4r/2/(您需要使 HTML 视图更宽才能看到它们并排显示)

<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 
    </div>
</div>​

【问题讨论】:

  • 首先,我会从摆脱所有内联 css 开始。很难看出到底发生了什么。
  • 点了。我通常从不使用内联 css,但在找到正确的属性之前使用它似乎更容易,然后将它们分开。
  • 我看不出问题和答案中给出的 jsfiddle 有什么区别。它们看起来都一样。

标签: html css


【解决方案1】:

vertical-align的默认值是baseline,当应用于不同高度的块时,通常不需要它。

应用 top 值将解决您的问题。这是一个有效的小提琴:http://jsfiddle.net/PhilippeVay/B2S4r/3/(因为你的小提琴中没有样式表,只有内联 CSS,我什至不会尝试找到如何瞄准右边的那个)

【讨论】:

    【解决方案2】:

    一个快速的解决方案是在两个 div 中添加 float:left...

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

    (请使用 css ;))

    【讨论】:

      【解决方案3】:

      我尝试将 html 粘贴到下一个块中的第一个块中,但没有问题。

      <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>
      

      你可以在这里看到更新的小提琴:http://jsfiddle.net/B2S4r/6/

      【讨论】:

        【解决方案4】:

        如果你把float:left添加到两个div,你的问题就解决了。

        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

        【讨论】:

          【解决方案5】:

          这似乎是一个更好、更清洁的解决方案:(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>
          

          【讨论】:

          • 非常优雅的解决方案,我以前从未遇到过hgroup 标签。希望我能接受 > 1 个答案,谢谢。
          • @sooper: hgroup 是一个 HTML5 元素。
          • 旧帖子,但其他人偶然发现了这一点。不幸的是,hgroup 已从规范中删除。 html5doctor.com/the-hgroup-element
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-07-19
          • 2013-09-30
          • 2019-06-19
          • 2012-02-26
          • 1970-01-01
          • 1970-01-01
          • 2017-05-01
          相关资源
          最近更新 更多