force line break in html table cell











up vote
19
down vote

favorite
5












I'm trying to find a way to force line break in table cell after text inside of it will become longer than say 50% of max allowed size.



How can I do it without any JS function, using just pure HTML with CSS?










share|improve this question
























  • @Phil its onebiglonglineoftext so ideas given here should be enough
    – dantuch
    Jul 27 '11 at 11:30










  • it will not work if it is only one single word (see jsfiddle.net/NweWX)
    – Fender
    Jul 27 '11 at 11:34






  • 2




    @Fender, thanks... - it works fine with added something like <td style="word-wrap: break-word" width="100">
    – dantuch
    Jul 27 '11 at 12:33










  • @dantuch can it work on IE 7+ ?
    – Salman Mushtaq
    Feb 20 '17 at 6:51















up vote
19
down vote

favorite
5












I'm trying to find a way to force line break in table cell after text inside of it will become longer than say 50% of max allowed size.



How can I do it without any JS function, using just pure HTML with CSS?










share|improve this question
























  • @Phil its onebiglonglineoftext so ideas given here should be enough
    – dantuch
    Jul 27 '11 at 11:30










  • it will not work if it is only one single word (see jsfiddle.net/NweWX)
    – Fender
    Jul 27 '11 at 11:34






  • 2




    @Fender, thanks... - it works fine with added something like <td style="word-wrap: break-word" width="100">
    – dantuch
    Jul 27 '11 at 12:33










  • @dantuch can it work on IE 7+ ?
    – Salman Mushtaq
    Feb 20 '17 at 6:51













up vote
19
down vote

favorite
5









up vote
19
down vote

favorite
5






5





I'm trying to find a way to force line break in table cell after text inside of it will become longer than say 50% of max allowed size.



How can I do it without any JS function, using just pure HTML with CSS?










share|improve this question















I'm trying to find a way to force line break in table cell after text inside of it will become longer than say 50% of max allowed size.



How can I do it without any JS function, using just pure HTML with CSS?







html css






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '13 at 0:06









Simon East

32k11101100




32k11101100










asked Jul 27 '11 at 11:15









dantuch

7,12843463




7,12843463












  • @Phil its onebiglonglineoftext so ideas given here should be enough
    – dantuch
    Jul 27 '11 at 11:30










  • it will not work if it is only one single word (see jsfiddle.net/NweWX)
    – Fender
    Jul 27 '11 at 11:34






  • 2




    @Fender, thanks... - it works fine with added something like <td style="word-wrap: break-word" width="100">
    – dantuch
    Jul 27 '11 at 12:33










  • @dantuch can it work on IE 7+ ?
    – Salman Mushtaq
    Feb 20 '17 at 6:51


















  • @Phil its onebiglonglineoftext so ideas given here should be enough
    – dantuch
    Jul 27 '11 at 11:30










  • it will not work if it is only one single word (see jsfiddle.net/NweWX)
    – Fender
    Jul 27 '11 at 11:34






  • 2




    @Fender, thanks... - it works fine with added something like <td style="word-wrap: break-word" width="100">
    – dantuch
    Jul 27 '11 at 12:33










  • @dantuch can it work on IE 7+ ?
    – Salman Mushtaq
    Feb 20 '17 at 6:51
















@Phil its onebiglonglineoftext so ideas given here should be enough
– dantuch
Jul 27 '11 at 11:30




@Phil its onebiglonglineoftext so ideas given here should be enough
– dantuch
Jul 27 '11 at 11:30












it will not work if it is only one single word (see jsfiddle.net/NweWX)
– Fender
Jul 27 '11 at 11:34




it will not work if it is only one single word (see jsfiddle.net/NweWX)
– Fender
Jul 27 '11 at 11:34




2




2




@Fender, thanks... - it works fine with added something like <td style="word-wrap: break-word" width="100">
– dantuch
Jul 27 '11 at 12:33




@Fender, thanks... - it works fine with added something like <td style="word-wrap: break-word" width="100">
– dantuch
Jul 27 '11 at 12:33












@dantuch can it work on IE 7+ ?
– Salman Mushtaq
Feb 20 '17 at 6:51




@dantuch can it work on IE 7+ ?
– Salman Mushtaq
Feb 20 '17 at 6:51












5 Answers
5






active

oldest

votes

















up vote
34
down vote



accepted










I think what you're trying to do is wrap loooooooooooooong words or URLs so they don't push the size of the table out. (I've just been trying to do the same thing!)



You can do this easily with a DIV by giving it the style word-wrap: break-word (and you may need to set its width, too).



div {
word-wrap: break-word; /* All browsers since IE 5.5+ */
overflow-wrap: break-word; /* Renamed property in CSS3 draft spec */
width: 100%;
}


However, for tables, you must either wrap the content in a DIV (or other block tag) or apply: table-layout: fixed. This means the columns widths are no longer fluid, but are defined based on the widths of the columns in the first row only (or via specified widths). Read more here.



Sample code:



table {
table-layout: fixed;
width: 100%;
}

table td {
word-wrap: break-word; /* All browsers since IE 5.5+ */
overflow-wrap: break-word; /* Renamed property in CSS3 draft spec */
}


Hope that helps somebody.






share|improve this answer



















  • 1




    I nested a long word inside a nested table using a div styled with break-word. It did not require table-layout:fixed style. Thanks for a solution that works in IE, Chrome and FF : )
    – davidjmcclelland
    Dec 2 '13 at 17:26












  • Hi David - yes if your content is inside a DIV (which is inside a table) then the first snippet is adequate.
    – Simon East
    Dec 2 '13 at 21:59










  • thanks, changed accepted to this1
    – dantuch
    Dec 20 '16 at 13:38


















up vote
10
down vote













I suggest you use a wrapper div or paragraph:



<td><p style="width:50%;">Text only allowed to extend 50% of the cell.</p></td>


And you can make a class out of it:



<td class="linebreak"><p>Text only allowed to extend 50% of the cell.</p></td>

td.linebreak p {
width: 50%;
}


All of this assuming that you meant 50% as in 50% of the cell.






share|improve this answer

















  • 2




    Ah, sorry for late update, but as @Fender commented, it do not help, if I want to break single,tolonglineoftext. And that's the real issue.
    – dantuch
    Jul 27 '11 at 11:45










  • How to center this text?
    – zygimantus
    Jan 19 '16 at 7:39


















up vote
3
down vote













You could put the text into a div (or other container) with a width of 50%.



http://jsfiddle.net/6gjsd/






share|improve this answer




























    up vote
    1
    down vote













    It's hard to answer you without the HTML, but in general you can put:



    style="width: 50%;"


    On either the table cell, or place a div inside the table cell, and put the style on that.



    But one problem is "50% of what?" It's 50% of the parent element which may not be what you want.



    Post a copy of your HTML and maybe you'll get a better answer.






    share|improve this answer




























      up vote
      0
      down vote













      Try using



      <table  border="1" cellspacing="0" cellpadding="0" class="template-table" 
      style="table-layout: fixed; width: 100%">


      as table style along with



      <td style="word-break:break-word">long text</td>


      for td
      it works for normal/real scenario text with words, not for random typed letters without gaps






      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














         

        draft saved


        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6843412%2fforce-line-break-in-html-table-cell%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        34
        down vote



        accepted










        I think what you're trying to do is wrap loooooooooooooong words or URLs so they don't push the size of the table out. (I've just been trying to do the same thing!)



        You can do this easily with a DIV by giving it the style word-wrap: break-word (and you may need to set its width, too).



        div {
        word-wrap: break-word; /* All browsers since IE 5.5+ */
        overflow-wrap: break-word; /* Renamed property in CSS3 draft spec */
        width: 100%;
        }


        However, for tables, you must either wrap the content in a DIV (or other block tag) or apply: table-layout: fixed. This means the columns widths are no longer fluid, but are defined based on the widths of the columns in the first row only (or via specified widths). Read more here.



        Sample code:



        table {
        table-layout: fixed;
        width: 100%;
        }

        table td {
        word-wrap: break-word; /* All browsers since IE 5.5+ */
        overflow-wrap: break-word; /* Renamed property in CSS3 draft spec */
        }


        Hope that helps somebody.






        share|improve this answer



















        • 1




          I nested a long word inside a nested table using a div styled with break-word. It did not require table-layout:fixed style. Thanks for a solution that works in IE, Chrome and FF : )
          – davidjmcclelland
          Dec 2 '13 at 17:26












        • Hi David - yes if your content is inside a DIV (which is inside a table) then the first snippet is adequate.
          – Simon East
          Dec 2 '13 at 21:59










        • thanks, changed accepted to this1
          – dantuch
          Dec 20 '16 at 13:38















        up vote
        34
        down vote



        accepted










        I think what you're trying to do is wrap loooooooooooooong words or URLs so they don't push the size of the table out. (I've just been trying to do the same thing!)



        You can do this easily with a DIV by giving it the style word-wrap: break-word (and you may need to set its width, too).



        div {
        word-wrap: break-word; /* All browsers since IE 5.5+ */
        overflow-wrap: break-word; /* Renamed property in CSS3 draft spec */
        width: 100%;
        }


        However, for tables, you must either wrap the content in a DIV (or other block tag) or apply: table-layout: fixed. This means the columns widths are no longer fluid, but are defined based on the widths of the columns in the first row only (or via specified widths). Read more here.



        Sample code:



        table {
        table-layout: fixed;
        width: 100%;
        }

        table td {
        word-wrap: break-word; /* All browsers since IE 5.5+ */
        overflow-wrap: break-word; /* Renamed property in CSS3 draft spec */
        }


        Hope that helps somebody.






        share|improve this answer



















        • 1




          I nested a long word inside a nested table using a div styled with break-word. It did not require table-layout:fixed style. Thanks for a solution that works in IE, Chrome and FF : )
          – davidjmcclelland
          Dec 2 '13 at 17:26












        • Hi David - yes if your content is inside a DIV (which is inside a table) then the first snippet is adequate.
          – Simon East
          Dec 2 '13 at 21:59










        • thanks, changed accepted to this1
          – dantuch
          Dec 20 '16 at 13:38













        up vote
        34
        down vote



        accepted







        up vote
        34
        down vote



        accepted






        I think what you're trying to do is wrap loooooooooooooong words or URLs so they don't push the size of the table out. (I've just been trying to do the same thing!)



        You can do this easily with a DIV by giving it the style word-wrap: break-word (and you may need to set its width, too).



        div {
        word-wrap: break-word; /* All browsers since IE 5.5+ */
        overflow-wrap: break-word; /* Renamed property in CSS3 draft spec */
        width: 100%;
        }


        However, for tables, you must either wrap the content in a DIV (or other block tag) or apply: table-layout: fixed. This means the columns widths are no longer fluid, but are defined based on the widths of the columns in the first row only (or via specified widths). Read more here.



        Sample code:



        table {
        table-layout: fixed;
        width: 100%;
        }

        table td {
        word-wrap: break-word; /* All browsers since IE 5.5+ */
        overflow-wrap: break-word; /* Renamed property in CSS3 draft spec */
        }


        Hope that helps somebody.






        share|improve this answer














        I think what you're trying to do is wrap loooooooooooooong words or URLs so they don't push the size of the table out. (I've just been trying to do the same thing!)



        You can do this easily with a DIV by giving it the style word-wrap: break-word (and you may need to set its width, too).



        div {
        word-wrap: break-word; /* All browsers since IE 5.5+ */
        overflow-wrap: break-word; /* Renamed property in CSS3 draft spec */
        width: 100%;
        }


        However, for tables, you must either wrap the content in a DIV (or other block tag) or apply: table-layout: fixed. This means the columns widths are no longer fluid, but are defined based on the widths of the columns in the first row only (or via specified widths). Read more here.



        Sample code:



        table {
        table-layout: fixed;
        width: 100%;
        }

        table td {
        word-wrap: break-word; /* All browsers since IE 5.5+ */
        overflow-wrap: break-word; /* Renamed property in CSS3 draft spec */
        }


        Hope that helps somebody.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 2 '13 at 22:03

























        answered Nov 26 '13 at 0:19









        Simon East

        32k11101100




        32k11101100








        • 1




          I nested a long word inside a nested table using a div styled with break-word. It did not require table-layout:fixed style. Thanks for a solution that works in IE, Chrome and FF : )
          – davidjmcclelland
          Dec 2 '13 at 17:26












        • Hi David - yes if your content is inside a DIV (which is inside a table) then the first snippet is adequate.
          – Simon East
          Dec 2 '13 at 21:59










        • thanks, changed accepted to this1
          – dantuch
          Dec 20 '16 at 13:38














        • 1




          I nested a long word inside a nested table using a div styled with break-word. It did not require table-layout:fixed style. Thanks for a solution that works in IE, Chrome and FF : )
          – davidjmcclelland
          Dec 2 '13 at 17:26












        • Hi David - yes if your content is inside a DIV (which is inside a table) then the first snippet is adequate.
          – Simon East
          Dec 2 '13 at 21:59










        • thanks, changed accepted to this1
          – dantuch
          Dec 20 '16 at 13:38








        1




        1




        I nested a long word inside a nested table using a div styled with break-word. It did not require table-layout:fixed style. Thanks for a solution that works in IE, Chrome and FF : )
        – davidjmcclelland
        Dec 2 '13 at 17:26






        I nested a long word inside a nested table using a div styled with break-word. It did not require table-layout:fixed style. Thanks for a solution that works in IE, Chrome and FF : )
        – davidjmcclelland
        Dec 2 '13 at 17:26














        Hi David - yes if your content is inside a DIV (which is inside a table) then the first snippet is adequate.
        – Simon East
        Dec 2 '13 at 21:59




        Hi David - yes if your content is inside a DIV (which is inside a table) then the first snippet is adequate.
        – Simon East
        Dec 2 '13 at 21:59












        thanks, changed accepted to this1
        – dantuch
        Dec 20 '16 at 13:38




        thanks, changed accepted to this1
        – dantuch
        Dec 20 '16 at 13:38












        up vote
        10
        down vote













        I suggest you use a wrapper div or paragraph:



        <td><p style="width:50%;">Text only allowed to extend 50% of the cell.</p></td>


        And you can make a class out of it:



        <td class="linebreak"><p>Text only allowed to extend 50% of the cell.</p></td>

        td.linebreak p {
        width: 50%;
        }


        All of this assuming that you meant 50% as in 50% of the cell.






        share|improve this answer

















        • 2




          Ah, sorry for late update, but as @Fender commented, it do not help, if I want to break single,tolonglineoftext. And that's the real issue.
          – dantuch
          Jul 27 '11 at 11:45










        • How to center this text?
          – zygimantus
          Jan 19 '16 at 7:39















        up vote
        10
        down vote













        I suggest you use a wrapper div or paragraph:



        <td><p style="width:50%;">Text only allowed to extend 50% of the cell.</p></td>


        And you can make a class out of it:



        <td class="linebreak"><p>Text only allowed to extend 50% of the cell.</p></td>

        td.linebreak p {
        width: 50%;
        }


        All of this assuming that you meant 50% as in 50% of the cell.






        share|improve this answer

















        • 2




          Ah, sorry for late update, but as @Fender commented, it do not help, if I want to break single,tolonglineoftext. And that's the real issue.
          – dantuch
          Jul 27 '11 at 11:45










        • How to center this text?
          – zygimantus
          Jan 19 '16 at 7:39













        up vote
        10
        down vote










        up vote
        10
        down vote









        I suggest you use a wrapper div or paragraph:



        <td><p style="width:50%;">Text only allowed to extend 50% of the cell.</p></td>


        And you can make a class out of it:



        <td class="linebreak"><p>Text only allowed to extend 50% of the cell.</p></td>

        td.linebreak p {
        width: 50%;
        }


        All of this assuming that you meant 50% as in 50% of the cell.






        share|improve this answer












        I suggest you use a wrapper div or paragraph:



        <td><p style="width:50%;">Text only allowed to extend 50% of the cell.</p></td>


        And you can make a class out of it:



        <td class="linebreak"><p>Text only allowed to extend 50% of the cell.</p></td>

        td.linebreak p {
        width: 50%;
        }


        All of this assuming that you meant 50% as in 50% of the cell.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 27 '11 at 11:18









        orlp

        66.1k26156245




        66.1k26156245








        • 2




          Ah, sorry for late update, but as @Fender commented, it do not help, if I want to break single,tolonglineoftext. And that's the real issue.
          – dantuch
          Jul 27 '11 at 11:45










        • How to center this text?
          – zygimantus
          Jan 19 '16 at 7:39














        • 2




          Ah, sorry for late update, but as @Fender commented, it do not help, if I want to break single,tolonglineoftext. And that's the real issue.
          – dantuch
          Jul 27 '11 at 11:45










        • How to center this text?
          – zygimantus
          Jan 19 '16 at 7:39








        2




        2




        Ah, sorry for late update, but as @Fender commented, it do not help, if I want to break single,tolonglineoftext. And that's the real issue.
        – dantuch
        Jul 27 '11 at 11:45




        Ah, sorry for late update, but as @Fender commented, it do not help, if I want to break single,tolonglineoftext. And that's the real issue.
        – dantuch
        Jul 27 '11 at 11:45












        How to center this text?
        – zygimantus
        Jan 19 '16 at 7:39




        How to center this text?
        – zygimantus
        Jan 19 '16 at 7:39










        up vote
        3
        down vote













        You could put the text into a div (or other container) with a width of 50%.



        http://jsfiddle.net/6gjsd/






        share|improve this answer

























          up vote
          3
          down vote













          You could put the text into a div (or other container) with a width of 50%.



          http://jsfiddle.net/6gjsd/






          share|improve this answer























            up vote
            3
            down vote










            up vote
            3
            down vote









            You could put the text into a div (or other container) with a width of 50%.



            http://jsfiddle.net/6gjsd/






            share|improve this answer












            You could put the text into a div (or other container) with a width of 50%.



            http://jsfiddle.net/6gjsd/







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jul 27 '11 at 11:19









            Jamie Dixon

            36.9k13103144




            36.9k13103144






















                up vote
                1
                down vote













                It's hard to answer you without the HTML, but in general you can put:



                style="width: 50%;"


                On either the table cell, or place a div inside the table cell, and put the style on that.



                But one problem is "50% of what?" It's 50% of the parent element which may not be what you want.



                Post a copy of your HTML and maybe you'll get a better answer.






                share|improve this answer

























                  up vote
                  1
                  down vote













                  It's hard to answer you without the HTML, but in general you can put:



                  style="width: 50%;"


                  On either the table cell, or place a div inside the table cell, and put the style on that.



                  But one problem is "50% of what?" It's 50% of the parent element which may not be what you want.



                  Post a copy of your HTML and maybe you'll get a better answer.






                  share|improve this answer























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    It's hard to answer you without the HTML, but in general you can put:



                    style="width: 50%;"


                    On either the table cell, or place a div inside the table cell, and put the style on that.



                    But one problem is "50% of what?" It's 50% of the parent element which may not be what you want.



                    Post a copy of your HTML and maybe you'll get a better answer.






                    share|improve this answer












                    It's hard to answer you without the HTML, but in general you can put:



                    style="width: 50%;"


                    On either the table cell, or place a div inside the table cell, and put the style on that.



                    But one problem is "50% of what?" It's 50% of the parent element which may not be what you want.



                    Post a copy of your HTML and maybe you'll get a better answer.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jul 27 '11 at 11:19









                    Ariel

                    19.9k34264




                    19.9k34264






















                        up vote
                        0
                        down vote













                        Try using



                        <table  border="1" cellspacing="0" cellpadding="0" class="template-table" 
                        style="table-layout: fixed; width: 100%">


                        as table style along with



                        <td style="word-break:break-word">long text</td>


                        for td
                        it works for normal/real scenario text with words, not for random typed letters without gaps






                        share|improve this answer



























                          up vote
                          0
                          down vote













                          Try using



                          <table  border="1" cellspacing="0" cellpadding="0" class="template-table" 
                          style="table-layout: fixed; width: 100%">


                          as table style along with



                          <td style="word-break:break-word">long text</td>


                          for td
                          it works for normal/real scenario text with words, not for random typed letters without gaps






                          share|improve this answer

























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            Try using



                            <table  border="1" cellspacing="0" cellpadding="0" class="template-table" 
                            style="table-layout: fixed; width: 100%">


                            as table style along with



                            <td style="word-break:break-word">long text</td>


                            for td
                            it works for normal/real scenario text with words, not for random typed letters without gaps






                            share|improve this answer














                            Try using



                            <table  border="1" cellspacing="0" cellpadding="0" class="template-table" 
                            style="table-layout: fixed; width: 100%">


                            as table style along with



                            <td style="word-break:break-word">long text</td>


                            for td
                            it works for normal/real scenario text with words, not for random typed letters without gaps







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 8 at 11:05

























                            answered Nov 8 at 11:00









                            Vee_Sa

                            12




                            12






























                                 

                                draft saved


                                draft discarded



















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6843412%2fforce-line-break-in-html-table-cell%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Schultheiß

                                Verwaltungsgliederung Dänemarks

                                Liste der Kulturdenkmale in Wilsdruff