<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 8 年前。
<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 8 年前。
我想拍摄一张 Logo 图像,将其准确居中放置在屏幕中间,同时保持图像的原始大小。然后就在该图像下方(- 5px 向下),放一个小文本行,我在其中选择它的字体。
当我尝试这样做时,一切都搞砸了,所以我会把基本的。我怎样才能实现这个简单的目标?
<html>
<body>
<img src="images/logoBlackBig.png" alt="W3Schools.com" >
</body>
</html>
最佳答案
您可以使用 css transform 来实现此目的和 absolute
位置是这样的:
JSFiddle - DEMO
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
* {margin: 0; padding: 0;}
.div {
position: absolute;
top: 50%;
left: 50%;
-o-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
border: 1px solid #000;
text-align: center;
}
.img {
vertical-align:middle;
}
.p {
margin: 0;
position: absolute;
bottom: 0;
width: 100%;
color: #000;
font-size: 24px;
}
</style>
</head>
<body>
<div class="div">
<img class="img" src="http://media.tumblr.com/tumblr_m6cfbeVrUy1qegis6o1_400.gif" alt="W3Schools.com">
<p class="p">MY TEXT</p>
</div>
</body>
</html>
关于html - 使用 CSS 居中图像(水平和垂直),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26316705/