[英]Responsive image with text overlay
I am trying to make a grid where the images have an overlay on hover, containing text. 我正在尝试制作一个网格,其中图像在悬停时有一个叠加层,包含文本。
However, I can't seem to do this AND have responsive issues. 但是,我似乎无法做到这一点,并且有响应问题。
I've tried removing the fixed heights but this just causes the grids to loose their sizing all together. 我尝试过删除固定高度,但这只会使网格失去所有尺寸。 I have tried tables and absolute positioning without any joy.
我尝试过桌子和绝对定位,没有任何乐趣。
The markup is: 标记是:
<div class="work-item clickable">
<a href="http://localhost:8888/work-example-client-name-11/"></a>
<div class="front">
<img width="460" height="380" src="http://localhost:8888/wp-content/uploads/2016/02/stock-photo-4772348-1500x1000-460x380.jpg" class="attachment-work-featured-image size-work-featured-image" alt="stock-photo-4772348-1500x1000" />
</div>
<div class="back">
<div class="inner">
<h3>Lorem ipsum dolor sit amet</h3>
</div>
</div>
</div>
CSS: CSS:
section#homepage .content article .work-item {
cursor: pointer;
border-right: 20px solid #fff;
border-bottom: 20px solid #fff;
float: left;
overflow: hidden;
position: relative;
width: 33.33%;
}
section#homepage .content article .work-item .front {
color: #fff;
width: 100%;
z-index: 50;
opacity: 1;
-webkit-transition: all 300ms ease-out;
-moz-transition: all 300ms ease-out;
-o-transition: all 300ms ease-out;
-ms-transition: all 300ms ease-out;
transition: all 300ms ease-out;
}
section#homepage .content article .work-item .back {
display: table;
width: 100%;
opacity: 0;
z-index: 25;
position: absolute;
}
section#homepage .content article .work-item .back .inner {
display: table-cell;
padding: 20px;
vertical-align: middle;
}
maybe you could rethink your html structure about links and images, and play with absolute positionning for either image or title. 也许您可以重新考虑有关链接和图像的html结构,并使用图像或标题的绝对定位进行播放。
example: 例:
body { text-align: center; } .work-item { display: inline-block; vertical-align: top; width: 25%; margin: 1em; } a { display: inline-table; border: solid; width: 100%; color: black; text-decoration: none; } figure { position: relative; display: table-row; } figure img { width: 100%; height: 100%; top: 0; position: absolute; transition: 0.5s } figcaption:before { content: ''; display: inline-block; vertical-align: middle; width: 0; padding-top: 92%; } figcaption { display: table-cell; } figure:hover img { opacity: 0; } figcaption h3 { display: inline-block; max-width: 98%; margin: 0 -2%; vertical-align: middle; }
<div class="work-item clickable"> <a href="http://dev.charlyanderson.co.uk/Testing/work-example-client-name-12/"> <figure> <img width="460" height="380" src="http://dev.charlyanderson.co.uk/Testing/wp-content/uploads/2016/02/stock-photo-4772348-1500x1000-460x380.jpg" class="attachment-work-featured-image size-work-featured-image" alt="stock-photo-4772348-1500x1000" /> <figcaption> <h3>Lorem ipsum dolor sit amet</h3> </figcaption> </figure> </a> </div> <div class="work-item clickable"> <a href="http://dev.charlyanderson.co.uk/Testing/work-example-client-name-12/"> <figure> <img src="http://dev.charlyanderson.co.uk/Testing/wp-content/uploads/2016/02/stock-photo-4772348-1500x1000-460x380.jpg" class="attachment-work-featured-image size-work-featured-image" alt="stock-photo-4772348-1500x1000" /> <figcaption> <h3>Lorem ipsum dolor sit amet</h3> </figcaption> </figure> </a> </div> <div class="work-item clickable"> <a href="http://dev.charlyanderson.co.uk/Testing/work-example-client-name-12/"> <figure> <img width="460" height="380" src="http://dev.charlyanderson.co.uk/Testing/wp-content/uploads/2016/02/stock-photo-4772348-1500x1000-460x380.jpg" class="attachment-work-featured-image size-work-featured-image" alt="stock-photo-4772348-1500x1000" /> <figcaption> <h3>Lorem ipsum dolor sit amet</h3> </figcaption> </figure> </a> </div> <div class="work-item clickable"> <a href="http://dev.charlyanderson.co.uk/Testing/work-example-client-name-12/"> <figure> <img width="460" height="380" src="http://dev.charlyanderson.co.uk/Testing/wp-content/uploads/2016/02/stock-photo-4772348-1500x1000-460x380.jpg" class="attachment-work-featured-image size-work-featured-image" alt="stock-photo-4772348-1500x1000" /> <figcaption> <h3>Lorem ipsum dolor sit amet</h3> </figcaption> </figure> </a> </div> </div> <div class="work-item clickable"> <a href="http://dev.charlyanderson.co.uk/Testing/work-example-client-name-12/"> <figure> <img src="http://dev.charlyanderson.co.uk/Testing/wp-content/uploads/2016/02/stock-photo-4772348-1500x1000-460x380.jpg" class="attachment-work-featured-image size-work-featured-image" alt="stock-photo-4772348-1500x1000" /> <figcaption> <h3>Lorem ipsum dolor sit amet</h3> </figcaption> </figure> </a> </div> <div class="work-item clickable"> <a href="http://dev.charlyanderson.co.uk/Testing/work-example-client-name-12/"> <figure> <img width="460" height="380" src="http://dev.charlyanderson.co.uk/Testing/wp-content/uploads/2016/02/stock-photo-4772348-1500x1000-460x380.jpg" class="attachment-work-featured-image size-work-featured-image" alt="stock-photo-4772348-1500x1000" /> <figcaption> <h3>Lorem ipsum dolor sit amet</h3> </figcaption> </figure> </a> </div>
Hope this works for you! 希望这对您有用!
HTML HTML
<a href="#" class="outer">
<div class="inner"><img src="http://farm9.staticflickr.com/8311/8023199579_f52f648727_m.jpg"></div>
<div class="overlay">Text</div>
</a>
<a href="#" class="outer">
<div class="inner"><img src="http://farm9.staticflickr.com/8311/8023199579_f52f648727_m.jpg"></div>
<div class="overlay">Text</div>
</a>
<a href="#" class="outer">
<div class="inner"><img src="http://farm9.staticflickr.com/8311/8023199579_f52f648727_m.jpg"></div>
<div class="overlay">Text</div>
</a>
CSS CSS
.outer {
width: 33.333%;
overflow: auto;
float: left;
position: relative;
}
.outer .inner {
display: block;
margin: 10px;
}
.outer .inner img {
width: 100%;
height: auto;
border: 0px;
display: block;
}
.outer .overlay {
position: absolute;
top: 0;
opacity: 0;
width:100%;
height:100%;
background-color:pink;
-webkit-transition: opacity .15s ease-in-out;
-moz-transition: opacity .15s ease-in-out;
-ms-transition: opacity .15s ease-in-out;
-o-transition: opacity .15s ease-in-out;
transition: opacity .15s ease-in-out;
}
.outer:hover .overlay {
opacity: 0.7;
}
Your outer would be of coarse your article tag 你的外衣很粗糙你的文章标签
Here is an example layout, based on your example, which includes overlapping text on hover of an image. 这是一个基于您的示例的示例布局,其中包括图像悬停时的重叠文本。
The <figure>
element seems good for this. <figure>
元素似乎对此很好。 It needs an image and a <figcaption>
. 它需要一个图像和一个
<figcaption>
。 Let's wrap it in an anchor tag so the entire image is clickable: 让我们将其包装在锚定标记中,以便可以单击整个图像:
<a>
<figure>
<img />
<figcaption></figcaption>
</figure>
</a>
The anchor element can line up the images. 锚元素可以使图像对齐。 The width is 33.33% so that there are 3 images to a row.
宽度为33.33%,因此一行有3张图像。 The images can be spaced with some padding here as well:
图像也可以在此处填充一些间距:
a { display: inline-block; width: 33.33%; vertical-align: top; padding: 10px; }
The figure is made position: relative
so that the position: absolute
figcaption is positioned relative to it. 将图形设置为
position: relative
从而使position: absolute
figcaption相对于其定位。 This will ensure that the text is layered over the image. 这将确保文本在图像上分层。 We can center it vertically with
top: 50%
and the transform to offset it correctly: 我们可以将其垂直居中并与
top: 50%
对齐,并进行正确的偏移:
figure { position: relative; } figcaption { position: absolute; text-align: center; top: 50%; right: 0; left: 0; transform: translateY(-50%); }
The image takes up the entire width of its parent: 图像占据其父对象的整个宽度:
figure img { width: 100%; display: block; }
*, *:before, *:after { margin: 0; padding: 0; box-sizing: inherit; } html { box-sizing: border-box; } body { min-width: 600px; } a { display: inline-block; width: 33.33%; vertical-align: top; padding: 10px; } figure { position: relative; transition: opacity .2s; } figure:before { position: absolute; top: 0; right: 0; bottom: 0; left: 0; background: rgba(0, 0, 0, 0.4); content: ''; opacity: 0; transition: inherit; } figcaption { position: absolute; text-align: center; top: 50%; right: 0; left: 0; transform: translateY(-50%); opacity: 0; transition: inherit; } figcaption h2 { font-size: 3vw; } figcaption p { font-size: 2vw; } a { color: #FFF; text-decoration: none; } figure img { width: 100%; display: block; } figure:hover:before, figure:hover figcaption { opacity: 1; }
<a href="http://dev.charlyanderson.co.uk/Testing/work-example-client-name-12/"> <figure> <img src="http://dev.charlyanderson.co.uk/Testing/wp-content/uploads/2016/02/stock-photo-4772348-1500x1000-460x380.jpg" class="attachment-work-featured-image size-work-featured-image" alt="stock-photo-4772348-1500x1000" /> <figcaption> <h2>Lorem ipsum dolor sit amet</h2> <p>More text here, plenty of space</p> </figcaption> </figure> </a><a href="http://dev.charlyanderson.co.uk/Testing/work-example-client-name-12/"> <figure> <img src="http://dev.charlyanderson.co.uk/Testing/wp-content/uploads/2016/02/stock-photo-4772348-1500x1000-460x380.jpg" class="attachment-work-featured-image size-work-featured-image" alt="stock-photo-4772348-1500x1000" /> <figcaption> <h2>Lorem ipsum dolor sit amet</h2> <p>More text here, plenty of space</p> </figcaption> </figure> </a><a href="http://dev.charlyanderson.co.uk/Testing/work-example-client-name-12/"> <figure> <img src="http://dev.charlyanderson.co.uk/Testing/wp-content/uploads/2016/02/stock-photo-4772348-1500x1000-460x380.jpg" class="attachment-work-featured-image size-work-featured-image" alt="stock-photo-4772348-1500x1000" /> <figcaption> <h2>Lorem ipsum dolor sit amet</h2> <p>More text here, plenty of space</p> </figcaption> </figure> </a><a href="http://dev.charlyanderson.co.uk/Testing/work-example-client-name-12/"> <figure> <img src="http://dev.charlyanderson.co.uk/Testing/wp-content/uploads/2016/02/stock-photo-4772348-1500x1000-460x380.jpg" class="attachment-work-featured-image size-work-featured-image" alt="stock-photo-4772348-1500x1000" /> <figcaption> <h2>Lorem ipsum dolor sit amet</h2> <p>More text here, plenty of space</p> </figcaption> </figure> </a><a href="http://dev.charlyanderson.co.uk/Testing/work-example-client-name-12/"> <figure> <img src="http://dev.charlyanderson.co.uk/Testing/wp-content/uploads/2016/02/stock-photo-4772348-1500x1000-460x380.jpg" class="attachment-work-featured-image size-work-featured-image" alt="stock-photo-4772348-1500x1000" /> <figcaption> <h2>Lorem ipsum dolor sit amet</h2> <p>More text here, plenty of space</p> </figcaption> </figure> </a><a href="http://dev.charlyanderson.co.uk/Testing/work-example-client-name-12/"> <figure> <img src="http://dev.charlyanderson.co.uk/Testing/wp-content/uploads/2016/02/stock-photo-4772348-1500x1000-460x380.jpg" class="attachment-work-featured-image size-work-featured-image" alt="stock-photo-4772348-1500x1000" /> <figcaption> <h2>Lorem ipsum dolor sit amet</h2> <p>More text here, plenty of space</p> </figcaption> </figure> </a>
vw
as a font-size
.
vw
作为font-size
来调整文字大小以适合图像宽度。
Image size: Place a min width on the body so that the images don't get too small. 图像尺寸:在身体上放置最小宽度,以使图像不会变得太小。
Overlay background: This is provided with the figure:before
element. 覆盖背景:随
figure:before
一起提供。
Note: The <a>
elements have no whitespace between them, this prevents an inline gap. 注意:
<a>
元素之间没有空格, 这可以防止内联空格。 The box-sizing: border-box
incorporates padding and borders into the width and height calculation. box-sizing: border-box
在宽度和高度计算box-sizing: border-box
合并了填充和边框。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.