Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background gradient as two tone solid color - one color width in px

div {
  height: 50px;
  width: 100%;
  background-image: linear-gradient(to right, white 50%, red 46px);
}

body {
  padding: 20px;
}
<div></div>

I'm trying to use linear gradients as a two tone solid color background in a div.

The div can be any width, and I would like one of the colors to have a specified width in px - and the other color to fill up whatever is left of the total width. Is that possible as all?

Like:

div {
  background-image: linear-gradient(to right, white auto, red 46px);
}
like image 642
Meek Avatar asked Nov 24 '17 09:11

Meek


People also ask

What is a gradient compare it to solid color?

Solid fill applies a single color uniformly within the whole of an object. Gradient fill applies a continuous blend of two or more colors, where one color gradually fades and changes into another.

How do I have two background colors in CSS?

CSS allows you to add multiple background images for an element, through the background-image property. The different background images are separated by commas, and the images are stacked on top of each other, where the first image is closest to the viewer.

How do I make the background of two colors in HTML?

To add background color in HTML, use the CSS background-color property. Set it to the color name or code you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a table, heading, div, or span tag.

Can we use two or more colors as background colors?

Essentially, the background property can hold two contents one over the other: 2 gradients, 2 images or you can mix-and-match any of those. To use more than 2 colors (because why not), put the powerful rgba() to use and use 2 gradient parameters.


1 Answers

You Can simply go with:

Use the fixed background colour first then just put 0 in the second colour it will fill the rest of the div.

background: linear-gradient(to right, lightgreen 19px, darkgreen 0);

This will work fine for you.

div {
  display: inline-block;
  background: linear-gradient(to right, lightgreen 19px, darkgreen 0);
  width: 50%;
  height: 100px;
  color: white;
  text-align: center;
}
<div>
  Test
</div>

Hope this was helpfull.

like image 83
Jithin Raj P R Avatar answered Sep 28 '22 14:09

Jithin Raj P R