gpt4 book ai didi

html - 使固定标题的宽度与容器 div 的大小相同

转载 作者:行者123 更新时间:2023-11-28 05:10:44 24 4
gpt4 key购买 nike

header 元素位于 div 容器内。我将 header 设置为 fixed 。它会导致 2 个问题:

  1. header 的宽度超出了div 元素的宽度。它应该与 div 匹配。
  2. 当我将 header 设置为固定时,其他元素 navarticle 不可见。

现在是这样的: output

这是我希望它出现的方式: Image2

这是我的代码片段:

<!DOCTYPE html>
<html>
<head>
<style>
div.container {
width: 100%;
border: 1px solid gray;
}
header{
position:fixed;
width:100%;
right:0px;
}
header, footer {
padding: 1em;
color: white;
background-color: black;
clear: left;
text-align: center;
}

nav {
float: left;
max-width: 160px;
margin: 0;
padding: 1em;
}

nav ul {
list-style-type: none;
padding: 0;
}

nav ul a {
text-decoration: none;
}

article {
margin-left: 170px;
border-left: 1px solid gray;
padding: 1em;
overflow: hidden;
}
</style>
</head>
<body>

<div class="container">

<header>
<h1>City Gallery</h1>
</header>

<nav>
<ul>
<li><a href="#">London</a></li>
<li><a href="#">Paris</a></li>
<li><a href="#">Tokyo</a></li>
</ul>
</nav>

<article>
<h1>London</h1>
<p>London is the capital city of England. It is the most populous city in the United Kingdom, with a metropolitan area of over 13 million inhabitants.</p>
<p>Standing on the River Thames, London has been a major settlement for two millennia, its history going back to its founding by the Romans, who named it Londinium.</p>
</article>

<footer>Copyright © W3Schools.com</footer>

</div>

</body>
</html>

最佳答案

这给了我你想要的结果:

header {
/* Everything else is same as your code*/
position: relative;
}
header,footer {
/* Everything else is same as your code*/
padding: 1em 0;
}
  1. 您将 div 宽度设置为 100%,将其父项的标题宽度设置为 100%,然后将 padding left 和 right 设置为 1em(padding: 1em -> 它的 padding from all 4 sides, padding:1em 0 -> top&bottom 1em,左右 0).
  2. 你为什么设置固定?您需要标题粘在顶部吗?
div.container {
width: 100%;
border: 1px solid gray;
}
header{
position:fixed;
width:100%;
}
header, footer {
/*padding: 1em 0;*/
color: white;
background-color: black;
clear: left;
text-align: center;
}
header h1 {
line-height: 2em;
font-size: 2em;
}
nav {
float: left;
max-width: 160px;
margin: 0;
padding: 1em;
margin-top: 6em;
}
nav ul {
list-style-type: none;
padding: 0;
}
nav ul a {
text-decoration: none;
}
article {
margin-left: 170px;
border-left: 1px solid gray;
padding: 1em;
overflow: hidden;
margin-top: 6em;
}

关于html - 使固定标题的宽度与容器 div 的大小相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39579318/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com