Skip to content

Instantly share code, notes, and snippets.

@speir-wang
Last active November 14, 2018 04:52
Show Gist options
  • Save speir-wang/796897746e423259b71d0da10f9005d5 to your computer and use it in GitHub Desktop.
Save speir-wang/796897746e423259b71d0da10f9005d5 to your computer and use it in GitHub Desktop.
Change pseudo element properties with JavaScript
// SCSS
.element {
--dynamic-width: 0%;
width: 200px;
height: 200px;
background-color: #ccc;
margin: 0 auto;
&:after {
content: "";
display: block;
background-color: #000;
height: 2px;
width: var(--dynamic-width);
position:relative;
left: 100%;
top: 50%;
transition: width 0.2s ease;
}
}
// JavaScript
let element = document.getElementsByClassName("element")[0];
element.style.setProperty("--dynamic-width", '50%');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment