简体   繁体   中英

Make a text visible only when inside a div

I'm trying to make a text visible to the user only when it is inside the div, while it must be hidden when it is out of the div.

The text is animated from left to right, so it's not fixed.

It's pretty hard to explain, so i made a image to make you understand better:

我的问题

Basically everything else except the div and the box should have a higher z-index to obtain this effect, but that's impossible because it would cover everything .

Any idea, possibly using CSS and JS or JQuery?

overflow: hidden; white-space: nowrap; will do the job for you. white-space: nowrap prevents text from wrapping, while overflow: hidden hides everything that exceeds div's borders.

Snippet

 .container { height: 300px; width: 300px; overflow: hidden; border: 1px solid black; white-space: nowrap; } 
 <div class="container"> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </div> 

Just make the text not wrap, and give the box overflow: hidden

 .box { max-width: 80px; white-space: nowrap; overflow: hidden; background: #efefef; } 
 <div class='box'> <p>Wow look at this text it goes out of the box</p> </div> 

The CSS property overflow: hidden; will cause everything which is outside the surface area of the element not to be rendered.

 div { width: 100px; height: 20px; background-color: blue; overflow: hidden; } 
 <div> text will go outside this div</div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM