Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does nested CSS have lower specificity? #3961

Closed
davep opened this issue Jan 5, 2024 · 1 comment · Fixed by #3963
Closed

Does nested CSS have lower specificity? #3961

davep opened this issue Jan 5, 2024 · 1 comment · Fixed by #3963
Labels
bug Something isn't working question Further information is requested Task

Comments

@davep
Copy link
Contributor

davep commented Jan 5, 2024

Consider this code:

from textual.app import App, ComposeResult
from textual.widgets import Static

class BaseTester(Static, can_focus=True):
    DEFAULT_CSS = """
    BaseTester:focus {
        background: yellow;
        border: thick magenta;
    }
    """

class NonNestedCSS(BaseTester):
    DEFAULT_CSS = """
    NonNestedCSS {
        width: 1fr;
        height: 1fr;
        background: red 10%;
        border: blank;
    }

    NonNestedCSS:focus {
        background: red 20%;
        border: round red;
    }
    """

class NestedCSS(BaseTester):
    DEFAULT_CSS = """
    NestedCSS {
        width: 1fr;
        height: 1fr;
        background: green 10%;
        border: blank;

        &:focus {
            background: green 20%;
            border: round green;
        }
    }
    """

class NestedPseudoClassesApp(App[None]):

    CSS = """
    Screen {
        layout: horizontal;
    }
    """

    def compose(self) -> ComposeResult:
        yield NonNestedCSS("This isn't using nested CSS")
        yield NestedCSS("This is using nested CSS")

if __name__ == "__main__":
    NestedPseudoClassesApp().run()

There is a base class with a :focus pseudo class that sets a garish border/background combination. There are then two subclassed widgets. One (NonNestedCSS) overrides the garish :focus with something nicer using a non-nested CSS approach. There is another (NestedCSS) which overrides the garish :focus using nested CSS. When the above code is run with 0.47.0 you'll see that NonNestedCSS overrides the :focus rules just fine; on the other hand the :focus rules for NestedCSS seem to be ignored.

It feels like there should be no difference here. Initially flagging this up as something to clarify, but perhaps also a bug.

Tagging @willmcgugan for triage as it relates to a new feature.

@davep davep added bug Something isn't working question Further information is requested Task labels Jan 5, 2024
davep added a commit to davep/tinboard that referenced this issue Jan 5, 2024
In part this was intended to switch things up to using nested CSS where
possible, to see how well it works in the field. It did throw up one
possible bug:

   Textualize/textual#3961

and I also can't get the token input dialog to use nested CSS without things
going crazy wrong; I need another run at that to try and isolate what's
going on and perhaps get it down to a MRE if it's a genuine bug.
Copy link

github-actions bot commented Jan 5, 2024

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested Task
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant