Home  >  Q&A  >  body text

Title rewritten to: Use z-index to display text on hover with CSS, causing div to move

<p>When I hover over the first div to display the full text, the position of the second div changes so that it is behind the first div. I want the second div to stay in place and the first div's text to cover it. </p> <p><strong>Demo: </strong>https://codepen.io/adivity/pen/OJEzoPm</p> <pre class="brush:php;toolbar:false;"><html> <body> <div class="container"> <div>1) This is the full title I want to display. It's very long and will completely cover the next div. </div> <div>2) This is the full title I want to display. It's very long and will completely cover the next div. </div> <div>3) This is the full title I want to display. It's very long and will completely cover the next div. </div> </div> </body> </html></pre> <pre class="brush:php;toolbar:false;">.container { max-width: 100px; margin: 0 auto; } .container div { height: 80px; background-color: gray; } .container div { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .container div:hover { overflow: visible; white-space: normal; z-index: 2; max-width: 100px; }</pre></p>
P粉826283529P粉826283529411 days ago376

reply all(1)I'll reply

  • P粉759451255

    P粉7594512552023-09-04 13:51:03

    Removing position: absolute from .container div:hover solved the problem for me. Is this what you are looking for?

    .container div:hover {
      overflow: visible;
      white-space: normal;
      z-index: 2;
      position: absolute; <---移除这个
      max-width: 100px;
    }

    reply
    0
  • Cancelreply