-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
UI Element Attribute #10146
Comments
FYI there's a related proposal by @LeaVerou in w3c/csswg-drafts#10001, although I'm not certain it'd solve all of your use cases. |
First, I love the thought and care that has been put into writing this proposal, props for that! However, I'm missing a solid value proposition for either authors, spec editors, or implementors that would make the effort to add or use this worth it. Authors have had Perhaps something like this could work if it also comes with conveniences to target these in selectors, or get their values in CSS. But still, why invent a new thing instead of paving the cowpaths and facilitating this for class names? FWIW, there are also proposals to allow any hyphen-containing attribute, which would trivially allow |
@Malvoz and @LeaVerou thank you for your feedback. I have read through the linked proposals / comments. There are clearly still a lot of wrinkles in the proposal to iron out. I do like the idea that any hyphen-containing attribute would be allowed, which immediately solves this proposal. It also aligns well with hyphenated, custom HTML elements. During my experimentations with this idea, one of the value props I found from a developer perspective, was the potential for IDEs to auto-complete The following excerpt comes from Bootstrap:
Now, how does this translate to [ui-padding*="0"] {
padding: 0;
}
[ui-padding*="1"] {
padding: 10px;
}
[ui-padding*="2"] {
padding: 20px;
}
[ui-padding*="bottom:0"] {
padding-bottom: 0;
}
[ui-padding*="bottom:1"] {
padding-bottom: 10px;
}
[ui-padding*="bottom:2"] {
padding-bottom: 20px;
}
[ui-padding*="top:0"] {
padding-top: 0;
}
[ui-padding*="top:1"] {
padding-top: 10px;
}
[ui-padding*="top:2"] {
padding-top: 20px;
}
/* utils */
[ui-bgcolor="red"] {
background-color: red;
}
[ui-bgcolor="blue"] {
background-color: blue;
} We can then style our document like so: <div ui-bgcolor="red"
ui-padding="2">
padding: all=20px
</div>
<div ui-bgcolor="blue"
ui-padding="top:0 bottom:2">
padding: top=10px, bottom=20px
</div>
<div ui-bgcolor="red"
ui-padding="2 bottom:1">
padding: all=20px, bottom=10px
</div> JSFiddle here if you want to play with it At this point, you may be wondering "okay, so where does IDE auto-completion fit in?" Currently, if you type in The advantage of Now, whilst writing this I've become very aware that the WHATWG doesn't/shouldn't care about how IDE vendors implement auto-completion...in all honesty, the argument does feel a bit "tail wagging the dog". Perhaps a better way of looking at this particular value prop is to go back to "everything and the kitchen sink thrown into one huge list". In contrast One final criticism before I sign out; I mentioned above "I prefer clarity over brevity, even at the expense of a few bytes here and there"... This is a lot longer... <div ui-padding="top:2 bottom:1" ui-margin="0 start:4"> ...than this... <div class="pt-2 pb-1 m-0 ms-4"> In this case, are front-end engineers going to equate clarity with verbosity, leading to markup that is bloated with In summary, I think that allowing all hyphenated attributes is probably the best approach and clearly has many use-cases, some of which are in existence already. I could envisage a future where design systems are partitioned by their prefix; i.e. rather than |
(random passerby comment) This feels like a self-inflicted problem of trying to bring CSS back into HTML markup. In any case, some of those things could be fixed with full support of attr(): [data-ui-bgcolor] {
background-color: attr(data-ui-bgcolor color, var(--ui-bgcolor));
} // Avoid doing that, seriously… As for things like “ui-padding="top:0 bottom:2"”, at this point it's time to just use style="…", IMO. |
@flashymittens Yes, the more I think about it, this approach for utility classes probably is a worse idea than using classes. Even for more complex scenarios, it's not great. I think it has some use-cases, but probably not enough to warrant the "make |
The purpose of this issue is to formally propose the introduction of a new global element attribute named
ui-*
. This attribute is designed to offer an alternative or potentially complementary approach to utilising theclass
attribute for the styling of HTML elements.Problem Statement
There are many design systems in existence that are implemented in (but not limited to) CSS; for example, Bootstrap, Semantic UI, Radix UI, Material Design, Fluent Design, UIkit, Tailwind, etc.
Most, if not all of these design systems provide front-end developers with a comprehensive set of CSS classes that implement the design system.
The following example illustrates the use of Bootstrap’s CSS classes to create a card, with a header, and a body containing some content:
Whilst this example is fairly trivial, it highlights the following:
class
attribute.card-header
could be replaced withheader
, andcard-body
could be replaced withsection
, and styled accordingly. I suspect that this would break due to the nature of cascading styles; i.e. if the card body itself contained headers and sections, and it would also depend on whether the semantics made sense in this context, so perhaps this is a moot point?btn
provides the base style, andbtn-primary
is effectively a modifier of the base style. These modifiers could be considered as an entry in a finite set enumeration.The following example illustrates the use of Tailwind’s CSS classes:
This example is less trivial and illustrates the following:
class
attribute.figure
,blockquote
, andfigcaption
.w-24 h-24 md:w-48 md:h-auto md:rounded-none rounded-full mx-auto
Whilst the use of CSS classes is (at least currently) the de-facto approach for implementing web-based design systems, they do suffer some drawbacks.
btn
) and their modifiers (i.e.btn-primary
).History of Styling Attributes
Before delving into the proposal for a
ui-*
attribute, it is pertinent, for the sake of completeness, to grasp the historical context of styling attributes and the reasons they fell out of favour. Historically, several attributes were utilised, most of which have since been deprecated in the context of modern web development.Reference: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
align
Specifies the horizontal alignment of the element.
Applies to
caption>
,<col>
,<colgroup>
,<hr>
,<iframe>
,<img>
,<table>
,<tbody>
,<td>
,<tfoot>
,<th>
,<thead>
,<tr>
background
Specifies the URL of an image file.
Applies to
<body>
,<table>
,<td>
,<th>
bgcolor
Specifies the background color of the element.
Applies to
<body>
,<col>
,<colgroup>
,<marquee>
,<table>
,<tbody>
,<tfoot>
,<td>
,<th>
,<tr>
border
Specifies the border width.
Applies to
<img>
,<object>
,<table>
color
Specifies the text color using either a named color, or a color specified in the hexadecimal #RRGGBB format.
Applies to
<font>
,<hr>
height
Specifies the height of elements.
Applies to
<canvas>
,<embed>
,<iframe>
,<img>
,<input>
,<object>
,<video>
width
Specifies the width of elements.
Applies to
<canvas>
,<embed>
,<iframe>
,<img>
,<input>
,<object>
,<video>
While I might have overlooked some details, a review of the list suggests that the use of these attributes for styling is generally considered undesirable. This is primarily because they are applicable to only a limited set of elements, leading inevitably to either a hybrid styling approach—where CSS is also employed—or the complete abandonment of these attributes in favor of CSS, which offers a more comprehensive and consistent method for styling.
Proposal
Given that the past has demonstrated styling attributes to be undesirable, it may seem an odd proposal to bring them back, however there is a stark difference in how they would work.
Historically, attributes such as
align
,background
,bgcolor
,border
,color
,height
, andwidth
would have been reserved by the W3C and/or WHATWG, and would need to be implemented by browser vendors in order to work correctly.This is where
ui-*
is different. Essentially, it would work the same way thatdata-*
attributes work today, in thatui-*
would essentially just be a reserved attribute which can be used as a CSS selector.In fact, this can already be done with
data-*
attributes, but I feel like that muddies the intent ofdata-*
attributes.Why is this better than using classes?
At this point, some examples may be useful.
First we’ll start with the original Bootstrap card example:
We could modify this to adopt
ui-*
attributes like so:In this case, there has been a mild reduction in the source size (which, even with our blazing-fast broadband connections, is still beneficial). Most notably is the change from
btn btn-primary
toui-button="primary"
. In my opinion, it looks much cleaner.We could go further still by introducing semantic elements:
In this case,
ui-card-header
andui-card-body
were replaced withheader
andsection
elements respectively. Whilst this may not always be desirable, it highlights thatui-*
attributes in this context appear to augment existing elements.Another example I’ve been thinking about is utilising flexbox styles to implement a “stack panel”:
The key here is that rather than bundling display, orientation and alignment classes into a single class list, we can separate them out into distinct, meaningful attributes with well defined values that can be selected accordingly in CSS.
Conclusion
We have identified that the current trend when designing CSS based design systems tends towards classes as the de-facto approach, and that historically, using attributes to style HTML elements has been undesirable, due to their limitations.
In contrast, the
ui-*
attribute aligns well with modern web design standards and trends. It allows custom attributes to be targeted by CSS in a familiar way, whilst providing clearer semantics and augmentation over HTML elements.Most importantly, we can already do this today! - there is nothing technical standing in the way of using
ui-*
attributes, albeit they would currently yield invalid HTML.Therefore, the proposal is really for nothing more than the reservation of
ui-*
as an attribute, intended for extensible, CSS compliant styling.I'm keen to hear your thoughts. Thank you.
The text was updated successfully, but these errors were encountered: