简体   繁体   English

在HTML中对齐图像

[英]Aligning Image in HTML

I have an image in the css, but I want to add a link to that image so I pulled it out of the css and inserted directly in the HTML with an anchor tag. 我在CSS中有一个图片,但是我想添加到该图片的链接,因此我将其从CSS中拉出,并使用锚标记直接插入HTML中。

I am having trouble aligning the image to the top left of the div class. 我在将图像对齐div类的左上方时遇到麻烦。

<div class="logoHeader" style="height:125px; width:900px" >
    <a href="http://www.abc123.com/">
        <img src="images/logo.jpg" alt="" style="position:relative;" />
    </a>
</div>

I've tried adding 我尝试添加

style="padding-top:0px; padding-left:0px;"

but the image is floating instead of being in the top left corner. 但图片是浮动的,而不是位于左上角。

Any thoughts on how to get the image to align to the top left corner via the HTML? 关于如何通过HTML使图像与左上角对齐的任何想法?

The div tag css is: div标签css是:

.logoHeader {padding:45px 20px 0px 20px;}

The padding is causing it to float , it's conforming to the rules you set (have a margin of 45px), if you want it in the top left corner: 如果您想在左上角使用填充,填充会导致其浮动 ,并符合您设置的规则(边距为45px):

See http://jsfiddle.net/9KJA7/ for an example 有关示例,请参见http://jsfiddle.net/9KJA7/

/* Ignore */
.logoHeader {background-color: red}
img {height: 50px; width: 50px; background-color: green}

/* Your code */
.logoHeader {padding:45px 20px 0px 20px; position: relative}
​img {position:absolute;top:0px;left:0px;}​​​​​​

.logoHeader上设置的填充将防止图像碰到左上角。

I think you are looking for; 我认为您正在寻找;

position: absolute;
top: 0;
left:0;

Here is the Live demo. 是现场演示。 Or do you want this ? 还是您想要这个

Do you want the image to stay fixed all the time? 您要图像始终保持固定吗? In that case, remove the padding and use this: 在这种情况下,请删除该填充并使用:

.logoHeader {position: fixed; top: 0; left: 0; z-index: 5; padding: 0;}

Fiddle: http://jsfiddle.net/3u8k6/ 小提琴: http//jsfiddle.net/3u8k6/

Check your code: http://jsfiddle.net/3u8k6/1/ 检查您的代码: http : //jsfiddle.net/3u8k6/1/

You can try with position absolute on the img tag, like this: 您可以尝试在img标签上使用绝对位置,如下所示:

<div class="logoHeader" style="height:125px; width:900px; position: relative" ><a href="http://www.abc123.com/"><img src="images/logo.jpg" alt="" style="position:absolute; top:0px; left:0px;" /></a></div>

To make use of position absolute in the image, your div must be position relative. 要在图像中使用绝对位置,您的div必须相对于位置。 I hope this helps. 我希望这有帮助。

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

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