gpt4 book ai didi

html - 如何使用 css 设置导航区域颜色?

转载 作者:行者123 更新时间:2023-11-28 03:54:29 24 4
gpt4 key购买 nike

我想将我的导航栏区域更改为浅蓝色,我设置了我认为可以做到的 css,但它只将颜色放在我的导航项后面,而不是整个导航栏区域。我如何获得整个导航栏区域下的颜色?

HTML

<!doctype html>
<html>
<head>
<title>Cities Gallery</title>
<link rel="stylesheet" type="text/css" href="css/myStyles.css">
</head>

<body>
<div class="container">

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

<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="london.html">London</a></li>
<li><a href="paris.html">Paris</a></li>
<li><a href="tokio.html">Tokyo</a></li>
</ul>
</nav>


<article>
<div class="intro_text">
<h1>City Navigator</h1>
<p>Welcome to my cities website. Click on a link on a left to view City information</p>
</div>

<img src="images/cities.jpg" alt="Cities" style="width:352px;height:211px;">


</article>
</div>
</body>



<footer>
<p>Posted by: German Jimenez</p>
<p>Contact information: <a href="mailto:german.jimenez@company.com">
German Jimenez</a>.</p>
</footer>
</html>

CSS

div.container{
width:100%;
border:1px solid gray;
}


header, footer{
padding:1em;
color:white;
background-color:black;
clear:left;
text-align:center
}


nav {
float:left;
max-width:160px;
margin:0;
padding:1em;
background-color:lightblue; //this did not work
}


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;
}

最佳答案

我会在 .container 上使用 flex,将其设置为 wrap,将 header 设置为 flex: 0 0 100% 所以它是 flex-basis: 100% 并且会占据一整行。然后将 article 设置为 flex: 1 0 0;(因此它将 flex-grow 填充 nav< 留下的空间)

div.container {
width: 100%;
border: 1px solid gray;
display: flex;
flex-wrap: wrap;
}

header,
footer {
padding: 1em;
color: white;
background-color: black;
clear: left;
text-align: center
}

nav {
max-width: 160px;
margin: 0;
padding: 1em;
background-color: lightblue; //this did not work
}

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

nav ul a {
text-decoration: none;
}

article {
border-left: 1px solid gray;
padding: 1em;
overflow: hidden;
flex: 1 0 0;
}

header {
flex: 0 0 100%;
}
<!doctype html>
<html>

<head>
<title>Cities Gallery</title>
<link rel="stylesheet" type="text/css" href="css/myStyles.css">
</head>

<body>
<div class="container">

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

<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="london.html">London</a></li>
<li><a href="paris.html">Paris</a></li>
<li><a href="tokio.html">Tokyo</a></li>
</ul>
</nav>


<article>
<div class="intro_text">
<h1>City Navigator</h1>
<p>Welcome to my cities website. Click on a link on a left to view City information</p>
</div>

<img src="images/cities.jpg" alt="Cities" style="width:352px;height:211px;">


</article>
</div>
</body>

关于html - 如何使用 css 设置导航区域颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43551383/

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