skip to Main Content

I am developing a portfolio website. I tried to put some text over an image. I put the picture (#projet-indiv) in position: relative and the text (#texte_projet) in position: absolute.

.projet-indiv {
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  width: 100%;
  margin-bottom: 60px;
}

.texte-projet {
  position: absolute;
  z-index: 15;
}

.images-projet {
  position: relative;
  width: 50%;
}
<div class="images-projet">
  <img src="https://placekitten.com/300/230" style="height: 230px; width: 300px;">
  <div class="texte_projet">
    <p>Projet</p>
  </div>
  <p>๐Ÿ”— Dataviz</p>
</div>

render

2

Answers


  1. Link to main stackoverflow answer : Display text on MouseOver for image in html

    The simplest solution for you is to use title attribute :

    <img img src="/images/Capture dโ€™eฬcran 2023-01-27 aฬ€ 11.52.29 AM.png" style="height: 230px; width: 300px;" title="Projet : ๐Ÿ”— Dataviz"/>
    

    Or else to use CSS hover :

    div {
        display: none;
        border:1px solid #000;
        height:30px;
        width:290px;
        margin-left:10px;
    }
    
    a:hover + div {
        display: block;
    }โ€‹
    <a><img img src="/images/Capture dโ€™eฬcran 2023-01-27 aฬ€ 11.52.29 AM.png" style="height: 230px; width: 300px;" /></a>
    <div>Projet : ๐Ÿ”— Dataviz</div>
    Login or Signup to reply.
  2. Apart from spelling the class name correctly, you need to use position parameters like top or bottom and left or right (all in relation to the relative-positioned parent) to place the absolutely positioned text where you want it:

    .projet-indiv {
      display: flex;
      flex-direction: row;
      justify-content: space-around;
      width: 100%;
      margin-bottom: 60px;
    }
    
    .texte-projet {
      position: absolute;
      top: 50%;
      left: 30px;
    }
    
    .images-projet {
      position: relative;
      width: 50%;
    }
    <div class="images-projet">
      <img src="https://placekitten.com/300/230" style="height: 230px; width: 300px;">
      <div class="texte-projet">
        <p>Projet</p>
      <p>๐Ÿ”— Dataviz</p>
      </div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search