html - html中的正文在导航栏上方
问题描述
当我想在我的正文中放置一些文本或在其中放置 div 或部分之类的内容时,我遇到了这个问题,文本位于标题中的导航栏上方。我试图在正文顶部放置一个边距,但它只做了顶部之间的差距更大。
* {
margin: 0;
padding: 0;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
.logo {
float: left;
height: auto;
width: auto;
}
body{
background-color: #444064;
margin-top:10ex;
}
nav {
float: right;
margin-bottom: 50px;
}
nav ul {
margin: 0,auto;
padding: 0,auto;
list-style: none;
}
nav li {
display: inline-block;
margin-right: 2ex;
padding: 5ex;
}
nav a {
color: #ffffff;
font-weight: 600;
font-size: 4ex;
text-decoration: none;
}
nav a:hover {
color: #a34963;
}
section{
text-align: center;
margin-top: 5ex;
}
h1{
font-size: xx-large;
}
<html>
<div id="wrapper">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="testsite.css">
</head>
<header>
<div id="menu">
<img src="Logo.png" alt="logo" class="logo">
<home>
<nav class="menu">
<ol>
<li><a href="#">Home</a></li>
<li><a href="#">About Me</a></li>
<li><a href="#">Skills</a></li>
<li><a href="#">Contact</a></li>
</ol>
</nav>
</home>
</div>
</header>
<body>
Hello world
</body>
</div>
</html>
请帮我
解决方案
您的代码存在许多问题,因为您的 HTML 结构无效。但是您遇到的问题是:nav { float: right; }
这会导致您的导航栏浮动,因此内容可以围绕导航元素流动并显示在上方。如果您删除该行,它将不再发生。
但是,我强烈建议回到一些不错的教程并从基础开始。就像我说的那样,您的 HTML 结构是无效的混乱。一些 css 声明也是不正确/不必要的(例如用于 box-sizing 的 webkit 前缀)。也不要使用浮动作为造型技术。使用现代解决方案,如 flexbox 或 css-grid。
最后但同样重要的是,这就是 html 结构的外观。
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.logo {
float: left;
height: auto;
width: auto;
}
body {
background-color: #444064;
margin-top: 10ex;
}
nav {
margin-bottom: 50px;
}
nav ul {
margin: 0 auto;
padding: 0 auto;
list-style: none;
}
nav li {
display: inline-block;
margin-right: 2ex;
padding: 5ex;
}
nav a {
color: #ffffff;
font-weight: 600;
font-size: 4ex;
text-decoration: none;
}
nav a:hover {
color: #a34963;
}
section {
text-align: center;
margin-top: 5ex;
}
h1 {
font-size: xx-large;
}
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="testsite.css">
</head>
<body>
<header>
<div id="menu">
<img src="Logo.png" alt="logo" class="logo">
<home>
<nav class="menu">
<ol>
<li><a href="#">Home</a></li>
<li><a href="#">About Me</a></li>
<li><a href="#">Skills</a></li>
<li><a href="#">Contact</a></li>
</ol>
</nav>
</home>
</div>
</header>
<div id="wrapper">
Hello world
</div>
</body>
</html>
推荐阅读
- discord.js - CLIENT_MISSING_INTENTS 与 d.js
- terraform - 为不受 terraform 管理的用户创建用户 ID 列表
- python - 在熊猫中 str.replace regex=True 或类似的最有效方法?
- docker - 更改 Docker-compose.yml 以进行端口绑定未正确更新
- c# - 替换为 IFileOpenDialog 的 InvalidCastException
- c# - 为什么最小化屏幕后图片框回到原来的位置?
- python - gem5 构建失败并显示“需要嵌入式 python 库 3.6 或更新版本,找到 2.7.17。”
- git - 特定回购的 Git 推送挂起
- java - 使用 vm 选项 jasypt 私钥的 gradle 不起作用
- python - 如何在不使用'except'python的情况下退出不和谐机器人