Home >Web Front-end >CSS Tutorial >How can I use SASS to randomly select a background image from a list of URLs?
SASS, a powerful CSS extension, provides advanced functionality for stylizing web pages. One such feature is the ability to dynamically select background images from a predetermined list. This article explores how to achieve this using SASS.
Question:
How can I use SASS to generate CSS code that randomly selects a background image from a provided list of URLs?
Answer:
To accomplish this task, harness the power of SASS's random() function, introduced in version 3.3.0. This function generates a random number within a specified range. By combining this functionality with a pre-defined list of image URLs, you can create dynamic background image assignments.
Consider the following SASS code snippet:
$imgKey: random(5); $list: "image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg", "image5.jpg"; $nth: nth($list, $imgKey); #footer-widgets .container .row { background-image: url("http://domain.com/blablabla/#{$nth}"); background-position: right bottom; background-repeat: no-repeat; }
In this example, the $list variable holds the list of image URLs. The $imgKey variable generates a random number between 1 and 5, representing the index of the image to be selected from the list. The nth($list, $imgKey) function extracts the image URL at that index.
Additional Notes:
The above is the detailed content of How can I use SASS to randomly select a background image from a list of URLs?. For more information, please follow other related articles on the PHP Chinese website!