简体   繁体   English

如何在背景颜色上方设置背景图像

[英]How to set background-image above background-color

I want to set background-image above the background-color (the background-image is a line). 我想在background-color上方设置background-imagebackground-image是一条线)。 See codepen and snippet: 请参阅codepen和snippet:

 table { border-collapse: collapse; } table, td { border: 1px solid black; } td.class1 { background: transparent url(http://davidrhysthomas.co.uk/linked/strike.png) repeat-x; } .class2 { text-align: center; color: #fff; height: 20px; width: 20px; background-color: red; border-radius: 5rem !important; display: inline-block; } 
 <table> <tr> <td>S</td> <td>B</td> </tr> <tr> <td>S</td> <td class="class1"> <span class="class2">S</span> </td> </tr> </table> 

Note that instead of setting background on two classes you can set it in .class2 itself by using background: url(http://davidrhysthomas.co.uk/linked/strike.png) repeat-x, red (the image mentioned first will be stacked over the red background mentioned last) - see demo below: 请注意,不是在两个类上设置背景,而是可以使用background: url(http://davidrhysthomas.co.uk/linked/strike.png) repeat-x, red (首先提到的图像background: url(http://davidrhysthomas.co.uk/linked/strike.png) repeat-x, red将它设置在.class2中。堆叠在最后提到的红色背景上) - 见下面的演示:

 table { border-collapse: collapse; } table, td { border: 1px solid black; } td.class1 { padding: 10px; /* for illustration */ } .class2 { text-align: center; color: #fff; height: 20px; width: 20px; background: url(http://davidrhysthomas.co.uk/linked/strike.png) repeat-x, red; /* changed*/ border-radius: 5rem !important; display: inline-block; } 
 <table> <tr> <td>S</td> <td>B</td> </tr> <tr> <td>S</td> <td class="class1"> <span class="class2">S</span> </td> </tr> </table> 


Solution

If you want to have the background image to extend over the full td , one option is to use radial-gradient for the red circle and combining it with background-image for the line. 如果您想让背景图像延伸到整个td ,一个选项是对红色圆圈使用radial-gradient ,并将其与线条的背景图像组合。 Note that here the text is above the red background color and the line : 请注意,此处文本位于红色背景颜色和线条上方

 table { border-collapse: collapse; } table, td{ border: 1px solid black; } td.class1 { position: relative; width: 20px; height: 20px; padding: 10px; background: url(http://davidrhysthomas.co.uk/linked/strike.png) repeat-x, radial-gradient(farthest-side,red 70%, transparent 72%); background-position: center; text-align: center; color: #fff; } 
 <table> <tr> <td>S</td> <td>B</td> </tr> <tr> <td>S</td> <td class="class1"> <span class="class2">S</span> </td> </tr> </table> 


If you want a strikethrough effect , you could place the line background-image over the <span> background and text, by use negative z-index on the <span> - see demo below: 如果你想要一个删除线效果 ,你可以将行background-image放在<span>背景和文本上,在<span>上使用z-index - 见下面的演示:

 table { border-collapse: collapse; } table, td { border: 1px solid black; } td.class1 { padding: 10px; /* for illustration */ background: transparent url(http://davidrhysthomas.co.uk/linked/strike.png) repeat-x; background-position: center; } .class2 { text-align: center; color: #fff; height: 20px; width: 20px; background-color: red; border-radius: 5rem !important; display: inline-block; position: relative; /* added */ z-index: -1; /* added */ } 
 <table> <tr> <td>S</td> <td>B</td> </tr> <tr> <td>S</td> <td class="class1"> <span class="class2">S</span> </td> </tr> </table> 

Yet another option for the strikethrough effect is using a pseudo element so that you don't have to mess with z-index - see demo below: 删除线效果的另一个选择是使用伪元素,这样您就不必乱用z-index - 请参阅下面的演示:

 table { border-collapse: collapse; } table, td { border: 1px solid black; } td.class1 { /* added */ padding: 10px; /* for illustration */ position: relative; } td.class1:after { /* added */ content: ''; position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: transparent url(http://davidrhysthomas.co.uk/linked/strike.png) repeat-x; background-position: center; /* added */ } .class2 { text-align: center; color: #fff; height: 20px; width: 20px; background-color: red; border-radius: 5rem !important; display: inline-block; } 
 <table> <tr> <td>S</td> <td>B</td> </tr> <tr> <td>S</td> <td class="class1"> <span class="class2">S</span> </td> </tr> </table> 

You don't need to have a different class named table.class1 rather this will work well 你不需要有一个名为table.class1的不同类,而不是这样

.class2 {
   text-align: center;
    color: #fff;
    height: 20px;
    width: 20px;
    border-radius: 5rem !important;
    display: inline-block;
    background: transparent url(http://davidrhysthomas.co.uk/linked/strike.png) repeat-x;
    background-color: red;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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