垂直对齐图像

时间:2009-10-05 15:36:49

标签: css vertical-alignment

我有一个包含图像和p标签的div(如下所示)。我想根据段落的行数垂直对齐div中间的图像。垂直对齐不起作用。我现在正在使用JavaScript来计算添加到margin-top的数量,但宁可使用CSS。有什么想法吗?

<div>
    <img>
    <p>Multi-line</p>
</div>

7 个答案:

答案 0 :(得分:60)

这是纯CSS,垂直对齐图像,如果图像比包含框更高(或更宽),也会调整图像大小。因此,盒子和图像都可以是任何尺寸而不会破坏垂直对齐。此外,您可能需要在<p>标记中添加左边距,以防止它们被图像隐藏。

CSS

/* Positioning */
.absoluteCenterWrapper {
    position: relative; /* Contains the image in the div */
}
.absoluteCenter { /* Aligns image vertically */
    margin: auto;
    position: absolute;
    top: 0;
    bottom: 0;
}
.absoluteCenterWrapper p { /* Pushes <p> to edge of image */
    margin-left: 101px; /* Width of image */
}
.absoluteCenter { /* Specifies width of image to avoid overlap if image changes */
    width: 101px; /* Width of image */
}

/* Sizing - resizes down to avoid cutting off */
.absoluteCenter {
    max-height: 100%;
    max-width: 100%;
}

/* Making it "pretty" */
.absoluteCenterWrapper { margin: 1em; padding: 0.001em; /* <- Hack to contain margins */ outline: 1px solid red; }
.absoluteCenterWrapper p { margin-top: 1em; margin-bottom: 1em; padding-left: 1em;}
<div class="absoluteCenterWrapper">
 <img class="absoluteCenter" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGUAAAAXCAMAAAAlSHE3AAAAA3NCSVQICAjb4U/gAAAACXBIWXMAAA2/AAANvwELq/fgAAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAAjFQTFRF/38A/4AB/4AC/4ED/4EE/4IF/4IG/4MH/4MI/4QJ/4QK/4UL/4UM/4YN/4YO/4cP/4cQ/4gR/4gS/4kT/4kU/4oV/4oW/4sX/4sY/4wZ/4wa/40b/40c/44d/44e/48g/5Ai/5Ej/5Ek/5Im/5Mo/5Qp/5Qq/5Ur/5Us/5Yt/5Yu/5cv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dQegSAqxiaIp4+QQwNkoe0mVD6qqqqq6aliplFyAmwPaQEtj3EBZSnC5IpOJCdYC5MtGbLJJU6yyC+BQtO33XdbaYWWVLAdoeqvdnOgrHEqj5eSj18CneoAyWaxXYoPHAC5FUx53poxUpg5eBSz+m9pAgajtcLzt+zmgvjcZuN8/Qa/Q9gowfY6mZHlbCyxZxQ/ide/YhWCN1dHqn6kNmH1yBpaJFAFzgs3zMo7KESKUErmemV3fE/V7yXkuIr4Gk06JPT0o8s4RHuCpuCDeG0gCWPZRxHfQGKlQ5ZPArf06ZWSNn59tHtGKnTMtahxm5Jp+b8bljfrf9L/+bP0CQB36KehprcoAAAAASUVORK5CYII=">
 <p>Paragraph goes here.</p>
 <p>Paragraph goes here.</p>
</div>
<div class="absoluteCenterWrapper">
 <img class="absoluteCenter" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGUAAAAXCAYAAAASloEFAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAANvwAADb8BC6v34AAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAWSSURBVGiB7Zl7TNVlGMc/5xwunsNN7kSEIISIhiSFNS+IGrREdGmmmzlb5kpZy7k2c20utfqndP1hTsPL1paVa2Yz84aIpZSoaCp5g4OAiAICQtwPpz8e2Ln+OCBndebOdzv77fe87/O8l+/zPs/z/o7KuB4jbrgU1P/3BNywhZsUF4SbFBeEmxQXhC0pag0kzoOIFPsa/lHS7hs+9NF8IyD3GiTNN8lSlonMy2fo9lwNQfGylvgsx31T3+5bt69Nky0pHlpYtB/SVtk3FjNd2iOfG+KMAY0nhIwB7wCTTBskMtVjcGg1Xn3r83fcVxssfdUam6bHYCceP3g41ZpPGERNgoBoqCuFyt/B0P1otlRqeDINIiZAWz1UFUFLjWO9qEnw8A48rDbJwsaBlx/UFEOvQWTefhA6DuqvQUeTqW9ANES9ACNGQm0J1JwDo9lVzlML4RPgwS3oajXN8cp3jucV8Sw0XIfqPwbs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dFP4q2DRctdOL8dJr4lSX1kjNwbFv8s7YUbHNuoOi2JNybdMpnr86Wq8/azlAOc2ihELj4AkakQOBpmbIKxr0LR5sGFK3s4tUlybk6e5LKQRJi/Fzx1iirOuac03ICv0yB7Oyw9ZpL3dIp3Gg1Ds3dkjehmbJDKDqDhJuxOhwdljvW726X0jc2wPBGVp6GnA9SecLvQUqf2EnyTKSX8inOm+Resh98+Gdr8zVH6o1SlmV+Io/Ua4PK3QvSMjXZVVE7/59HbX77pdLZI/O9uf3RbGi+5F7XVS8z/rxAQLWGs/u9H/yJhDQ9vqeAay2RvBoDzSXFj2HB/kHRBuElxQbhJcUH8Cxuwzh5I0D/eAAAAAElFTkSuQmCC">
 <p>Paragraph goes here.</p>
 <p>Paragraph goes here.</p>
 <p>Paragraph goes here.</p>
</div>

答案 1 :(得分:11)

尝试将line-height元素的p属性设置为图像的高度,例如:

div p {
  line-height: 18px;
}

编辑:刚刚意识到我误解了这个问题并错过了p将是多行的事实。要尝试的一个选项是完全删除img元素并将其设置为background-image的{​​{1}},而pbackground-position。类似的东西:

left, center

答案 2 :(得分:1)

给图像显示:block,float:left和vertical-align:middle。或者,给它一个等于包含元素的行高。

答案 3 :(得分:1)

CSS vertical-align属性仅适用于表格单元格和内联元素。由于段落标记默认指定了一个块元素,因此它什么都不做。为了使您的文本按照您的描述进行对齐,您必须将div分成两个容器或使用表格。这是一个很好的页面,可以帮助您更好地理解:Understanding vertical-align

答案 4 :(得分:0)

这是一个两栏目的工作吗? (左边的图像,右边的文字)

如果是这样,你肯定可以将每个元素都粘贴在自己的div中......

<div>
    <div style="width:100; float:left"><img /></div>
    <div style="width:100; float:left"><p>multiline</p></div>
</div>

认为这会做你想要的事情

答案 5 :(得分:0)

也许Jak psát web的解决方案会有所帮助。

答案 6 :(得分:-1)

添加

div img {
    float: left;
}