当 body 的宽度尚未设置时,如何使用 header 的百分比
how could one use percentage for the header when the width for the body hasn't been set
这是html:
<body>
<!-- Forked from a template on Tutorialzine: https://tutorialzine.com/2016/06/freebie-landing-page-template-with-flexbox -->
<header>
<h2><a href="#">Mountain Travel</a></h2>
<nav>
<li class="new"><a href="#">Tours</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</nav>
</header>
<section class="hero">
<div class="go_crazy"></div>
<div class="background-image" style="background-image: url(assets/img/main.jpg);" >
</div>
<div class="hero-content-area">
<h1>Mountain Travel</h1>
<h3>Unmissable Adventure Tours Around The World</h3>
<span class="old"><a href="#" class="btn">Contact Us Now</a></span>
</div>
</section>
</body>
这是 CSS:
/*Header Styles*/
header {
position: absolute;
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
padding: 35px 100px 0;
animation: 1s fadein 0.5s forwards;
opacity: 0;
color: #fff;
}
在CSS文件中,body的宽度没有设置,但是为什么开发者可以将header的宽度设置为100%??
我认为这意味着 parent 宽度的 100%。那么有人可以为我解释一下吗??
浏览器自动将 <body>
元素的宽度设置为等于浏览器 window 的宽度。
示例:如果您的浏览器 window 是 800px 宽,您可以想象浏览器插入以下内容 CSS:
body {
width: 800px;
}
您在自己的代码中看不到 CSS,但它就在那里,隐藏在浏览器中。所以你认为将 <header>
设置为 100% 宽度意味着它应该是父级宽度的 100% 是正确的,这就是为什么在这种情况下有效。
这是html:
<body>
<!-- Forked from a template on Tutorialzine: https://tutorialzine.com/2016/06/freebie-landing-page-template-with-flexbox -->
<header>
<h2><a href="#">Mountain Travel</a></h2>
<nav>
<li class="new"><a href="#">Tours</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</nav>
</header>
<section class="hero">
<div class="go_crazy"></div>
<div class="background-image" style="background-image: url(assets/img/main.jpg);" >
</div>
<div class="hero-content-area">
<h1>Mountain Travel</h1>
<h3>Unmissable Adventure Tours Around The World</h3>
<span class="old"><a href="#" class="btn">Contact Us Now</a></span>
</div>
</section>
</body>
这是 CSS:
/*Header Styles*/
header {
position: absolute;
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
padding: 35px 100px 0;
animation: 1s fadein 0.5s forwards;
opacity: 0;
color: #fff;
}
在CSS文件中,body的宽度没有设置,但是为什么开发者可以将header的宽度设置为100%?? 我认为这意味着 parent 宽度的 100%。那么有人可以为我解释一下吗??
浏览器自动将 <body>
元素的宽度设置为等于浏览器 window 的宽度。
示例:如果您的浏览器 window 是 800px 宽,您可以想象浏览器插入以下内容 CSS:
body {
width: 800px;
}
您在自己的代码中看不到 CSS,但它就在那里,隐藏在浏览器中。所以你认为将 <header>
设置为 100% 宽度意味着它应该是父级宽度的 100% 是正确的,这就是为什么在这种情况下有效。