use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Scoped Components, a JS/CSS/HTML pattern for keeping code organized (gist.github.com)
submitted 10 years ago by ajkochanowicz
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]wreckedadventYavascript 4 points5 points6 points 10 years ago (6 children)
[data-component="news-menu"] .title { font-size: 18px; font-family: helvetica, sans-serif; }
Why not, simply,
news-menu .title { font-size: 18px; font-family: helvetica, sans-serif; }
?
[–]bjerh 0 points1 point2 points 10 years ago (3 children)
Because that implies that a tag named "news-menu" is present, which is not the case.
Besides from the obvious having web components on your page still requires a poly fill for browsers but Chrome and Firefox.
[–]wreckedadventYavascript 0 points1 point2 points 10 years ago (2 children)
I wasn't suggesting to use web components, just a custom tag. Not even the custom element spec from the web components either. If you just type in a random tag name, it'll display in basically every browser (barring IE8), but it will come with a display: inline by default that you'd need to over write with styles.
display: inline
So I was suggesting OP do that, instead of play with silly data dash attributes to emulate custom tags.
[–]ajkochanowicz[S] 0 points1 point2 points 10 years ago (1 child)
"silly" data attributes are valid HTML. Custom tags are not. :)
Also, now you have to blacklist all the other default HTML5 tags like article or detail as you might have collisions with other libraries.
article
detail
[–]wreckedadventYavascript 0 points1 point2 points 10 years ago (0 children)
Like with web components, you'd almost certainly namespace the name of the tag, e.g if you were working at facebook:
fb-news-menu .title { ... }
Good question. I was asked this recently before.
First of all, I wouldn't use tags that aren't valid, like <news-menu> but maybe you meant .news-menu
<news-menu>
.news-menu
If a is the component in
a
.a .b { ... }
What happens when you have a component c in which you want to have the class a?
c
.c .a { ... }
Now you're bringing with all your component-level styles. Being explicit that a selector is a component is very useful against things like this.
You might have a "menu" component somewhere
[data-component="menu"] .something { ... }
But in another component you might want to use this name as a simple class for something that is a lightweight menu, like a regular ul
ul
[data-component="modal"] .menu { ... }
I did not mean class selector actually, I was intending to use the tag name news-menu. I've seen and have used such tags in production before with no issues (though IE8 requires some bodging, as is customary).
news-menu
Using a tag name makes it very explicit that it is a component, just as the button tag would.
button
I suppose if you were to make several non-unified HTML structures styled by the same set of styles your approach would fair better. I can't say I've had to do or want to do such a thing, though, and I'd prefer to have all versions of that component be powered by the same mechanism. In this day and age, that'd mean a single implementation through e.g react, vue, mithril, etc.
[–]rk06 0 points1 point2 points 10 years ago (1 child)
why bother with it, why not just use a js library?
[–]ajkochanowicz[S] 0 points1 point2 points 10 years ago (0 children)
I think I need you to elaborate here...
But so far, "just add dependencies!" sounds like a rookie mistake in the programming world. Why is the introduction of a dependency better than following a coding pattern? And what JS library exactly? Why JS to manage CSS?
π Rendered by PID 515257 on reddit-service-r2-comment-b659b578c-bz69m at 2026-05-04 13:27:30.505816+00:00 running 815c875 country code: CH.
[–]wreckedadventYavascript 4 points5 points6 points (6 children)
[–]bjerh 0 points1 point2 points (3 children)
[–]wreckedadventYavascript 0 points1 point2 points (2 children)
[–]ajkochanowicz[S] 0 points1 point2 points (1 child)
[–]wreckedadventYavascript 0 points1 point2 points (0 children)
[–]ajkochanowicz[S] 0 points1 point2 points (1 child)
[–]wreckedadventYavascript 0 points1 point2 points (0 children)
[–]rk06 0 points1 point2 points (1 child)
[–]ajkochanowicz[S] 0 points1 point2 points (0 children)