Table of Contents

Class CssMediaRule

Namespace
Egami.Css
Assembly
Egami.Css.dll

An @media rule: rules that apply when the media query list matches.

public sealed class CssMediaRule : CssRule
Inheritance
CssMediaRule
Inherited Members

Examples

// Parsing never throws on bad CSS: it recovers as CSS Syntax 3 says and reports each problem.
var errors = new List<CssParseError>();
var options = new CssParseOptions { Errors = errors.Add };
CssStyleSheet sheet = CssStyleSheet.Parse("""
    @import url(print.css) print;
    @namespace svg url(http://www.w3.org/2000/svg);
    rect.note, svg|circle { fill: gold !important; stroke: #333 }
    @media (max-width: 600px) { rect { stroke-width: 2px } }
    @supports (display: grid) { g { display: grid } }
    @font-face { font-family: "Inter"; src: url(inter.woff2) format("woff2") }
    @layer base { text { fill: gray } }
    @keyframes spin { to { rotate: 1turn } }
    p:unknown { color: red }
    """, options);

foreach (CssRule rule in sheet.Rules)
{
    switch (rule)
    {
        case CssStyleRule style:
            Console.WriteLine($"{style.SelectorText}: {style.Declarations.Count} declarations");
            break;
        case CssMediaRule media:
            Console.WriteLine($"@media {media.Media.MediaText}: {media.Rules.Count} rules");
            break;
        case CssSupportsRule supports:
            Console.WriteLine($"@supports {supports.ConditionText}");
            break;
        case CssFontFaceRule fontFace:
            Console.WriteLine($"@font-face {fontFace.FontFace.Family}");
            break;
        case CssImportRule import:
            Console.WriteLine($"@import {import.Url} for {import.Media.MediaText}");
            break;
        case CssNamespaceRule ns:
            Console.WriteLine($"@namespace {ns.Prefix} = {ns.Uri}");
            break;
        case CssLayerRule layer:
            Console.WriteLine($"@layer {string.Join(", ", layer.Names)}");
            break;
        case CssUnknownAtRule other:
            Console.WriteLine($"@{other.Name} (kept, not interpreted)");
            break;
    }
}

// The rule with ':unknown' was dropped and reported.
CssParseError dropped = errors[0];
Console.WriteLine($"{dropped.Line}:{dropped.Column} {dropped.Message}");

Properties

Media

Gets the media query list.

public CssMediaQueryList Media { get; }

Property Value

CssMediaQueryList

Rules

Gets the rules inside.

public IReadOnlyList<CssRule> Rules { get; }

Property Value

IReadOnlyList<CssRule>