2017-02-16 58 views
1

我想要做的是將width: {the rest of width}設置爲具有float:right屬性的元素。這裏是我的代碼:如何將餘下的寬度設置爲正確的float元素?

.wrapper{ 
 
    border: 1px solid; 
 
    padding-left: 10px; 
 
} 
 

 
.fix{ 
 
    border: 1px solid blue; 
 
    width: 80px; 
 
    display:inline-block; 
 
} 
 

 
.rest{ 
 
    float: right; 
 
    border: 1px solid red; 
 
}
<div class='wrapper'> 
 
    <div class='fix'> fix width </div> 
 
    <div class='rest'> it should has both right float and the rest of width</div> 
 
</div>

我怎麼能填補這一空白?這是預期輸出:

enter image description here

+0

你可以把'寬度:理論值(100% - 80px)''上.rest' – Santi

+1

這似乎是一個完美的時間使用'flexbox'(幾乎所有的瀏覽器支持) – Phil

回答

1

不知道你是在CSS如何靈活,但如果你在固定的div使用float:left您可以在正確的使用overflow:auto

.wrapper{ 
 
    border: 1px solid; 
 
    padding-left: 10px; 
 
} 
 

 
.fix{ 
 
    border: 1px solid blue; 
 
    width: 80px; 
 
    display:inline-block; 
 
    float: left; 
 
} 
 

 
.rest{ 
 
    border: 1px solid red; 
 
    overflow: auto; 
 
}
<div class='wrapper'> 
 
    <div class='fix'> fix width </div> 
 
    <div class='rest'> it should has both right float and the rest of width</div> 
 
</div>

如果需要浮動權div的實際內容則仍然是正確的,你可以添加一個內容容器右內浮動。

.wrapper{ 
 
    border: 1px solid; 
 
    padding-left: 10px; 
 
} 
 

 
.fix{ 
 
    border: 1px solid blue; 
 
    width: 80px; 
 
    display:inline-block; 
 
    float: left; 
 
} 
 

 
.rest{ 
 
    border: 1px solid red; 
 
    overflow: auto; 
 
} 
 

 
.right-content{ 
 
    float:right; 
 
}
<div class='wrapper'> 
 
    <div class='fix'> fix width </div> 
 
    <div class='rest'><div class="right-content"> it should has both right float and the rest of width</div></div> 
 
</div>

1

使用calc減去值。

.rest{ 
    float: right; 
    border: 1px solid red; 
    width: calc(100% - 80px); 
} 
+0

你知道,我網站的大部分用戶使用舊瀏覽器,他們的瀏覽器不支持'clac()'。沒有任何解決方法嗎? – stack

+0

沒有Javascript,下面是當前的兼容性圖表:http://caniuse.com/#feat=calc –

0

在申請%寬度

.fix{ 
     border: 1px solid blue;  
     width: 20%;  
     display:inline-block;  
     float:left; 
} 
.rest{ 
     float: right; 
     border: 1px solid red; 
     width:80%; 
} 
相關問題