gpt4 book ai didi

css - 导航栏定位左上角

转载 作者:行者123 更新时间:2023-11-28 02:03:07 25 4
gpt4 key购买 nike

我是 HTML CSS 和 JS 的新手,正在创建一个网站,现在遇到了几天的问题,我创建了一个无法在页面之间切换的导航,但我似乎无法将其正确定位在左上角我想知道是否有人可以提供帮助。我希望它正好位于左上角,但它似乎错位了。我尝试了很多解决方案,但似乎无法获得我想要的结果。

body {
margin-left: 50px;
margin-right: auto;
background-color:
font-family: Arial, Helvetica, sans-serif;
width: 800px;

}

.topnav {
background-color: #333;
overflow: hidden;

}


.topnav a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 12px 16px;
text-decoration: none;
font-size: 20pt;
}


.topnav a:hover {
background-color: #ddd;
color: black;
}


.topnav a.active {
color: white;
}

html {
background: url(UFC.jpg);
background-attachment: fixed;
background-position: center center;
background-repeat: no-repeat;
background-size: cover;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="website.CSS">
</head>
<body>
<div class="topnav">
`enter code here` <a class="active" href="home.html"><u>Home</u></a>
<a href="topfighters.html"><u>Top Fighters</u></a>
<a href="bestknockouts.html"><u>Best Knockouts</u></a>
<a href="contactpage.html"><u>Contact Page</u></a>
<a href="https://twitter.com/ItsssOwen">
<img src="twitter%20link.png" width="42" height="32" border="0">
</a>
</div>
</body>
</html>

最佳答案

可以通过css position修改元素的位置,指定top、left、right、bottom位置(只需要一个x和一个y)。

body {
margin-left: 50px;
margin-right: auto;
background-color:
font-family: Arial, Helvetica, sans-serif;
width: 800px;

}

.topnav {
background-color: #333;
overflow: hidden;
position: fixed;
top: 0;
left: 0;
}


.topnav a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 12px 16px;
text-decoration: none;
font-size: 20pt;
}


.topnav a:hover {
background-color: #ddd;
color: black;
}


.topnav a.active {
color: white;
}

html {
background: url(UFC.jpg);
background-attachment: fixed;
background-position: center center;
background-repeat: no-repeat;
background-size: cover;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="website.CSS">
</head>
<body>
<div class="topnav">
`enter code here` <a class="active" href="home.html"><u>Home</u></a>
<a href="topfighters.html"><u>Top Fighters</u></a>
<a href="bestknockouts.html"><u>Best Knockouts</u></a>
<a href="contactpage.html"><u>Contact Page</u></a>
<a href="https://twitter.com/ItsssOwen">
<img src="twitter%20link.png" width="42" height="32" border="0">
</a>
</div>
</body>
</html>

但是你的问题是你指定了一个

 margin-left: 50px;

对于你的 body ,使元素向右移动50px,你可以通过删除它来达到它:

body {
background-color:
font-family: Arial, Helvetica, sans-serif;
width: 800px;

}

.topnav {
background-color: #333;
overflow: hidden;

}


.topnav a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 12px 16px;
text-decoration: none;
font-size: 20pt;
}


.topnav a:hover {
background-color: #ddd;
color: black;
}


.topnav a.active {
color: white;
}

html {
background: url(UFC.jpg);
background-attachment: fixed;
background-position: center center;
background-repeat: no-repeat;
background-size: cover;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="website.CSS">
</head>
<body>
<div class="topnav">
`enter code here` <a class="active" href="home.html"><u>Home</u></a>
<a href="topfighters.html"><u>Top Fighters</u></a>
<a href="bestknockouts.html"><u>Best Knockouts</u></a>
<a href="contactpage.html"><u>Contact Page</u></a>
<a href="https://twitter.com/ItsssOwen">
<img src="twitter%20link.png" width="42" height="32" border="0">
</a>
</div>
</body>
</html>

关于css - 导航栏定位左上角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49193472/

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