SASS के बिल्ट-इन मॉड्यूल
यह लेख SASS के बिल्ट-इन मॉड्यूल की व्याख्या करता है।
हम मूलभूत से लेकर उन्नत उपयोग तक चरण-दर-चरण SASS के बिल्ट-इन मॉड्यूल को कवर करते हैं।
YouTube Video
SASS के बिल्ट-इन मॉड्यूल
SASS कई प्रकार के बिल्ट-इन मॉड्यूल प्रदान करता है, और उनका उपयोग करने से स्टाइलशीट बनाना और भी अधिक कुशल हो जाता है।
SASS के बिल्ट-इन मॉड्यूल क्या हैं?
SASS के बिल्ट-इन मॉड्यूल ऐसे मॉड्यूल हैं जो पुन: प्रयोज्य फ़ंक्शन और मिक्सिन प्रदान करते हैं। उनका उपयोग करने से जटिल गणनाएँ और कस्टम स्टाइल बनाना आसान हो जाता है।
मुख्य बिल्ट-इन मॉड्यूल में निम्न शामिल हैं:।
sass:colorsass:stringsass:mathsass:listsass:mapsass:selectorsass:meta
प्रत्येक मॉड्यूल में विशिष्ट कार्यों को सुव्यवस्थित करने वाली विशेषताएँ शामिल होती हैं।
प्रत्येक मॉड्यूल के विवरण और उदाहरण
sass:color मॉड्यूल
sass:color मॉड्यूल ऐसे फ़ंक्शन प्रदान करता है जो रंगों में बदलाव को आसान बनाते हैं।
मुख्य फ़ंक्शन
mix(): दो रंगों को मिलाएँadjust(): रंग, चमक, संतृप्ति और अन्य गुणों को एक साथ समायोजित करें
उपयोग उदाहरण
1@use "sass:color";
2
3$primary-color: #3498db;
4$secondary-color: #e74c3c;
5
6// Mix two colors with equal weight
7$blended-color: color.mix($primary-color, $secondary-color, 50%);
8
9// Adjust hue by 45 degrees using color.adjust()
10$adjusted-color: color.adjust($primary-color, $hue: 45deg);
11
12div {
13 background-color: $blended-color; // Result of mixing two colors
14 border-color: $adjusted-color; // Hue adjusted by 45 degrees
15}- यह कोड दो रंगों को मिलाकर और एक अन्य रंग की रंगत समायोजित करके नया रंग उत्पन्न करता है। उत्पन्न रंगों को तत्वों पर बैकग्राउंड और बॉर्डर रंग के रूप में लागू किया जाता है। यह उदाहरण रंगों में बदलाव की बुनियादी बातें समझने में मदद करता है।
sass:string मॉड्यूल
sass:string मॉड्यूल स्ट्रिंग संचालन के लिए उपयोगी फ़ंक्शन प्रदान करता है।
मुख्य फ़ंक्शन
quote(),unquote(): किसी स्ट्रिंग को उद्धरण चिह्नों में रखना या हटानाlength(): किसी स्ट्रिंग की लंबाई प्राप्त करेंto-upper-case(),to-lower-case(): किसी स्ट्रिंग को बड़े या छोटे अक्षरों में बदलें
उपयोग उदाहरण
1@use "sass:string";
2
3// base values
4$base-url: "https://example.com";
5$path: "/assets/style.css";
6
7// 1) Combine strings using interpolation and then quote the result
8$full-quoted: string.quote("#{$base-url}#{$path}");
9// Example result: "\"https://example.com/assets/style.css\""
10
11// 2) Remove quotes from a quoted string
12$full-unquoted: string.unquote($full-quoted);
13// Example result: https://example.com/assets/style.css
14
15// 3) Get the length of the unquoted string
16$url-length: string.length($full-unquoted);
17// Example output: number of characters in the URL
18
19// 4) Convert strings to upper/lower case and quote for safe CSS output
20$block-name: "main-header";
21// "MAIN-HEADER"
22$upper-quoted: string.quote(string.to-upper-case($block-name));
23// "main-footer"
24$lower-quoted: string.quote(string.to-lower-case("MAIN-FOOTER"));
25
26a::after {
27 /* Use quoted strings for content to ensure valid CSS */
28 content: $full-quoted; /* "https://example.com/assets/style.css" */
29}
30
31:root {
32 /* Insert numeric values with interpolation when needed */
33 --url-length: #{ $url-length }; /* Example: --url-length: 31; */
34}
35
36.header::before {
37 /* Output uppercase version */
38 content: $upper-quoted; /* "MAIN-HEADER" */
39}
40
41.footer::after {
42 /* Output lowercase version */
43 content: $lower-quoted; /* "main-footer" */
44}string.quote()औरstring.unquote()का उपयोग करके आप आउटपुट CSS में स्ट्रिंग के निरूपण को सटीक रूप से नियंत्रित कर सकते हैं।string.length()एक फ़ंक्शन है जो स्ट्रिंग की लंबाई प्राप्त करता है।string.to-upper-case()/string.to-lower-case()क्लास नाम बनाने और BEM नामों को फ़ॉर्मैट करने में उपयोगी हैं।
sass:math मॉड्यूल
sass:math मॉड्यूल गणितीय गणनाओं के लिए फ़ंक्शन प्रदान करता है।
मुख्य फ़ंक्शन
pow(): घातांक गणनाsqrt(): वर्गमूलabs(): परम मानround(),ceil(),floor(): गोल करना, ऊपर की ओर गोल करना, नीचे की ओर गोल करना
उपयोग उदाहरण
1@use "sass:math";
2
3// Using pow() to calculate exponential values
4$base-size: math.pow(2, 3) * 10px; // 80px
5
6// Using sqrt() to compute a square root
7$root-size: math.sqrt(144) * 1px; // 12px
8
9// Using abs() to ensure a positive value
10$offset: math.abs(-15px); // 15px
11
12// Using round(), ceil(), and floor() for different rounding methods
13$rounded: math.round(12.6px); // 13px
14$ceiled: math.ceil(12.1px); // 13px
15$floored: math.floor(12.9px); // 12px
16
17.container {
18 width: $base-size; // 80px
19 height: $root-size; // 12px
20 margin-left: $offset; // 15px
21}
22
23.values {
24 /* Demonstrating different rounding operations */
25 padding: $rounded; // 13px
26 border-width: $ceiled; // 13px
27 margin-top: $floored; // 12px
28}math.pow()औरmath.sqrt()आकार से जुड़ी गणनाओं में उपयोगी हैं, जबकिmath.abs()और राउंडिंग फ़ंक्शन समायोजनों को संभालने में मदद करते हैं। इनको मिलाकर सुसंगत UI स्केल की गणना करना आसान हो जाता है।
sass:list मॉड्यूल
sass:list मॉड्यूल सूची संबंधी कार्यों के लिए विशेषीकृत फ़ंक्शन प्रदान करता है।
मुख्य फ़ंक्शन
append(): तत्व जोड़ेंjoin(): सूचियों को जोड़ेंnth(): निर्दिष्ट स्थिति पर तत्व प्राप्त करेंlength(): सूची की लंबाई प्राप्त करें
उपयोग उदाहरण
1@use "sass:list";
2
3// Base list
4$colors: ("red", "blue", "green");
5
6// Add an element to the end of the list
7$colors-appended: list.append($colors, "yellow");
8// ("red", "blue", "green", "yellow")
9
10// Add an element to the beginning of the list using join()
11$colors-prepended: list.join(("black",), $colors);
12// ("black", "red", "blue", "green", "yellow")
13
14// Join two lists together
15$extra-colors: ("pink", "cyan");
16$merged-colors: list.join($colors-prepended, $extra-colors);
17// ("black", "red", "blue", "green", "yellow", "pink", "cyan")
18
19// Get list length
20$total-length: list.length($merged-colors);
21
22// Example usage in a loop: assign each color to a list item
23ul {
24 @for $i from 1 through $total-length {
25 li:nth-child(#{$i}) {
26 /* Get the color at index $i */
27 color: list.nth($merged-colors, $i);
28 }
29 }
30}- आप
append()का उपयोग करके सूची के अंत में तत्व जोड़ सकते हैं, और कई सूचियों को लचीले ढंग से जोड़ने के लिएjoin()का उपयोग कर सकते हैं। यदि आप शुरुआत में कोई तत्व जोड़ना चाहते हैं, तो आपjoin()का उपयोग करके एक एकल-तत्व सूची को सामने जोड़ सकते हैं।length()औरnth()को मिलाकर, उन UI शैलियों को बनाना आसान हो जाता है जिनमें डायनामिक सूची प्रोसेसिंग की आवश्यकता होती है।
sass:map मॉड्यूल
sass:map मॉड्यूल मैप्स (एसोसिएटिव ऐरे) के साथ काम करने के लिए फ़ंक्शन प्रदान करता है।
मुख्य फ़ंक्शन
get(): किसी कुंजी के लिए मान प्राप्त करेंset(): कुंजी-मूल्य जोड़ी जोड़ें या अपडेट करेंkeys(): सभी कुंजियाँ प्राप्त करें
उपयोग उदाहरण
1@use "sass:map";
2
3// Base theme map
4$theme-colors: (
5 "primary": #3498db,
6 "secondary": #e74c3c
7);
8
9// Update or add a value using set()
10$updated-theme: map.set($theme-colors, "warning", #f1c40f);
11// Map now has "warning": #f1c40f added
12
13// Get a value from the map
14$primary-color: map.get($updated-theme, "primary");
15
16// Get all keys from the map
17$all-keys: map.keys($updated-theme);
18// Example: ("primary", "secondary", "warning")
19
20button {
21 /* Apply color retrieved from the theme map */
22 background-color: $primary-color;
23}
24
25.debug {
26 /* Print keys as content for demonstration */
27 content: "#{$all-keys}";
28}map.set()का उपयोग करके आप मैप्स को डायनेमिक रूप से अपडेट कर सकते हैं, औरmap.get()के साथ मिलाकर आप लचीली थीम संरचनाएँ बना सकते हैं।map.keys()से आप कॉन्फ़िगरेशन प्रविष्टियों की सूची प्राप्त कर सकते हैं, जो विस्तारणीय स्टाइल्स डिज़ाइन करने में मदद करता है।
sass:selector मॉड्यूल
sass:selector मॉड्यूल सेलेक्टर में बदलाव के लिए सहायक फ़ंक्शन प्रदान करता है।
मुख्य फ़ंक्शन
nest(): सेलेक्टर को नेस्ट करेंis-superselector(): सेलेक्टर के समावेशन की जाँच करेंreplace(): सेलेक्टर को प्रतिस्थापित करें
उपयोग उदाहरण
1@use "sass:selector";
2
3// Nest selectors (combine parent and child)
4$nested-selector: selector.nest(".parent", ".child");
5// Result: ".parent .child"
6
7// Check if one selector is a superselector of another
8$is-super: selector.is-superselector(".parent", $nested-selector);
9// true because ".parent" matches all elements that
10// ".parent .child" can match as an ancestor
11
12// Replace part of a selector with another selector
13$replaced-selector: selector.replace(".parent .child", ".child", ".item");
14// Result: ".parent .item"
15
16// Use generated selectors in actual CSS output
17#{$nested-selector} {
18 /* Applies to .parent .child */
19 color: red;
20}
21
22@if $is-super {
23 .info::after {
24 /* Demonstrate boolean result */
25 content: "parent is a superselector";
26 }
27}
28
29#{$replaced-selector} {
30 /* Applies to .parent .item */
31 background: blue;
32}selector.nest()का उपयोग करके सेलेक्टर को लचीले ढंग से संयोजित करें औरselector.is-superselector()से उनके संबंधों की पुष्टि करें।selector.replace()के साथ मिलाकर उन्नत सेलेक्टर-जनरेशन लॉजिक को संक्षेप में संभालें।
sass:meta मॉड्यूल
sass:meta मॉड्यूल SASS में मेटा-प्रोग्रामिंग के लिए उपयोगी विशेषताएँ प्रदान करता है।
मुख्य फ़ंक्शन
variable-exists(): कोई वेरिएबल मौजूद है या नहीं, यह जाँचेंglobal-variable-exists(): कोई ग्लोबल वेरिएबल मौजूद है या नहीं, यह जाँचेंinspect(): डिबगिंग के लिए किसी मान को प्रदर्शित करें
उपयोग उदाहरण
1@use "sass:meta";
2
3// Define a global variable
4$color: #3498db;
5
6// Check if a variable exists in the current scope
7@if meta.variable-exists("color") {
8 body {
9 /* Apply style only if $color exists */
10 background-color: $color;
11 }
12}
13
14// Create a local variable inside a block
15.container {
16 $local-size: 20px;
17
18 @if meta.variable-exists("local-size") {
19 /* Demonstrates detection of local variable */
20 width: $local-size;
21 }
22}
23
24// Check if a global variable exists
25$result: meta.global-variable-exists("color"); // true
26
27.debug {
28 /* Use inspect() to output the inspected value as a string */
29 content: meta.inspect($result); // "true"
30}meta.variable-exists()औरmeta.global-variable-exists()आपको प्रत्येक स्कोप में वेरिएबल की स्थिति सुरक्षित रूप से निर्धारित करने देते हैं।meta.inspect()डिबगिंग के लिए बहुत उपयोगी है और मानों को स्ट्रिंग के रूप में प्रदर्शित कर सकता है।
व्यावहारिक उदाहरण
कई बिल्ट-इन मॉड्यूल को मिलाने से SASS की अभिव्यक्ति क्षमता और बढ़ जाती है। नीचे एक उदाहरण है जो color, math और list मॉड्यूल को साथ में उपयोग करके रंग प्रोसेसिंग और सूची संचालन को स्वचालित करता है।
1@use "sass:color";
2@use "sass:math";
3@use "sass:list";
4
5// Base color list
6$base-colors: (#3498db, #e74c3c, #2ecc71);
7$darkened-colors: (); // Empty list for processed colors
8
9// Loop through each base color and darken it by 10%
10@each $color in $base-colors {
11 $darkened-colors: list.append(
12 $darkened-colors,
13 // Darken color by decreasing lightness by 10%
14 color.adjust($color, $lightness: -10%)
15 );
16}
17
18div {
19 // Apply each processed color to a corresponding <div>
20 @for $i from 1 through list.length($darkened-colors) {
21 &:nth-child(#{$i}) {
22 // Set color by index
23 background-color: list.nth($darkened-colors, $i);
24 }
25 }
26}- इस कोड में, संसाधित रंगों को क्रमवार
list.append()के साथ जोड़ा जाता है, और रंगों को 10% गहरा करने के लिएcolor.adjust()के साथ$lightness: -10%का उपयोग किया जाता है। अंत में, प्रत्येक<div>पर अलग-अलग बैकग्राउंड रंग लागू करने के लिए@forकोlist.nth()के साथ जोड़ा जाता है।
सारांश
SASS के बिल्ट-इन मॉड्यूल CSS के लचीलेपन को काफी बढ़ाते हैं। प्रत्येक मॉड्यूल को समझकर और उनका उचित उपयोग करके, आप अधिक कुशल और रखरखाव योग्य स्टाइलशीट्स बना सकते हैं।
आप हमारे YouTube चैनल पर Visual Studio Code का उपयोग करके ऊपर दिए गए लेख के साथ आगे बढ़ सकते हैं। कृपया YouTube चैनल को भी देखें।