Home  >  Article  >  Web Front-end  >  How to adapt to width and wrap automatically?_html/css_WEB-ITnose

How to adapt to width and wrap automatically?_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 12:08:591104browse

<style>	#list {		width:200px;	}	#left {		float:left;		border:1px solid blue;	}	#right {		float:right;		word-break: break-all;		word-wrap: break-word;		/* 		有一前提不能写死width属性		因为像#block那块的#right宽度很明显与上面的不一致了		*/				border:1px solid red;	}	#block {		margin-left:60px;	}	.c {		clear:both;	}	</style>	<div id="list">		<div>			<div id="left">aaa</div>			<div id="right">dddddddddddddddddddddddddddd</div>			<div class="c"></div>		</div>		<div id="block">			<div id="left">aaa</div>			<div id="right">dddddddddddddddddddddddddddd</div>			<div class="c"></div>		</div>	</div>


上面有没css的解决方案?
用js来适应的话过于复杂了,因为可能套很多层、加上如很多要处理的话,一大批操作ie会吃不消。


回复讨论(解决方案)

#list {
        width:200px;
    }
由于你最外层已经限定了宽度。。。

#list {
        width:200px;
    }
由于你最外层已经限定了宽度。。。
所以没办法把外层撑开只能word-break

这个是外面的没关系,

现在需要效果是里面的left 与right两块并列一行显示

#list {
        width:200px;
    }
由于你最外层已经限定了宽度。。。

引用 1 楼 zsx841021 的回复:

#list {
width:200px;
}
由于你最外层已经限定了宽度。。。

所以没办法把外层撑开只能word-break
它已经是自适应了。。

那需要left 与right两块并列一行显示,需要怎么写法?


引用 2 楼 zsx841021 的回复:

引用 1 楼 zsx841021 的回复:

#list {
width:200px;
}
由于你最外层已经限定了宽度。。。

所以没办法把外层撑开只能word-break

它已经是自适应了。。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>    <title></title>    <style>                #right,#left        {            float: left;            border: 1px solid blue;            word-break: break-all;            word-wrap: break-word;        }       #right        {                        border: 1px solid red;        }        #block        {            margin-left: 60px;        }        .c        {            clear: both;        }    </style></head><body>    <div id="list">        <div>            <div id="left">                aaa</div>            <div id="right">                dddddddddddddddddddd</div>            <div class="c">            </div>        </div>        <div id="block">            <div id="left">                aaa</div>            <div id="right">                dddddddddddddddddddddddddddd</div>            <div class="c">            </div>        </div>    </div></body></html>

是这样的效果吗?。。最外层没有规定宽度。。

不是这样子呐~



如果没外层限制

根本不需要用这个了
word-break: break-all;
word-wrap: break-word;

也就没有需要自动换行之说了

HTML code
b585974ae3b7dba3039af53a9593f9c4
383eb734b02b508089ba2d78eb4c6f68
93f0f5c25f18dab9d176bd4f6de5d30e
    b2386ffb911b14667cb8f0f91ea547a7

不是这样子呐~



如果没外层限制

根本不需要用这个了
word-break: break-all;
word-wrap: break-word;

也就没有需要自动换行之说了

引用 6 楼 zsx841021 的回复:

HTML code
如果限制最外层宽度。。。里层在超过的时候会被自动换行的。。要不然就用JS。。外层的宽度根据里层的宽度再改变。。但总觉得还是不行。。这样的话就等于没有设置外层宽度了。。

这个是外面的没关系,

现在需要效果是里面的left 与right两块并列一行显示

引用 1 楼 zsx841021 的回复:

#list {
width:200px;
}
由于你最外层已经限定了宽度。。。
这个你给最外层加个背景色就看出来了。。。

b585974ae3b7dba3039af53a9593f9c4
383eb734b02b508089ba2d78eb4c6f68
93f0f5c25f18dab9d176bd4f6de5d30e
0f3eabac747cdf61a51589c5584ae6e8
b2386ffb911b14667cb8f0f91ea547a7无标题文档6e916e0f7d1e588d4f442bf645aedb2f
c9ccee2e6ea535a969eb3f532ad9fe89
.main{width:200px; font:12px/24px Arial, Helvetica, sans-serif; float:left; display:inline; margin:20px; background:#ccc; word-wrap: break-word; white-space: pre-wrap;}
531ac245ce3e4fe3d50054a55f265927
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
a1151789e2e3a08efc84d2d79aaf73bddsafsdfasdf16b28748ea4df4d9c2150843fecfba68
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

This problem can be solved with js, but there are many problems, because my actual application may have many layers.


It would be great if it could be solved using css. Writing it like this #right width:100px;
can achieve this effect. But it can’t be written to death.



Quoting the reply from ci1699 on the 7th floor:

That’s not the case~



If there are no outer restrictions

There is no need to use this at all
word-break: break-all;
word-wrap: break-word;

There is no need for automatic line wrapping

Quoting the reply from zsx841021 on the 6th floor:

HTML code
5a8f7468922cdda1aebe7282f0dbe8bc
68ccb177a5de0ef9542dde7d35bae727
93f0f5c25f18dab9d176bd4f6de5d30e
I haven’t seen many big websites that are adaptive

This problem has been perfectly solved.

The poster’s avatar is so cute. I hope the poster can reveal the solution and learn from it...

Optimized the structure. Without layering, the problem becomes simple.
You can also use js, and if you process it publicly, IE will not be stuck.

Posted

The poster’s avatar is so cute. I hope the poster can reveal the solution and learn from it...

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn