惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
A
About on SuperTechFans
G
Google Developers Blog
aimingoo的专栏
aimingoo的专栏
U
Unit 42
WordPress大学
WordPress大学
Y
Y Combinator Blog
罗磊的独立博客
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
博客园 - 叶小钗
Stack Overflow Blog
Stack Overflow Blog
Engineering at Meta
Engineering at Meta
Microsoft Security Blog
Microsoft Security Blog
GbyAI
GbyAI
V
V2EX
雷峰网
雷峰网
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
SegmentFault 最新的问题
酷 壳 – CoolShell
酷 壳 – CoolShell

Ben Frain

Well done AI, that’s the end of the honest boxes The Beginnings, by Rudyard Kipling So, you want a React modal that uses the <dialog> element and transitions in AND out? Scroll indicators on tables with background colours using animation-timeline Review: SoundPEATS Clip1 Open ear clip-on headphones VS Code – highlight just the active indent guide Review: MoErgo Go60, a split ergonomic and fully programmable keyboard Review: Kinesis mWave mechanical ergonomic and programmable keyboard iOS26 Safari theme-color/tab-tinting with fixed position elements is a mess New Book: Responsive Web Design with HTML5 and CSS, 5th Edition Use @supports with a proxy feature/value for features you can’t test for (@starting-style) First adventures in View Transitions Review: Benq Screenbar Pro and Halo lightbars. The kit you never knew you needed! A single element CSS donut timer/countdown timer, that can sit on any background Review: Open Ear Headphones – Bose Open Ultra v Huawei FreeClip In search of the perfect autocomplete for CSS Managing multiple versions of node, without NVM or additional tools Review: Keychron Q14 Max Alice 96 Key mechanical keyboard NEW VIDEO COURSE: Responsive Web Design with HTML5 and CSS Is CSS Grid really slower than Flexbox? Review: Advantage360 Pro Signature Edition 2024 mechanical ergonomic keyboard More Keys or Fewer Keys for mechanical keyboards Yes! You can use position: sticky and overflow together Neovim – how to do project-wide find and replace? Review: Keyboardio Model 100, split, wooden, mechanical keyboard Struggling to learn SwiftUI How to create rounded gradient borders with any background in CSS How to get equal size icons in the cmp completion menu of Neovim with Kitty terminal Review: Dygma Defy, split, mechanical, programmable ergonomic keyboard What’s the best way to reset WAAPI chained animations?
Center items in a container, and make then left aligned w...
Ben Frain · 2025-01-25 · via Ben Frain

I wanted a number of items, centered horizontally in a container, and when there are too many items to fit, the items should overflow the container, from left to right (first item flush with the left hand side of the container, overflowing items go off the right).

Here is where I started:

The problem

<div class="container">
    <div class="thing">item1</div>
    <div class="thing">item2</div>
    <div class="thing">item3</div>
    <div class="thing">item4</div>
    <div class="thing">item5</div>
    <div class="thing">item6</div>
    <div class="thing">item7</div>
    <div class="thing">item8</div>
    <div class="thing">item9</div>
    <div class="thing">item10</div>
    <div class="thing">item11</div>
    <div class="thing">item12</div>
</div>

And here was the CSS:

.container {
    width: 360px;
    background-color: #ddd;
    display: flex;
    justify-content: center;
    overflow: auto;
}

.thing {
    font-size: 14px;
    font-weight: 700;
}

And here is a Codepen of just that. Notice how item1 is missing to the left and you can’t scroll left (at least on desktop, you can’t) to get to it? Technically, in the CSS specifications, this is referred to as ‘data loss’.

See the Pen Centre items until they overflow, then left align 2 by Ben Frain (@benfrain) on CodePen.

That’s because by default, with justify-content: center distributed alignment is not doing what we would like. Other alignments will, but not center. This very situation is described in the specs: https://www.w3.org/TR/css-align-3/#overflow-values and a lovely solution is provided: overflow-position: safe; but there is a catch; it isn’t implemented anywhere yet.

The simple solution for now

However, while you can’t do it cleanly with just CSS, you can fix the problem easy enough with an extra element to wrap the contents, and give it margin: auto on the sides.

If you just came here for a working approach, be on your way weary coder, as at the time of writing, this is the easiest solution I have found.

Here you go:

See the Pen Centre items until they overflow, then left align 2 by Ben Frain (@benfrain) on CodePen.

If for some reason that extra inner element is problematic, for example you need to do something different at a different viewport size, remember you can always make it seem like it doesn’t exist by making it display: contents;.