我正在学习 CSS 的定位和 HTML 的内容类别。我想知道为什么在 style.css
中的 header
元素内添加 position: absolute;
会导致我的浏览器出现一个狭窄的 header 元素 block 。
我希望 header 是一个 block 级元素并延伸到主体元素的边界,也就是说,触及我的屏幕边界。
index.html
<!DOCTYPE html>
<html>
<head>
<title>Please Participate in Our Survey!</title>
<link href="https://fonts.googleapis.com/css?family=Oswald:300,700|Varela+Round" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<header>
<ul>
<li>Question 1</li>
<li>Question 2</li>
<li>Question 3</li>
<li>Question 4</li>
<li>Question 5</li>
<li>Question 6</li>
</ul>
</header>
<div class="welcome">
<h1>Welcome to our survey!</h1>
<p>We're looking forward to getting your answers so we can make sure our products and services are the best they can be!</p>
</div>
<div class="question">
<h4>Question 1</h4>
<h2>I like participating in physical activity such as running, swimming, or biking.</h2>
<div class="answer">
<h3>Disagree</h3>
</div>
<div class="answer">
<h3>Neutral</h3>
</div>
<div class="answer">
<h3>Agree</h3>
</div>
</div>
<div class="question">
<h4>Question 2</h4>
<h2>I try to keep up to date with the latest fashion in active wear.</h2>
<div class="answer">
<h3>Disagree</h3>
</div>
<div class="answer">
<h3>Neutral</h3>
</div>
<div class="answer">
<h3>Agree</h3>
</div>
</div>
<div class="question">
<h4>Question 3</h4>
<h2>I purchase clothing online regularly.</h2>
<div class="answer">
<h3>Disagree</h3>
</div>
<div class="answer">
<h3>Neutral</h3>
</div>
<div class="answer">
<h3>Agree</h3>
</div>
</div>
<div class="question">
<h4>Question 4</h4>
<h2>I try to buy goods that are designed and/or manufactured in my home country.</h2>
<div class="answer">
<h3>Disagree</h3>
</div>
<div class="answer">
<h3>Neutral</h3>
</div>
<div class="answer">
<h3>Agree</h3>
</div>
</div>
<div class="question">
<h4>Question 5</h4>
<h2>I look to famous athletes when trying to choose what to wear when training.</h2>
<div class="answer">
<h3>Disagree</h3>
</div>
<div class="answer">
<h3>Neutral</h3>
</div>
<div class="answer">
<h3>Agree</h3>
</div>
</div>
</body>
</html>
样式.css
body {
background-color: #FFF;
margin: 0 auto;
}
header {
background-color: #466995;
border-bottom: 1px solid #466995;
position: absolute; // This line!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
border: thin solid red !important;
}
ul {
margin: 30px auto;
padding: 0 20px;
text-align: center;
}
li {
color: #FFF;
font-family: 'Oswald', sans-serif;
font-size: 16px;
font-weight: 300;
text-transform: uppercase;
}
li:hover {
color: #DBE9EE;
}
h1 {
color: #466995;
font-family: 'Oswald', sans-serif;
font-size: 32px;
font-weight: 300;
text-transform: uppercase;
}
h2 {
color: #333;
font-family: 'Varela Round', sans-serif;
font-size: 26px;
font-weight: 100;
margin: 0 auto 20px auto;
}
h3 {
color: #466995;
font-family: 'Oswald', sans-serif;
font-size: 18px;
text-align: center;
font-weight: 700;
text-transform: uppercase;
padding: 30px;
}
h4 {
color: #466995;
font-family: 'Oswald', sans-serif;
font-size: 18px;
font-weight: 300;
letter-spacing: 2px;
text-align: center;
text-transform: uppercase
}
p {
color: #333;
font-family: 'Varela Round', sans-serif;
font-size: 18px;
}
footer {
background-color: #DBE9EE;
text-align: center;
}
.welcome {
background-color: #DBE9EE;
box-sizing: border-box;
padding: 40px;
text-align: center;
width: 100%;
}
.question {
text-align: center;
position: relative;
top: 40px;
}
.answer {
border: 1px solid #466995;
margin: 20px;
}
.answer:hover {
background: #C0D6DF;
color: #FFF;
}
最佳答案
一个流入的、非替换的 block 级框观察到这个等式:
margin-left + border-left-width + padding-left + width +
padding-right + border-right-width + margin-right = width of containing block
如果像您的情况一样,将宽度指定为 auto
,则宽度将被拉伸(stretch)以占据所有必要的空间以使等式成立。
一个绝对定位的、不可替换的 block 级框遵守这种平等:
left + margin-left + border-left-width + padding-left + width +
padding-right + border-right-width + margin-right + right = width of containing block
首先,如果在您的情况下,left
和 right
是 auto
,则宽度是包含内容所需的最小宽度(即它是收缩以适应),并且正确的值被拉伸(stretch)以占据所有必要的空间以使等式成立。
如果将 left
和 right
设置为 0
,则 width
如果是 auto,宽度会被拉伸(stretch)以占据所有必要的空间来保持相等,并且您的标题将增长到触及边框或两侧。
有关更多案例和详细信息,请参阅Section 10.3 Calculating widths and margins CSS 2.2 规范。
关于html - 将 HTML 元素的位置更改为绝对位置是否也会更改该元素的内容类别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53583091/