我想知道为什么这些p
元素似乎没有位于其父元素footer
中。
我的想法是,由于p
元素是footer
元素的子元素,因此它们应位于同一位置。但是,当我使用footer
突出显示background-color: red;
的背景时,它们位于完全不同的位置,看起来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>
答案 0 :(得分:2)
您的HTML无效。如果您有used a validator,将会接听。
不允许footer
元素是html
元素的子元素(header
元素也不可以)。您似乎对the body
element的the main
element和the header
element的the nav
element感到困惑。
浏览器执行的错误恢复将footer
和header
元素移动到body
元素内(允许的位置)。
这会使带有选择器body p
的CSS规则集应用于footer
内的段落,并因此移动其呈现的位置。
答案 1 :(得分:0)
问题在于
body p {
position: relative;
width: 50%;
top: 250px;
left: 325px;
}
尝试此操作,它将用于您的HTML
body > p {
position: relative;
width: 50%;
top: 250px;
left: 325px;
}
您的HTML无效。页眉和页脚应位于体内。
答案 2 :(得分:0)
解决方案:
* {
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;
}
footer p{
position: relative;
width: 50%;
top: 0px;
left: 325px;;
}
<!DOCTYPE html>
<html>
<head>
<title>About the Fruit Site</title>
<link href="index.css" type="text/css" rel="stylesheet">
</head>
<body>
<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>
<div>
<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>
</div>
<footer>
<p>Author: Person</p>
<p>Contact: email@place.edu</p>
</footer>
</body>
</html>
答案 3 :(得分:0)
如果将 footer 元素移到 body 元素内,然后相应地修改CSS,则应该解决您的问题。原因是因为 footer 元素不能是 html 元素的子元素。
<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>
<footer>
<p>Author: Person</p>
<p>Contact: email@place.edu</p>
</footer>
</body>