Vishnu Sadanandan
Vishnu Sadanandan

Reputation: 64

How to set multiple color for a single border

I need to set multiple color for a single border like the below one. But here i used three td element in a tr. I need a single td element and border must be its (td's) bottom or top border.

<table style="border-spacing:0px;margin-top:5%;margin-left:2%" cellpadding="0" width="25%">
  <tbody>
    <tr>
      <td width="5%" style="border-bottom: 3px solid red;">       </td>
      <td width="90%" style="border-bottom: 3px solid green;">
      </td>
      <td width="5%" style="border-bottom: 3px solid red;">       </td>
    </tr>
  </tbody>
 </table>

Upvotes: 0

Views: 6075

Answers (5)

Mohammad Usman
Mohammad Usman

Reputation: 39322

With CSS border property we can create borders having single color only. However we can mimic this effect by using CSS3.

With linear-gradient() we can create a spectrum having as many colors as we want.

Below are few examples:

Border having 2 colors:

Necessary CSS:

background-image: linear-gradient(to right, red 50%, green 50%);

Screenshot:

Borders with two colors

Working Example:

td {
  padding: 10px;
  background: linear-gradient(to right, red 10%, green 10%, green 90%, red 90%) no-repeat;
  background-size: 100% 3px;
  background-position: left bottom;
}
<table style="border-spacing:0px;margin-top:5%;margin-left:2%" cellpadding="0" width="25%">
<tbody>
  <tr>
    <td>Some text here</td>
  </tr>
  <tr>
    <td>Some text here</td>
  </tr>
</tbody>
</table>


Border having 3 colors:

Necessary CSS:

background-image: linear-gradient(to right, red 33.33%, green 33.33%,
                                            green 66.67%, red 66.67%);

Screenshot:

Border with three colors

Working Example:

td {
  padding: 10px;
  background: linear-gradient(to right, red 33.33%, green 33.33%, green 66.67%, red 66.67%) no-repeat;
  background-size: 100% 3px;
  background-position: left bottom;
}
<table style="border-spacing:0px;margin-top:5%;margin-left:2%" cellpadding="0" width="25%">
<tbody>
  <tr>
    <td>Some text here</td>
  </tr>
  <tr>
    <td>Some text here</td>
  </tr>
</tbody>
</table>

Upvotes: 1

Mathijs Flietstra
Mathijs Flietstra

Reputation: 12974

You can do it this way, setting a regular border and using :before and :after pseudo elements to create the red parts.

table {
  width: 25%;
}

td {
  border-bottom: 3px solid green;
  position: relative;
}

td:before,
td:after {
  border-bottom: 3px solid red;
  content: "";
  display: block;
  width: 5%;
  position: absolute;
  bottom: -3px;
}

td:before {
  left: 0;
}

td:after {
  right: 0;
}
<table>
  <tr>
    <td>
    </td>
  </tr>
</table>

Upvotes: 0

Navneet vaghasiya
Navneet vaghasiya

Reputation: 117

<table style="border-spacing:0px;margin-top:5%;margin-left:2%" cellpadding="0" width="25%">
  <tbody>
    <tr>
      <td width="5%" style="border-left: 3px solid red;border-right: 3px solid red; border-top: 3px solid green;border-bottom: 3px solid green;">text     </td>
  
    </tr>
  </tbody>
 </table>

Upvotes: 0

shubham agrawal
shubham agrawal

Reputation: 3541

You can use pseudo class for that:

.box {
  text-align: center;
  position: relative;
  line-height: 100px;
  background: #fff;
  height: 100px;
  width: 300px;
}

.box:after {
  background: linear-gradient(to right, #bcbcbc 25%, #ffcd02 25%, #ffcd02 50%, #e84f47 50%, #e84f47 75%, #65c1ac 75%);
  position: absolute;
  content: '';
  height: 4px;
  right: 0;
  left: 0;
  top: 0;
}
<table>
  <td class="box"></td>
</table>

Upvotes: 0

Pranav Patel
Pranav Patel

Reputation: 1559

you can do by following way. Please check.

.test {
  width: 500px;
  height: 100px;
  background-color: #ccc;
  position: relative;
}

.test:before, .test:after {
  content: "";
  position: absolute;
  left: 0px;
  right: 0px;
  height: 10px;
  background-image: -webkit-linear-gradient(0deg, red 20px, blue 20px, blue 40px, yellow 40px, yellow 60px, green 60px, green 80px);
  background-image: -ms-linear-gradient(0deg, red 20px, blue 20px, blue 40px, yellow 40px, yellow 60px, green 60px, green 80px);
  background-size: 80px;
}

.test:before {
  top: 0px;
}
.test:after {
  bottom: 0px;
}
<table>
<tr>
  <td class="test">
    1
  </td>
  <td class="test">
    2
  </td>
</tr>
</table>

Upvotes: 1

Related Questions