我为可响应的图像网格制作了此代码,并放大了图像并显示其标题。不幸的是,我不知道如何使字幕响应。带有模糊类的深色覆盖层应与图像大小相同,文字应保留在中间。但是,一旦我更改位置:绝对和/或将宽度和高度更改为自动或100%,元素就会消失。
我有一段时间没有编码了,我是否缺少明显的东西?知道如何解决这个问题吗?
<div class="wrapper">
<div class="columns">
<div class="column">
<img src="http://lorempixel.com/400/200/">
<div class="caption">
<div class="blur"></div>
<div class="caption-text">
<h5>Info 1</h5>
</div>
</div>
</div>
<div class="column">
<img src="http://lorempixel.com/400/200/">
</div>
<div class="column">
<img src="http://lorempixel.com/400/200/">
</div>
<div class="column">
<img src="http://lorempixel.com/400/200/">
</div>
<div class="column">
<img src="http://lorempixel.com/400/200/">
</div>
</div>
.column{
float: left;
padding: 0px;
position: relative;
overflow: hidden;
}
.column:hover .caption{
opacity: 1;
}
.column:hover img{
opacity: 1;
transform: rotate(3deg) scale(1.15,1.15);
-webkit-transform: rotate(3deg) scale(1.15,1.15);
-moz-transform: rotate(3deg) scale(1.15,1.15);
-ms-transform: rotate(3deg) scale(1.15,1.15);
-o-transform: rotate(3deg) scale(1.15,1.15);
-moz-transform-origin: center;
-webkit-transform-origin: center;
transform-origin: center;
}
.column img{
margin: 0px;
padding: 0px;
float: left;
z-index: 0;
width:100%;
}
.column .caption{
cursor: pointer;
position: absolute;
opacity: 0;
-webkit-transition:all 0.45s ease-in-out;
-moz-transition:all 0.45s ease-in-out;
-o-transition:all 0.45s ease-in-out;
-ms-transition:all 0.45s ease-in-out;
transition:all 0.45s ease-in-out;
}
.column img{
-webkit-transition:all 0.25s ease-in-out;
-moz-transition:all 0.25s ease-in-out;
-o-transition:all 0.25s ease-in-out;
-ms-transition:all 0.25s ease-in-out;
transition:all 0.25s ease-in-out;
}
.column .blur{
background-color: rgba(0,0,0,0.65);
height: 400px;
width: 300px;
z-index: 5;
position: absolute;
top:0;
left:0;
}
.column .caption-text h1{
text-transform: uppercase;
font-size: 24px;
}
.column .caption-text{
z-index: 10;
color: #fff;
position: absolute;
width: 400px;
height: 300px;
text-align: center;
top:100px;
}
/* Grid */
* {
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
}
.wrapper{
max-width: 1200px;
margin:0 auto;
overflow: hidden;
}
.columns {
display: flex;
flex-flow: row wrap;
justify-content: center;
margin:-1.5%;
}
.column {
position: relative;
width:50%;
flex: 32%;
overflow:hidden;
margin: 5px;
&:first-child { margin-left: 0; }
&:last-child { margin-right: 0; }
}
@media screen and (max-width: 980px) {
.columns .column {
flex-basis: 40%;
&:nth-last-child(2) {
margin-right: 0;
}
&:last-child {
flex-basis: 100%;
margin: 0;
}
}
}
@media screen and (max-width: 680px) {
.columns .column {
flex-basis: 100%;
}
}
答案 0 :(得分:0)
您可以使用flexbox
来解决它,以在CSS中更改/添加这些属性:
.column .blur {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
width: 100%;
left: 0;
top: 0;
}
.column .caption {
width: 100%;
height: 100%;
}
.column .caption-text {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
left: 0;
top: 0;
}
由于.column .blur
和.column .caption-text
具有相似性,因此您可以为两者创建一个class
。
最终结果在这里:https://jsfiddle.net/b42ehq8o
希望有帮助。
答案 1 :(得分:0)
要垂直居中,我使用了flex
.column .caption-text{
display: flex;
align-items: center;
justify-content: center;
...
以及您的h5
文字
.column .caption-text h5{
max-width:50%
...
}
您的div
<div class="caption">
<div class="blur"></div>
<div class="caption-text">
其高度和宽度应为100%
like.column .blur{
background-color: rgba(0,0,0,0.65);
height: 100%;
width: 100%;
z-index: 5;
position: absolute;
top:0;
left:0;
}
.caption{
width:100%;
height:100%;
}
.column .caption-text h5{
text-transform: uppercase;
font-size: 24px;
z-index:99;
max-width:50%
}
.column .caption-text{
display: flex;
align-items: center;
justify-content: center;
z-index: 10;
color: #fff;
width: 100%;
height: 100%;
text-align: center;
top:100px;
}