【问题标题】:why is my content showing outside the div?为什么我的内容显示在 div 之外?
【发布时间】:2020-07-02 16:03:53
【问题描述】:

我有一个包含内容的“气泡”,它运行良好。现在,我想显示一个计数(2 行),它应该始终位于该 div 的右下角,在它里面。我尝试了很多东西,但由于某种原因,它总是与 div 重叠并在外面显示。我做错了什么?

<style type="text/css">
body{
background-color:#f3f3f3;
}
.commentbox{
background-color: #ffffff;
width: 200px;
border-color: #D1D1D1;
border-radius: 4px;
border-style: solid;
border-width: 1px; 
padding-bottom: 9px;
padding-left: 9px;
padding-right: 9px;
padding-top: 9px;
position:relative;
}
.count{
float:right;
text-align:right;
}
</style>

<div class="commentbox">
<div class="title">Some several lines long long long long content text goes here 
</div>
<div class="count">123<br>456</div>
</div>

【问题讨论】:

    标签: css html


    【解决方案1】:

    你正在浮动.count,所以它不会影响它的父容器的高度。

    在父级 (.commentbox) 上设置 overflow: hidden 或使用 one of the other float containing techniques 以便这样做。

    【讨论】:

    • @SangramNandkhile — 链接已修复,但问题中的方法仍然几乎总是最好的。
    【解决方案2】:

    你真的需要float: right; 来换取.count 吗?我认为text-align 应该足以满足所需的布局。

    【讨论】:

      【解决方案3】:

      因为您已经在父 div 上使用了position:relative。试试这个:

      .count {
         position:absolute;
         right:0;
         bottom:10px;
      }
      

      【讨论】:

      • 这可能会使计数文本与标题文本重叠。
      【解决方案4】:

      可能您必须在“计数”div 之后添加一个清除。

      <style type="text/css">
      body{
      background-color:#f3f3f3;
      }
      .commentbox{
      background-color: #ffffff;
      width: 200px;
      border-color: #D1D1D1;
      border-radius: 4px;
      border-style: solid;
      border-width: 1px; 
      padding-bottom: 9px;
      padding-left: 9px;
      padding-right: 9px;
      padding-top: 9px;
      position:relative;
      }
      .count{
      float:right;
      text-align:right;
      }
      </style>
      
      <div class="commentbox">
      <div class="title">Some several lines long long long long content text goes here 
      </div>
      <div class="count">123<br>456</div>
      <div style="clear: both"></div>
      </div>
      

      【讨论】:

      • 添加额外的标记来包含浮点数是解决问题的一种非常丑陋的方法。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-04
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多