html页面在浏览器居中,如何使元素在浏览器窗口中间居中?

本文介绍了一种使用纯CSS实现元素精确居中的方法,无需借助JavaScript。通过设置特定的CSS属性,可以使文本或元素始终保持在页面的正中央,并附带了具体的代码示例。

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

这完全可以通过CSS实现——不需要JavaScript:

Here's an example

下面是该示例背后的源代码:

Dead Centre

body

{

color: white;

background-color: #003;

margin: 0px

}

#horizon

{

color: white;

background-color: transparent;

text-align: center;

position: absolute;

top: 50%;

left: 0px;

width: 100%;

height: 1px;

overflow: visible;

visibility: visible;

display: block

}

#content

{

font-family: Verdana, Geneva, Arial, sans-serif;

background-color: transparent;

margin-left: -125px;

position: absolute;

top: -35px;

left: 50%;

width: 250px;

height: 70px;

visibility: visible

}

.bodytext

{

font-size: 14px

}

.headline

{

font-weight: bold;

font-size: 24px

}

#footer

{

font-size: 11px;

font-family: Verdana, Geneva, Arial, sans-serif;

text-align: center;

position: absolute;

bottom: 0px;

left: 0px;

width: 100%;

height: 20px;

visibility: visible;

display: block

}

a:link, a:visited

{

color: #06f;

text-decoration: none

}

a:hover

{

color: red;

text-decoration: none

}

-->

This text is

DEAD CENTRE

and stays there!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值