简体   繁体   English

右浮动div元素未水平显示,请换行

[英]Right floated div element not show horizontally, break into new line

Dynamically added div element not show horizontally into a container div element which has a specific width value, always break into new line. 动态添加的div元素不会水平显示在具有特定宽度值的容器div元素中,始终会换行。

Here is my code 这是我的代码

 var i=1; $(document).ready(function () { $("#add").click(function(){ $('#container').append( $('<div/>') .addClass("dDiv") .text("(hello world "+i+")") ); i++; }); }) 
 #main{ float:right; width:500px; border: 1px solid; overflow:hidden; white-space: nowrap; display: block; } .dDiv{ display: inline-block; float:right; background-color:#ff0000; margin:2px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <div id="add">Add</div> <div id="chat" style="float:right"> <div id="main"> <div id="container" style="float:right;"> </div> </div> </div> 

在此处输入图片说明

Simply remove the float: right declaration for .dDiv . 只需删除.dDivfloat: right声明。 The trick is that all floated elements will automatically be declared as block level elements . 诀窍是所有浮动元素将自动声明为块级元素 If you want to force no line break (therefore an overflow), you will have to treat them as inline or inline-block level elements. 如果要不强制换行(因此会产生溢出),则必须将它们视为内联或内联块级元素。

To force .dDiv to honour its inline-block status, simply do not assign a float property to it. 要强制.dDiv遵守其内联块状态,只需不为其分配float属性。

 var i=1; $(document).ready(function () { $("#add").click(function(){ $('#container').prepend( $('<div/>') .addClass("dDiv") .text("(hello world "+i+")") ); i++; }); }); 
 #chat { float: right; } #main{ width:500px; border: 1px solid; overflow: hidden; } #container { overflow:hidden; white-space: nowrap; text-align: right; } .dDiv{ background-color:#ff0000; display: inline-block; margin:2px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <div id="add">Add</div> <div id="chat"> <div id="main"> <div id="container"> </div> </div> </div> 

Here are the changes you need to make: 这是您需要进行的更改:

#main {
  text-align: right;
  direction: rtl;
  /* white-space: no-wrap; /* take this out */
}

.dDiv {
   /* float: right /* take this out */
}

Results: 结果:

 var i=1; $(document).ready(function () { $("#add").click(function(){ $('#container').append( $('<div/>') .addClass("dDiv") .text("(hello world "+i+")") ); i++; }); }) 
 #main{ float:right; direction:rtl; text-align: right; width:500px; border: 1px solid; overflow:hidden; display: block; } .dDiv{ display: inline-block; background-color:#ff0000; margin:2px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <div id="add">Add</div> <div id="chat" style="float:right"> <div id="main"> <div id="container" style="float:right;"> </div> </div> </div> 

1) remove float:right 1)删除float:right

2) set direction:rtl on the #main div 2)在#main div上设置direction:rtl

3) You may want to set overflow:auto to the #main div as well is order to see the additional dDivs 3)您可能还想将overflow:auto设置为#main div,以便查看其他dDivs

 var i = 1; $(document).ready(function() { $("#add").click(function() { $('#container').append( $('<div/>') .addClass("dDiv") .text("(hello world " + i + ")") ); i++; }); }) 
 #main { width: 500px; border: 1px solid; overflow: hidden; white-space: nowrap; display: block; direction: rtl; } .dDiv { display: inline-block; background-color: #ff0000; margin: 2px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <div id="add">Add</div> <div id="chat" style="float:right"> <div id="main"> <div id="container" style="float:right;"> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM