Skip to content

[css-overflow-4] line-clamp max-lines - support auto value to fit container  #9368

Closed

Description

@yairEO

Feature request

Spec links

Currently -webkit-line-clamp property only supports an integer number which represents the maximum
number of lines to possibly render before truncating the text with an ellipsis.

I have a use-case where I need an dynamic value for the -webkit-line-clamp so it matches the
number of lines which can fit in the container (which is being truncated), assuming the container
has a dynamic height.

Also important - nothing should be painted past the ellipsis point (unlike today when the container's height is set to a value greater than the height of the truncated lines)

Example:

https://jsbin.com/kovuwow/edit?html,css,output

<div>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt 
  ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco 
  laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in 
  voluptate velit esse cillum dolore eu fugiat nulla
</div>
div {
  display: -webkit-box;
  -webkit-line-clamp: auto; /* 👈 keyword to let line-clamp according to the container */
  -webkit-box-orient: vertical;
  overflow: hidden;
  resize: auto;
}

If you want line-clamp to (show N lines that can) fit the container element, it is currently impossible:

line-clamp--auto-value.mp4

Javascript-based solution

I've hacked this using (a lot of) javascript: https://codepen.io/vsync/pen/eYbGgGL

line-clamp--auto-value.mp4

Related StackOverflow questions:

Activity

  1. SebastianZ commented on Sep 17, 2023

    @SebastianZ
    Contributor

    If I am not mistaken, this use case is already covered by continue: discard in combination with block-ellipsis: auto.

    Though it looks like this can't be set via the shorthand line-clamp yet. So I suggest to change its syntax so setting the max-lines value is not required.
    I think it is not necessary to add direct control over continue but instead set it to discard in case a block ellipsis is set.

    So I suggest to change the syntax to

    none | [ <integer [1,∞]>? <'block-ellipsis'>? ]!

    With that change, one could write line-clamp: auto (with auto standing for the block-ellipsis value) to achieve the above.

    Sebastian

  2. frivoal commented on Sep 21, 2023

    @frivoal
    Collaborator

    If I am not mistaken, this use case is already covered by continue: discard in combination with block-ellipsis: auto.

    Correct.

    So I suggest to change the syntax to

    none | [ <integer [1,∞]>? <'block-ellipsis'>? ]!

    Seems reasonable to me.

  3. frivoal commented on Sep 21, 2023

    @frivoal
    Collaborator
  4. andreubotella commented on Sep 21, 2023

    @andreubotella
    Member

    none is a valid value for block-ellipsis, so with this syntax there would be no way to represent a height-based clamping without ellipsis. That might be fine, though.

  5. andyearnshaw commented on Oct 13, 2023

    @andyearnshaw

    FYI @yairEO you can sort of do this in pure CSS now, but you need to provide container queries for a fixed number of lines. Here's an example:

    https://codepen.io/AndyE/pen/xxmeXKM

  6. SebastianZ commented on Oct 13, 2023

    @SebastianZ
    Contributor

    none is a valid value for block-ellipsis, so with this syntax there would be no way to represent a height-based clamping without ellipsis. That might be fine, though.

    Right, but for that case authors can still use the longhands. Alternatively, they could provide an empty string for the block-ellipsis to achieve the same effect.

    Sebastian

  7. changed the title [-][css-overflow-4] line-clamp "max-lines" - support auto value to fit container [/-] [+][css-overflow-4] line-clamp `max-lines` - support `auto` value to fit container [/+] on Jan 1, 2024
  8. added
    Async Resolution: Proposed
    on Jan 1, 2024
  9. SebastianZ commented on Jan 1, 2024

    @SebastianZ
    Contributor

    The syntax proposal for line-clamp I made earlier got some positive feedback. So it seems to be uncontroversial. So maybe we can resolve on that.

    Sebastian

  10. yairEO commented on Jan 2, 2024

    @yairEO
    Author

    I really hope this tiny easy change, with a lot of potential for the web, will be implemented ASAP

  11. SebastianZ commented on Jan 2, 2024

    @SebastianZ
    Contributor

    I really hope this tiny easy change, with a lot of potential for the web, will be implemented ASAP

    The syntax change to line-clamp is small, yes. And I also wish for it landing in browsers soon. Though to set expectations, implementors are very hesitant to add line-clamp and its longhands so far, because it seems generally hard to implement. From what I understand, this is mostly due to #7708.

    Sebastian

  12. astearns commented on Jan 3, 2024

    @astearns
    Member

    Given that this is just allowing in the shorthand something useful that’s already possible with the longhands, and the consensus in the discussion so far, I think this is reasonable to resolve asynchronously.

    The CSSWG will automatically accept this resolution one week from now if no objections are raised here. Anyone can add an emoji to this comment to express support. If you do not support this resolution, please add a new comment.

    Proposed Resolution: Change the line-clamp shorthand syntax to allow omitting the max-lines value

  13. added
    Async Resolution: Call For Consensus
    and removed
    Async Resolution: Proposed
    on Jan 3, 2024
  14. 10 remaining items

  15. cdoublev commented on Jan 23, 2024

    @cdoublev
  16. Loirooriol commented on Jan 23, 2024

    @Loirooriol
    Contributor

    Yes the grammar should use !

  17. frivoal commented on Jan 24, 2024

    @frivoal
    Collaborator

    I've always thought that there was an implicit ! on the whole value syntax definition of every property, so that it wasn't necessary to include in cases like this one. If not, I agree it should be added, but given that empty values are never valid, and based on "what else could it possibly mean", it seems we'd be better off adopting that convention if we haven't already.

    So, should I add a ! here, or should we clarify in css-values that it's implied in cases like that, or do we already have that somewhere?

    cc: @fantasai @tabatkins

  18. fantasai commented on Jan 24, 2024

    @fantasai
    Collaborator

    I think the correct answer here is to use ||. :) There's no reason for these two values to not be reorderable.

    (This side-steps your question about !... frankly it's never come up before.)

  19. cdoublev commented on Jan 24, 2024

    @cdoublev
    Collaborator

    The meaning of line-clamp: 1 "..." seems slightly easier to understand than line-clamp: "..." 1 but || would give the choice to the author, at least.

    It seems awkward to accept an empty value in a property value definition but reject it implicitly as a convention. And from an implementation perspective, things like @supports (line-clamp: ), line-clamp: !important, --custom: , require a little extra work.

  20. Loirooriol commented on Jan 24, 2024

    @Loirooriol
    Contributor

    @frivoal Empty values are valid on custom properties, since they have grammar <declaration-value>?

    CSS.supports("--a", ""); // true
  21. yairEO commented on Feb 14, 2024

    @yairEO
    Author

    This is kind of like the field-sizing: content property but in reverse,
    because the content's size is determined by the outside (ie. the "field" container).

    Just now it occurred to me. interesting similarities with completely different naming.

  22. frivoal commented on Feb 20, 2024

    @frivoal
    Collaborator

    Reopening so that I don't loose track of the fact I've got follow up edits to make

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    Sponsor
    SponsoredKunjungi sekarang
    Promo