这是一种方法:
html,
body {
margin: 0;
}
.wpr {
display: flex;
align-items: center;
/* align vertical */
justify-content: center;
/* align horizontal */
height: 100vh;
}
figure {
text-align: center;
}
figcaption {
width: 350px;
text-align: left;
margin: 0 auto;
}
img {
max-width: 80vw; /* shrink img to viewport */
max-height: 80vh; /* shrink img to viewport */
}Run Code Online (Sandbox Code Playgroud)
<div class="wpr">
<figure>
<img src="http://placehold.it/150x80" alt="">
<figcaption>Caption for the awesome picture Caption for the awesome picture Caption for the awesome picture</figcaption>
</figure>
</div>Run Code Online (Sandbox Code Playgroud)
抱歉,我真的必须更加注意维护这个问题。
看来没有Javascript就无法解决这个问题。我从 @Danield 提出的一个想法开始,应用了一些 Javascript 并提出了以下解决方案。
$figure = $('figure');
$figcaption = $('figcaption');
$img = $('img');
$(window).resize(function() {
var height = $figure.height() - $figcaption.height();
$img.css('max-height', Math.max(height, 0));
}).resize();Run Code Online (Sandbox Code Playgroud)
@import url("//cdnjs.cloudflare.com/ajax/libs/normalize/3.0.2/normalize.min.css");
figure {
align-items: center;
display: flex;
flex-direction: column;
height: 100vh;
justify-content: center;
margin: 0;
}
img {
display: block;
max-width: 100vw;
}
figcaption {
flex: none;
text-align: center;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<figure>
<img src="http://lorempixel.com/640/480/fashion" />
<figcaption>Whatever the foobar,<br>she said</figcaption>
</figure>Run Code Online (Sandbox Code Playgroud)