我无法在块中间垂直对齐某些h2文本。
基本上我有许多不同高度的图像,宽度相同(300px) 不同数量的文本,可以在悬停的图像顶部显示的数字行。
我想做的是在中间垂直对齐此文字......
他是一个演示版 http://jsfiddle.net/wn3Kj/2/
<div class="post-thumb">
<a href="http://eclectivism.com/text/">
<h2 class="entry-title">Text text text text text text text text text text text text text text text text text text text text text text text text</h2>
<img src="http://eclectivism.com/wp-content/uploads/2013/09/space-doodle-01-580x7041.jpg" class="attachment-blog-large wp-post-image" />
</a>
</div>
CSS
.post-thumb {
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-size: 100%;
vertical-align: baseline;
background: transparent; }
a {position: relative; display: inline-block; }
h2 {
opacity: 0;
padding-right: 20px;
padding-left: 20px;
padding-bottom: 0px;
padding-top: 50%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: 0;
background: rgba(214,238,219,0.8);
color: #fff;
text-decoration: none;
text-shadow: 0px 1px 5px rgb(63, 97, 56);
text-align:center;
display:block;
}
img {
width: 300px;
}
a:hover h2 {opacity:1}
答案 0 :(得分:0)
没有JS的解决方案。
在HTML中为背景添加一个额外元素:
<div class="post-thumb">
<a href="http://eclectivism.com/text/">
<span class="post-thumb-highlight"></span> <!-- HERE -->
<h2 class="entry-title">Text text text text text text text text text text text text text text text text text text text text text text text text</h2>
<img src="http://eclectivism.com/wp-content/uploads/2013/09/space-doodle-01-580x7041.jpg" class="attachment-blog-large wp-post-image" />
</a>
</div>
使用CSS:
.post-thumb-highlight {
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
background: rgba(214,238,219,0.8);
opacity: 0;
}
h2 {
opacity: 0;
text-decoration: none;
display: inline-block;
color: #fff;
text-decoration: none;
text-shadow: 0px 1px 5px rgb(63, 97, 56);
text-align:center;
width: 300px; /* same width as IMG */
vertical-align: middle;
margin-right: -300px; /* negative value of IMG width */
position: relative;
font-size: 24px;
}
img {
width: 300px;
vertical-align: middle;
}
a:hover h2, a:hover .post-thumb-highlight {opacity:1}
查看演示 - http://jsfiddle.net/3epgP/1/
答案 1 :(得分:0)
我在这个javascript行中放置一个div(垂直,水平):
$('.dialogdiv').css('top', Math.max(0, (($(window).height() - $('.dialogdiv').outerHeight()) / 2) + $(window).scrollTop()) + 'px');
$('.dialogdiv').css('left', Math.max(0, (($(window).width() - $('.dialogdiv').outerWidth()) / 2) + $(window).scrollLeft()) + 'px');
我希望这会有所帮助
答案 2 :(得分:0)
在CSS中添加:
left: 50%
top: 50%
分别减去边距文本宽度和高度的一半。
例如:
HTML:
<div>
<h3>Hello</h3>
</div>
CSS:
div {
height: 500px;
width: 500px;
}
h3 {
height: 50px;
width: 100px;
left: 50%;
top: 50%;
margin-left: -50px;
margin-top: -25px;
}
将位于div的中心。