flex+margin打开你的新大陆

博客介绍了在CSS中,flex与margin结合能大大简化布局。以元素水平垂直居中为例,以往需给父元素设置多个属性,而使用margin+flex,只需在子元素里设置margin:auto,因为在弹性盒子中margin:auto可吃掉元素剩余空间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

大家在最初学习CSS的时候想必对flex,margin已经很熟悉了,但是flex+margin能给你意想不到的结果,它能大大简化我们的布局。

举个例子:

如果我们要想实现一个元素水平垂直居中,以前是这样做的给父元素设置display:flex,justify-content:center,aligin-content:center。使用margin+flex.你只需要在子元素里面设置margin:auto。

在弹性盒子里面设置margin:aoto的意思是吃掉元素剩余的空间。假设设置margin-left:aoto就是吃掉它左边的剩余空间,同理其它边也是一样。

这里就是各吃掉一半,所以就水平居中了。

这种复杂的布局

<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>原神登录注册表单</title> <style> body { font-family: 'Arial', sans-serif; background: url('https://i.imgur.com/3QZQZ9m.jpg') no-repeat center center fixed; background-size: cover; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; } .form-container { background: rgba(0, 0, 0, 0.7); padding: 30px; border-radius: 10px; box-shadow: 0 0 20px rgba(255, 255, 255, 0.2); width: 350px; text-align: center; color: white; } .form-container h2 { font-size: 24px; margin-bottom: 20px; color: #ffcc00; } .form-container input { width: 100%; padding: 10px; margin: 10px 0; border: 1px solid #ccc; border-radius: 5px; background: rgba(255, 255, 255, 0.1); color: white; font-size: 16px; } .form-container input::placeholder { color: #ccc; } .form-container button { width: 100%; padding: 10px; background: #ffcc00; color: #000; border: none; border-radius: 5px; cursor: pointer; font-size: 16px; font-weight: bold; margin-top: 10px; } .form-container button:hover { background: #e6b800; } .toggle-button { background: none; border: none; color: #ffcc00; cursor: pointer; text-decoration: underline; font-size: 14px; } .toggle-button:hover { color: #e6b800; } .hidden { display: none; } </style> </head> <body> <div class="form-container"> <!-- 登录表单 --> <div id="login-form"> <h2>欢迎登录提瓦特大陆</h2> <input type="text" id="login-username" placeholder="用户名" required> <input type="password" id="login-password" placeholder="密码" required> <button type="submit" onclick="login()">登录</button> <p>没有账号?<button class="toggle-button" onclick="toggleForm('register-form', 'login-form')">注册</button></p> </div> <!-- 注册表单 --> <div id="register-form" class="hidden"> <h2>注册提瓦特大陆</h2> <input type="text" id="register-username" placeholder="用户名" required> <input type="password" id="register-password" placeholder="密码" required> <input type="email" id="register-email" placeholder="邮箱" required> <button type="submit" onclick="register()">注册</button> <p>已有账号?<button class="toggle-button" onclick="toggleForm('login-form', 'register-form')">登录</button></p> </div> </div> <script> // 切换表单显示 function toggleForm(showId, hideId) { document.getElementById(showId).classList.remove('hidden'); document.getElementById(hideId).classList.add('hidden'); } // 登录功能 function login() { const username = document.getElementById('login-username').value; const password = document.getElementById('login-password').value; alert(`欢迎回到提瓦特大陆,${username}!\n登录成功!`); } // 注册功能 function register() { const username = document.getElementById('register-username').value; const password = document.getElementById('register-password').value; const email = document.getElementById('register-email').value; alert(`欢迎加入提瓦特大陆,${username}!\n注册成功!`); } </script> </body> </html> 创建一个首页页面
最新发布
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值