Html Can';t垂直对齐:中间绝对定位块文本 我在块的中间有一些垂直对齐H2文本的问题。
基本上,我有许多不同高度、相同宽度(300px)的图像 不同数量的文本可能出现在悬停图像顶部的数字行上 <>我想做的是在中间垂直对齐这个文本… 他是一个演示者Html Can';t垂直对齐:中间绝对定位块文本 我在块的中间有一些垂直对齐H2文本的问题。,html,css,Html,Css,基本上,我有许多不同高度、相同宽度(300px)的图像 不同数量的文本可能出现在悬停图像顶部的数字行上 我想做的是在中间垂直对齐这个文本… 他是一个演示者 没有JS的解决方案 在HTML中添加一个额外元素作为背景: <div class="post-thumb"> <a href="http://eclectivism.com/text/"> <span class="post-thumb-highligh
没有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>
查看演示-我在中间放了一个div(垂直、水平),上面有以下javascript行:
$('.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');
我希望这有助于CSS添加:
left: 50%
top: 50%
减去文本宽度和高度的一半作为边距
例如:
HTML:
它将位于分区的中心。快速搜索
垂直对齐
将说明它不能用于此上下文。。i、 e.在块内垂直居中。你可以用Javascript来做,也可以不用。使用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}
$('.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');
left: 50%
top: 50%
<div>
<h3>Hello</h3>
</div>
div {
height: 500px;
width: 500px;
}
h3 {
height: 50px;
width: 100px;
left: 50%;
top: 50%;
margin-left: -50px;
margin-top: -25px;
}