Wednesday, May 31, 2023
HomeSoftware DevelopmentMethods to load css assets conditionally ?

Methods to load css assets conditionally ?


On this planet of net improvement, CSS performs a vital position in styling HTML code. Usually, the CSS code is linked within the head tag of an HTML doc, permitting builders to simply apply kinds to the content material of an online web page. Nevertheless, there could also be occasions when the specified styling relies on particular situations, corresponding to the dimensions of the viewport or the kind of gadget getting used to entry the web site.

In these conditions, it turns into necessary to have the power to conditionally alter CSS kinds primarily based on these situations. That is the place the ability of CSS conditional styling comes into play. On this article, we’ll delve into numerous approaches to conditionally altering CSS kinds, supplying you with the instruments and information wanted to take your net improvement abilities to the subsequent stage.

Listed below are some approaches to utilizing CSS conditionally:

  1.  Utilizing Media Queries.
  2.  Utilizing Media Attribute in “model” or “hyperlink” tags.
  3.  Loading Printer-specific CSS.
  4.  Loading CSS primarily based on Darkish/Mild Mode.

Method 1: Utilizing Media Queries:

Media queries are a characteristic of CSS (Cascading Type Sheets) that allow you to specify completely different kinds for a webpage primarily based on the size of the viewport or the gadget getting used. That is helpful for creating responsive designs, that are web sites that may adapt to completely different display sizes and gadget sorts.

In a media question, the media sort specifies the kind of media that the kinds must be utilized to, and the expressions test for the size of the viewport or the gadget getting used. If the media sort and expressions match the traits of the gadget, the kinds specified within the media question might be utilized.

You possibly can seek advice from Media Question to be taught extra about it.

Instance:

HTML

<!DOCTYPE html>

<html>

  

<head>

    <title>Load CSS conditionally</title>

    <model>

        physique {

            background-color: black;

            coloration: white;

        }

  

        @media display and (max-width: 900px) {

            physique {

                background-color: lightgrey;

                coloration: black;

            }

        }

  

        h2 {

            coloration: inexperienced;

        }

    </model>

</head>

  

<physique>

    <h2>Welcome To GFG</h2>

    <p>Now, we're going to be taught some 

          stuff concerning CSS.</p>

</physique>

  

</html>

Output:

Utilizing Media Question for Conditional CSS.

Method 2: Utilizing the “media” attribute in “model” or “hyperlink” Components: 

Use the “media” attribute of the “hyperlink” component to specify {that a} stylesheet ought to solely be utilized when sure media sorts or media options are supported. You possibly can set the “media” attribute of the “hyperlink” component to a media question.

A media question is a CSS3 characteristic that lets you specify situations for when a stylesheet must be utilized. The “media” attribute of the “hyperlink” component takes a media question as its worth. When the situations of the media question are met, the kinds within the stylesheet might be utilized to the web page. When the situations should not met, the kinds is not going to be utilized.

Syntax:

<model media="display and (max-width: 800px)"></model>

Instance:

HTML

<!DOCTYPE html>

<html>

  

<head>

    <title>Load CSS conditionally</title>

    <model media="display and (max-width: 800px)">

        physique {

            background-color: black;

            coloration: white;

        }

  

        h2 {

            coloration: inexperienced;

        }

    </model>

</head>

  

<physique>

    <h2>Welcome To GFG</h2>

    <p>Now, we're going to be taught some stuff concerning CSS.</p>

</physique>

  

</html>

Output:

Utilizing the “Media” Attribute to load CSS conditionally

You should use quite a lot of media sorts and media options in your media queries to specify the situations beneath which a stylesheet must be utilized. For instance, you should use the print media sort to use kinds solely when the web page is printed, or you should use the orientation media characteristic to use kinds solely when the display is in panorama orientation.

Method 3: Loading Printer-specific CSS:

On the subject of net improvement, you will need to have the power to regulate kinds primarily based on the output gadget getting used to view the web site. That is the place media queries in CSS come into play. Through the use of media queries, you’ve got the ability to customise kinds particularly for printing, by concentrating on the print output gadget attribute.

With media queries, you possibly can apply kinds to your web site which are particular to the printing, making certain that the content material is offered in the absolute best method when printed. This can be a highly effective device in your net improvement arsenal that may assist you create a extra streamlined and optimized person expertise, each on display and in print.

@media print {
    /* kinds particular to printing go right here */
}

Media queries make sure that the kinds inside them are solely utilized throughout printing. To forestall undesirable kinds from the display model from carrying over, it’s beneficial to reset them in your important CSS.

Instance:

HTML

<!DOCTYPE html>

<html>

  

<head>

    <title>Load CSS conditionally</title>

    <model>

        @media print {

            physique {

                coloration: black;

            }

  

            h2 {

                coloration: inexperienced;

            }

        }

    </model>

</head>

  

<physique>

    <h2>Welcome To GFG</h2>

    <p>Now, we're going to be taught some 

          stuff concerning CSS.</p>

</physique>

  

</html>

Output:

Utilizing “Media Print” to load CSS conditionally 

Rationalization: On this instance, the primary CSS units the font dimension and coloration for the physique, whereas the printer-specific CSS resets these kinds for the printed model of the web page.

Method 4: Loading CSS primarily based on Darkish/Mild Mode:

One solution to dynamically swap between darkish and lightweight mode kinds in CSS is by utilizing CSS Variables and JavaScript. The method includes setting default values in your kinds utilizing CSS Variables, then utilizing JavaScript to toggle between completely different units of kinds primarily based on person choice or gadget settings.

Instance: Right here’s a primary instance of how this may be carried out:

HTML

<!DOCTYPE html>

<html lang="en">

  

<head>

    <meta charset="UTF-8" />

    <meta http-equiv="X-UA-Appropriate" content material="IE=edge" />

    <meta title="viewport" 

          content material="width=device-width, initial-scale=1.0" />

    <title>Conditional Styling</title>

    <model>

        :root {

            --bg-color: #fff;

            --text-color: #000;

        }

  

        .container {

            background-color: var(--bg-color);

            coloration: var(--text-color);

            padding: 20px;

        }

  

        [data-mode="dark"] {

            --bg-color: #000;

            --text-color: #fff;

        }

    </model>

</head>

  

<physique>

    <div class="container">

        

        <p>Welcome to GFG</p>

    </div>

    <button id="toggle-mode">Toggle Mode</button>

    <script>

        const toggleBtn = doc.getElementById("toggle-mode");

        const container = doc.querySelector(".container");

  

        toggleBtn.addEventListener("click on", () => {

            container.dataset.mode =

                container.dataset.mode === "darkish" ? "gentle" : "darkish";

        });

    </script>

</physique>

  

</html>

Output:

Loading CSS Primarily based on Darkish/Mild Mode.

Rationalization: On this instance, the CSS units default values for the background and textual content colours utilizing CSS Variables. The JavaScript then listens for a click on occasion on the toggle button and switches the data-mode attribute on the container between darkish and lightweight. The CSS then makes use of the [data-mode=”dark”] selector to regulate the values of the CSS Variables primarily based on the data-mode attribute.

Through the use of this method, you possibly can create two units of kinds, one for darkish mode and one for gentle mode, and swap between them dynamically, offering a greater and extra customized person expertise.”

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments