Tagged “layout”
inline-block versus flexbox for horizontal arrangements
Something I’ve found interesting while reviewing recent code test submissions (within job applications) at work is that more developers than I’d expect still use display: inline-block
when they need to lay out multiple elements horizontally such as the items in a navigation menu. It’s interesting because Flexbox – which has now been around for almost 10 years – gives you a modern, purpose-built solution to that.
Of course, there is no crime in taking the inline-block
approach. It’s an option. And I did take a moment just to challenge my assumptions. But having given it a little thought, I am pretty convinced that Flexbox is the better approach and so I think their choice is worth a query.
Here’s why I think Flexbox is much better:
- Modern CSS layout is purpose-built for constructing multi-element layouts. Older methods were lacking, quirky and slightly hacky. Using (and knowing to use) purpose-built tools is generally a good thing for reliability, predictability and scalability.
- Modern CSS layout provides more control, because it’s fundamentally different. You get a parent context (the flex container or grid) that lets you style the context (with
gap
, for example). We didn’t have this before. - Styling a context rather than individual items plays better with modern component-oriented organisation and ideas. It‘s good to keep components encapsulated and context-agnostic, handing off layout to separate, fit-for-purpose tools.
- Flexbox is built for responsiveness, i.e. built for the modern era.
inline-block
was not. - the default
align-items:stretch
means that items laid out in a row are automatically full parent-height which is often desirable for larger touch targets and achieving hover styles. inline-block
retains some of the properties ofinline
that are undesirable for this type of use-case. Who can be bothered with whitespace in the code appearing on the screen, or having to work withvertical-align
when you don’t need to.
I also don’t see any reason to not use Flexbox. It’s conventional (see for example navigation menus on BBC, Tetralogical), recommended by CSS experts (Rachel Andrew, Jen Simmons etc), performant and well-supported by browsers. You are not going “fancy”, “cutting edge” or “overkill” by using Flexbox. For the “horizontal menu” use case, it’s simply a more suitable choice than the older alternatives.
Related: Chris Coyier’s article When do you use inline block is an excellent investigation into whether any necessary uses for inline-block
remain. I reserve the right to change my mind on this, but aside from the “apply transform
to an inline element” use case I can’t really see any that have not now been superseded.
BBC WebCore Design System
A Storybook UI explorer containing the components and layouts for making the front end of a BBC web experience.
Conditional border-radius in CSS (by Ahmad Shadeed via CSS-Tricks)
Here’s a “media query free” CSS one-liner which lets you set an element to have no border-radius when it is the full width of the viewport, but otherwise to have a border-radius.
It uses the same 9999 multiplication technique as Every Layout do to create a toggle.
Great for Card components which need to be full-width and non-rounded only on narrow viewports.
A First Look at aspect-ratio (on CSS-Tricks)
Chris Coyier takes the new CSS aspect-ratio
property for a spin and tests how it works in different scenarios.
Note that he’s applying it here to elements which do not have an intrinsic aspect-ratio. So, think a container element (div
or whatever is appropriate) rather than an img
. This is line with a Jen’s Simmons’ recent replies to me when I asked her whether or not we should apply aspect-ratio
to an img
after she announced support for aspect-ratio
in Safari Technical Preview 118.
A couple of interesting points I took from Chris’s article:
- this simple new means of declaring aspect-ratio should soon hopefully supersede all the previous DIY techniques;
- if you apply a CSS aspect-ratio to an element which has no explicit
width
set, we still get the effect because the element’sauto
(rendered) width is used, then by combining that with the CSSaspect-ratio
the browser can calculate the required height, then apply that height; - if the content would break out of the target aspect-ratio box, then the element will expand to accommodate the content (which is nice). If you ever need to override this you can by applying
min-height: 0
; - if the element has either a height or a width set, the other of the two is calculated from the aspect ratio;
- if the element has both a height and width set,
aspect-ratio
is ignored.
Regarding browser support: at the time of writing aspect-ratio
is supported in Chrome and Edge (but not IE), is coming in Firefox and Safari, but as yet there’s no word regarding mobile. So I’d want to use it as a progressive enhancement rather than for something mission critical.
Use CSS Clamp to create a more flexible wrapper utility (on Piccalilli)
Here’s Andy Bell recommending using CSS clamp()
to control your wrapper/container width
because it supports setting a preferred value in vw
to ensure sensible gutters combined with a maximum tolerance in rem
—all in a single line of code.
If we use clamp() to use a viewport unit as the ideal and use what we would previously use as the max-width as the clamp’s maximum value, we get a much more flexible setup.
The code looks like this:
.container {
width: clamp(16rem, 90vw, 70rem);
margin-left: auto;
margin-right: auto;
}
This is pretty cool because I know from experience that coding responsive solutions for wrappers can be tricky and you can end up with a complex arrangement of max-width and media queries whilst still—as Andy highlights—not providing optimal readability for medium-sized viewports.
Using CSS Grid with minmax() is one possible approach to controlling wrappers however this article offers another (potentially better) tool for your kit.
It’s worth noting that Andy could probably have just used width: min(90vw, 70rem)
here (as Christopher suggested) because setting the lower bound provided by clamp()
is only necessary if your element is likely to shrink unexpectedly and a regular block-level element wouldn’t do that. The clamp
approach might be handy for flex items, though.
(via @piccalilli_)
A Utility Class for Covering Elements (on CSS { In Real Life })
Need to overlay one HTML element on top of and fully covering another, such as a heading with translucent background on top of an image? Michelle Barker has us covered with this blog post in which she creates an overlay
utility to handle this. She firstly shows how it can be accomplished with positioning, then modernises her code using the inset
CSS logical property, before finally demonstrating a neat CSS Grid based approach.
I like this and can see myself using it – especially the Grid-based version because these days I try to avoid absolute positioning and use modern layout tools instead where possible.
I’ve mocked up a modified version on Codepen, sticking with CSS Grid for simplicity. I was going to also wrap it in an @supports (display:grid)
however the styles are all grid-based so in the case of no grid support they simply wouldn’t run, rather than causing any problems.
BBC GEL | The lessons learnt creating a design system for BBC Online
Interesting insight into the BBC design system and the five areas it’s split into: Foundations, Components, Layout Components (including Stack, Grid etc) Levers and Containers.
(via @piccalilli_)
Create an Automatically Responsive Flexbox Gallery (on egghead.io)
Here’s a lovely intrinsically responsive (no media queries) photo gallery solution from Stephanie Eckles. It can accommodate differently sized images and achieves its layout by a combination of flexbox features (flex-wrap
, flex-basis
) and by applying object-fit: cover
to photos to make them fully cover their parent list items.
Breaking Out With CSS Grid Layout (on cloudfour.com)
While bookmarking the mastery.games article yesterday, I started getting the feeling that something was awfully familiar. It was! I’ve seen this layout before – from Tyler Sticka back in 2017 to be precise – but failed to bookmark it at the time.
Here, then, is the original and still the best CSS Grid “article with breakout images” layout!
I particularly love the way that, by naming the lines and appending -start
and -end
as appropriate you can then target the area between those lines using its short name.
.Prose {
display: grid;
grid-template-columns:
[full-start] minmax(1em, 1fr)
[main-start] minmax(0, 40em) [main-end]
minmax(1em, 1fr) [full-end];
}
.Prose > * {
grid-column: main;
}
.Prose-splash {
grid-column: full;
}
Article Layout with CSS Grid (on mastery.games)
Very clever responsive <article>
layout (with gutters and breakout images) achieved using CSS Grid, minmax()
, the ch
unit and a minimum of fuss. It scales automatically from narrow to wide viewports with no auto
margins, max-width
or media query manual overrides in sight.
For the blog post page (the page you're looking at right now) I wanted a mobile-friendly layout where the text was centered and readable, where the images/code examples are wide.
The gist of it is this:
.post {
display: grid;
grid-template-columns:
minmax(1.2rem, 1fr)
minmax(auto, 57ch)
minmax(1.2rem, 1fr);
}
Simple and deadly.
I can potentially see this as a pattern, or layout primitive, rather than just as something you might use at the top-level of the page for overall layout. I’m looking forward to trying it out!
(via Ahmad Shadeed)
3 Popular Website Heroes Created With CSS Grid Layout (on Modern CSS Solutions)
Some serious CSS Grid positioning, image manipulation and alpha transparency ninjitsu on display here from Stephanie Eckles!
This episode explores creating website heroes - aka “headers” - with one of my favorite ways to use CSS grid layout: by turning it into a canvas.
Bustle
Here’s a beautiful, magazine style website design for digital publication Bustle. The typography, use of whitespace, responsive layout, menu pattern, colour palette and imagery are all on point!
Subgrid for CSS Grid launches in Firefox 71
Subgrid for CSS Grid Layout has arrived in Firefox and it looks great. Here’s how I wrapped my head around the new concepts.
While MDN has some nice examples, I generally find I need a little extra time, trial and error and note-making in order to fully grasp CSS Grid concepts.
For example, I always need to remind myself that parts of the syntax – such as grid-template-columns
selectors – refer to grid lines rather than columns.
So I created a Subgrid example pen with guideline notes for future reference. Note: make sure to open the pen in Firefox rather than any other browser, because at the time of writing only Firefox supports Subgrid!
Relearn CSS layout: Every Layout
Every now and then something comes along in the world of web design that represents a substantial shift. The launch of Every Layout, a new project from Heydon Pickering and Andy Bell, feels like one such moment.
In simple terms, we get a bunch of responsive layout utilities: a Box, a Stack, a Sidebar layout and so on. However Every Layout offers so much more—in fact for me it has provided whole new ways of thinking and talking about modern web development. In that sense I’m starting to regard it in terms of classic, game-changing books like Responsive Web Design and Mobile First.
Every Layout’s components, or primitives, are self-governing and free from media queries. This is a great step forward because media queries tie layout changes to the viewport, and that’s suboptimal when our goal is to create modular components for Design Systems which should adapt to variously-sized containers. Every Layout describe their components as existing in a quantum state: simultaneously offering both narrow and wide configurations. Importantly, the way their layouts adapt is also linked to the dynamic available space in the container and the intrinsic width of its contents, which leads to more fluid, organic responsiveness.
Every Layout’s approach feels perfect for the new era of CSS layout where we have CSS Grid and Flexbox at our disposal. They use these tools to suggest rather than dictate, letting the browser make appropriate choices based on its native algorithms.
Intrinsically Responsive CSS Grid with minmax and min
Evan Minto notes that flexible grids created with CSS Grid’s repeat
, auto-fill
, and minmax
are only intrinsically responsive (responsive to their container rather than the viewport) up to a point, because when the container width is narrower than the minimum width specified in minmax
the grid children overflow.
Applying media queries to the grid is not a satisfactory solution because they relate to the the viewport (hence why Every Layout often prefer Flexbox to CSS Grid because it allows them to achieve intrinsic responsiveness).
However we’ll soon be able to suggest grid item width as a percentage of the parent container, avoiding the overflow problem. The new “CSS Math functions” to help us achieve this are min()
, max()
and clamp()
. At the time of writing, these are only supported in Safari however Chrome support is in the pipeline.
Katherine Kato’s personal website
Some simple but inspiring work here from Seattle-based web developer Katherine Kato. I really like the use of space, the typography, the colour palette and the use of CSS grid for layout.
Using CSS display: contents to snap grandchild elements to a grid
I realised last night while watching a presentation by Lea Verou that I could streamline my CSS Grid layouts.
I’d been creating an overall page grid by setting body { display: grid; }
then some grid areas but realised that this only worked for direct children and didn’t help with aligning more deeply nested elements to that outer grid.
For example in the case of the main header
if I wanted its child logo, nav
and search elements to snap to the body
grid then I found myself having to duplicate the display: grid
and grid-template-areas
again on the header
.
It didn’t feel very DRY but my understanding was that while we await subgrid
, it was a necessary evil.
What I should have been using is display: contents
.
If you set your header
to display: contents
then the parent (body
) grid layout will apply to the header’s contents (logo, nav
, etc) as if the header
element (the “real” direct child of the grid) wasn’t there. This gives us good semantics without the need to redefine the grid on the header
.
Here’s a codepen to illustrate.
I was aware of the existence of display: contents
but somehow it hadn’t sunk in because I’d only read about it in the abstract. Lea Verou’s explanation made all the difference. Cheers, Lea!
Update 4/5/2019
Frustratingly, I’ve learned that although we can apply this technique, we shouldn’t… or at least not for a while.
Due to a bug in all supporting browsers the property will currently also remove the element (header
in our case) from the accessibility tree meaning that its semantics will be lost.
For reference, see:
Update 11/4/2021
Thanks to Rachel Andrew for the heads-up that this issue is now fixed in both Firefox and Chrome.
We’re now just waiting for Edge and Safari to roll out fixes before we can regard this as a safe option.
See all tags.