Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS gradient: define width in pixels

Tags:

css

Please take a look at this fiddle: http://jsfiddle.net/jpftqc26/

A CSS gradient, starts black from left, turns into red, then back to black again. Really simple.

Is there any way I can make the red part 500px wide and the black parts fill the screen, whatever the resolution? With red in the middle, just like in the fiddle.

Is there a way do define a width in pixels, between color stops, in a CSS gradient?

Code:

.test_gradient {
background: 
linear-gradient(
      to right, 
      #000000,
      #000000 20%,
      #ff0000 20%,
      #ff0000 80%,
      #000000 80%
    );
like image 253
Malasorte Avatar asked Oct 06 '14 18:10

Malasorte


People also ask

How you define the function in CSS image for a linear gradient?

The linear-gradient() function sets a linear gradient as the background image. To create a linear gradient you must define at least two color stops. Color stops are the colors you want to render smooth transitions among. You can also set a starting point and a direction (or an angle) along with the gradient effect.

How do you add opacity to a linear gradient?

Linear Gradient - Transparency To add transparency, we use the rgba() function to define the color stops. The last parameter in the rgba() function can be a value from 0 to 1, and it defines the transparency of the color: 0 indicates full transparency, 1 indicates full color (no transparency).

What is Webkit gradient?

First, -webkit-gradient uses a two-point syntax that lets you explicitly state where a linear gradient starts and ends. linear-gradient does away with this in favor of convenient box-filling behavior. If you really want the gradient to stop before the edges of the box, you can do so via color stop placement.

What is linear gradient CSS?

The linear-gradient() CSS function creates an image consisting of a progressive transition between two or more colors along a straight line. Its result is an object of the <gradient> data type, which is a special kind of <image> .


2 Answers

Yes. you can do this with hard pixels points and the use of the calc function. Just set them as such:

http://jsfiddle.net/jpftqc26/9/

CSS:

.test_gradient {
background: 
linear-gradient(
      to right, 
      #000000 0px, /* Starting point */
      #000000 calc(50% - 250px), /* End black point */
      #ff0000 calc(50% - 250px), /* Starting red point */
      #ff0000 calc(50% + 250px), /* End red point */
      #000000 calc(50% + 250px), /* Starting black point */
      #000000 100% /* End black point */
    );
like image 151
Phlume Avatar answered Sep 20 '22 18:09

Phlume


Another way to do it, without using calc(), is to use 2 different gradients

.test_gradient {
background-image: 
linear-gradient( to left,  #ff0000 0px,  #ff0000 250px,  #000000 100px), linear-gradient( to right,   red 0px,  #ff0000 250px,  #000000 100px);

background-size: 50.1% 1000px;
background-position: top left, top right;
background-repeat: no-repeat;
}

One goes to the right, the other to the left, and each one has half the total width

fiddle

like image 30
vals Avatar answered Sep 21 '22 18:09

vals