2016-08-23 43 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>