Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[css-flexbox][css-align] Proposal: justify-items and justify-self to align flex items that are alone on their line #11244

Open
benface opened this issue Nov 19, 2024 · 5 comments
Labels
css-align-3 Current Work css-flexbox-1 Current Work

Comments

@benface
Copy link

benface commented Nov 19, 2024

Ok, I have to say that this is a very rough/raw idea, but the problem it would solve is a real one: there's currently no way to have a flex container with justify-content: space-between and flex-wrap: wrap and control the horizontal alignment ("justification"?) of the items that end up alone on their line (they are always start-aligned, at least in LTR). A workaround exists to make them end-aligned, but I couldn't find any to make them center-aligned. What if it was as simple as this?

.flex {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  justify-items: center; /* <-- this currently doesn't work; `justify-items` is ignored in flex layout */
}

Or, a more concrete example where we only want to center-align a specific item when it wraps:

<footer style="display: flex; flex-wrap: wrap; justify-content: space-between; gap: 1rem;">
  <nav>Nav content here</nav>
  <div>Social links here</div>
  <p style="justify-self: center; text-align: center;">
    Copyright © 2024 Example Company
  </p>
</footer>

Check out that example on Codepen. There is currently no way to center-align the copyright notice without using a media query, which is just sad.

@Loirooriol
Copy link
Contributor

It seems to me what you actually want is justify-content: space-between but with a fallback alignment of safe center instead of safe flex-start. Try space-around, which has your desired fallback, it works quite well (but of course you get spacing on the sides).

@benface
Copy link
Author

benface commented Nov 19, 2024

It seems to me what you actually want is justify-content: space-between but with a fallback alignment of safe center instead of safe flex-start.

That may well be what I want (though I think what I proposed is more powerful because it would allow setting the "fallback alignment" of individual items). In any case, is it even possible to change the fallback alignment? Is there a proposal for it?

Try space-around, which has your desired fallback, it works quite well (but of course you get spacing on the sides).

Yes, I'm familiar with that option. Unfortunately, the spacing on the sides is a dealbreaker.

@Loirooriol
Copy link
Contributor

I think what I proposed is more powerful

Well, yes, but it seems very weird if it only applies when there is no other item on the line.

is it even possible to change the fallback alignment? Is there a proposal for it?

I don't know, but it seems reasonable and straightforward, e.g justify-content: space-between, safe center.

Could also be useful to e.g. choose unsafe fallbacks.

@Loirooriol Loirooriol added css-align-3 Current Work css-flexbox-1 Current Work labels Nov 20, 2024
@yisibl
Copy link
Contributor

yisibl commented Nov 21, 2024

I think you need a new container query that detects if flex-wrap: wrap is triggered.

@benface
Copy link
Author

benface commented Nov 21, 2024

I think you need a new container query that detects if flex-wrap: wrap is triggered.

How can a container query detect if flex-wrap: wrap is triggered? If you're suggesting I just figure out the exact container width at which it happens and then hardcode that in the container query, that's not a good solution for me. :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
css-align-3 Current Work css-flexbox-1 Current Work
Projects
None yet
Development

No branches or pull requests

3 participants