Ramlang Porigi
Ramlang Porigi

Reputation: 55

How to have 2 colors in TD background with CSS

I have an event calendar that made use of CSS to paint the background red, to hilight holidays:

.holiday {
        text-align: center;
        font-size: 11px;
        font-weight: bold;
        color: #ffffff;
        background-color: #da1030;
        border: 1px solid black;
        height:20px;
        width:20px;
}

For special events, I used purple as the background:

.sp_event {
        text-align: center;
        font-size: 11px;
        font-weight: bold;
        color: #ffffff;
        background-color: purple;
        border: 1px solid black;
        height:20px;
        width:20px;
}

Things work okay: This to hilight holiday on 1st of this month 1

This to indicate special event that falls on the 5th of this month 5

BUT there are times that my special event falls on a holiday. So in this case, I want to have the TD background to be painted in red and purple, dividing the TD in 2 equal halves. Could you guys show me how this can be done with CSS? TIA.

Upvotes: 1

Views: 1293

Answers (2)

Explosion Pills
Explosion Pills

Reputation: 191829

You could accomplish this with a pseudoelement in the td that takes up half of the width and is positioned on the right to get the full width/height.

.holiday.sp_event:after {
    position: absolute;
    width: 50%;
    height: 100%;
    right: 0;
    background-color: red;
    content: "";
    top: 0;
}

http://jsfiddle.net/rX7gd/

Upvotes: 3

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276596

Here is a working fiddle

One solution people use is to use a gradient:

background: #a423b2; /* Old browsers */
background: -moz-linear-gradient(left, #a423b2 0%, #bf24b4 49%, #da1030 50%, #da1030 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, right top, color-stop(0%,#a423b2), color-stop(49%,#bf24b4), color-stop(50%,#da1030), color-stop(100%,#da1030)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(left, #a423b2 0%,#bf24b4 49%,#da1030 50%,#da1030 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(left, #a423b2 0%,#bf24b4 49%,#da1030 50%,#da1030 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(left, #a423b2 0%,#bf24b4 49%,#da1030 50%,#da1030 100%); /* IE10+ */
background: linear-gradient(to right, #a423b2 0%,#bf24b4 49%,#da1030 50%,#da1030 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#a423b2', endColorstr='#da1030',GradientType=1 ); /* IE6-9 */

I created this using this tool. It is cross-browser complaint (IE6 up) and requires you to make no structural changes to your HTML at all.

Upvotes: 3

Related Questions