html - CSS:裁剪图像垂直居中,最大高度为 200px;

标签 html css

我想裁剪具有任何自然尺寸的图像,并仅使用 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 &quot;O que é o Zero Working Capital? (e o que pode beneficiar com isso)&quot;">
                      <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%;
}

JSFiddle

最佳答案

还有另一种可能性,将图像保留在流程和 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/

相关文章:

php - 访问 laravel blade 中的数组级别

javascript - 单击时如何计算具有绝对位置的vue组件的左侧和顶部

jquery - 增加 Cycle2 叠加层的字体大小

html - 如何使用 z-index 正确控制 div 的堆叠顺序?

php - 在非 wordpress 网站上显示 wordpress 页脚

javascript - 通过下拉菜单更改 iframe src 不会加载新的 iframe

javascript - jQuery - 仅获取没有子元素的父元素文本

python - Django 处理可选的 url 参数

css - IE 11 : error CSS3111 in my own code, 和 google.com/fonts 不呈现任何字体

javascript - 第一行而不是最后一行不完整的图像换行