我想构建一个html页面,显示带有描述的图像,描述和图像是动态生成的,并且可以找到这两种常见情况:
<div style="width:500px; padding:10px; border:solid 1px;">
<div style="width:100%; overflow: auto; border:solid 1px;">
<img style="float:right; width:40%" src="http://isc.stuorg.iastate.edu/wp-content/uploads/sample.jpg">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam tempus justo pharetra, tincidunt erat nec, euismod quam. Fusce vel purus aliquet, posuere nulla ac, porta urna. Aliquam feugiat ligula vitae maximus tincidunt.
</div>
</div>
<div style="width:500px; padding:10px; border:solid 1px;">
<div style="width:100%; overflow: auto; border:solid 1px;">
<img style="float:right; width:40%" src="http://isc.stuorg.iastate.edu/wp-content/uploads/sample.jpg">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam tempus justo pharetra, tincidunt erat nec, euismod quam. Fusce vel purus aliquet, posuere nulla ac, porta urna. Aliquam feugiat ligula vitae maximus tincidunt. Suspendisse vel tristique massa. Ut lobortis sapien nec libero varius, vitae ullamcorper risus pretium. Nunc vulputate posuere auctor. Phasellus pulvinar nibh a risus semper, a lobortis justo ultricies. Etiam at pretium ligula. Nullam lobortis, nisl nec sollicitudin vestibulum, turpis neque porta neque, pellentesque egestas ipsum nunc id nisl. Morbi quis lacus quam. Pellentesque eget congue neque. Vestibulum elementum arcu nec condimentum posuere. Donec risus lorem, ornare pharetra ex a, lobortis molestie nunc. Suspendisse iaculis est eros, eu vestibulum nisi accumsan eget. Proin eget eleifend eros.
</div>
</div>
虽然第一个文字太短而无法包装图像,但第二个文字足够长以包裹它。
我喜欢这种效果,但我想在第一种情况下稍微改变一下,所以这是我的问题:
是否可以垂直对齐文本,使其根据图像高度垂直居中? (当然我想用更长的文本保留包装效果)
图形:
我想要这个:
成为这个:
答案 0 :(得分:1)
Here就是这样做的
<强> HTML 强>
<div class="container">
<div class="something">
<img src="http://isc.stuorg.iastate.edu/wp-content/uploads/sample.jpg" />
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam tempus justo pharetra, tincidunt erat nec, euismod quam. Fusce vel purus aliquet, posuere nulla ac, porta urna. Aliquam feugiat ligula vitae maximus tincidunt.</div>
</div>
</div>
<div class="container">
<div class="something">
<img src="http://isc.stuorg.iastate.edu/wp-content/uploads/sample.jpg" />
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam tempus justo pharetra, tincidunt erat nec, euismod quam. Fusce vel purus aliquet, posuere nulla ac, porta urna. Aliquam feugiat ligula vitae maximus tincidunt. Suspendisse vel tristique massa. Ut lobortis sapien nec libero varius, vitae ullamcorper risus pretium. Nunc vulputate posuere auctor. Phasellus pulvinar nibh a risus semper, a lobortis justo ultricies. Etiam at pretium ligula. Nullam lobortis, nisl nec sollicitudin vestibulum, turpis neque porta neque, pellentesque egestas ipsum nunc id nisl. Morbi quis lacus quam. Pellentesque eget congue neque. Vestibulum elementum arcu nec condimentum posuere. Donec risus lorem, ornare pharetra ex a, lobortis molestie nunc. Suspendisse iaculis est eros, eu vestibulum nisi accumsan eget. Proin eget eleifend eros.</div>
</div>
</div>
<强> CSS 强>
.container {
width:500px;
padding:10px;
border:solid 1px;
}
.something {
width:100%;
overflow: auto;
border:solid 1px;
}
.something>img {
float:right;
width:40%
}
<强> JS 强>
$(document).ready(function () {
var numberOfItems = $(".container").length;
for (var i = 0; i < numberOfItems; i++) {
var imageHeight = $(".container:eq(" + i + ")").children(".something").children("img").height();
var divHeight = $(".container:eq(" + i + ")").children(".something").children("div").height();
if (divHeight < imageHeight) {
$(".container:eq("+i+")").children(".something").children("div").css({
"margin-top": (imageHeight - divHeight) / 2 + "px"
});
}
}
});
修改强>
要使上面的jQuery工作,请在<header>
标记中包含下面的代码段(在调用所有jQuery文档之前)
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
请查看this网站,了解有关此
的更多信息答案 1 :(得分:0)
make wrapper div display:table and text div display:table-cell;垂直对齐:中部;
如果它不起作用,那么也应用overflow:隐藏在文本div上