简体   繁体   English

为什么我不能在div内居中放置此图像?

[英]Why can't I center this image within the div?

I'm trying to center my image inside the div, but its seemingly glued to the right. 我正在尝试将图像居中放置在div中,但它似乎粘在右侧。

Here are my divs: 这是我的div:

<div align="left" style="display:inline-block;">
    <div style="width:210px;">
        <img src="res/img/TopAd.png" style="top:-100px; padding:10 0 10 0;">
    </div>
</div>

This is what it looks like: 看起来是这样的:

div中的图片

How can I fix this? 我怎样才能解决这个问题?

img tags are inline by default so all you have to do is call text-align: center on its parent: img标签默认是inline ,因此您所要做的就是调用text-align: center于其父对象:

<div style="display:inline-block;">
  <div style="width:210px;text-align: center;">
    <img src="res/img/TopAd.png" style="padding:10px 0;">
  </div>
</div>

A few other notes: 其他一些注意事项:

  1. You can remove align="left" , that's not supported in HTML5. 您可以删除align="left" ,HTML5不支持。
  2. You can also remove top: -100px because the top , left , right , and bottom properties don't work unless an element is set to fixed , absolute or relative . 您还可以删除top: -100px因为除非将元素设置为fixedabsoluterelative ,否则topleftrightbottom属性不起作用。
  3. This line: padding: 10 0 10 0; 这行: padding: 10 0 10 0; is missing px added to it and can be condensed to padding: 10px 0; 缺少添加到其中的px ,可以压缩为padding: 10px 0; which is top and bottom. 这是顶部和底部。

UPDATE UPDATE

if you want to just center all of this in the middle of the page you can add text-align: center to the body but I would suggest removing the first div and adding margin:auto to the second one which has a defined width: 如果你只想中心所有这一切都在您可以添加页面中间text-align: centerbody ,但我会建议删除第一个div并增加margin:auto到具有规定宽度的第二个:

<div style="width:210px;text-align: center; margin: auto;">
   <img src="http://www.placecage.com/200/400" style="padding:10px 0;"/>
</div>

FIDDLE 小提琴

<div align="left"

The align attribute was deprecated in HTML4. 在HTML4中不推荐使用align属性。 Use style="text-align: left;" 使用style="text-align: left;" instead of align="left" . 而不是align="left"

padding:10 0 10 0;

10 of what? 10个是什么? You are missing the units. 您缺少单位。 You probably want 10px for pixels: 您可能希望像素为10px

padding:10px 0 10px 0;

I don't see any code in there that even looks like it's trying to center the image. 我在那里看不到任何代码甚至看起来都在试图居中图像。 Try text-align: center on the container that the image should be centered within. 尝试text-align: center在图像应居中的容器上居中。

 <div style="display:inline-block;"> <div style="width:210px; text-align: center; border: 1px solid #ccc;"> <img src="http://placecage.com/160/160" style="padding: 10px 0 10px 0;" /> </div> </div> 

<div align="left" style="display:inline-block;">
    <div style="width:210px; vertical-align:middle; text-align:center">
        <img src="res/img/TopAd.png" style="top:-100px; padding:10 0 10 0;">
    </div>
</div>

The following code will do the task. 以下代码将完成任务。

<div align="left" style="display:inline-block;">
    <div style="width:210px;" align="center">
    <img src="res/img/TopAd.png" style="top:-100px; padding:10 0 10 0;">
</div>

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

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