源码教程 2025年06月7日
0 收藏 0 点赞 1,001 浏览 2665 个字
摘要 :

要实现一个登css实现让登录框始终处于页面的中心位置 要实现一个登录框始终位于页面的中心位置,你可以使用CSS的多种方法。下面是一些常用的方法: 方法1:使用Flexbox ……

实现一个登css实现让登录始终处于页面中心位置

css实现让登录框始终处于页面的中心位置

要实现一个登录框始终位于页面的中心位置,你可以使用CSS的多种方法。下面是一些常用的方法:

方法1:使用Flexbox

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Centered Login Form</title>
<style>
  body, html {
    height: 100%;
    margin: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #f0f0f0;
  }
  .login-form {
    background-color: white;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  }
</style>
</head>
<body>
<div>
  <h2>Login</h2>
  <form>
    <label for="username">Username:</label>
    <input type="text" id="username" name="username"><br><br>
    <label for="password">Password:</label>
    <input type="password" id="password" name="password"><br><br>
    <input type="submit" value="Login">
  </form>
</div>
</body>
</html>

方法2:使用Grid

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Centered Login Form</title>
<style>
  body, html {
    height: 100%;
    margin: 0;
    display: grid;
    place-items: center; /* This is a shorthand for align-items and justify-items */
    background-color: #f0f0f0;
  }
  .login-form {
    background-color: white;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  }
</style>
</head>
<body>
<div>
  <h2>Login</h2>
  <form>
    <label for="username">Username:</label>
    <input type="text" id="username" name="username"><br><br>
    <label for="password">Password:</label>
    <input type="password" id="password" name="password"><br><br>
    <input type="submit" value="Login">
  </form>
</div>
</body>
</html>

方法3:使用定位(Positioning)和Transform(变换)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Centered Login Form</title>
<style>
  body, html {
    height: 100%;
    margin: 0;
  }
  .login-form {
    position: absolute; /* or fixed */
    top: 50%; /* positions the top edge of the element at the middle of the parent */
    left: 50%; /* positions the left edge of the element at the middle of the parent */
    transform: translate(-50%, -50%); /* moves the element back by its own width and height */
    background-color: white;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  }
</style>
</head>
<body>
<div>
  <h2>Login</h2>
  <form>
    <label for="username">Username:</label>
    <input type="text" id="username" name="username"><br><

方法4:使用calc()函数,并且让margin 左右为auto

// 首先需要获取登录框的整体高度,设置父元素高度宽度为100%,这里假设高度为登录框高度为500px
.container{
    width:100%;
    height:100%;
}
.login{
    margin:calc((100% - 500px)/2) auto;
}


微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/1945.html

管理员

相关推荐
2025-07-05

对于一些使用WordPress进行外贸建站的商家来说,大部分人会通过在WordPress中添加JS代码和CSS样式表…

700
2025-07-05

当商家遇到WordPress独立站改版或者域名到期等问题时,不免会涉及到WordPress域名的更改,那么这一…

714
2025-07-05

用户在使用WooCommerce进行跨境电商独立站搭建工作时,有可能会借助WooCommerce短代码实现更加醒目…

305
2025-07-05

随着外贸建站行业的不断深入发展,WordPress的多语言功能也显得越来越重要,一个具有多语言的独立站…

1,038
2025-07-05

WooCommerce作为WordPress外贸建站生态的一部分,WooCommerce运费设置是商家在建站过程中不可忽视的…

834
2025-07-05

在外贸建站的过程中,有些商家会选择使用WordPress幻灯片为网站页面增加一定的动感和可观性,进而提…

721
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号