Tim*_*Tim 5 html css image center
我搜索了 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;
}
Run Code Online (Sandbox Code Playgroud)
和这个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>
Run Code Online (Sandbox Code Playgroud)
它使页面上的所有内容相对居中,但我无法使文本垂直居中。
似乎有很多想法,但我尝试了几种,但没有任何效果。
这是简单的DEMO ..希望有帮助。
超文本标记语言
<div>
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</span>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS
div {
display: table;
width: 250px;
height: 500px;
text-align: center;
border: 1px solid red;
}
span {
display: table-cell;
vertical-align: middle;
}
Run Code Online (Sandbox Code Playgroud)
你有一些不必要的CSS,例如margin: -257px 0 0 -500px; left:50%;
top:50%; width: 1000px; height: 515px;on .innie,它会破坏你的中心对齐方式。
我想这就是你想要的。
更新
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%;
background:url('https://placeimg.com/600/300/any')no-repeat;
background-position: center;
}
.middle {
display: table-cell;
vertical-align: middle;
}
.innie {
text-align: center;
}
Run Code Online (Sandbox Code Playgroud)
jsFiddle 演示 - https://jsfiddle.net/8dphnor5/