css實現自動換行以防撐破div影響排版

來源:互聯網
上載者:User
自動換行問題,正常字元的換行是比較合理的,而連續的數字和英文字元常常將容器撐大,挺讓人頭疼,下面介紹的是CSS如何?換行的方法

對於p,p等區塊層級元素
正常文字的換行(亞洲文字和非亞洲文字)元素擁有預設的white-space:normal,當定義的寬度之後自動換行
html

< id="wrap">正常文字的換行(亞洲文字和非亞洲文字)元素擁有預設的white-space:normal,當定義< /p >
css
#wrap{white-space:normal; width:200px; }
1.(IE瀏覽器)連續的英文字元和阿拉伯數字,使用word-wrap : break-word ;或者word-break:break-all;實現強制斷行

#wrap{word-break:break-all; width:200px;}
或者
#wrap{word-wrap:break-word; width:200px;}

< id="wrap">abcdefghijklmnabcdefghijklmnabcdefghijklmn111111111< /p >
效果:可以實現換行

2.(Firefox瀏覽器)連續的英文字元和阿拉伯數位斷行,Firefox的所有版本的沒有解決這個問題,我們只有讓超出邊界的字元隱藏或者,給容器添加捲軸

#wrap{word-break:break-all; width:200px; overflow:auto;}

< id="wrap">abcdefghijklmnabcdefghijklmnabcdefghijklmn111111111< /p >
效果:容器正常,內容隱藏

對於table

1. (IE瀏覽器)使用 table-layout:fixed;強制table的寬度,多餘內容隱藏

< style="table-layout:fixed" width="200"><><>abcdefghigklmnopqrstuvwxyz1234567890ssssssssssssss< /td >< /tr >< /table >
效果:隱藏多餘內容

2.(IE瀏覽器)使用 table-layout:fixed;強制table的寬度,內層td,th採用word-break : break-all;或者word-wrap : break-word ;換行

< width="200" style="table-layout:fixed;"><>< width="25%" style="word-break : break-all; ">abcdefghigklmnopqrstuvwxyz 1234567890< /td >< style="word-wrap : break-word ;">abcdefghigklmnopqrstuvwxyz 1234567890< /td >< /tr >< /table >
效果:可以換行

3. (IE瀏覽器)在td,th中嵌套p,p等採用上面提到的p,p的換行方法

4.(Firefox瀏覽器)使用 table-layout:fixed;強制table的寬度,內層td,th採用word-break : break-all;或者word-wrap : break-word ;換行,使用overflow:hidden;隱藏超出內容,這裡overflow:auto;無法起作用

< style="table-layout:fixed" width="200"><>< width="25%" style="word-break : break-all; overflow:hidden; ">abcdefghigklmnopqrstuvwxyz1234567890< /td >< width="75%" style="word-wrap : break-word; overflow:hidden; ">abcdefghigklmnopqrstuvwxyz1234567890< /td >< /tr >< /table >

效果:隱藏多於內容

5.(Firefox瀏覽器) 在td,th中嵌套p,p等採用上面提到的對付Firefox的方法

以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.