TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

CSS is powerful, you can do a lot of things without JS

493 点作者 NamPNQ将近 9 年前

52 条评论

ctvo将近 9 年前
I've worked with people who did things like this. It's not fun for anyone else on the team to try and figure out why you're using specific pseudo selectors on tags in a child inside a sibling in some label to make an accordion that would take 3 lines in Javascript.
评论 #12036651 未加载
评论 #12037779 未加载
评论 #12037895 未加载
评论 #12036797 未加载
评论 #12038708 未加载
评论 #12036745 未加载
评论 #12040005 未加载
评论 #12036313 未加载
评论 #12037702 未加载
评论 #12037912 未加载
评论 #12036709 未加载
achairapart将近 9 年前
Most of these are just clever CSS hacks. There is a website collecting these kind of experiments since the early &#x27;00: <a href="http:&#x2F;&#x2F;www.cssplay.co.uk&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.cssplay.co.uk&#x2F;</a><p>There was a time when separation of concerns and the &quot;Rule of Least Power&quot;[0] where the foundation principles of web development.<p>Today you have preprocessors, postprocessors, transpilers, content and style in JavaScript, behavior in CSS. Very powerful tools that can easily lead to unnecessary complexity.<p>On the other hand, somehow W3C failed to turn these principles into a truly interoperable and semantic web.<p>Ah! Also, nobody cares about unobtrusive JavaScript anymore.<p>[0]: <a href="https:&#x2F;&#x2F;www.w3.org&#x2F;2001&#x2F;tag&#x2F;doc&#x2F;leastPower.html" rel="nofollow">https:&#x2F;&#x2F;www.w3.org&#x2F;2001&#x2F;tag&#x2F;doc&#x2F;leastPower.html</a>
评论 #12036437 未加载
评论 #12038820 未加载
评论 #12035651 未加载
评论 #12035861 未加载
danielnixon将近 9 年前
Numerous accessibility issues here. Many of these examples can&#x27;t be operated by a keyboard, for example. &quot;CSS only&quot; is not a virtue if it comes at the cost (as it always does) of needlessly excluding people from the web.
评论 #12035383 未加载
评论 #12036677 未加载
评论 #12035384 未加载
mcv将近 9 年前
I prefer using CSS for my styling and javascript for my functionality, as God intended. These examples could be educational for getting a bit more out of your CSS while keeping only the pure functionality in javascript. Too much styling-related stuff seems to be sneaking into code these days. I like my concerns separated.
评论 #12037512 未加载
code_research将近 9 年前
I am missing one important argument in this discussion:<p>CSS only design is an important piece of a future web with reduced security and privacy threads.<p>The (interesting) model of allowing remote code execution per default was a beautiful, but naive vision. We have to make big advances in technology, politics and society to make this model work in a way that does not make internet users victims by default. We are not there yet. Reality is: the crooks destroyed that vision and are advantaged by the current situation, while all internet users are being trapped in their first moment of browser usage without their consent or knowledge.<p>For many use cases, (e.g. government websites, banking, anything where you type in your name) css-only design should become a requirement by law to protect the user until we figured out how to write secure software that respects user privacy and how to form governments that will respect their citizens (possibly will take longer). Until then browser vendors should implement more and better possibilities for CSS that help to avoid JavaScript whenever possible.<p>I very much like JS animations and stuff happening in the browser window, also there are some edge cases where JS brings some important advancements to a UI, but we have to face that privacy and security are much more important issues than having a nice UI and we have to change the current situation, as we, as programmers, are responsible for it.<p>The &quot;remote-execution-by-default&quot; experiment has failed, we need to change that, and CSS is a great way to go on with a web that might be less problematic for everyone, but still offers very nice usage experiences.
评论 #12036505 未加载
评论 #12035801 未加载
评论 #12035876 未加载
评论 #12035870 未加载
adamjc将近 9 年前
The problem with relying on anchors means it stays in the browser history, so if you press &#x27;back&#x27;, you get the modal dialog again!<p>I don&#x27;t think this is very useful, but it is a fun exercise in CSS.
评论 #12035180 未加载
评论 #12035302 未加载
评论 #12035647 未加载
评论 #12035182 未加载
nnq将近 9 年前
May the gods have mercy on the soul of who&#x27;ll maintain code using these... &quot;techniques&quot;.<p>(I know because back in the days I&#x27;ve written things like this myself :) ..probably the curses whispered by those who&#x27;ve had to endure their progenitors are finally getting at me)
zaidf将近 9 年前
History is going to look back at CSS in disbelief when it calculates the amount of engineering time was spent trying to position elements correctly.<p>This isn&#x27;t hating on CSS as much as how broken layout creation is with no end in sight--but plenty of hacks.
评论 #12035279 未加载
评论 #12036850 未加载
zuxfer将近 9 年前
And here I am, trying for past 10 years to centre align a div, both horizontally and vertically.
评论 #12036020 未加载
评论 #12036454 未加载
评论 #12036569 未加载
评论 #12036820 未加载
评论 #12036381 未加载
jordanwallwork将近 9 年前
The problem with using css this way is that it&#x27;s not as obvious what&#x27;s supposed to happen as it is when using js. I&#x27;d love for there to be some explanation within the examples of _why_ they work - I&#x27;ve just been reading the first modal example but I can&#x27;t really understand it
评论 #12035176 未加载
评论 #12035089 未加载
awjr将近 9 年前
Although I think this is an interesting exercise, I need these behaviours to work the same across all browsers. This gets even more complicated within JavaScript apps where the state of the components is something I need to have fine grained control over.<p>Really cool and good to see what how powerful CSS3 is, but not sure how useful it is, particularly when I need this behaviour working on older mobile browsers.
drinchev将近 9 年前
I&#x27;m still a huge fan of &quot;If you can do something with CSS, avoid JS&quot;, but these days, working with React, everything is JS.<p>Although IMHO it makes logical sense to put the logic for the modal windows in the JS, not the CSS. And definitely easier for maintaining it.
评论 #12035204 未加载
kyriakos将近 9 年前
Using checkboxes and radio buttons for keeping state is hacky to say the least. At the end of the day you can do the same with a few lines of easier to understand javascript.
progval将近 9 年前
Ironically, all of these links point to jsfiddle&#x2F;codepen, which require Javascript to see the demo.
mxuribe将近 9 年前
Traditionally, I remember the rule that presentation should be managed by css, while behavior should be managed by javascript. But does the fact that some things can be reasonably done either way change - for example - a site&#x27;s or app&#x27;s maintainability? How about it performance? Is a CSS implementation rendered faster than javascript? I struggle with this at times, especially in some cases where - admittedly older - frameworks allowed for some overlap. Ah, well, much like lots of the web, i&#x27;ll just work to get stuff done.
评论 #12036023 未加载
the_mitsuhiko将近 9 年前
Whenever someone makes a menu with CSS instead of javascript i go crazy as a user. The lack of delay is such a frustration.
评论 #12036049 未加载
评论 #12035189 未加载
评论 #12035168 未加载
评论 #12035181 未加载
评论 #12035216 未加载
评论 #12035156 未加载
acbabis将近 9 年前
The biggest problem with these hacks isn&#x27;t a preference for CSS over JS (which IMO is fine in moderation); it&#x27;s the fact that they aren&#x27;t keyboard or screenreader accessible. A person using a screenreader can&#x27;t even navigate to the accordion (<a href="http:&#x2F;&#x2F;jsfiddle.net&#x2F;yn4ls&#x2F;" rel="nofollow">http:&#x2F;&#x2F;jsfiddle.net&#x2F;yn4ls&#x2F;</a>), and if they could, it would tell them it&#x27;s a form element.
评论 #12037874 未加载
everydaypanos将近 9 年前
Still not good enough. For example the popover&#x2F;tooltip that pops up on hover is fixed right below the target. If for example the viewport&#x2F;window is smaller than the available space it will &quot;bleed&quot; out of view. You still need javascript to place it properly, and javascript that manipulates :after and :before pseudo elements is not straightforward.. Just saying :)
评论 #12035233 未加载
评论 #12035245 未加载
nachtigall将近 9 年前
Would be nice if this would list the minium browser versions required for each example.
jliptzin将近 9 年前
Just because you can, doesn&#x27;t mean you should.
dotdi将近 9 年前
Look ma! I replaced a few lines of JS with a crap-ton of CSS.
ivanhoe将近 9 年前
IMHO this is a big step back, what do you really gain by sacrificing semantics just to avoid a little js code? CSS was supposed to help us separate content and presentation, and adding a bunch of unneeded tags just to make these hacks work is the very opposite of that.
Fletch137将近 9 年前
A fun exercise - while you might not _need_ JS, it&#x27;s still the best option in a lot of cases.<p>I&#x27;d never even consider using most of these techniques in production - while they&#x27;re interesting and showcase what CSS can do, they&#x27;re practically unmaintainable.
MrPatan将近 9 年前
The accordion example cheats a bit. The height of the items is fixed to 300px.<p>I don&#x27;t know if it&#x27;d be possible with flexbox nowadays.<p>The usual max-height trick is not ideal, as the animation then doesn&#x27;t have the right duration.
IanCal将近 9 年前
So in the first modal example, the document contains both dialog windows at the same time.<p>Is that really what you want? Does that properly describe the content of your document?
评论 #12035289 未加载
BinaryIdiot将近 9 年前
These are cool &quot;CSS tricks&quot; but I would seriously never allow someone to use these in a production web application.<p>- It&#x27;s not an accessible solution in most cases (no keyboard navigation)<p>- The CSS classes are very and overly complex to the point where it&#x27;s not intuitive <i>at all</i> what they&#x27;re doing (honestly the code being intuitive is more important to me than any minor performance benefits that may be seen here)
brador将近 9 年前
Not everyone is using the latest CSS standard browsers. Keep it simple for production code. It&#x27;s easier to work with, support, and maintain.
wnevets将近 9 年前
Is it wrong of me to not like using the css content property? I feel like actual content shouldn&#x27;t be in css.
评论 #12036802 未加载
评论 #12036840 未加载
vzip将近 9 年前
I&#x27;ve seen people create some beautiful things in pure CSS but often the code looks like a horrendous hack.
supernintendo将近 9 年前
There are almost 300 comments here and no one has pointed out the obvious: these can be used within email templates where JavaScript is unavailable. For that reason alone, I think this is pretty cool.<p>But sure. Always use the right tool for the job, as they say, and for application logic that tool is not CSS.
vladootz将近 9 年前
Even if you can do a lot of stuff in css, sometimes you shouldn&#x27;t do them. Razvan Caliman from Adobe has some good points on this topic: <a href="https:&#x2F;&#x2F;youtu.be&#x2F;WupAsZGHDcY" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;WupAsZGHDcY</a>
评论 #12035573 未加载
amelius将近 9 年前
&gt; Css is more powerful today and you can do a lot of thing dont need js<p>Yes, but the problem is that adding or changing functionality implemented in CSS can easily lead you into a brick wall.<p>At that point you will either have to rethink your approach completely, or move back to JS.
imafish将近 9 年前
In my opinion, using Cascading <i>Style</i>Sheets for this stuff is just broken. Animations are not <i>styles</i>. Behavior is not a <i>style</i>.<p>Readability and usability of animations could be improved by adding animation tags to the HTML Canvas (like WPF Storyboards: <a href="http:&#x2F;&#x2F;www.codeproject.com&#x2F;Articles&#x2F;364529&#x2F;Animation-using-Storyboards-in-WPF" rel="nofollow">http:&#x2F;&#x2F;www.codeproject.com&#x2F;Articles&#x2F;364529&#x2F;Animation-using-S...</a>).<p>The view behavior-part could be done like WPF triggers. In fact let&#x27;s just get rid of HTML&#x2F;CSS and implement WPF for the browser.
vkjv将近 9 年前
The accordion example isn&#x27;t really what I think of as an accordion because it doesn&#x27;t auto-close other elements.<p>For that behavior, you can make some minor tweaks swapping out the checkbox elements for radio buttons.
majewsky将近 9 年前
I would like a superset of HTML that wraps these hacks up into nice controls, then compiles everything down to plain HTML + CSS.
curiousgal将近 9 年前
I&#x27;ve always been told that the best way to learn something is to delve into it. Tried so with CSS but it was such a hassle that I gave up. Now, years later, all those past frustrations of my younger self remain to the point it&#x27;s like a bête noire of mine. Seeing all this coolness makes me feel kinda bad for missing out. &#x2F;rant
erlehmann_将近 9 年前
If I do not need JS, why do at least the first four demos do not work without it? I stopped trying the linked demos after that.
lucaspiller将近 9 年前
Is it possible to display a &quot;There are no records&quot; message if a container is empty (i.e. tbody) using CSS only?
评论 #12035937 未加载
drydenwilliams将近 9 年前
It&#x27;s a really nice solution for experiments but I&#x27;ve found it quite difficult to get people to adopt this CSS approach in some companies (regardless of any cross browser implications). Everyone needs to be on the same page and of course be up to date with CSS3 animations, which can be over looked.
josephjrobison将近 9 年前
Related to this - is it possible to do something with pure CSS like shown in the black filter buttons area on this - <a href="http:&#x2F;&#x2F;www.siegemedia.com&#x2F;creation&#x2F;best-infographics" rel="nofollow">http:&#x2F;&#x2F;www.siegemedia.com&#x2F;creation&#x2F;best-infographics</a> ?
20years将近 9 年前
While I don&#x27;t think I would use a lot of this in production for the maintainability alone, it does showcase what you can do with CSS. I see a lot of convoluted Javascript being used on things that can easily and more cleanly be accomplished with a little CSS.
anacleto将近 9 年前
You definitely missed the Pure CSS dancing tree: <a href="https:&#x2F;&#x2F;codepen.io&#x2F;yukulele&#x2F;pen&#x2F;KCvbi" rel="nofollow">https:&#x2F;&#x2F;codepen.io&#x2F;yukulele&#x2F;pen&#x2F;KCvbi</a>
wehadfun将近 9 年前
I&#x27;&#x27;m glad that the general consent here seems to be doing this in CSS is not a great idea. I feel the same way about XAML where programmers try to do things in XML instead of C#
d33将近 9 年前
On a side note, HTML + CSS3 is Turing complete:<p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=2300836" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=2300836</a>
评论 #12035531 未加载
nzjrs将近 9 年前
Related, does anyone else have a preferred pure css treeview?
评论 #12037471 未加载
iLoch将近 9 年前
I think it would actually be wise to stick to JS style rendering (as is the case with React) as we&#x27;re only going to see more and more styling being delegated to scripts with the rise of wasm. Only a matter of time before CSS will only really be useful for completely static websites IMO.<p>Edit: Downvoting me because you disagree isn&#x27;t really in the spirit of HN.<p>Please provide a counter argument if you disagree, I&#x27;d be interested to hear it.
评论 #12035609 未加载
reitoei将近 9 年前
ITT: people taking this way too seriously
lasfter将近 9 年前
It is pretty easy to cheat at the rocketship game by dragging your mouse to avoid enemies.
boubiyeah将近 9 年前
Please don&#x27;t. CSS is unmaintenable. JS can be made very maintainable; end of story.
hartator将近 9 年前
I think that&#x27;s great, but everything is based on hacks using checkboxes.
lsh将近 9 年前
hm. ironically (?) none of the linked examples work without javascript.
ClassyJacket将近 9 年前
Is that title supposed to be in English?