作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我搜索了 Stackoverflow,但找不到有效的解决方案。
似乎很难在 div 中垂直和水平居中。
我正在使用这个 CSS:
.title {
font-family: "Book Antiqua", "Palatino Linotype", Palatino, serif;
font-size: 400%;
}
.subtitle {
font-family: "Book Antiqua", "Palatino Linotype", Palatino, serif;
font-size: 150%;
}
.subnote {
font-family: "Book Antiqua", "Palatino Linotype", Palatino, serif;
font-size: 75%;
}
.outer {
display: table;
position: absolute;
height: 100%;
width: 100%;
}
.middle {
display: table-cell;
vertical-align: middle;
}
.innie {
width: 1000px;
height: 515px;
background-image: url("bkgrnd3.png");
position:absolute;
left:50%;
top:50%;
margin: -257px 0 0 -500px;
text-align: center;
}
和这个 html:
<!DOCTYPE html>
<html>
<head>
<title>Timothy Eldon Official</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="eldon.css">
</head>
<body>
<div class="outer">
<div class="middle">
<div class="innie">
<span class="title">
Timothy Eldon
</span>
<br/>
<span class="subtitle">
Author
</span>
<br/>
<span class="subnote">
(amateur)
</span>
</div>
</div>
</div>
</body>
</html>
它使页面上的所有内容相对居中,但我无法使文本垂直居中。
似乎有很多想法,但我尝试了几种,但没有任何效果。
最佳答案
这里很简单 DEMO .. 希望对您有所帮助。
HTML
<div>
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</span>
</div>
CSS
div {
display: table;
width: 250px;
height: 500px;
text-align: center;
border: 1px solid red;
}
span {
display: table-cell;
vertical-align: middle;
}
关于css div 将多行文本垂直和水平居中并带有背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34912619/
我是一名优秀的程序员,十分优秀!