[英]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: 看起来是这样的:
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: 其他一些注意事项:
align="left"
, that's not supported in HTML5. align="left"
,HTML5不支持。 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
因为除非将元素设置为fixed
, absolute
或relative
,否则top
, left
, right
和bottom
属性不起作用。 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: center
到body
,但我会建议删除第一个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>
<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.