Place text on image

I was looking for an answer, but can't find one that works for me.

How do I get the text "HEY YOU" 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 tried position:relativeon .gallery_projectand position:absoluteon .gallery_text.

Also tried to make a container and .sort_item position:relative(not at the same time)

+4
source share
2 answers

Here you will find the 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>
Run codeHide result

Make the parent div (image and text) like position: relative. Enter position: absoluteand top:0into the text.

Hope this helps you.

+2
source

How about something like this: http://jsfiddle.net/EgLKV/3/

position:absolute z-index, .

.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>
Hide result
0

Source: https://habr.com/ru/post/1684518/


All Articles