css – Internet Explorer 6和7:当浮动元素包含向右浮动的子元素时,浮动元素会展开为100%宽度 有解决方法吗?

前端之家收集整理的这篇文章主要介绍了css – Internet Explorer 6和7:当浮动元素包含向右浮动的子元素时,浮动元素会展开为100%宽度 有解决方法吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个父div浮动左边,有两个孩子div,我需要向右浮动。

父div应该(如果我理解规范正确)根据需要包含子div,这是它的行为在Firefox等人。

在IE中,父div扩展为100%宽度。这似乎是浮动元素的一个问题,有孩子漂浮的权利。测试页:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2.  
  3. <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  4.  
  5. <head>
  6. <title>Float test</title>
  7. </head>
  8.  
  9. <body>
  10. <div style="border-top:solid 10px #0c0;float:left;">
  11. <div style="border-top:solid 10px #00c;float:right;">Tester 1</div>
  12. <div style="border-top:solid 10px #c0c;float:right;">Tester 2</div>
  13. </div>
  14. </body>
  15.  
  16. </html>

不幸的是,我无法修复子div的宽度,所以我不能在父设置一个固定的宽度。

有没有一个CSS的唯一的解决方法,使父div像孩子div一样宽?

解决方法

这里有一个解决方案,使inline-block在IE6上工作在 http://foohack.com/2007/11/cross-browser-support-for-inline-block-styling/,使元素的行为更像右浮动< div> s:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  3. <head>
  4.  
  5. <title>Float with inline-block test</title>
  6.  
  7. <style type="text/css">
  8. .container {
  9. border-top: solid 10px green;
  10. float: left;
  11. }
  12.  
  13. .tester1,.tester2 {
  14. float: right;
  15. }
  16.  
  17. .tester1 {
  18. border-top: solid 10px blue;
  19. }
  20.  
  21. .tester2 {
  22. border-top: solid 10px purple;
  23. }
  24. </style>
  25.  
  26. <!--[if lte IE 7]>
  27. <style type="text/css">
  28. .container {
  29. text-align: right;
  30. }
  31.  
  32. .tester1,.tester2 {
  33. float: none;
  34. zoom: 1; display: inline;/* display: inline-block; for block-level elements in IE 7 and 6. See http://foohack.com/2007/11/cross-browser-support-for-inline-block-styling/ */
  35. }
  36. </style>
  37. <![endif]-->
  38.  
  39. </head>
  40.  
  41. <body>
  42. <div class="container">
  43. <div class="tester1">Tester 1</div>
  44. <div class="tester2">Tester 2</div>
  45. </div>
  46. </body>
  47. </html>

猜你在找的CSS相关文章