你好我要求你的专业人士帮忙......我正在用HTML和CSS创建流畅的画廊。我需要在图像悬停时在图像上添加文本,我希望文本水平和垂直居中。到目前为止,我所尝试的一切都失败了。目前文本是绝对的图像,但文本在左上方。请帮忙!
body {
margin: auto;
}
.portfolio-container,
.portfolio-list {
width: 100%;
}
.portfolio-list {
margin: 0;
padding: 0;
list-style: none;
}
.portfolio-list li {
position: relative;
display: block;
float: left;
width: 25%;
}
.portfolio-list li img {
display: block;
width: 100%;
margin: 0;
vertical-align: top;
}
.portfolio-list a:after {
width: 100%;
}
.portfolio-list li img {
width: 100%;
margin: 0 auto;
}
h1,
h2 {
font-weight: normal;
}
p {
line-height: 1.5;
}
#description {
max-width: 40em;
margin: 0 auto 4.125em;
padding: 0 3%;
}
.group:before,
.group:after {
content: "";
display: table;
}
.group:after {
clear: both
}
.group {
zoom: 1
}
#wrapper .text {
left: 0;
right: 0;
margin: 0 auto;
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
opacity: 0;
transition: all 0.5s;
-webkit-transition: all 0.5s;
background: rgba(47, 193, 108, 0.6);
}
#wrapper:hover .text {
opacity: 1;
}
<section class="portfolio-container group">
<ul class="portfolio-list group">
<li id="wrapper">
<img src="http://placehold.it/600x400/c69/?text=%20" class="hover">
<p class="text">text</p>
</li>
<li id="wrapper">
<img src="http://placehold.it/600x400/9c6/?text=%20" class="hover">
<p class="text">text</p>
</li>
</ul>
</section>
答案 0 :(得分:3)
在display:flex;
内justify-content: center;
和align-items: center;
使用#wrapper .text
body {
margin: auto;
}
.portfolio-container,
.portfolio-list {
width: 100%;
}
.portfolio-list {
margin: 0;
padding: 0;
list-style: none;
}
.portfolio-list li {
position: relative;
display: block;
float: left;
width: 25%;
}
.portfolio-list li img {
display: block;
width: 100%;
margin: 0;
vertical-align: top;
}
.portfolio-list a:after {
width: 100%;
}
.portfolio-list li img {
width: 100%;
margin: 0 auto;
}
h1,
h2 {
font-weight: normal;
}
p {
line-height: 1.5;
}
#description {
max-width: 40em;
margin: 0 auto 4.125em;
padding: 0 3%;
}
.group:before,
.group:after {
content: "";
display: table;
}
.group:after {
clear: both
}
.group {
zoom: 1
}
#wrapper .text {
left: 0;
right: 0;
margin: 0 auto;
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
opacity: 0;
transition: all 0.5s;
-webkit-transition: all 0.5s;
background: rgba(47, 193, 108, 0.6);
display:flex;
justify-content: center;
align-items: center;
}
#wrapper:hover .text {
opacity: 1;
}
<section class="portfolio-container group">
<ul class="portfolio-list group">
<li id="wrapper">
<img src="" class="hover">
<p class="text">text</p>
</li>
<li id="wrapper">
<img src="" class="hover">
<p class="text">text</p>
</li>
</ul>
</section>