html - <p> 元素似乎不在父 &lt;footer&gt; 元素中

标签 html css

我想知道为什么这些 p 元素似乎没有定位在它们的父元素 footer 中。

我的想法是,由于 p 元素是 footer 元素的子元素,因此它们应该位于同一位置。但是当我使用 background-color: red; 突出显示 footer 的背景时,它们位于完全不同的位置,看起来像 p内容从页面底部的 footer 框中移开,而 footer 似乎位于中心。这是我一直在使用的代码:

* {
  margin: 0px;
  padding: 0px;
}

body {
  background-color: hsl(190, 33%, 58%);
  height: 500px;
}

header {
  background-color: hsl(190, 33%, 35%);
  position: fixed;
  width: 100%;
  height: 50px;
  border: 3px solid black;
  z-index: 2;
}

header .headerpos {
  position: relative;
  left: 135px;
}

header .headerpos h2 {
  display: inline;
  padding-left: 10px;
  padding-right: 10px;
  padding-top: 12px;
  padding-bottom: 12px;
  position: relative;
  top: 10px;
  margin-left: 10px;
  margin-right: 10px;
}

body p {
  position: relative;
  width: 50%;
  top: 250px;
  left: 325px;
}

footer {
  position: relative;
  top: 300px;
  text-align: center;
  height: 100px;
  width: 100%;
  background-color: red;
}
<header>
  <div class="headerpos">
    <a href="home.html">
      <h2>HOME</h2>
    </a>
    <a href="fruit.html">
      <h2>FRUIT</h2>
    </a>
    <a href="about.html">
      <h2>ABOUT</h2>
    </a>
  </div>
</header>

<body>
  <p>
    The fruit site is an independent project that was created for the purpose of developing my skills with HTML and CSS. While the appearance and layouts are naive, the more I experiment with things, the more I learn!
  </p>
</body>
<footer>
  <p>Author: Person</p>
  <p>Contact: email@place.edu</p>
</footer>

最佳答案

您的 HTML 无效。如果你有used a validator,这将被拾取.

footer 元素不允许是 html 元素的子元素(header 元素也不允许)。你好像搞糊涂了the body element对于the main elementthe header element对于the nav element .

浏览器执行的错误恢复将 footerheader 元素移动到 body 元素内(允许它们的位置)。

这会导致带有选择器 body p 的 CSS 规则集应用于 footer 内的段落,并因此移动它们的渲染位置。


  1. 编写有效的 HTML
  2. 调整您的 CSS 选择器以匹配您更正后的 HTML

关于html - <p> 元素似乎不在父 &lt;footer&gt; 元素中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56674807/

相关文章:

javascript - AngularJS - 专注于嵌套滚动中的对象

javascript - Jquery 单击函数不适用于弹出框内的按钮

'memory' 中下一篇文章的 Javascript (Jquery) 代码

html - 段落中文本上方的位置跨度

html - Mozilla firefox 表列拆分不起作用

javascript - 网页不会随着窗口大小的调整而调整

html - 更改下拉菜单的背景颜色 - 移动

javascript - 如何更改 Chartist.js 中图表的线条颜色

php - 无法使用php将图像上传到mysql

html - 在没有高度或宽度的父项中居中子项