[HTML]<div>标签绝对定位设置内容绝对居中

本文介绍了一种使用CSS实现网页元素在不同分辨率下绝对居中的方法,通过设置特定的margin值和百分比位置,使得指定的div元素无论在何种屏幕尺寸下都能保持水平和垂直方向上的居中。

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

绝对居中:即在客户端上任何分辨率下纵横方向均居中

涉及知识点:

CSS类、id属性定义
DIV标签绝对定位属性

样例代码:
必需的重点部分由红色标记

<html>
<head>
<title>Nice and Free CSS Template 11</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css"><!--
/*   body und schrift deffinitionen */
body {
background-color: #e1ddd9;
font-size: 12px;
font-family: Verdana, Arial, Helvetica, SunSans-Regular, Sans-Serif;
color:#564b47;
padding:0px;
margin:0px;

}
#inhalt {
position:absolute;
height:200px;
width:400px;

margin:-100px 0px 0px -200px;
top: 50%;
left: 50%;

text-align: left;
padding: 0px;
background-color: #f5f5f5;
border: 1px dotted #000000;
overflow: auto;
}
p, h1 {
margin: 0px;
padding: 10px;
}
h1 {
font-size: 11px;
text-transform:uppercase;
text-align: right;
color: #564b47;
background-color: #90897a;
}
a {
color: #ff66cc;
font-size: 11px;
background-color:transparent;
text-decoration: none;
}
--></style>
</head>
<body>
<div id="inhalt">

<p><b>centered</b><br /><br />
This area should be horizontally and vertically centered.<br />
This text stays left aligned<br />
<b>ie mac doesn’t like this! </b><br />
<a href="/">more nice and free css templates</a>
</p>
<p>
</div>
</body>
</html>

注意:#inhalt的定义中 height和width值应分别是margin中第一参数、第四参数的两倍,否则不居中。

转载于:https://www.cnblogs.com/cn23317/archive/2009/06/28/absolute_center.html

<!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; margin: 0; padding: 0; display: flex; flex-direction: column; min-height: 100vh; } header { background-color: #51addb; color: white; padding: 1rem; text-align: center; } nav { background-color: #ff7829; } nav ul { list-style-type: none; margin: 0; padding: 0; display: flex; justify-content: center; } nav ul li { padding: 1rem; } nav ul li a { color: white; text-decoration: none; } main { flex: 1; padding: 1rem; } footer { background-color: #51addb; color: white; text-align: center; padding: 1rem; margin-top: auto; } .content { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 1rem; } .content article { background-color: #f4f4f4; padding: 1rem; border: 1px solid #ddd; border-radius: 8px; } @media (max-width: 768px) { nav ul { flex-direction: column; align-items: center; } .content { grid-template-columns: 1fr; } } .content article { transition: transform 0.3s ease, background-color 0.3s ease; } .content article:hover { background-color: #e0f7fa; transform: scale(1.05); box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); } .content article { position: relative; perspective: 1000px; height: 200px; background-color: transparent !important; } .card-front, .card-back { position: absolute; width: 100%; height: 100%; backface-visibility: hidden; transition: all 0.6s ease; padding: 1rem; border-radius: 8px; background-color: #f4f4f4; box-sizing: border-box; } .content article:hover .card-front { transform: rotateY(180deg); } .content article:hover .card-back { transform: rotateY(360deg); } .content article:hover { transform: scale(1.1) !important; box-shadow: 0 10px 20px rgba(0, 0, 0, 0.3) !important; background-color: transparent !important; } body { transition: background-color 0.5s ease; } .content:hover { background-color: #e0f7fa; } </style> </head> <body> <header> <h1>校园活动公告</h1> </header> <nav> <ul> <li><a href="#">首页</a></li> <li><a href="#">活动日历</a></li> <li><a href="#">关于我们</a></li> </ul> </nav> <main> <section class="content"> <article> <div class="card-front"> <h2>校园音乐会</h2> <p><strong>时间:</strong>2025年10月15日 18:00</p> <p><strong>地点:</strong>学校大礼堂</p> <p>欢迎全体师生参加,享受一场音乐盛宴!</p> </div> <div class="card-back"> <p>欢迎全体师生参加,享受一场音乐盛宴!</p> </div> </article> <article> <div class="card-front"> <h2>编程马拉松</h2> <p><strong>时间:</strong>2025年10月20日 09:00</p> <p><strong>地点:</strong>计算机学院实验室</p> <p>与同学们一起挑战编程极限,赢取丰厚奖品!</p> </div> <div class="card-back"> <p>与同学们一起挑战编程极限,赢取丰厚奖品!</p> </div> </article> <article> <div class="card-front"> <h2>校园运动会</h2> <p><strong>时间:</strong>2025年10月25日 08:00</p> <p><strong>地点:</strong>学校操场</p> <p>参与各项运动项目,展现你的运动风采!</p> </div> <div class="card-back"> <p>参与各项运动项目,展现你的运动风采!</p> </div> </article> </section> </main> <footer> <p>© 2025 校园活动中心 | 联系方式:activity@school.edu</p> </footer> </body> </html> 以上代码的运行结果不符合我预期的效果,当鼠标移动至卡片上时翻转一百八十度,但是没有对齐,帮我修改一下
最新发布
08-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值