HTML 图片调整大小和居中

标签 html image resize

虽然我已经阅读了很多试图调整我的图像大小的帖子,但它不起作用,有人可以帮助我将图像居中并缩小吗?

HTML:

<div id="mainBody">
<div id="image">
    <img src="http://s10.postimg.org/6b22cnbfd/jpg">
</div>

CSS:

.mainBody{
    width: 100%;
    margin-right: auto;
    margin-left: auto;}
.image {
    width: 60%;
    position: fixed;
    align-items: center;}
.image img{
    position: fixed;
    display:block;
    margin-left: auto;
    margin-right: auto;
    vertical-align: middle;}
'

最佳答案

它没有居中,因为它的位置设置为固定的。 margin: 0 auto 不适用于固定位置元素。您必须将 left 设置为 50%,然后使用负 margin-left 将其拉回宽度的一半。例如:

img {
    position:fixed;
    width:300px;
    left:50%;
    margin-left:-150px;
}

http://jsfiddle.net/e24d0bx6/1/

关于HTML 图片调整大小和居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32642909/

相关文章:

c++ - Cimg 随机抛出 CImgIOException

Android 应用程序下载模式涉及不同设备/屏幕上的相同图像

c# - 按比例调整 pictureBox 的大小以调整 Form 的大小

arrays - 数组内部的 jquery 模板数组

python - 使用 python 从没有表单标签但有文本输入的网站中抓取数据

html - thymeleaf 的条件运算符

html - 滚动时的静态标题

c++ - 在 C++/OpenGL 中加载 tga/bmp 文件

resize - Dropzone.js +客户端图像调整大小

html - 使用javascript,html重置放大(捏合)android平板电脑浏览器(google chrome)