Introduction
You want to style websites easily and keep your styles organized. SASS and CSS-in-JS are two ways to do this, each good for different situations.
Jump into concepts and practice - no test required
/* SASS example */ $main-color: #3498db; .button { background-color: $main-color; &:hover { background-color: darken($main-color, 10%); } } // CSS-in-JS example (React) const buttonStyle = { backgroundColor: '#3498db' };
$primary-color: #e74c3c; .card { border: 1px solid $primary-color; padding: 1rem; &:hover { background-color: lighten($primary-color, 40%); } }
const cardStyle = {
border: '1px solid #e74c3c',
padding: '1rem'
};<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>SASS vs CSS-in-JS Example</title> <style> /* Compiled SASS styles */ .button { background-color: #3498db; color: white; padding: 1rem 2rem; border: none; cursor: pointer; font-size: 1rem; border-radius: 0.5rem; transition: background-color 0.3s ease; } .button:hover { background-color: #2c80b4; } </style> </head> <body> <button class="button">SASS Styled Button</button> <script> // CSS-in-JS style applied via JavaScript const cssInJsButton = document.createElement('button'); cssInJsButton.textContent = 'CSS-in-JS Styled Button'; cssInJsButton.style.backgroundColor = '#e67e22'; cssInJsButton.style.color = 'white'; cssInJsButton.style.padding = '1rem 2rem'; cssInJsButton.style.border = 'none'; cssInJsButton.style.cursor = 'pointer'; cssInJsButton.style.fontSize = '1rem'; cssInJsButton.style.borderRadius = '0.5rem'; cssInJsButton.style.transition = 'background-color 0.3s ease'; cssInJsButton.addEventListener('mouseover', () => { cssInJsButton.style.backgroundColor = '#b36116'; }); cssInJsButton.addEventListener('mouseout', () => { cssInJsButton.style.backgroundColor = '#e67e22'; }); document.body.appendChild(cssInJsButton); </script> </body> </html>
SASS instead of CSS-in-JS?@import 'filename.scss'; to include other SASS files.@import 'variables.scss'; which is correct. @import 'styles.css'; imports a CSS file, which is allowed but not typical for SASS partials. Options C and D use JavaScript syntax, which is incorrect in SASS.$primary-color: blue;
.button {
color: $primary-color;
&:hover {
color: darken($primary-color, 20%);
}
}$primary-color is set to blue and used as the button text color.darken($primary-color, 20%) function makes the blue color 20% darker on hover.const styles = {
button: {
color: 'blue',
'&:hover': {
color: 'darkblue'
}
}
};