html - 为什么在应用填充时 <p> 结束在另一个 <div> 中,而不是在其中扩展 div?

标签 html css css-float

为什么是<p>结束于另一个 <div>应用填充时,而不是在其中扩展 div?

这是我的代码。最终,我试图向 <p> 添加一些填充。元素将其向下推,但在其 div 内。

我将在右上角有一个按钮,我希望它下方有文本,但不被按钮覆盖。按钮将是 z-index:999 以及与其 div 的相对位置。

编辑:这是完整代码的外观,试图包含内容。 Clearfix 在这里不起作用,按钮有 z-index:1 所以应该在最上面?

  .clearfix::after {
    display: block;
    content: "";
    clear: both;
  }
  
  .header {
    background-color: bisque;
  }
  
  .wrap {
    max-width: 960px;
  }
  
  .content h1 {
    display: inline-block;
    padding-top: 0px;
  }
  
  .content p {
    float: right;
    padding-top: 0px;
  }
  
  .button {
    background-color: red;
    position: relative;
    right: 0;
    top: 0;
    z-index: 1;
    float: right;
  }
<header class="header ">
  <div class="wrap clearfix">
    <div class="content ">
      <h1>left</h1>
      <p>right</p>
      <a href="#" class="button">button</a>
    </div>
  </div>
</header>

最佳答案

这是因为元素是 float 的,父类需要“清除” float ,这样元素才不会出现在父类之外。您可以查找 CSS“clearfix”技术以清楚地说明问题和解决方法。

我添加了一个新的 .clearfix 类,其中包含一种流行的清除 float 子元素的技术。

.clearfix::after {
  display: block;
  content: "";
  clear: both;
}

.first {
    background-color: bisque;
  }
  
  .first h1 {
    display: inline-block;
    padding-top: 10px;
  }
  
  .first p {
    float: right;
    display: inline-block;
    padding-top: 15px;
  }
  
  .second {
    background-color: grey;
  }
  
  .third {
    background-color: teal;
  }
<div class="first clearfix">
      <h1>I expand "first" div with padding</h1>
      <p>I do not expand, I am outside "first" div. End up on top of another div... Why?? </p>
    </div>
    
    <div class="second">
      <p>section 2 text</p>
    </div>
    
    <div class="third">
      <p>section 3 text</p>
    </div>
    

关于html - 为什么在应用填充时 <p> 结束在另一个 <div> 中,而不是在其中扩展 div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42318204/

相关文章:

javascript - 使用什么来创建在 apache2 上运行的蜂鸣器 Web 应用程序

html - 为导航栏的图像添加文本

javascript - 为什么使用window.navigator.userAgent检索浏览器Explorer 11被识别为Mozilla?如何检索用户代理和版本?

javascript - 我想将网页从顶部导航按钮滚动到同一网页的多个选项卡

css - 网站中心出现问题

html - 有没有比重复 ../更简单的 HTML 路径方法?

javascript - 如何更新多张图片的 'src'属性?

javascript - 激活 jQuery Accordion 脚本后从 Div 底部额外填充

css - 不使用负边距的基本 Css float 查询

css - Fieldset不会展开父Div float