简体   繁体   English

将图像居中放置在锚点内

[英]centering an image inside an anchor

I am trying to horizontally center an image that is contained within an anchor tag.我试图将包含在锚标记中的图像水平居中。 I have tried setting it as a block and setting margins to auto with no luck.我尝试将其设置为块并将边距设置为自动但没有运气。 I have tried wrapping it in a div tag and doing the same, still, no luck.我试过将它包装在一个 div 标签中并做同样的事情,但仍然没有运气。 Even tried centering the img within the div within the div.甚至尝试在 div 中将 img 居中。 I looked at other posts, and nothing seems to help.我看了其他帖子,似乎没有任何帮助。 The image is still aligned to the left no matter what I do.无论我做什么,图像仍然与左侧对齐。

###HTML ###HTML

<div id="slideWrapper">
    <h1 id="slideHeading">This is a Header</h1> 
    <hr/>       
    <div class="centerMe">
        <a href="javascript:slidelink()" class="slideA">
            <img src="./images/Slides/cheeseBoards.jpg" name="slide" id="slideImg"/>
        </a>
    </div>
    <hr/>
    <div class="centerMe">
        <a href="javascript:slidelink()"class="slideA" id="slideText">Check out our services and other great stuff.</a>
    </div>
</div>

###CSS ###CSS

.centerMe {
    margin-left: auto;
    margin-right: auto;
    border: 1px solid green;
    text-align: center;
}
#slideWrapper {
    border: 1px solid blue; 
}
#slideImg {
    display: block;
    height:500px;
    width:900px;
    border:1px solid red;
    margin-left: auto;
    margin-right: auto;
}

I put borders around everything so that I could see where things were getting placed.我在所有东西周围设置了边界,以便我可以看到东西放置的位置。 It seems like the image isn't being contained within the div for some reason.由于某种原因,图像似乎没有包含在 div 中。 This seems like it should be so simple.这似乎应该如此简单。 Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks.谢谢。

Make sure there is no float:left inherited from parents.确保没有 float:left 从父母那里继承。 margin:0 auto;保证金:0 自动; does not work when you have float:left;当你有 float:left 时不起作用; or position:absolute;或位置:绝对;

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

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