2016-08-23 49 views
-1

我需要把我的跨度放在一个div内,但是因为div的内容更多,它溢出了父div的边界。 如何解决它? 我的外部div应该是灵活的,因为跨度中的内容是动态的。 这里是plunker链路= DEMO如何把溢出跨度在一个div内

.outerDiv { 
 
    width: 100%; 
 
    border: 2px solid black; 
 
} 
 
.div40 { 
 
    width: 40%; 
 
} 
 
.div60 { 
 
    width: 60%; 
 
    float: right; 
 
}
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <link rel="stylesheet" href="style.css"> 
 
    <script src="script.js"></script> 
 
</head> 
 

 
<body> 
 
    <h1>Hello Plunker!</h1> 
 
    <div class="outerDiv"> 
 
    <span class="div40"> 
 
     hello 
 
    </span> 
 
    <span class="div60"> 
 
     hello i am trying to insert this content inside the parent div,but i am not able to do so. My content should be inside the border of this div and i want the height not to be fixed as my content is dynamic. 
 
    </span> 
 
    </div> 
 
</body> 
 

 
</html>

+0

您已经定义了'浮动:right'你'div60'即为什么这个问题正在发生,你需要的是清除'float'为:'.outerDiv ::后,.outerDiv ::前{content:“”;显示:表格; } .outerDiv :: after {clear:both; }' – vivekkupadhyay

+0

在你的outerDiv中添加'overflow:auto;'。 – KunJ

+0

@KunJ首先,您的“尝试”解决方案不适合此目的。其次,这里提供的解决方案需要简洁,明确和有用,而不是“尝试”的东西。 –

回答

0

设置outerDiv到display: inline-block

/* Styles go here */ 
 

 
.outerDiv 
 
{ 
 
    width:100%; 
 
    border:2px solid black; 
 
    display: inline-block; 
 
} 
 
.div40 
 
{ 
 
    width:40%; 
 
} 
 
.div60 
 
{ 
 
    width:60%; 
 
    float:right; 
 
}
<h1>Hello Plunker!</h1> 
 
    <div class ="outerDiv"> 
 
     <span class = "div40"> 
 
     hello 
 
     </span> 
 
     <span class = "div60"> 
 
     hello i am trying to insert this content inside the parent div,but i am not able to do so. My content should be inside the border of this div and i want the height not to be fixed as my content is dynamic. 
 
     </span> 
 
    </div>

0

通过添加显示直列块到外层div类可以实现这一目标。

/* Styles go here */ 
 

 
.outerDiv 
 
{ 
 
    width:100%; 
 
    border:2px solid black; 
 
    display:inline-block; 
 
} 
 
.div40 
 
{ 
 
    width:40%; 
 
} 
 
.div60 
 
{ 
 
    width:60%; 
 
    float:right; 
 
}
<!DOCTYPE html> 
 
<html> 
 

 
    <head> 
 
    <link rel="stylesheet" href="style.css"> 
 
    <script src="script.js"></script> 
 
    </head> 
 

 
    <body> 
 
    <style>.outerDiv span{}</style></style> 
 
    <h1>Hello Plunker!</h1> 
 
    <div class ="outerDiv"> 
 
     <span class = "div40"> 
 
     hello 
 
     </span> 
 
     <span class = "div60"> 
 
     hello i am trying to insert this content inside the parent div,but i am not able to do so. My content should be inside the border of this div and i want the height not to be fixed as my content is dynamic. 
 
     </span> 
 
    </div> 
 
    </body> 
 

 
</html>

1

你需要后div60

<div class ="outerDiv"> 
    <span class = "div40"> 
    hello 
    </span> 
    <span class = "div60"> 
    hello i am trying to insert this content inside the parent div,but i am not able to do so. My content should be inside the border of this div and i want the height not to be fixed as my content is dynamic. 
    </span> 
    <div class="clear"></div> 
</div> 

添加新的元素,并添加在你的CSS以下

.clear { 
    clear: both 
} 
+0

而不是添加一个* DOM *你可以引用他去**伪clearfix方法**清除'浮动' – vivekkupadhyay

+0

不确定OP需要支持的浏览器,添加一个div是最安全的。 –

0

移动到流体内容...引导。

<!DOCTYPE html> 
<html> 
    <head> 
    <link rel="stylesheet" href="style.css"> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/> 
    </head> 

    <body> 
    <h1>Hello Plunker!</h1> 
    <div class="container"> 
     <div class="row"> 
     <div class="col-md-4"> 
     hello 
     </div> 
     <div class="col-md-8"> 
     hello i am trying to insert this content inside the parent div,but i am not able to do so. My content should be inside the border of this div and i want the height not to be fixed as my content is dynamic. 
     </div> 
     </div> 
    </div> 
    </body> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
    <script src="https://getbootstrap.com/dist/js/bootstrap.min.js"></script> 
    <script src="https://getbootstrap.com/assets/js/ie10-viewport-bug-workaround.js"></script> 
</html>