展会信息港展会大全

div css 实现文字居中,无需js控制
来源:互联网   发布日期:2016-03-25 16:19:08   浏览:2718次  

导读:!DOCTYPE HTML html head meta http-equiv=content-type content=text/html; charset=utf-8 / meta http-equiv=cache-control content=no-cache / style !-- html, body { height: 100%; margin: 0; padding: 0; } body { position: relative; } #div { back...

<!DOCTYPE HTML>

<html>

<head>

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<meta http-equiv="cache-control" content="no-cache" />

<style>

<!--

html, body {

height: 100%;

margin: 0;

padding: 0;

}

body {

position: relative;

}

#div {

background: blue;

color: #fff;

position: absolute;

top: 50%;

left: 50%;

height: 240px;

width: 240px;

margin: -120px 0 0 -120px;

}

--></style>

</head>

<body>

<div id="div">

i'm Mr. Middle.

</div>

</body>

</html>

方法二

<!DOCTYPE HTML>

<html>

<head>

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<meta http-equiv="cache-control" content="no-cache" />

<style>

<!--

html, body {

height: 100%;

margin: 0 auto;

padding: 0;

}

body {

position: relative;

display: table;

}

#wrapper {

display: table-cell;

vertical-align: middle;

text-align: middle;

}

#div {

background: blue;

color: #fff;

}

* html #wrapper, *+html #wrapper {

position: absolute;

top: 50%;

}

* html #div, *+html #div {

position: relative;

top: -50%;

}

--></style>

</head>

<body>

<div id="wrapper">

<div id="div">

i'm Mr. Middle.

i'm Mr. Middle.

i'm Mr. Middle.

</div>

</div>

</body>

</html>

赞助本站

人工智能实验室

相关热词: 开发 编程 android

相关内容
AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港