Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="banner" id="dynamicBanner">

  <div class="banner-header" id="dynamicHeader">

    <svg width="72" height="72" viewBox="0 0 72 72" xmlns="http://www.w3.org/2000/svg">
      <path d="M25.5915 27.9395L31.6323 35.1616L31.8108 35.3749L32.0862 35.3358L41.4078 34.0109L36.406 41.9877L36.2582 42.2234L36.3805 42.4732L40.5212 50.9292L31.3891 48.6372L31.1193 48.5695L30.9195 48.763L24.1568 55.314L23.5148 45.9206L23.4958 45.6431L23.25 45.5129L14.9298 41.1056L23.6651 37.5922L23.9231 37.4884L23.971 37.2144L25.5915 27.9395Z" fill="currentColor" stroke="currentColor" />
      <path d="M39.1961 15.5517L41.4513 18.2479L41.6298 18.4613L41.9052 18.4221L45.3853 17.9275L43.5179 20.9055L43.3701 21.1412L43.4925 21.391L45.0383 24.5479L41.629 23.6922L41.3592 23.6245L41.1594 23.8181L38.6347 26.2638L38.3949 22.7569L38.376 22.4794L38.1301 22.3492L35.024 20.7038L38.2851 19.3921L38.5432 19.2883L38.5911 19.0143L39.1961 15.5517Z" fill="currentColor" stroke="currentColor" />
      <path d="M52.0745 34.8681L54.3297 37.5643L54.5082 37.7777L54.7836 37.7385L58.2637 37.2439L56.3963 40.2219L56.2485 40.4576L56.3709 40.7074L57.9167 43.8643L54.5074 43.0086L54.2376 42.9409L54.0378 43.1345L51.5131 45.5802L51.2734 42.0733L51.2544 41.7958L51.0086 41.6656L47.9024 40.0202L51.1635 38.7085L51.4216 38.6047L51.4695 38.3307L52.0745 34.8681Z" fill="currentColor" stroke="currentColor" />
    </svg>

    Respect for All
  </div>
  <div class="banner-body">
    <span id="points">Shout Out! <b>2540 points</b></span>
  </div>
</div>

<div class="color-picker-container">
  <input type="color" id="colorPicker" value="#FFCF27">
  <button onclick="updateColors()">Change Background</button>
</div>
              
            
!

CSS

              
                body {
  font-family: Arial, sans-serif;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f4f4f4;
}
.banner {
  width: 100%;
  max-width: 600px;
  border-radius: 8px;
  overflow: hidden;
  padding: 32px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  text-align: center;
  font-size: 16px;
  color: white;
  background-color: #ffcf27; /* Default background */
  transition: background-color 0.3s, color 0.3s;
}
.banner-header svg {
  vertical-align: middle;
  margin-bottom: 10px;
}

.banner-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  align-items: center;
  padding: 16px 0px 0px 0px;
  font-size: 26px;
  font-weight: bold;
}
.banner-body {
  padding: 32px;
}
.color-picker-container {
  margin-top: 16px;
  text-align: center;
}

              
            
!

JS

              
                function luminance(r, g, b) {
    var a = [r, g, b].map(function (v) {
        v /= 255;
        return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4);
    });
    return a[0] * 0.2126 + a[1] * 0.7152 + a[2] * 0.0722;
}

function contrastRatio(rgb1, rgb2) {
    var lum1 = luminance(rgb1[0], rgb1[1], rgb1[2]);
    var lum2 = luminance(rgb2[0], rgb2[1], rgb2[2]);
    var brightest = Math.max(lum1, lum2);
    var darkest = Math.min(lum1, lum2);
    return (brightest + 0.05) / (darkest + 0.05);
}

function rgbToHsl(r, g, b){
    r /= 255, g /= 255, b /= 255;
    var max = Math.max(r, g, b), min = Math.min(r, g, b);
    var h, s, l = (max + min) / 2;

    if(max === min){
        h = s = 0; // achromatic
    } else {
        var d = max - min;
        s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
        switch(max){
            case r: h = (g - b) / d + (g < b ? 6 : 0); break;
            case g: h = (b - r) / d + 2; break;
            case b: h = (r - g) / d + 4; break;
        }
        h /= 6;
    }

    return [h, s, l];
}

function hslToRgb(h, s, l){
    var r, g, b;

    if(s === 0){
        r = g = b = l; // achromatic
    } else {
        function hue2rgb(p, q, t){
            if(t < 0) t += 1;
            if(t > 1) t -= 1;
            if(t < 1/6) return p + (q - p) * 6 * t;
            if(t < 1/2) return q;
            if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
            return p;
        }

        var q = l < 
0.5 ? l * (1 + s) : l + s - l * s;
var p = 2 * l - q;
r = hue2rgb(p, q, h + 1/3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1/3);
}
  return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}

function adjustColorLightness(r, g, b, amount){
var hsl = rgbToHsl(r, g, b);
hsl[2] += amount; // adjust lightness
hsl[2] = Math.min(1, Math.max(0, hsl[2])); // clamp between 0 and 1
return hslToRgb(hsl[0], hsl[1], hsl[2]);
}

function updateColors() {
var colorPicker = document.getElementById("colorPicker");
var banner = document.getElementById("dynamicBanner");
var header = document.getElementById("dynamicHeader");
var points = document.getElementById("points");
var bgColor = colorPicker.value;
  banner.style.backgroundColor = bgColor;

var r = parseInt(bgColor.slice(1, 3), 16);
var g = parseInt(bgColor.slice(3, 5), 16);
var b = parseInt(bgColor.slice(5, 7), 16);

var lightened = adjustColorLightness(r, g, b, 0.9); // Lighten color by 90%
var darkened = adjustColorLightness(r, g, b, -0.45); // Darken color by 45%

var lightenedRgb = [lightened[0], lightened[1], lightened[2]];
var darkenedRgb = [darkened[0], darkened[1], darkened[2]];

var contrastWithLightened = contrastRatio([r, g, b], lightenedRgb);
var contrastWithDarkened = contrastRatio([r, g, b], darkenedRgb);

var textColor;
if (contrastWithLightened > contrastWithDarkened) {
    textColor = `rgb(${lightenedRgb.join(",")})`;
} else {
    textColor = `rgb(${darkenedRgb.join(",")})`;
}

header.style.color = textColor;
points.style.color = textColor;
}

window.onload = updateColors;
              
            
!
999px

Console