简体   繁体   English

相对于HTML-CSS中图像的居中标题

[英]Centring heading relative to image in HTML-CSS

I have floated an image left using: 我使用以下方法浮动了图像:

img {
    float:left;
}

and have also centred a heading using: 并使用以下内容将标题居中:

<h1 align="center">Text</h1>

The heading moves position as the image is inserted so its no longer centred, its pushed to the right. 插入图像时,标题移动的位置使其不再居中,而是向右推动。 How can I centre the heading again? 如何再次将标题居中? I have tried a lot of different CSS properties for the heading but cant seem to get it working. 我已经为标题尝试了许多不同的CSS属性,但似乎无法正常工作。 Thanks. 谢谢。

try fiddle 尝试摆弄

http://jsfiddle.net/aqofdz8u/ http://jsfiddle.net/aqofdz8u/

<div>
    <img src="http://drublic.de/blog/wp-content/uploads/2011/10/rotate-images.jpg" width="220" height="220" alt="A photo of me.">
    <h1>Text</h1>
</div>

CSS 的CSS

img {
    float:left;
}

h1 { text-align: center; }

HTML 的HTML

<div>
    <img src="http://www.nnsheriff.org/img/placeholder/blogpost-placeholder-100x100.png" alt="">
     <h1>Text</h1>
</div>

CSS 的CSS

img {
    float:left;
}
h1 {
    text-align: center;
    margin-left:10px;
    vertical-align:top;
    margin:0;
    padding:0;
}

Fiddle Sample 小提琴样本

To make h1 be centered regading the whole page you need to add position: absolute to the img and add img and h1 inside a div with position relative. 为了使h1居中重整整个页面,您需要在img中添加position:absolute,并在div中将img和h1添加到相对位置。

 .wrap { position: relative; } img { position: absolute; top: 0px; left: 0px; } h1 { color: red; font-family: Trebuchet MS; font-size: 250%; text-align: center; } 
 <div class="wrap"> <img src="http://blog.grio.com/wp-content/uploads/2012/09/stackoverflow.png" alt="Highdown logo" /> <h1>Sports day</h1> </div> 

 .wrap { position: relative; } img { position: absolute; top: 0px; left: 0px; } h1 { color: red; font-family: Trebuchet MS; font-size: 250%; text-align: center; } 
 <div class="wrap"> <img src="http://blog.grio.com/wp-content/uploads/2012/09/stackoverflow.png" alt="Highdown logo" /> <h1>Sports day</h1> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM