gpt4 book ai didi

javascript - html span 类导致链接错误地居中

转载 作者:行者123 更新时间:2023-11-28 06:52:14 24 4
gpt4 key购买 nike

我一直在尝试为我正在制作的游戏制作自己的网站。我一直在尝试让所有链接都集中在一个 div 类中,而另一个 div 则水平显示每个选项卡的一些信息。但是为了将文本放在水平线上,我不得不使用 span 类,这导致了链接 1 居中的一些问题。知道如何正确居中该链接吗?

网站链接:https://mywebsite-rowanharley.c9users.io/main.php

代码:

<!DOCTYPE html>
<html>
<head>
<title>My Site - Home</title>
<!--<link rel="stylesheet" type="text/css" src="main.css">-->
<style type="text/css">
#sidebarlink{
display: block;
color: white;
width: 200px;
line-height: 50px;
font-size: 20px;
}
#sidebarlink/*.sidebarlink*/:link {
width:200px;
background-color: black;
color:white;
text-decoration:none;
}
#sidebarlink/*.sidebarlink*/:visited{
width:200px;
background-color: black;
color:white;
}
#sidebarlink/*.sidebarlink*/:hover{
width:200px;
background-color: #454545;
color: white;
text-decoration: none;
}
#sidebarlink/*.sidebarlink*/:active{
width:200px;
background-color: black;
color: white;
text-decoration: none;
}
#sidebar{
width: 200px;
height: 100%;
background-color: black;
}
#playerFriendsDiv{
/*display: inline;*/
float: left;
position: relative;
left: 200px;
}
#showYourInfoDiv{
/*display: inline;*/
float: left;
position: relative;
left: 200px;
}
#logoutDiv{
/*display: inline-block;*/
float: left;
position: relative;
left: 200px;
}
html{
height: 100%;
}
body{
margin: 0px;
height: 100%;
}
</style>
</head>
<body>
<div id="showYourInfoDiv">
test1
</div>
<div id="playerFriendsDiv">
test2
</div>
<div id="logoutDiv">
test3
</div>
<div id="sidebar" style="text-align: center">
<div id="sidebarlinkdiv">
<a onclick="showyourinfo()" id="sidebarlink">Player info</a>
</div>
<div id="sidebarlinkdiv">
<a onclick="showfriends()" id="sidebarlink">Friends</a>
</div>
<div id="sidebarlinkdiv">
<a onclick="showlogout()" id="sidebarlink">Logout</a>
</div>

</div>

<script type="text/javascript">
document.getElementById("showYourInfoDiv").style.display = "block";
document.getElementById("playerFriendsDiv").style.display = "none";
document.getElementById("logoutDiv").style.display = "none";
function showyourinfo(){
document.getElementById("showYourInfoDiv").style.display = "block";
document.getElementById("playerFriendsDiv").style.display = "none";
document.getElementById("logoutDiv").style.display = "none";
}
function showfriends(){
document.getElementById("showYourInfoDiv").style.display = "none";
document.getElementById("playerFriendsDiv").style.display = "block";
document.getElementById("logoutDiv").style.display = "none";
}
function showlogout(){
document.getElementById("showYourInfoDiv").style.display = "none";
document.getElementById("playerFriendsDiv").style.display = "none";
document.getElementById("logoutDiv").style.display = "block";
}
</script>

</body>

</html>

我也试过在中心标签中包围链接

最佳答案

如果你想让“玩家信息”链接居中,只需将 position: absolute 添加到 #sidebar div 样式即可。

此外,将 id="sidebarlink" 更改为 class="sidebarlink" 并将 css #sidebarlink 更改为 .sidebarlink,因为只能有一个具有给定 id 的元素,它应该是唯一的。

演示:

https://jsfiddle.net/90Lgck79/1/

关于javascript - html span 类导致链接错误地居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33707261/

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