Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Practical HTML and CSS

You're reading from   Practical HTML and CSS Elevate your internet presence by creating modern and high-performance websites for the web

Arrow left icon
Product type Paperback
Published in Nov 2024
Publisher Packt
ISBN-13 9781835080917
Length 492 pages
Edition 2nd Edition
Languages
Arrow right icon
Authors (3):
Arrow left icon
Brett Jephson Brett Jephson
Author Profile Icon Brett Jephson
Brett Jephson
Lewis Coulson Lewis Coulson
Author Profile Icon Lewis Coulson
Lewis Coulson
Ana Carolina Silveira Ana Carolina Silveira
Author Profile Icon Ana Carolina Silveira
Ana Carolina Silveira
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Part 1: Introducing HTML and CSS
2. Chapter 1: Introduction to HTML and CSS FREE CHAPTER 3. Chapter 2: Structure and Layout 4. Chapter 3: Text and Typography Styling 5. Part 2: Understanding Website Fundamentals
6. Chapter 4: Creating and Styling Forms 7. Chapter 5: Adding Animation to Web Pages 8. Chapter 6: Themes, Color, and Polishing Techniques 9. Part 3: Building for All
10. Chapter 7: Using CSS and HTML to Boost Performance 11. Chapter 8: Responsive Web Design and Media Queries 12. Chapter 9: Ensuring Accessibility in HTML and CSS 13. Part 4: Advanced Concepts
14. Chapter 10: SEO Essentials for Web Developers 15. Chapter 11: Preprocessors and Tooling for Efficient Development 16. Chapter 12: Strategies for Maintaining CSS Code 17. Chapter 13: The Future of HTML and CSS – Advancements and Trends 18. Index 19. Other Books You May Enjoy

Introducing the light-dark() function

A groundbreaking addition to CSS, the light-dark() function simplifies the process of establishing dynamic themes. This function enables the setting of foreground and background colors for various color schemes in just one line of code, eliminating the need for the prefers-color-scheme media queries.

Here is the syntax for this:

element {
  property: light-dark(
    [color for light or default],
    [color for dark]
  );
}

The light-dark() CSS color function returns the first value if the user’s preference is set to light or if no preference is set, and the second value if the user’s preference is set to dark:

:root {
  color-scheme: light dark;
}
.element {
  color: light-dark(black, white);
  background-color: light-dark(white, black);
}

Important: light-dark() is an experimental feature

Please note that this feature is experimental and may have limited availability. It’s recommended to test it in your browser environment.

Exercise 4 – simplifying our CSS theming with media queries

To celebrate our journey into the world of CSS theming, let’s practice once more with our bookstore application. We’re going to refactor the CSS theming to set it dynamically based on the user’s preferences. For testing purposes, we’ll use the Microsoft Windows operating system as an example:

  1. Start by opening the project folder in VS Code and creating a new file named color-variation.css in the same location as the other CSS files. We’ll use a single CSS file for both light and dark themes to reduce code repetition.
  2. After creating the file, let’s define that our :root element (representing the whole HTML) will accept variations of light and dark modes. We’ll set this writing the following notation in color-variation.css:
    :root {
      color-scheme: light dark;
    }
  3. After setting this up, let’s create a media query to configure the colors for light mode. We’re creating the prefers-color-scheme media query in color-"variation.css" and pasting the entire content from light.css inside it:
    @media (prefers-color-scheme: light) {
      :root {
        --primary-color: #0c457d;
        --secondary-color: #e8702a;
        --text-color: #061b1a;
        --background-color: #fcf6ed;
        --highlight-color: #6bd2db;
      }
    }
  4. Great! We’re halfway there. Now, let’s do the same with the dark.css file. Create a prefers-color-scheme media query and paste all the content from dark.css inside it. Note that we’re not using the invert() filter because, in this context, when one theme is selected, the browser does not render the other one, so invert() does not have anything to invert from:
    @media (prefers-color-scheme: dark) {
      :root {
        --primary-color: hsl(31, 84%, 26%);
        --secondary-color: hsl(200, 81%, 51%);
        --text-color: hsl(358, 100%, 100%);
        --background-color: hsl(225, 83%, 0%);
        --highlight-color: hsl(3, 66%, 67%);
      }
    }

    The resulting CSS file should look like this:

    :root {
      color-scheme: light dark;
    }
    @media (prefers-color-scheme: light) {
      :root {
        --primary-color:   #0c457d;
        --secondary-color: #e8702a;
        --text-color: #061b1a;
        --background-color: #fcf6ed;
        --highlight-color: #6bd2db;
      }
    }
    @media (prefers-color-scheme: dark) {
      :root {
        --primary-color: hsl(31, 84%, 26%);
        --secondary-color: hsl(200, 81%, 51%);
        --text-color: hsl(358, 100%, 100%);
        --background-color: hsl(225, 83%, 0%);
        --highlight-color: hsl(3, 66%, 67%);
      }
    }
  5. Now, it’s time to connect our new CSS file to our HTML. Delete the <link> elements that refer to light.css and dark.css and replace them with the new file that contains all the coloring and logic for our dynamic theme, color-variation.css:
    <head>
        <meta charset="UTF-8">
        <meta
            name="viewport"
            content="width=device-width,
                    initial-scale=1.0"
        >
        <title>Rarities - Bookstore</title>
        <link rel="stylesheet" href="style.css">
        <link rel=»stylesheet» href=»color-variation.css»>
    </head>
  6. After that, let’s configure our operating system to prefer the dark theme, so we can test it properly.

    On Windows 11, right-click with your mouse on the desktop and select Personalization | Colors. Open Colors setting and, under Choose your color, select Custom. Under Choose your default app mode, select Dark.

    All set! Open the application with your browser to see the result.

Figure 6.13 – How the application should look in the browser

Figure 6.13 – How the application should look in the browser

That’s it! We’ve created an application that changes themes dynamically with no need for JavaScript, just with CSS. Theming extends beyond mere color choices; typography is equally vital in shaping a well-designed experience. Neglecting typography can detract from an otherwise thoughtfully curated theme, highlighting the significance of this aspect, which we’ll explore in the next section.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image