html - 如何将导航栏放在单独的边框中?
问题描述
我在如何将导航按钮放在单独的边框中遇到问题,如下所示(这个没有弹性框): https ://i.stack.imgur.com/mABM9.png
我需要使用 flexbox 的帮助,以便我可以通过添加必要的功能来改进我的网站。如果有人可以帮助我,我将不胜感激。
*{
margin: 0;
padding: 0;
}
body{
background-image: linear-gradient(to top, rgb(49, 28, 240), rgb(62, 131, 235));
background-attachment: fixed;
background-repeat: no-repeat;
}
/* Heading */
.container1{ /* Contains All Contianers */
display: flex;
justify-content: center;
text-align: center;
}
#header{
font-family: Impact;
border: 2px solid black;
background-color: rgba(169, 169, 169, 0.418);
padding: 25px;
flex: 1;
}
#heading_Text{
font-size: 50px;
color: rgb(230, 230, 230);
}
/* Navigation */
.container2{
display: flex;
justify-content: center;
text-align: center;
align-items: center;
}
#nav{
text-align: center;
}
<!DOCTYPE html>
<html>
<head>
<title>My Sample Website</title>
<link rel="stylesheet" href="style.css" type="text/css">
<!-- Created By Joshua Wong -->
<!-- Updated On: 2/28/2021 -->
</head>
<body>
<div class="container1">
<div id="header">
<h2 id="heading_Text">Sample Website</h2>
</div>
</div>
<div class="container2">
<div id="nav">
<a href="index.html" id="home" class="active">Home</a>
<a href="#" id="about">About</a>
<a href="#" id="info">Information</a>
<a href="#" id="gallery">Gallery</a>
</div>
</div>
</body>
</html>
解决方案
我会让#nav
div 成为一个 flex 容器,并且每个容器都是<a>
宽度的四分之一。见下文,我的补充在最后。您可以根据自己的喜好自定义视觉效果。关键部分是在父级中使用 flex。
如果你不知道第二行会有多少项目,考虑flex-grow
自动增长a
s。
我非常喜欢这个教程和关于 css 中 flex 的参考:https ://css-tricks.com/snippets/css/a-guide-to-flexbox/ 。它有一些很好的例子。
另请注意,您的.container2
div 是不必要的,可以删除。
要调整填充,请在标签上使用padding
css 属性。a
*{
margin: 0;
padding: 0;
}
body{
background-image: linear-gradient(to top, rgb(49, 28, 240), rgb(62, 131, 235));
background-attachment: fixed;
background-repeat: no-repeat;
}
#header{
font-family: Impact;
border: 2px solid black;
background-color: rgba(169, 169, 169, 0.418);
padding: 25px;
justify-content: center;
text-align: center;
}
#heading_Text{
font-size: 50px;
color: rgb(230, 230, 230);
}
/* Navigation */
/*.container2{
display: flex;
justify-content: center;
text-align: center;
align-items: center;
}*/
#nav{
text-align: center;
/* My additions start here*/
width: 100%;
display: flex;
flex-direction: row;
}
#nav > a {
width: 25%;
border: 1px solid black;
}
<!DOCTYPE html>
<html>
<head>
<title>My Sample Website</title>
<link rel="stylesheet" href="style.css" type="text/css">
<!-- Created By Joshua Wong -->
<!-- Updated On: 2/28/2021 -->
</head>
<body>
<div id="header">
<h2 id="heading_Text">Sample Website</h2>
</div>
<div id="nav">
<a href="index.html" id="home" class="active">Home</a>
<a href="#" id="about">About</a>
<a href="#" id="info">Information</a>
<a href="#" id="gallery">Gallery</a>
</div>
</body>
</html>
推荐阅读
- python - 如何安排 shell 命令在 GCP 上的 VM 实例中运行?
- html - 从 Figma 到 HTML 和 CSS
- amazon-web-services - 尽管有权限,但通过 Lambda 函数访问 S3 存储桶时出错
- github - 在 GitHub GraphQL 中,contributionCollection->totalPullRequestContributions 和 user->pullRequests->totalCount 有什么区别?
- reactjs - 为什么 getUserPlaylists() 函数不起作用?
- matlab - Matlab - 如何在提供的示例中转换日期格式
- c# - 如何聚合数据,通过和失败作为最终结果?
- php - Access-Control-Allow-Origin 从本地主机到在线服务器
- java - 将 Generic 扩展为 Comparable 后出现编译错误
- r - 在 stat_poly_eq() 中添加阻塞因子