Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS Sass Basics (retired) Advanced Sass Concepts Creating Loops with @for and @each

Jess Hardy
Jess Hardy
12,811 Points

Is there a way that we can display the rendered background color hex value inside the div

Is there a way that we can display the rendered background color hex value inside the div

3 Answers

Hi Jess,

I think you want something like this?

<div class="box">#ffffff</div>

There's no way that I know of to get the hex code inside the html without getting javascript involved.

However, you can use the content property in conjunction with either the ::before or ::after pseudo elements and you could achieve it with sass alone. The hex code will not be part of the html but you will still be able to see it on the page.

Here's a codepen demo that should be enough to get you started: http://codepen.io/anon/pen/myeXOg I worked off the video example with the gradient and 100 box div's

The .scss:

.box {
    width: 100%;
    height: 20px;

    @for $i from 1 through 100 {
      $bg-color: darken(white, $i);
      $color: invert($bg-color);
      &:nth-child(#{$i}) {
        background: $bg-color;

        &::before {
          content: '#{$bg-color}';
          position: absolute;
          color: $color;
       }
    }
  }
}

The basic idea is that you want to get the background color into the content property and then choose a suitable text color to display it in.

I used the invert() function here but as you can see, it's a bad choice for right in the middle. You'll probably need to look at other functions or you might need to calculate the color each time through the loop in order to get a properly contrasting color.

Here's one example of how you might handle it: http://thesassway.com/intermediate/dynamically-change-text-color-based-on-its-background-with-sass

That technique checks if the lightness is greater than 50 and uses black for the color, otherwise it uses white for the color. This would be ok if you're fine with only 2 text colors being used. I have not tried it with this codepen demo but I imagine it would make the text on the top half black and the bottom half would be white.

The other problem is that some of these hexcodes transate to css color keywords. I'm not sure if there's a way to force it to use hexcodes but that's something else to look into if you don't want color names displayed.

Let me know if you were talking about something else entirely.

Jess Hardy
Jess Hardy
12,811 Points

Hey Jason, this was perfect. I am just trying to build a quick "palette" system for our dev group to look at. So I'll be trying to put the rendered hex code/nam under the color box, like a caption.

Ok, that makes the text color choice easier then. It can probably be the same color throughout.

Probably something more like this is what you're going for then? http://codepen.io/anon/pen/OPyQQP

You can use the position properties, top left right and bottom, to put the hex code where you want it and then apply whatever caption styles you need to the ::before pseudo element.

Had another thought. These hex codes won't be selectable doing it like this. Will your dev's need to be able to select the hex codes for copy/pasting?

Jess Hardy
Jess Hardy
12,811 Points

Yep, that is exactly ha i'm looking for