CSS Tips
markodenic.com
markodenic.com
This can actually be done with 2 lines now!
.center {
display: grid;
place-items: center;
} <td valign="middle">
worked flawlessly.Of course maybe there is some good explanation... I certainly never contributed to the CSS spec.
foo { display: table-cell; vertical-align: middle; }
And it has also worked a lot longer than the flex-approach. But now when flex is available it seem the more appropriate tool for the job.While it's a ton of markup, tables are build upon a few easy-to-remember concepts and you can do anything (including vertically align without googling!). I'm still constantly looking up flex and grid rules.
That said, I did move on.
The flex-box approach is the most intuitive in my opinion. The flex display-type basically means you want to distribute the area of a box among its immediate child elements, and control how the children and aligned relative to the container and each other. Centering is naturally part of this.
Using grid is IMHO less intuitive, since a grid with a single cell seem just as much a hack as a table with a single cell.
https://www.smashingmagazine.com/2020/01/understanding-css-g...
MDN also has a list of shorthand properties if someone didn't know what 'shorthand property' really meant: https://developer.mozilla.org/en-US/docs/Web/CSS/Shorthand_p...
But yeah, the discoverability or suggestion-to-refactor-something-to-shorthand is typically non-existent in CSS. Lists like these are really the only way I discover CSS stuff.
They have the best CSS completion I’ve ever seen, and it includes live (and cached) mdn documentation for each rule. It also suggests shorthand collapses as a feature
Source: https://developer.mozilla.org/en-US/docs/Web/CSS/place-items
I think there's a lot of confusion for most people between justify-content/justify-items and align-content/align-items
1: https://preset-env.cssdb.org/ 2: https://preset-env.cssdb.org/features#place-properties
The notable exception is IE 11…though even then, with a bit of autoprefixer magic, you may be able to use this.
I do use grid when I need it, but I don't see the point of using grid in places where flexbox will do. Granted this is on the verge of not mattering at all anymore, but I just don't see the point in using something less compatible when you don't have to.
But as I stated in elsewhere in this thread, grid is not a replacement for flexbox. There is overlap, they both have their strengths.
I’ve seen this said, and for the life of me, I can’t find an actual reference for it.
At least it’s first-hand.
A good use case for flex would be a list of tags, that need to wrap.
- Particularly in Firefox they are much slower. When grids start to pile up on a page, performance goes down the drain
- Implicit row-sizing is not really a thing in Safari or iOS -> https://stackoverflow.com/questions/44770074/css-grid-row-he...
Quote from that SO post: "This is not a Safari bug. It's just a different interpretation of the spec.
When dealing with percentage heights, some browsers (like Safari) adhere to the traditional interpretation of the spec, which requires a defined height on the parent.
In other words, a percentage height on an in-flow element will be recognized only when the parent has a defined height.
Some browsers, such as Chrome and Firefox, have moved past this interpretation and now accept flex and grid heights as an adequate parent reference for a child with a percentage height.
But Safari is stuck in the past. This doesn't mean it's wrong, invalid or a bug."
Just worth noting, as I had a lot of fun with that one.
place-content will position all the children to the center, which is generally what's desired here.
place-items, would make each child element center itself within its own grid square
.center {
display: table-cell;
vertical-align: center;
} CENTER{LINE-HEIGHT:100VH}
https://jsfiddle.net/pnyr1u8d/actually, neither of your versions work without specifying a document height. (or outer container)
.center {
display: grid;
place-items: center;
}
https://jsfiddle.net/a67gzsjf/something like this would work
Occasionally a project requires IE 10/11 support (state government work) and it's such a shame to get excited about an impossible CSS approach.
I agree, though what I would find really helpful is not just can-i-use but should-i-use, taking into account not only feature availability but also quality of implementation. Even if a feature is theoretically supported in all major browsers, it’s not much use for a production site or app if what you actually see on screen in at least one of those browsers looks terrible, for example because of bad anti-aliasing or colour handling or animation calculations. I’ve been building new UI/design systems from scratch for a couple of projects recently, and it’s amazing how often that still happens, even with popular CSS features used to implement relatively simple effects.
I understand that one should aim for a certain level of backward compatibility, but when does the time come to drop IE11 support for good?
Especially as a smaller webdev-shop, how would one even come up with the resources to maintain that level of compatibility? Eventually there is a trade-off where one may end up losing a (hopefully reasonably small) percentage of customers in exchange for focusing on the experience of those customers who are more up to date, but it's a tricky decision to make.
[1] https://techcommunity.microsoft.com/t5/microsoft-365-blog/mi...
This was the very first bit of CSS I ever learned thanks to my first internet friend, Tom.
Libraries like popper.js (no affiliation) exist to solve this specific problem.
Considering how many people (myself included) consider CSS a veritable minefield, I love seeing examples like this.
scrollbar-color: rebeccapurple gold;
scrollbar-width: thin;
As a bonus it works in firefox. position: sticky
is a new one for me. window.onload = function()
{
var titlebarfixcoverfunction = function()
{
if (location.hash.indexOf('#') > -1)
{
var titlebar = document.getElementById('title');
window.scrollBy(0, -titlebar.offsetHeight);
}
};
titlebarfixcoverfunction();
window.addEventListener('hashchange', titlebarfixcoverfunction);
}javascript:(function()%7B(function%20()%20%7Bvar%20i%2C%20elements%20%3D%20document.querySelectorAll('body%20*')%3Bfor%20(i%20%3D%200%3B%20i%20%3C%20elements.length%3B%20i%2B%2B)%20%7Bif%20(getComputedStyle(elements%5Bi%5D).position%20%3D%3D%3D%20'fixed')%20%7Belements%5Bi%5D.parentNode.removeChild(elements%5Bi%5D)%3B%7D%7D%7D)()%7D)()
Click the bookmark and sticky things disappear. Some websites are horrific with how much screen space they take up with sticyky banners. That also of course breaks some sites.
I used `position:sticky` in a productive website back in 2015, when it was already well-supported in Firefox. What's true is that Chrome only got support for it recently, and since Chrome == the internet nowadays, you will be forgiven for considering it new.
Surprisingly most of these have great browser support.
That’s not to say this is a bad article; it’s not. It’s just that there is more to CSS than “here are some rules and ways of using them you might not have thought of”. That’s the real reason front-end dev and design is as difficult as it is.
---
Supported Everywhere:
- Typing Effect (@keyframes)
- Drop Shadow
- Smooth Scrolling
- "center" (flexbox)
- Truncate Text (uses line-camp)
- Resize
- Modals (uses :target)
- calc
- Style Empty (:empty)
- Position Sticky (minus some table elements in old versions)
- Scoll Snap (partial support in even IE)
- Dynamic Tooltips (is just pseudo elements...)
- Caret color (except old IE)
- Fancy Text (uses background clip, so all but old IE)
---
Not Supported Everywhere:
- Cursors (mobile, obviously...)
- ::selection (mobile ignores)
- Custom Scrollbars
---
I think if you put it side-by-side like this you really see how inaccurate your comment is...
Things that work: - Typing Effect (@keyframes) - Drop Shadow - "center" (flexbox) - Truncate Text (uses line-camp) - Modals (uses :target) - calc - Style Empty (:empty) - Position Sticky - Scroll Snap - Dynamic Tooltips (when using touchpad) - Fancy Text
Things that don’t work: - Smooth Scrolling - Resize - Cursors (even when using the touchpad cursor) - ::selection (even using Safari in “Desktop mode”) - Custom Scrollbars
I could test due to not having a live example: - Caret color
.tile--custom-scrollbar {
scrollbar-color: rebeccapurple gold;
scrollbar-width: auto;
}
1: https://caniuse.com/css-scrollbarLet me guess, "Everywhere" is only the Chrome based, latest browsers?
The only argument being "but not on IE". Which, 100% of these work on almost all versions of Edge. Edge is provided side-by-side with Internet Explorer these days (mostly...).
Internet explorer market share is near zero. Also reminding you that these are just CSS properties (meaning fallbacks are dead-simple or it just looks/behaves boring-er and doesn't break anything).
- Drop shadow - Custom cursors - Smooth scrolling - Truncate text to a number of lines - Custom scrollbars - Sticky position - Scroll snap - Fancy text - The CodePen CSS button
There is no longer any reason developers building general-audience websites, web apps, or libraries should care about IE support. It's not part of the general conversation anymore.
You only need to cater to IE if you're a developer on one of those legacy sites, or maintain a library that has been around for so long it's still actively used by those legacy sites.
And healthcare. And government. And a lot of other non-Silicon Valley and non-hobby industries.
What did you think I was talking about instead?
https://pilabor.com/blog/tricks-you-might-not-know/html-and-...
> <a href="#!">
(don't jump to page top on click)
But some of those have been added as standard (with the webkit prefix for all browsers), like explained here: https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-lin...
Which of course is a consequence of it being widely used before it was a standard forcing other browsers to adopt..
There are legitimate uses for snap-scrolling, e.g. a horizontal carousel on mobile (or even desktop, depending on implementation), where you want swiping to swipe so the next item is in view.
Also, you said half these things should be avoided -- care to list the others? It seems like the theme of the post is "you don't need JavaScript to do this." If some functionality is rarely needed, but does have use cases, it's nice to know that it can be done in CSS without requiring JS (which would be clearly worse).
https://shouldiuseacarousel.com/
So... no, there isn't a legitimate use for snap-scrolling. Leave scrolling alone.
1) You implementing something boards (like trello) on mobile. The well-established-in-all-the-apps-that-specialize-in-this behavior is snap scrolling.
2) Tiktok/stories/etc. When you scroll up or right, does it ever land you in between two stories? No. This is the exact behavior.
3) Presentations. Say you are building a presentation tool. The whole point is to define separate slides that are on screen one at a time. This is a very natural and appropriate use of this behavior.
So, yeah, carousels suck. But, this behavior is commonly used and useful in a lot of modern software.
It's also about making your home page a giant carousel, instead of showing multiple things vertically. Don't do that, either.
IMO, one great use for carousels is when you have a product catalog with a bunch of category sections, like Netflix. Each category gets a header, e.g. "Action" "Family" "Romance" "Horror" and each category is a horizontally scrolling list of products, with the next one on the list half-visible on the side, so it's clear that you can scroll it into view.
Scroll snap can be pretty good for that.
My understanding is that NY Times spends quite a lot of effort on user metrics, so I would guess, but can't say for sure, that they have evidence that their carousels work.
would probably be best to split up the galleries into smaller groupings, though
See something like https://airbnb.com which uses it for categories a’la the App Store. Image galleries, Netflix-type layouts, etc.
What's the UX benefit here?
I get that mobile apps use horizontal swiping and some websites want to be app-like, but even in apps the only common horiztonal-swipe pattern I see is the Android hidden menus pattern, which is (a) a subject of debate in UX circles and (b) absolutely nothing like horizontally scrolling item selection, which is still a bad idea even in native mobile apps.
The other major example I can think of is Instagram's story circles, which I feel must be intentionally unusable to try and force people to view stories in their full-screen tap-to-advance format.
Actually I was thinking the top comment would be something about the site breaking the back button
Yet you can't see the examples without JavaScript.
I suppose the author could show the CSS snippets inline and make you click to a demo, but that's not very fun is it?
I really think comments like this aren't helpful and presumptuous to pressing forward. People hate change and love their table layouts... Just going to make a friendly counter argument:
- Did you read what this actually does? It's for anchor tags...
- Bootstrap 5 uses this by default.
- Lot of crappy websites out there. I choose loading one line of CSS versus jQuery + animate all day...
- For perspective, almost 100% of every little interaction on native apps are animated these days. It's liked so much by the general population that, some would say, made iOS so appealing in the beginning to take off. People like interaction.
- It's possible your opinion does not represent the majority anymore (or even that the HN general opinion).
- By making it a browser-defined CSS property versus being JavaScript, you are actually opening accessibility doors for people having this be device configurable.
I really think if someone hates it so much or for accessibility reasons, instead they should just disable it in their browser.
So literally the opposite of what you said.
---
Edit: Example of why this is important to be part of CSS for accessibility versus JavaScript:
```
@media (prefers-reduced-motion: no-preference) {
:root {
scroll-behavior: smooth;
}
}
```or:
```
@media (prefers-reduced-motion) {
.animation { animation: none; }
:root {
scroll-behavior: auto;
}
}
```I think HN can chill a bit here.
scroll-behavior: smooth seems to only affect the behavior of clicking named anchor tags. It doesn't seem to change the behavior when scrolling with a mouse wheel or trackpad. Other than general accessibility issues related to animations, I don't see any usability concerns here.
scroll-snap-type and scroll-snap-align also don't seem to affect the behavior when scrolling with a mouse wheel or trackpad, except that shortly after you stop scrolling the browser will smoothly scroll to the specified snap points. I suppose this could be over-used (like anything), but it seems to me that there are some pretty obvious places where using this would be entirely appropriate and not unpredictable or disorienting.
::-webkit-scrollbar styling strikes me as the most concerning, both because of how easy and tempting it is to over-use and because the browser support has some subtle gotchas. But this one doesn't seem related to your complaint about smooth versus snappy scrolling.
The custom scrollbars example did not work on Firefox on OSX. all the boxes had identical scrollbars, the OS default.
https://developer.mozilla.org/en-US/docs/Web/CSS/scrollbar-c...
I really liked the ones about truncating text - which also taught me what "ellipsis" means. It is the dots in this example:
This text is trun...
For anyone looking to really learn CSS for layout the resource I recommend is always https://every-layout.dev
Heydon Pickering and Andy Bell know what they’re talking about, and they explain the “how and why”