114

I generated a bubble chat thingy from http://www.ilikepixels.co.uk/drop/bubbler/

In my page I put a number inside of it

.bubble {
  position: relative;
  width: 20px;
  height: 15px;
  padding: 0;
  background: #FFF;
  border: 1px solid #000;
  border-radius: 5px;
}

.bubble:after {
  content: "";
  position: absolute;
  top: 4px;
  left: -4px;
  border-style: solid;
  border-width: 3px 4px 3px 0;
  border-color: transparent #FFF;
   display: block;
  width: 0;
  z-index: 1;
}

.bubble:before {
  content: "";
  position: absolute;
  top: 4px;
  left: -5px;
  border-style: solid;
  border-width: 3px 4px 3px 0;
  border-color: transparent #000;
  display: block;
  width: 0;
  z-index: 0;
}

I want the background-color of the bubble to change according to the number inside of it via rgb

so if my div is

<div class="bubble" style="background-color: rgb(100,255,255);"> 100 </div>

I want the color to be rgb(100,255,255)

The thing is this doesn't affect the triangle.

How do I write the inline css so it will include the :before and :after?

6 Answers 6

130

You can, using CSS variables (more precisely called CSS custom properties).

  • Set your variable in your style attribute:
    style="--my-color-var: orange;"
  • Use the variable in your stylesheet:
    background-color: var(--my-color-var);

Browser compatibility

Minimal example:

div {
  width: 100px;
  height: 100px;
  position: relative;
  border: 1px solid black;
}

div:after {
  background-color: var(--my-color-var);
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
}
<div style="--my-color-var: orange;"></div>

Your example:

.bubble {
  position: relative;
  width: 30px;
  height: 15px;
  padding: 0;
  background: #FFF;
  border: 1px solid #000;
  border-radius: 5px;
  text-align: center;
  background-color: var(--bubble-color);
}

.bubble:after {
  content: "";
  position: absolute;
  top: 4px;
  left: -4px;
  border-style: solid;
  border-width: 3px 4px 3px 0;
  border-color: transparent var(--bubble-color);
  display: block;
  width: 0;
  z-index: 1;
  
}

.bubble:before {
  content: "";
  position: absolute;
  top: 4px;
  left: -5px;
  border-style: solid;
  border-width: 3px 4px 3px 0;
  border-color: transparent #000;
  display: block;
  width: 0;
  z-index: 0;
}
<div class='bubble' style="--bubble-color: rgb(100,255,255);"> 100 </div>

2
  • 3
    Not supported by Internet Explorer, unfortunately Commented Sep 25, 2019 at 8:15
  • 2
    Yup, if you need to support IE, you'll want to limit custom properties to use cases that won't cripple website functionality for IE users. In other words, use progressive enhancement. Commented Sep 25, 2019 at 12:46
80

You can't. With inline styles you are targeting the element directly. You can't use other selectors there.

What you can do however is define different classes in your stylesheet that define different colours and then add the class to the element.

4
  • can i do it with javascript? Commented Jan 21, 2013 at 10:37
  • 6
    Apparently yes: stackoverflow.com/questions/7330355/… Commented Jan 21, 2013 at 10:37
  • @NickGinanto sure you can - it just won't work :D your fighting here with browser internals, you cannot reinvent the weel with a language laying on top of that...
    – jebbie
    Commented Jun 19, 2018 at 8:31
  • 1
    Oddly, in spite of being marked as 'The Answer', this answer is quite incorrect. See Nathan Arthur's answer above.
    – Spinner
    Commented Oct 2, 2021 at 4:31
58

The key is to use background-color: inherit; on the pseudo element.
See: http://jsfiddle.net/EdUmc/

1
  • 1
    good answer. inheritance can be very very handy with :after when you want things like arrows made out of borders with parent-dependent colors... Commented Aug 13, 2020 at 12:52
18

If you really need it inline, for example because you are loading some user-defined colors dynamically, you can always add a <style> element right before your content.

<style>#project-slide-1:before { color: #ff0000; }</style>
<div id="project-slide-1" class="project-slide"> ... </div>

Example use case with PHP and some (wordpress inspired) dummy functions:

<style>#project-slide-<?php the_ID() ?>:before { color: <?php the_field('color') ?>; }</style>
<div id="project-slide-<?php the_ID() ?>" class="project-slide"> ... </div>

Since HTML 5.2 it is valid to place style elements inside the body, although it is still recommend to place style elements in the head.

Reference: https://www.w3.org/TR/html52/document-metadata.html#the-style-element

2
  • 1
    great solution! It was perfect for my situation (aplying conditionals in liquid that affect styles). Commented Dec 3, 2018 at 23:55
  • emphasis on "if you really need it"! :) Commented Aug 13, 2020 at 12:54
2

I resolved a similar problem by border-color: inherit

, see:

<li style="border-color: <?php echo $hex ?>;">...</li>

li {
    border-width: 0;
}

li:before {
    content: '';
    display: inline-block;
    float: none;
    margin-right: 10px;
    border-width: 4px;
    border-style: solid;
    border-color: inherit;
}
0

I found two methods of adding color to an external SVG: https://codepen.io/nathanhiemstra/pen/LYaMzOG?editors=1100

.icon-mask {
    -webkit-mask-image: url(https://www.svgrepo.com/show/322934/path-distance.svg);
    mask-image: url(https://www.svgrepo.com/show/322934/path-distance.svg);
    display: inline-flex;
    height: 100px;
    width: 100px;
    mask-repeat: no-repeat;
    mask-position: center center;
    mask-size: contain;
}

.icon-bg-image {
    background-image: url(https://www.svgrepo.com/show/322934/path-distance.svg);
    display: inline-flex;
    height: 100px;
    width: 100px;
    background-repeat: no-repeat;
    background-position: center center;
    background-size: contain;
}

.icon-red {
  background-color: #AD122A;
}
.icon-pink {
  background-color: #FF00FF;
}

/* Get filter value from hex here: https://codepen.io/sosuke/pen/Pjoqqp */
.icon-filter-red {
  filter: invert(12%) sepia(77%) saturate(3846%) hue-rotate(340deg) brightness(103%) contrast(99%);
}

.icon-filter-pink {
  filter: invert(28%) sepia(100%) saturate(5081%) hue-rotate(296deg) brightness(114%) contrast(133%);
<h4>Original</h4>
<i class="icon-bg-image"></i>

<h4>Image Mask on color background</h4>
<i class="icon-mask icon-red"></i>
<i class="icon-mask icon-pink"></i>

<h4>Filter on background image</h4>
<i class="icon-bg-image icon-filter-red"></i>
<i class="icon-bg-image icon-filter-pink"></i>

Not the answer you're looking for? Browse other questions tagged or ask your own question.