div之间的空间不等

时间:2016-08-12 10:01:22

标签: html css

我有几个div-margin:5px。对我来说不逻辑的是,div之间的间距不相等。如果我在div中插入一些文本,则空间会减少。没有文字,空间增加。

这是example from code pen

看起来像这样:

enter image description here

所有div都有位置:relative和margin-bottom:5px:

.divNote{
  position: relative;
  display: inline-block;  
  left: 20%;
  width: 60%;
  min-height: 60px;  
  margin-bottom: 5px;
  background-color: #FAC28A;
  font-family: verdana, sans-serif;
  font-size: 15px;
  color: #003399;
  padding-left: 5px;
  padding-top: 1px;
  padding-bottom: 20px;
  box-shadow: 3px 3px 3px gray;    
  box-sizing: border-box;
}

2 个答案:

答案 0 :(得分:2)

float:left设置为.divNote这将有效。



@border: 1px;

.divMain{
  position: absolute;
  height: 100vh;
  width: 100vw;  
}

.divNote{
  position: relative;
  display: inline-block;  
  left: 20%;
  width: 60%;
  min-height: 60px;  
  margin-bottom: 5px;
  background-color: #FAC28A;
  font-family: verdana, sans-serif;
  font-size: 15px;
  color: #003399;
  padding-left: 5px;
  padding-top: 1px;
  padding-bottom: 20px;
  box-shadow: 3px 3px 3px gray;    
  box-sizing: border-box;
  float:left;
}

.divNoteDatum{
  position: absolute;
  display: inline-block;   
  font-weight: bold;      
  bottom: 0;
  right: 5px;
  color: #003399;
}

.divUpper{
  position: relative;
  height: 20%;
}

<div id="divMain" class="divMain">

  <div class="divUpper"></div>

  <div id="divNote" class="divNote">
    test<br/>test<br/>test
    <div id="divNoteDatum" class="divNoteDatum">12.08.2016 11:08</div>
  </div>
  
    <div id="divNote" class="divNote">
    
    <div id="divNoteDatum" class="divNoteDatum">12.08.2016 11:08</div>
  </div>
  
    <div id="divNote" class="divNote">
    test<br/>test<br/>test
    <div id="divNoteDatum" class="divNoteDatum">12.08.2016 11:08</div>
  </div>
  
    <div id="divNote" class="divNote">
    test<br/>test<br/>test
    <div id="divNoteDatum" class="divNoteDatum">12.08.2016 11:08</div>
  </div>
  
    <div id="divNote" class="divNote">
    
    <div id="divNoteDatum" class="divNoteDatum">12.08.2016 11:08</div>
  </div>
  
  
    <div id="divNote" class="divNote">
    
    <div id="divNoteDatum" class="divNoteDatum">12.08.2016 11:08</div>
  </div>
  
    <div id="divNote" class="divNote">
    
    <div id="divNoteDatum" class="divNoteDatum">12.08.2016 11:08</div>
  </div>


</div>
<!-- divMain -->
&#13;
&#13;
&#13;

答案 1 :(得分:1)

为内联块divNote元素添加vertical-align:top。内联块元素的内容溢出会导致此问题。使用vertical-align

总是很好的做法

@border: 1px;

.divMain{
  position: absolute;
  height: 100vh;
  width: 100vw;  
}

.divNote{
  position: relative;
  display: inline-block;  
  float: left;
  left: 20%;
  width: 60%;
  min-height: 60px;  
  margin-bottom: 5px;
  background-color: #FAC28A;
  font-family: verdana, sans-serif;
  font-size: 15px;
  color: #003399;
  padding-left: 5px;
  padding-top: 1px;
  padding-bottom: 20px;
  box-shadow: 3px 3px 3px gray;    
  box-sizing: border-box;
  vertical-align: top;
}

.divNoteDatum{
  position: absolute;
  display: inline-block;   
  font-weight: bold;      
  bottom: 0;
  right: 5px;
  color: #003399;
}

.divUpper{
  position: relative;
  height: 20%;
}
<div id="divMain" class="divMain">

  <div class="divUpper"></div>

  <div id="divNote" class="divNote">
    test<br/>test<br/>test
    <div id="divNoteDatum" class="divNoteDatum">12.08.2016 11:08</div>
  </div>
  
    <div id="divNote" class="divNote">
    
    <div id="divNoteDatum" class="divNoteDatum">12.08.2016 11:08</div>
  </div>
  
    <div id="divNote" class="divNote">
    test<br/>test<br/>test
    <div id="divNoteDatum" class="divNoteDatum">12.08.2016 11:08</div>
  </div>
  
    <div id="divNote" class="divNote">
    test<br/>test<br/>test
    <div id="divNoteDatum" class="divNoteDatum">12.08.2016 11:08</div>
  </div>
  
    <div id="divNote" class="divNote">
    
    <div id="divNoteDatum" class="divNoteDatum">12.08.2016 11:08</div>
  </div>
  
  
    <div id="divNote" class="divNote">
    
    <div id="divNoteDatum" class="divNoteDatum">12.08.2016 11:08</div>
  </div>
  
    <div id="divNote" class="divNote">
    
    <div id="divNoteDatum" class="divNoteDatum">12.08.2016 11:08</div>
  </div>


</div>
<!-- divMain -->