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

              
                <h1>Focus indicator demo</h1>
<p>To demonstrate <a href="https://www.w3.org/WAI/WCAG21/Techniques/css/C40">C40: Creating a two-color focus indicator to ensure sufficient contrast with all components</a> use the button in box 1 to change the colours of Box 2. Hover or focus on the button in box 2 to see that the focus indicator is visiable whatever colours are chosen.<br />For convenience the hover state and focus state of the button are the same.</p>


<div class="grid-container">

  <div class="grid-child ">
        <h2>Box 1</h2>
    <div class="content">
      <button id="genNew">Change colour</button>
    </div>
  </div>

  <div id="show" class="grid-child ">
    <h2>Box 2</h2>
    <div class="content">
      <button id="exampleButton">Example button</button>
    </div>
  </div>
</div>

<p>Box 2 fill colour: <span id="color"></span></p>
<p>Box 2 button colour: <span id="bcolor"></span></p>
              
            
!

CSS

              
                button:hover,
button:focus {
  /* ensure high-contrast mode still has an indicator */
  outline: 2px transparent solid;
  /* Apply a thick white box-shadow with 
	   a thin dark blue indicator in the middle */
  box-shadow: 0 0 0 2px white, 0 0 0 4px #002e3b, 0 0 4px 8px white;
}
.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-gap: 20px;
}
h2 {
  color: white;
  background: black;
  border:white solid 1px;
  margin: 1rem;
  padding: 1rem;
}
@media (max-width: 800px) {
  .grid-container {
  display: grid;
  grid-template-columns: 1fr;
  grid-gap: 20px;
}
}
.grid-child {
  border: 2px solid black;
  outline: 2px solid white;
  border-radius: 0.5rem;
}

button {
  border-radius: 0.5rem;
  vertical-align: middle;
  padding: 0.5rem 1rem 0.5rem 1rem;
  font-size: 1.2rem;
  text-align: center;
  margin: 1rem;
  color: #FCBC00;
  background: #002e3b;
  border: none;
}

#exampleButton {
  color: #231f20;
}

.content {
  display: flex;
  justify-content: center;
  align-items: center;
}

* {
    box-sizing: border-box;
    font-family: Arial, Helvetica, sans-serif;
}

              
            
!

JS

              
                const setBg = () => {
  // put into element variable
  const element = document.getElementById("show");
  // get button id
  const buttonElement = document.getElementById("exampleButton");
  // list possible background buttons
  const buttonBackgrounds = [
    "#FFFFFF",
    "#9FB1BD",
    "#E1E8EC",
    "#74C9E5",
    "#3CBAC6",
    "#B3DBD2",
    "#C1D100",
    "#FCBC00"
  ];

  //pick random button background put in variable
  const randomButtonColor = Math.floor(
    Math.random() * buttonBackgrounds.length
  );


  
  // set button background
  buttonElement.style.backgroundColor = buttonBackgrounds[randomButtonColor];
  // put button colour in text
  bcolor.innerHTML = buttonBackgrounds[randomButtonColor];
  
// Contrast arrays for text 
  
const textcontrastsFFFFFF = [
"#231F20",
"#00131D",
"#495961",
"#002E3B",
"#005C84",
"#D5007F",
"#8D3970"
];
	
const textcontrasts9FB1BD = [
"#231F20",
"#00131D",
"#002E3B"
];
const textcontrastsE1E8EC = [
"#231F20",
"#00131D",
"#495961",
"#002E3B",
"#005C84",
"#8D3970"
];
	
const textcontrasts74C9E5 = [
"#231F20",
"#00131D",
"#E1E8EC"	
  ];
	
const textcontrasts3CBAC6 = [
"#231F20",
"#00131D",
"#002E3B"
];
	
const textcontrastsB3DBD2 = [
"#231F20",
"#00131D",
"#495961",
"#002E3B",
"#005C84",
"#8D3970",
];
	
const textcontrastsC1D100 = [
"#231F20",
"#00131D",
"#002E3B",
];
	
const textcontrastsFCBC00 = [
"#231F20",
"#00131D",
"#002E3B",
];  //Big if thing
  
if (buttonBackgrounds[randomButtonColor]=="#FFFFFF") {
    let divBackgroundColor = Math.floor(
    Math.random() * textcontrastsFFFFFF.length
  );
    element.style.backgroundColor = textcontrastsFFFFFF[divBackgroundColor];
	var boxColour = textcontrastsFFFFFF[divBackgroundColor];
} else if (buttonBackgrounds[randomButtonColor]=="#9FB1BD") {
    let divBackgroundColor = Math.floor(
    Math.random() * textcontrasts9FB1BD.length
  );
    element.style.backgroundColor = textcontrasts9FB1BD[divBackgroundColor];
	var boxColour = textcontrasts9FB1BD[divBackgroundColor];
} else if (buttonBackgrounds[randomButtonColor]=="#E1E8EC") {
    let divBackgroundColor = Math.floor(
    Math.random() * textcontrastsE1E8EC.length
  );
    element.style.backgroundColor = textcontrastsE1E8EC[divBackgroundColor];
		var boxColour = textcontrastsE1E8EC[divBackgroundColor];
} else if (buttonBackgrounds[randomButtonColor]=="#74C9E5") {
    let divBackgroundColor = Math.floor(
    Math.random() * textcontrasts74C9E5.length
  );
    element.style.backgroundColor = textcontrasts74C9E5[divBackgroundColor];
		var boxColour = textcontrasts74C9E5[divBackgroundColor];
} else if (buttonBackgrounds[randomButtonColor]=="#3CBAC6") {
    let divBackgroundColor = Math.floor(
    Math.random() * textcontrasts3CBAC6.length
  );
    element.style.backgroundColor = textcontrasts3CBAC6[divBackgroundColor];
		var boxColour = textcontrasts3CBAC6[divBackgroundColor];
} else if (buttonBackgrounds[randomButtonColor]=="#B3DBD2") {
    let divBackgroundColor = Math.floor(
    Math.random() * textcontrastsB3DBD2.length
  );
    element.style.backgroundColor = textcontrastsB3DBD2[divBackgroundColor];
		var boxColour = textcontrastsB3DBD2[divBackgroundColor];
} else if (buttonBackgrounds[randomButtonColor]=="#C1D100") {
    let divBackgroundColor = Math.floor(
    Math.random() * textcontrastsC1D100.length
  );
    element.style.backgroundColor = textcontrastsC1D100[divBackgroundColor];
		var boxColour = textcontrastsC1D100[divBackgroundColor];
} else   {
      let divBackgroundColor = Math.floor(
    Math.random() * textcontrastsFCBC00.length
  );
    element.style.backgroundColor = textcontrastsFCBC00[divBackgroundColor];
		var boxColour = textcontrastsFCBC00[divBackgroundColor];
} 
color.innerHTML = boxColour;  
};

genNew.addEventListener("click", setBg);
setBg();

              
            
!
999px

Console