如何覆盖现有的CSS让孩子1有固定的宽度,孩子有父母的剩余宽度?

前端之家收集整理的这篇文章主要介绍了如何覆盖现有的CSS让孩子1有固定的宽度,孩子有父母的剩余宽度?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试为StackExchange网站编写Firefox时尚css(全屏宽度样式).

标记的问题列表页面(例如:https://stackoverflow.com/questions/tagged/java)中,HTML如下所示

  1. <div class='question-summary'>
  2. <div class='statscontainer'>
  3. <div>n votes</div>
  4. <div>n answers</div>
  5. <div>n views</div>
  6. </div>
  7. <div class='summary'>
  8. <h3>Question title</h3>
  9. <div>question excerpt ...</div>
  10. <div>tag1 tag2 tagN </div>
  11. </div>
  12. </div>

原始CSS在父/子1 /子2上使用固定宽度

  1. <style>
  2. .question-summary
  3. {
  4. float: left;
  5. width: 730px;
  6. background-color: silver;
  7. }
  8. .statscontainer
  9. {
  10. float: left;
  11. width: 86px;
  12. margin-right: 8px;
  13. background-color: gray;
  14. }
  15. .summary
  16. {
  17. float: left;
  18. width: 635px;
  19. background-color: lightgray;
  20. }
  21. </style>

现在我尝试覆盖CSS以使其适合全屏宽度

  1. .question-summary
  2. {
  3. float: left;
  4. width: 100% !important; /* parent: full screen width */
  5. background-color: silver;
  6. }
  7. .statscontainer
  8. {
  9. float: left;
  10. width: 86px; /* child 1: fixed width */
  11. margin-right: 8px;
  12. background-color: gray;
  13. }
  14. .summary
  15. {
  16. float: left;
  17. /*
  18. width: 80% !important; <--- how to let child 2 has remaining width ?
  19. left: 95px;
  20. right: 0px;
  21. */
  22. background-color: lightgray;
  23. }

问题是,如何让孩子2保持宽度?我知道什么时候使用< table>控制布局,很容易.

  1. <table style='width:100%'>
  2. <tr>
  3. <td bgcolor='gray' style='width:80px'>fixed width</td>
  4. <td bgcolor='lightgray'>automatically has remaining width</td>
  5. <tr>
  6. </table>

编辑

根据@sandeep和@MrLister的答案,它应该覆盖3个CSS属性才能使其工作

  1. .question-summary .summary {
  2. width: auto !important; /* original = width: 735px; */
  3.  
  4. float: none !important; /* original = float: left; set to 'none' to get the 'overflow' property work */
  5. overflow: hidden !important; /* original = not set,default is visible */
  6. }

解决方法

您应该将宽度重置为其初始值,即auto.

编辑:正如您所注意到的,为了使width:auto工作,您还应该重置float属性,否则宽度将不占用剩余的可用空间.

猜你在找的CSS相关文章