我正在尝试为StackExchange网站编写Firefox时尚css(全屏宽度样式).
在标记的问题列表页面(例如:https://stackoverflow.com/questions/tagged/java)中,HTML如下所示
- <div class='question-summary'>
- <div class='statscontainer'>
- <div>n votes</div>
- <div>n answers</div>
- <div>n views</div>
- </div>
- <div class='summary'>
- <h3>Question title</h3>
- <div>question excerpt ...</div>
- <div>tag1 tag2 tagN </div>
- </div>
- </div>
原始CSS在父/子1 /子2上使用固定宽度
- <style>
- .question-summary
- {
- float: left;
- width: 730px;
- background-color: silver;
- }
- .statscontainer
- {
- float: left;
- width: 86px;
- margin-right: 8px;
- background-color: gray;
- }
- .summary
- {
- float: left;
- width: 635px;
- background-color: lightgray;
- }
- </style>
现在我尝试覆盖CSS以使其适合全屏宽度
- .question-summary
- {
- float: left;
- width: 100% !important; /* parent: full screen width */
- background-color: silver;
- }
- .statscontainer
- {
- float: left;
- width: 86px; /* child 1: fixed width */
- margin-right: 8px;
- background-color: gray;
- }
- .summary
- {
- float: left;
- /*
- width: 80% !important; <--- how to let child 2 has remaining width ?
- left: 95px;
- right: 0px;
- */
- background-color: lightgray;
- }
问题是,如何让孩子2保持宽度?我知道什么时候使用< table>控制布局,很容易.
- <table style='width:100%'>
- <tr>
- <td bgcolor='gray' style='width:80px'>fixed width</td>
- <td bgcolor='lightgray'>automatically has remaining width</td>
- <tr>
- </table>
编辑
根据@sandeep和@MrLister的答案,它应该覆盖3个CSS属性才能使其工作
- .question-summary .summary {
- width: auto !important; /* original = width: 735px; */
- float: none !important; /* original = float: left; set to 'none' to get the 'overflow' property work */
- overflow: hidden !important; /* original = not set,default is visible */
- }
解决方法
您应该将宽度重置为其初始值,即auto.
编辑:正如您所注意到的,为了使width:auto工作,您还应该重置float属性,否则宽度将不占用剩余的可用空间.