Slashdot is powered by your submissions, so send in your scoop

 



Forgot your password?
typodupeerror
×
The Internet

W3C Publishes First Public Working Draft of CSS Nesting Module (w3.org) 60

koavf shares a blog post from the World Wide Web Consortium (W3C): The CSS Working Group has published a First Public Working Draft of CSS Nesting Module. This module introduces the ability to nest one style rule inside another, with the selector of the child rule relative to the selector of the parent rule. This increases the modularity and maintainability of CSS stylesheets.
This discussion has been archived. No new comments can be posted.

W3C Publishes First Public Working Draft of CSS Nesting Module

Comments Filter:
  • by pahles ( 701275 ) on Wednesday September 01, 2021 @05:16AM (#61751359)
    Isn't this why we have Sass/less?
    • Exactly. Only the W3C seems to want to invent their own wheel, and invent it differently.
    • Re:Sass/less (Score:5, Informative)

      by Junta ( 36770 ) on Wednesday September 01, 2021 @07:22AM (#61751575)

      Sure, there are workarounds that let you maintain a distinct 'source' and 'compile' css.

      Of course, if you don't think a website style should require a 'build' tool, then it's easy enough to see why they might want to extend CSS to cover such use cases without inflicting 'build' tools on web development. Particularly with the web developer console, technologies that can run 'live' without some build step are easier to experiment in the browser, and easier for the browser to help annotate the derivation from style rules as written by human to the appearance of an element.

      Other than WASM, nothing in the browser runtime was really *supposed* to be post-build assets of things like TypeScript, Svelte, Sass, etc.

    • Comment removed (Score:4, Interesting)

      by account_deleted ( 4530225 ) on Wednesday September 01, 2021 @07:42AM (#61751647)
      Comment removed based on user account deletion
      • Now if they can add some macros and basic arithmetic

        Let's go all the way and have JavaScript directly in the CSS. You know we will end up there sooner or later.

    • And we'll continue to have it until we know this is actually implemented correctly in most web browsers.

      But it's possible that people would move over to this once that happens, to reduce bandwidth

      Of course, looking source HTML & CSS these days, most people stopped caring a long time ago, and just rely on the server to compress the file in transit, like apache's mod_deflate.

    • 10 years ago people said the same about HTML5 - isn't this why we have Flash?

      Aren't we glad that the browser doesn't require hacky workarounds anymore?

  • Fuck. Another Firefox upgrade
  • by DrXym ( 126579 ) on Wednesday September 01, 2021 @05:27AM (#61751369)
    Nested styles are obviously a really nice feature to reduce repetition in CSS as well as make intent clearer to people reading it.

    But dear god the syntax they used to achieve it is horrible. WTF is that ampersand about? That just makes a mess of the readability and is a surefire way to introduce errors into the rules. I bet they only did it because parsers already had some kind of selector logic so they half assed this in to work the same way to simplify extending existing parsers.

    • Re:Ugh (Score:5, Insightful)

      by UPi ( 137083 ) on Wednesday September 01, 2021 @05:41AM (#61751393) Homepage

      I think the ampersand is there so that you can write a context-free parser for CSS. Otherwise a parser would need a potentially "infinite lookahead" to determine if an expression inside a rule block is a the beginning of a rule or a child selector. Personally I've seen syntax far worse than this in other languages (I'm looking at you, Apple, you mutant alien enemy, for calling interfaces "protocols" and unions "enums").

      Overall nested rules make CSS a lot easier to read and write, and in turn, eliminate some of the need for preprocessors. Because we all know how much fun those are.

    • Yea that's what it looks like to me exactly; just something like "How can we make the JavaScript parser handle this too?"

    • by pjt33 ( 739471 )

      It's borrowed from earlier CSS-generating projects like SASS [sass-lang.com] and Less [lesscss.org].

      • Stylus too - that ampersand is the syntax that pretty much everyone has already settled on, so it makes sense that they'd adopt the existing convention.

        However, I think DrXym issue was not so much "why did they pick ampersand as the symbol?" and more "why not let us omit the ampersand when it can be inferred, as most style preprocessors already allow?".

        The proposed feature is very nice as it is and, while being able to omit the ampersand would most certainly be even better, it comes with a rather heavy time

    • Re:Ugh (Score:5, Informative)

      by augo ( 6575028 ) on Wednesday September 01, 2021 @06:34AM (#61751485)

      They have a comment about the apersand:

      Why can’t everything be directly nested?

      Nesting style rules naively inside of other style rules is, unfortunately, ambiguous—the syntax of a selector overlaps with the syntax of a declaration, so an implementation requires unbounded lookahead to tell whether a given bit of text is a declaration or the start of a style rule.

      For example, if a parser starts by seeing color:hover ..., it can’t tell whether that’s the color property (being set to an invalid value...) or a selector for a element. It can’t even rely on looking for valid properties to tell the difference; this would cause parsing to depend on which properties the implementation supported, and could change over time.

      Requiring directly-nested style rules to use nest-prefixed selectors works around this problem—an & can never be part of a declaration, so the parser can immediately tell it’s going to be parsing a selector, and thus a nested style rule.

      Some non-browser implementations of nested rules do not impose this requirement. It is, in most cases, eventually possible to tell properties and selectors apart, but doing so requires unbounded lookahead in the parser; that is, the parser might have to hold onto an unknown amount of content before it can tell which way it’s supposed to be interpreting it. CSS to date requires only a small, known amount of lookahead in its parsing, which allows for more efficient parsing algorithms, so unbounded lookahead is generally considered unacceptable among browser implementations of CSS.

      • by DrXym ( 126579 )
        Kind of my point. They fudged it to make existing parsers happier.
        • Re: (Score:3, Insightful)

          by augo ( 6575028 )

          As I understand it is not about "existing parsers", but the parsers that are required to be fast - i.e. browser parsers that work in real time as opposed to CSS generators like SASS or Less which are usually used offline and for which couple seconds slower or faster does not have a big difference.

        • It's not about existing parsers. In order for nesting to be useful, it has to be possible to write an efficient parser for it. Without the ampersand any parser would have to have infinite lookahead. But also see my other comment for another reason why you have to have something there.
          • by DrXym ( 126579 )
            I don't believe that to be the case. They could have used a delimiter inside the block to say anything below this point is a nested rule, anything before the delimited is a property. It wouldn't need any kind of forward lookup.
        • No, they set a rule-based limitation in order to permit parsers to continue to be fast, and not to need loads more memory. This affects future parsers, not present ones (which can't actually parse the ampersand)

    • The ampersand is a placeholder for the selector prefix. It is necessary to disambiguate between for example "selector.class" and "selector .class". For example, "div { color:green; &.test { color:red } }" colors all divs green, except divs with class test, which it colors red, even if they're not nested inside another div. "div { color:green; & .test { color:red } }" on the other hand colors all divs green, except divs and other elements with class test which are nested inside a div, which it colors
      • Aside from the ampersand, there is also the @nest. From the CSS draft:

        .foo {
        color: red;
        @nest .parent & {
        color: blue;
        }
        }

        .foo {
        color: red;
        @nest :not(&) {
        color: blue;
        }
        }

        @nest is introduced such that those nesting selector expression won't be mistaken as css property name when ampersand is not at the beginning.

    • WTF is that ampersand about?

      Assuming this is the same as SASS, it's kind of like the "this" in OOP, or the old $_ in Perl. It's self-referential, a placeholder for itself.

      (You can't retype div in place of the ampersand because that changes the nesting to "div div + p", "div div:not(#someID)", etc.)

      In SASS, if you have:

      div {
      color: red;
      & + p { // Same as " + p " - the ampersand is for clarity
      color: blue;
      }
      &:not(#someID) {
      color: green;
      }
      }

      That outputs this CSS:

      div { color

  • This increases the modularity and maintainability of CSS stylesheets.

    lol.

  • oh wow nice feature, what year is this? 1999?
  • by jellomizer ( 103300 ) on Wednesday September 01, 2021 @08:00AM (#61751711)

    I have been using CSS for nearly 20 years now, and what always bugs me, is how hard it is to deal with vertical styling.
    Having an object use vertical-align, height. I often want things to be in the exact middle of stuff, things that I don't know their exact size, I would think
    One would think in a control you could use something like
    div.middleme { align: center; vertical-align; middle; height: 100%; }
    would be a universal solution to have a div fill the screen (or the object that it is in) and have the content be right in the middle of it.
    But noo. You need to either use precise measurement, or give heights to all its parents first and you might just get lucky.
     

    • by omnichad ( 1198475 ) on Wednesday September 01, 2021 @08:44AM (#61751903) Homepage

      I can't seem to wrap my head around it at the moment well enough to explain, but it has to do with the fact that it isn't a static markup language. It re-flows based on the size of the device. A long as you're not hard-coding pixel values and doing it the proper way, you're fixed to consider all the possibilities.

      If you 3 items in a container, the height of the bounding box might be the height of any one item. Or, it wraps to two rows and the height is double. You may say you've set the width of each to 33% but the parser can't make a quick judgment on how that will actually render, so it would kill performance to have it try to analyze things that way.

      Anything that requires vertical alignment like you want probably requires some degree of rethinking.

      • I understand why it is hard technically to do this, however in terms of the commands giving the output you expect should be much more simple.

        • The technical hardships probably are the reason vertical-align doesn't behave as we expect, and now that is probably possible to solve them practically, retro compatibility means vertical-align will never be "fixed".

          But the vertcal align problem has been solved for a while now, with flex and align-items (or justify-content if you have flex-direction: column).

    • by Merk42 ( 1906718 )
      Are you sure flexbox doesn't address this?
      div.centercontent{ display:flex; align-items:center; justify-content:center; }

      Or are you frustrated you have put display:flex on the parent?
      • I never said they are not was to do it. But the way CSS is natively handled. the obvious set of commands isn't used, but having to a set of additional command, or other tricks.

        • by Merk42 ( 1906718 )
          Gotcha, so while it's entirely possible to do what you want, you just don't like it's not the syntax you want.
      • And if flexbox can't do it, surely grid can. These two should cover at least 95% of cases.

    • by Junta ( 36770 )

      You may know more than I do about why this is bad, but generally I do:
              top: 50%;
              transform: translateY(-50%);

      And that gives me vertical alignment. I'm not really that much into it so there's probably something broken about it, but so far it's been ok for me.

  • I can't wait to use that new feature in 2041 when the majority of people will have updated their browsers!

    • by SirSlud ( 67381 )

      huh? most browsers just auto update, even more so the mobile versions

      • Old joke from the no-auto-updating browsers era, when the W3C would announce something and it would take years before browsers added those features and even more years before the majority of people updated their browsers.

        So even with auto-updating you'll need to wait until all browsers add these new features and also wait until they all work correctly.

        The only thing that saves us work today is the fact that Internet Explorer doesn't exists anymore, and Safari, Chrome and Edge are basically the same engine f

        • Safari, Chrome and Edge are basically the same engine for the most part (yes I know, Webkit is not Blink - but a huge chunk of Blink is Webkit).

          Yet this huge chunk of Blink is far, far more up to date than upstream WebKit. For instance, Blink has had Push API for years and upstream still doesn't.

      • huh? most browsers just auto update, even more so the mobile versions

        Most, but not all. Especially the ones I use. Auto-updating is one of the first things I turn off not only in a browser, but every piece of software. The last thing I want is for an update to butcher things and either render the software unusable, or remove items which were useful.

        I tell the same thing to everyone that listens. Disable auto-update everywhere and you will find your experience that much better.

        Also, there ar
        • by SirSlud ( 67381 )

          yes, most, that's what I said

          the rest of your post just sounds like you need somebody validate how good at computers you imagine you are, it'd be hilarious if it wasn't so sad and misguided

      • Web browsers nowadays automatically update to a newer version of the same engine. If no new version of the same engine is available, web browsers don't switch to a different engine that has the features that a particular developer used. For example, updating a web browser on iOS won't add support for the numerous web platform features missing for years in WebKit for iOS [infrequently.org].

You know you've landed gear-up when it takes full power to taxi.

Working...