Loading...
(function(){<br />
let canvas = document.createElement('canvas'),<br />
ctx = canvas.getContext('2d'),<br />
w = canvas.width = innerWidth,<br />
h = canvas.height = innerHeight,<br />
particles = [],<br />
properties = {<br />
bgColor : 'rgba(17, 17, 19, 1)',<br />
particleColor : 'rgba(255, 40, 40, 1)',<br />
particleRadius : 3,<br />
particleCount : 60,<br />
particleMaxVelocity : 0.5,<br />
lineLength : 150,<br />
particleLife : 6,<br />
};<br />
<br />
document.querySelector('body').appendChild(canvas);<br />
<br />
window.onresize = function() {<br />
w = canvas.width = innerWidth;<br />
h = canvas.height = innerHeight;<br />
}<br />
<br />
class Particle {<br />
constructor() {<br />
this.x = Math.random()*w;<br />Loading...
To style an element that has no content inside, you can use the :empty pseudo-class.
For example, you can hide an element if it is empty:
1.element:empty {2 display: none;3}4
For flex and grid containers, you can use the gap property (or grid-gap in older grid implementations) to set spacing between items.
Unlike margins, gap:
You can also use:
To do this, you can use media queries such as @media (min-width: 700px) for a mobile-first approach, or @media (max-width: 700px) for a desktop-first approach.
1.cards {2 display: flex;3 flex-direction: column;45 @media (min-width: 700px) {6 flex-direction: row;7 }8}910//or1112.cards {13 display: flex;14 flex-direction: row;1516 @media (min-width: 700px) {17 flex-direction: column;18 }19}20
Media queries can also be combined. The following example specifies that the styles should be applied only when the screen width is between 500px and 1200px.
1@media (min-width: 500px) and (max-width: 1200px) {2 /* … */3}4
To use Container Queries, you must first set container-type: inline-size; on the container element. Then, apply conditional styles to the elements inside the container using the @container rule.
1.cards {2 container-type: inline-size;3}45@container (min-width: 400px) {6 .card {7 background-color: #F2E7DC;8 }9}10
Example: Styling based on a CSS custom property (CSS variable).
1/* To apply styles when the CSS variable is set to `true`: */2@container cards style(--featured: true) {3 .card__inner {4 color: red;5 }6}78/* To apply styles when the variable is `false` or not defined: */9@container cards not style(--featured: true) {10 .card__inner {11 color: black;12 }13}
You can use the @supports rule to apply styles only if the browser supports a specific CSS feature or property. This allows you to progressively enhance your styles while maintaining compatibility with older browsers.
:has selector1.card:has(.icon) {2 justify-content: space-around;3}
2) Show an element depending on the selected value in a <select> element.
1form:has(option[value="other"]:checked) textarea {2 display: block;3}4
3) Change the width of a block depending on the number of elements. For example, if the number of cards is greater than 5, reduce their size.
1.cards {2 --item-size: 200px;3 margin-top: 20px;4 width: 100%;5 display: grid;6 grid-template-columns: repeat(auto-fill, var(--item-size));7 gap: 20px;89 &:has(.card:nth-last-child(n + 5)) {10 --item-size: 100px;11 }12}13
The grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); property allows elements to automatically fill all available free space.
The :focus-within pseudo-class can help with this.
Below is an example of an input field that receives a shadow when it is focused.