gpt4 book ai didi

javascript - 如何将div定位在中心并将其隐藏?

转载 作者:太空宇宙 更新时间:2023-11-03 21:01:17 25 4
gpt4 key购买 nike

我想做一个登录页面,但是它隐藏在主页的中心。

然后我使用 jquery 使其周围的 div 可见,它就像 www.bytes.com 上的登录页面。

但我不知道如何使用 CSS 将其居中。它不会在不影响主页 div 位置的情况下工作。

我只是想让它浮在主页上。有人吗?

最佳答案

这是你想要的吗?注意 CSS 注释。让 div 居中很重要。 position: absolute 是让您网站的其他部分 float 的“技巧”。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Centering a DIV with CSS</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
</script>
<style type="text/css">

.center_center {
position: absolute;
background-color: #ccc;
border: 1px solid #000;
width: 300px;
height: 200px;
/* notice: margins are -(value/2) */
margin-left: -150px;
margin-top: -100px;
top: 50%;
left: 50%;
display: none;
}
</style>
</head>
<body>
<div id='login' class="center_center">
Login content
</div>

<p><input type="button" value="Login" onclick="$('#login').show()" /></p>
</body>
</html>

关于javascript - 如何将div定位在中心并将其隐藏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1843350/

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