11

I want to have one element with multiple background colors? Analog of:

<div class="multiplebackground">
  <div style="background-color:red"></div>
  <div style="background-color:green"></div>
  <div style="background-color:blue"></div>
</div>

but if I have only 1 div with some text content:

<div class="multiplebackground">
  ...
</div>

.multiplebackground {
  ???
}

Or it is not possible?

4
  • 2
    Describe what it should look like better. My understanding is that the last color defined would just overwrite the others so... what's the point. Commented Feb 22, 2016 at 14:04
  • 2
    @alexey Did you mean gradients? Commented Feb 22, 2016 at 14:06
  • possibile duplicate of stackoverflow.com/questions/32607835/… ? Commented Feb 22, 2016 at 14:17
  • Hey have you tried this below? stackoverflow.com/questions/19081355/… Commented Feb 22, 2016 at 14:24

6 Answers 6

12

You can achieve this with gradients. You just need to blend from a colour to the same colour, and then blend to the next colour across zero pixels and so on.

.Neapolitan {
  height: 200px;
  width: 200px;
  background: linear-gradient(to bottom, red 0%, red 33%, green 33%, green 66%, blue 66%, blue 100%);
}
<div class="Neapolitan">

</div>

(Prefixed alternatives and IE specific code is available for older browsers)

Sign up to request clarification or add additional context in comments.

Comments

3

You can do this with linear-gradient

body, html {
  margin: 0;
  padding: 0;
}

.multiplebackground {
  min-height: 100vh;
  background: linear-gradient(to bottom, red 0%, red 33%, green 33%, green 66%, blue 66%, blue 100%);
  background-size: 100%;
  background-repeat: no-repeat;
}
<div class="multiplebackground"></div>

Comments

2

Yes...using a linear gradient with color stops

.multiplebackground {
  height: 50px;
  background: linear-gradient(to right, red, red 33%, blue 33%, blue 66%, green 66%);
}
<div class="multiplebackground">

</div>

Comments

2

You can do this with the css3 gradient background property!

background: linear-gradient(to right, 
            red 33%, 
            green 33%, 
            green 66%, 
            blue 66%); 

Notice that I define green twice, from 33% to 66%. That's because I have to define where it starts AND where it ends, so i get a sharp edge between every gradient

Comments

2

May be linear gradient

http://www.w3schools.com/css/css3_gradients.asp

body, html {
margin: 0;
  padding: 0;
}

   #grad {
background: red; /* For browsers that do not support gradients */
background: -webkit-linear-gradient(red, yellow, green); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(red, yellow, green); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(red, yellow, green); /* For Firefox 3.6 to 15 */
background: linear-gradient(red, yellow, green); /* Standard syntax */
}
<div id="grad"></div>

4 Comments

Most of the modern browers do not need the prefixes. Only the standard syntax works fine. Get rid of prefixes
Yes but old browsers is still used too
Yeah I agree. But the numbers are very less. -o- prefixed browers are of 0.25% of the total global user base as stated in the above mentioned article. So are the others. I would prefer this as I want my code to look neat :)
In Russia Opera was very popular, I think 30% of market few years ago. But after they clone chrome I think popularity is not so big...
1

With CSS you can (as of right now) only style the first row different.

p:first-line{ background-color: coral;}

if you want something like :nth-line() you have to make it in javascript.

Here is an example of that: http://codepen.io/jnowland/pen/AifjK/

var nthLine = function () {
    var content = $('h1').text();
    var words = content.split(" ");
    var cache = words[0]; //because we miss the first word as we need to detect the height.
    var lines = [];
    $("header").append('<h1 id="sample">' + words[0] + '</h1>');
    var size = $('#sample').height();
    var newSize = size;
    for (var i = 1; i < words.length; i++) {
        var sampleText = $('#sample').html();
        cache = cache + ' ' + words[i];
        marker = [i];
        $('#sample').html(sampleText + ' ' + words[i]);
        var newSize = $('#sample').height();
        if (size !== newSize) {
            cache = cache.substring(0, cache.length - (words[i].length + 1));
            lines.push(cache);
            cache = words[i];
            size = $('#sample').height();
        }
    }
    lines.push(cache);
    cache = ''
    for (var i = 0; i < lines.length; i++) {
        cache = cache + ' <span class="line-' + [i] + '">' + lines[i] + '</span>'
    }
    $('#sample').remove();
    cache = cache.substring(1);
    //prints the result.
    $('h1').html(cache);
};
nthLine();
$(window).resize(nthLine);

2 Comments

As you can see in the question. I whant to use "css only" and only 1 element.
@alexey yeah I know, that's why I answered this way. The only way to solve your problem is by splitting up your text in multiple elements. Using the "linear-gradient" solution will be more complex if you want many colors and then you will style as if all rows were blocks. (you will have background colors where there is no text)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.