Make content force DIV grow vertically until max-height is reached, and then grow horizontally

Andrei

I have a div with some text. This div has min-height, min-width, max-height and max-width. I want to prioritize height in wrapping text, that means until there is an ability to stretch the div vertically (height < max-height), the text should be wrapped evenly to fill the height. Then if max-height is reached, the wrapping should be arranged in the way to fill width until max-width allows to do that. If both max-height and max-width is reached, text should be wrapped as usual.

Unfortunatelly, my text is not wrapped while width < max-width, and I can't find a way to make it be wrapped by height.

Please someone, give me an advice.

Thanks.

chimos

I think it does moreless what you need, using javascript/jQuery:

$(document).ready(function() {

    $('div.weird').each(function(){ 

        var el = $(this);
        var dW = +$(el).css('width').replace(/[^-\d\.]/g, '');
        var maxW = +$(el).css('max-width').replace(/[^-\d\.]/g, '');
        var maxH = +$(el).css('max-height').replace(/[^-\d\.]/g, '');

        var i;
        for (i=0; i+dW < maxW; i++) {
            if ( this.offsetHeight < this.scrollHeight ) {
                dW = dW+i;
                $(el).css({'width': dW+'px', 'height': maxH+'px'});
            }
        }

    });

});

CSS:

.weird {
    background: #eee; /*not needed*/
    width: 200px;
    min-width: 200px;
    min-height: 200px;
    max-height: 400px;
    max-width: 400px;
    overflow: auto;
    float: left; /*not needed*/
}

For this approach is needed to start defining a fixed width for the DIV, then the width incresses pregressively until there's no overflow or until reaches the max-width.

In this example you can see two DIVs with the same properties but containing different ammounts of text: https://jsfiddle.net/chimos/83ra9ysh/25/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

make button grow vertically to fit content, not horizontally

From Dev

div doesn't grow in height with content

From Dev

Full height flexbox, to grow with content

From Dev

div doesnt grow with dynamic content

From Dev

Make a div grow as wide as possible

From Dev

Make a div grow as wide as possible

From Dev

Force a div to grow wider than the browser window

From Dev

Grow div height to show all contents with javascript

From Dev

Grow div height depending on its neighbour

From Dev

Canvas makes height of parent DIV grow, why?

From Dev

Grow div height to show all contents with javascript

From Dev

Min height and div growing as inner divs grow

From Dev

how to make div grow right instead of down

From Dev

Make a div grow in all directions with CSS?

From Dev

How do I get a div to grow horizontally off the screen

From Dev

How to make inline-block elements to grow vertically

From Dev

How to vertically and horizontally center a div of unknown height

From Dev

Center horizontally and vertically text in relative height div

From Dev

Empty div wont grow

From Dev

How do I make a wrapped flexbox's content grow equally?

From Dev

How could prevent grow width of flex div by it's content?

From Dev

How could prevent grow width of flex div by it's content?

From Dev

center div content vertically and horizontally not work

From Dev

Centering div horizontally and vertically without distorting content

From Dev

TextView grow height automatically android

From Dev

Make a css triangle/arrow grow when its parent div is resized

From Dev

How to make a div overflow vertically instead of horizontally?

From Dev

Vertically center inside a `flex-grow: 1`

From Dev

Bootstrap 3 - Make a navbar's links grow vertically with the size of a brand image

Related Related

  1. 1

    make button grow vertically to fit content, not horizontally

  2. 2

    div doesn't grow in height with content

  3. 3

    Full height flexbox, to grow with content

  4. 4

    div doesnt grow with dynamic content

  5. 5

    Make a div grow as wide as possible

  6. 6

    Make a div grow as wide as possible

  7. 7

    Force a div to grow wider than the browser window

  8. 8

    Grow div height to show all contents with javascript

  9. 9

    Grow div height depending on its neighbour

  10. 10

    Canvas makes height of parent DIV grow, why?

  11. 11

    Grow div height to show all contents with javascript

  12. 12

    Min height and div growing as inner divs grow

  13. 13

    how to make div grow right instead of down

  14. 14

    Make a div grow in all directions with CSS?

  15. 15

    How do I get a div to grow horizontally off the screen

  16. 16

    How to make inline-block elements to grow vertically

  17. 17

    How to vertically and horizontally center a div of unknown height

  18. 18

    Center horizontally and vertically text in relative height div

  19. 19

    Empty div wont grow

  20. 20

    How do I make a wrapped flexbox's content grow equally?

  21. 21

    How could prevent grow width of flex div by it's content?

  22. 22

    How could prevent grow width of flex div by it's content?

  23. 23

    center div content vertically and horizontally not work

  24. 24

    Centering div horizontally and vertically without distorting content

  25. 25

    TextView grow height automatically android

  26. 26

    Make a css triangle/arrow grow when its parent div is resized

  27. 27

    How to make a div overflow vertically instead of horizontally?

  28. 28

    Vertically center inside a `flex-grow: 1`

  29. 29

    Bootstrap 3 - Make a navbar's links grow vertically with the size of a brand image

HotTag

Archive