简体   繁体   中英

Content background cut off

Scroll to the right and you'll see the background is truncated:

 div { width: 300px; overflow: auto; } p { background: green; } 
 <div> <p>ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.</p> </div> 

Why does it happen? What's the solution?

Try this code

this is because p doesn't get its actual width

p {
    background: green;
    float: left;
}

 div { width: 300px; overflow: auto; } p { background: green; float:left; } 
 <div> <p>ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.</p> </div> 

use the width and overflow in the same element as background . i have edited the snippet

 div { width: 300px; overflow: auto; } p { display: inline-block; background: green; } 
 <div> <p>ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.</p> </div> 

<div>
  <p>ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.</p>
</div>  


div {
      width: 300px;
      overflow: auto;
    }

    p {
      background: green;
    overflow: auto;
    }

Fiddler link https://jsfiddle.net/o0eqoqmh/

Or you can just set p as inline or inline-block element

 div { width: 300px; overflow: auto; } p { background: green; display: inline-block; /* OR display: inline; */ } 
 <div> <p>ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.ThisIsSomeText.</p> </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