我想裁剪具有任何自然尺寸的图像,并仅使用 CSS 将其宽度设置为 100%,最大高度设置为 200 像素。
看完similar question here ,我接近它了,但图像仍然水平拉伸(stretch)。
HTML:
<div class="moduleItemIntrotext"> <a class="moduleItemImage" href="/gestao/item/7479-o-que-é-o-zero-working-capital-e-o-que-pode-beneficiar-com-isso.html" title="Continue a ler "O que é o Zero Working Capital? (e o que pode beneficiar com isso)"">
<img src="http://www.portal-gestao.com/media/k2/items/cache/x99c309f8b22ccf674ef513c2b1fdd8b5_XL.jpg.pagespeed.ic.Me394YWuwk.jpg" alt="O que é o Zero Working Capital? (e o que pode beneficiar com isso)" width="900" height="900">
</a>
<p>O working capital (ou fundo de maneio) é a diferença entre o ativo corrente e passivo corrente. Numa empresa com capacidade para encarar as suas obrigações financeiras de curto-prazo, o ativo corrente supera o passivo corrente. Se essa empresa necessitasse, poderia converter todo o seu ativo corrente em dinheiro e assim liquidar todas as suas dívidas de curto-prazo.</p>
</div>
CSS:
.moduleItemIntrotext {
overflow: hidden;
position: relative;
}
.moduleItemIntrotext img {
position: relative;
margin: auto;
max-height: 200px;
width: 100%;
}
最佳答案
还有另一种可能性,将图像保留在流程和 HTML 中: <强> DEMO
line-height + negative-margin 以实际减少放置图像所需的高度。
.moduleItemIntrotext img {
position: relative;
margin: -50% auto;/* virtualy height needed turn don to zero */
width: 100%;/* height will follow within image ratio */
height:auto;/* to overrride attribute height set in tag */
vertical-align:middle;/* finalise vertical centering on baseline*/
}
.moduleItemImage {
display:block;
height:200px;/*set an height */
line-height:200px;/* set the baseline at 100px from top*/
overflow:hidden;/* crops/cut off */
}
关于html - CSS:裁剪图像垂直居中,最大高度为 200px;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25455389/