问题
I have searched for an answer but can't find one that works for me.
How do I get the "HEY YOU" text on top of the image?
HTML:
<div class="row gallery_container">
<div class="sort_item study">
<a><img src="images/city_1.jpg" class="gallery_project" style="width: 200px; height:200px;"/>
<div class="gallery_text">
<h2>HEY</h2>
<span>YOU</span>
</div>
</a>
</div>
</div>
CSS:
.gallery_container {
width: 850px;
display: block;
margin: 0 auto;
box-sizing: border-box;
}
.sort_item {
box-sizing: border-box;
float: left;
padding: 4px;
width: 25%;
}
I have tried position:relative on .gallery_project and position:absolute on .gallery_text.
Also tried making the container and .sort_item position:relative (not at the same time)
回答1:
Here you go with a solution https://jsfiddle.net/19gqkhvt/
.gallery_container {
width: 850px;
display: block;
margin: 0 auto;
box-sizing: border-box;
}
.sort_item {
box-sizing: border-box;
float: left;
padding: 4px;
width: 25%;
position: relative;
}
.gallery_text {
position: absolute;
z-index: 10;
top: 0;
}
<div class="row gallery_container">
<div class="sort_item study">
<a><img src="http://via.placeholder.com/350x150" class="gallery_project" style="width: 200px; height:200px;"/>
<div class="gallery_text">
<h2>HEY</h2>
<span>YOU</span>
</div>
</a>
</div>
Make the parent div of (image & text) as position: relative.
Provide position: absolute and top:0 to text.
Hope this will help you.
回答2:
How about something like this: http://jsfiddle.net/EgLKV/3/
Its done by using position:absolute and z-index to place the text over the image.
.gallery_container {
width: 850px;
display: block;
margin: 0 auto;
box-sizing: border-box;
}
.sort_item {
box-sizing: border-box;
float: left;
padding: 4px;
width: 25%;
position: relative;
}
.gallery_text {
position: absolute;
z-index: 10;
top: 0;
}
<div class="row gallery_container">
<div class="sort_item study">
<a><img src="images/city_1.jpg" class="gallery_project" style="width: 200px; height:200px;"/>
<div class="gallery_text">
<h2>HEY</h2>
<span>YOU</span>
</div>
</a>
</div>
来源:https://stackoverflow.com/questions/45894427/place-text-on-image