liveBook · Manning

CSS

As explained in Web Components in Action

CSS is the stylesheet language used to describe the presentation of structured web documents.

CSS describes presentation separately from component logic, letting you style custom elements and their inner children instead of assigning style properties with JavaScript. section 2.5

Start with a minimal HTML and CSS version when layout matters. Previewing and tweaking the design directly makes iteration easier before adding Web Component logic. section 6.2

The examples use CSS to:

  • Target a custom slider, set its dimensions and position, and create patterned backgrounds, gradients, and shadows. section 2.5
  • Style a business card with typography, colors, dimensions, background images, logos, alignment, and shadows. section 6.2
  • Keep visual design separate from the Web Component class while values such as names, titles, and contact details remain customizable. section 6.2

A styled custom slider

A <style> block targets wcia-slider and its .bg-overlay and .thumb children. CSS replaces JavaScript style assignments and produces a patterned, colored slider with a positioned thumb. section 2.5

Listing 2.4. Trading inline styles for CSS
<html>
<head>
    <title>Slider</title>

    <script>
        class Slider extends HTMLElement {
            connectedCallback() {
                this.innerHTML =
                  '<div class="bg-overlay"></div><div class="thumb"></div>';
            }
        }

        if (!customElements.get('wcia-slider')) {
            customElements.define('wcia-slider', Slider);
        }

    </script>

    <style>                              #1
        wcia-slider {
            display: inline-block;
            position: relative;
            border-radius: 3px;
            height: 50px;
            width: 500px;
            background-image: linear-gradient(45deg, #ccc 25%,
              transparent 25%),linear-gradient(-45deg, #ccc 25%,
              transparent 25%),linear-gradient(45deg, transparent 75%,
              #ccc 75%),linear-gradient(-45deg, transparent 75%, #ccc 75%);
            background-size: 16px 16px;
            background-position: 0 0, 0 8px, 8px -8px, -8px 0px;
        }

        .bg-overlay {
            width: 100%;
            height: 100%;
            position: absolute;
            border-radius: 3px;
            background: linear-gradient(to right, #ff0000 0%, #ff000000 100%);
        }

        .thumb {
            margin-top: -1px;
            left: 250px;
            width: 5px;
            height: calc(100% - 5px);
            position: absolute;
            border-style: solid;
            border-width: 3px;
            border-color: white;
            border-radius: 3px;
            pointer-events: none;
            box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2),
                        0 6px 20px 0 rgba(0, 0, 0, 0.19);

        }

    </style>
</head>
<body>
    <wcia-slider></wcia-slider>
</body>
</html>

Inspect the complete slider example, including the CSS rules that replace inline style changes. section 2.5

How Web Components in Action covers it

How deep it goes
Demonstrates concrete selectors and visual properties, but defers deeper CSS details and broader organization concerns.
Examples
Styles a custom slider and a customizable business card with backgrounds, images, typography, alignment, gradients, and shadows.
What it assumes
Assumes familiarity with HTML elements and basic Web Component structure while demonstrating styling from ordinary HTML and CSS.
What you can do after
Apply CSS to build and refine custom-element interfaces while keeping visual treatment separate from component logic.
How it connects
Connects CSS with HTML, JavaScript, Web Components, DOM elements, and Shadow DOM.

Also explained in

Compare how 13 books explain CSS →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Components in Action. The section links point to the parts of the book it draws on.