简体   繁体   中英

Why background-color:white; doesn't work?

Here is my code:

 body{ text-align: center; } span{ background-color: white; z-index: 2; } div{ border-top: 1px solid; position: absolute; width: 100%; margin-top: -8px; z-index: 1; } 
 <span>test</span> <div></div> 

As you see, that black line is still on the text. While I've set a white background color to the text. Why it doesn't appear?

I want to cut that black line on the text's edges. In other word, that line should be surround the text.

The span need position, ie position: relative for z-index to work

 body{ text-align: center; } span{ position: relative; background-color: white; z-index: 2; } div{ border-top: 1px solid; position: absolute; width: 100%; margin-top: -8px; z-index: 1; } 
 <span>test</span> <div></div> 

Add position:relative to the span.

 body{ text-align: center; } span{ position:relative; background-color: white; z-index: 2; } div{ border-top: 1px solid; position: absolute; width: 100%; margin-top: -8px; z-index: 1; } 
 <span>test</span> <div></div> 

Where is your Body TAG ?

 body{ text-align: center; } span{ background-color: red; z-index: 2; } div{ border-top: 1px solid; position: absolute; width: 100%; margin-top: -8px; z-index: 1; } 
 <body> <span>test</span> <div></div> </body> 

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