Csswg-drafts: css var & font-size bug

Created on 26 Dec 2017  路  5Comments  路  Source: w3c/csswg-drafts

css var & font-size bug

image


label css var & font-size bug

online demo

what's wrong with?

  1. the h1 is ok, font-size: var(--fs)*2;;
  2. the span is not work, font-size: var(--fs)*2;.
<!DOCTYPE html>
<html lang="zh-Hans">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>webpack3</title>
    <link rel="stylesheet" href="./index.css">
</head>

<body>
    <section>
        <header>
            <h1>hello, wp3!</h1>
            <span data-fs="font-size">this is a big font span!</span>
        </header>
    </section>
</body>

</html>

@charset "UTF-8";

/* var & css */

:root {
    --fontSize: 12px;
    --font-family: "Microsoft YaHei", sans-serif;
    --fs: 16px;
    --red: #f00;
    --green: #0f0;
}

h1 {
    /* font-size: var(--fontSize)*2; */
    font-size: var(--fs)*2;
    color: var(--red, red);
}

span[data-fs="font-size"] {
    /* font-size: var(--fs)*2; */
    font-size: var(--fontSize)*2;
    /* font-size: 23px; */
    color: var(--green);
}

Most helpful comment

CSS does not accept multiplications everywhere. Just wrap them inside calc().

All 5 comments

CSS does not accept multiplications everywhere. Just wrap them inside calc().

var(--fontSize)*2 is expanded to 12px*2 in your example, which is invalid. As @Loirooriol mentioned, if you want to calculate, you need to wrap the formula inside calc().

Just wrap them inside calc().

@Loirooriol @upsuper Thanks for your time!

Was this page helpful?
0 / 5 - 0 ratings