Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

resize - Inline-block line-wrap extra space

I've got an inline-block element that contains a very long word. When I resize the viewport until I reach the breakpoint of the text wrapping to the next line, I get a substantial amount of space. However, I would like the inline-block element to wrap immediately to the width of its contents.

I found it hard to explain exactly what's going on, so below an animated gif to illustrate my issue:

Upon resizing the viewport:

animated gif for illustration purposes

To be clear, the image above is me continuously resizing the viewport.

Does anybody know a way to achieve what I'd like? Even with CSS hyphenation the white-space still remains (which I don't want).

JSFiddle. Resize the frames to see what I mean.

div {
    display: inline-block;
    background-color: black;
    color: white;
    padding: 5px;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The inline-block indeed extends on resizing as your animation shows, so that it keeps place for the long word to go into that space again.

One simple solution would be to add text-align: justify, but I'm afraid it may not exactly be what you want (see demo).

Another one would be the use of media queries, as @Parody suggested, but you would have to know the dimentions of the containing div, and that would not be very scalable as you mentionned.

The word-break: break-all suggested by @yugi also works but causes the words to to collapse letter by letter, regardless of their length.

The only way to achieve the exact behavior is (as far as I know) to use javascript. For example, you would have to wrap your text into a span element inside the div, and then add something like this :

var paddingLeft = parseInt($('#foo').css('padding-left')),
    paddingRight = parseInt($('#foo').css('padding-left')),
    paddingTop = parseInt($('#foo').css('padding-top')),
    paddingBottom = parseInt($('#foo').css('padding-Bottom')),
    cloned = $('#foo span').clone(),
    cloned_wrap = document.createElement('div');

$(cloned_wrap).css({
    paddingLeft : paddingLeft,
    paddingRight : paddingRight,
    display : 'inline-block',
    visibility: 'hidden',
    float: 'left',
});

$(cloned_wrap).insertAfter('#foo');
cloned.appendTo(cloned_wrap);

$(window).on('resize', function(){
    $('#foo').css('width', cloned.width() + 1);
    $(cloned_wrap).css('margin-top',- $('#foo').height() - paddingTop - paddingBottom);
}).resize();

Please see the jsfiddle working demo. (← edited many times)

That's quite a lot of code, but it works ; )

(PS : I assumed jquery was available, if not, quite the same is achievable in pure JS)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...