簡體   English   中英

在浮動“百分比” div旁邊放置固定寬度的div?

[英]Positioning a fixed width div next to a floating “percent” div?

所以我一直在處理自己的問題(不要聽起來像是我在發牢騷)。 我遇到困難的事情是試圖找出最好的方法來使兩個div相鄰浮動,一個具有固定的寬度,另一個具有一定百分比的寬度來填充剩余的金額。

我的HTML代碼是這樣的:

<div class="wrapper">
        <div class="sidebar">
        </div>
        <div class="content">
            <div class="tile">
                <h1>Catalogs</h1>
                <p>1</p>
            </div><!--
            --><div class="tile">
                <h1>Products</h1>
                <p>2</p>
            </div><!--
            --><div class="tile">
                <h1>Specifications</h1>
                <p>3</p>
            </div><!--
            --><div class="tile">
                <h1>New Products</h1>
                <p>4</p>
            </div>
        </div>

        <div style="clear:both"></div>
    </div>

而我的CSS:

@charset "utf-8";
/* CSS Document */
/* WEB FONTS

font-family: 'Ubuntu', sans-serif;
font-family: 'Oswald', sans-serif;
font-family: 'Francois One', sans-serif;

*/
body {
    background-color:#191919;
    margin: 0 auto;
    overflow:hidden;
    font-family: 'Ubuntu', sans-serif;
}
.wrapper{
    width: 100%;
    min-height: 900px;
    margin: 0 auto;
}
.sidebar{
    width: 20%;
    height: 100%;
    float: left;
    color:white;
    background-color:#191919;
    margin:0 auto;
    display: block;

}
.content{
    width: 80%;
    height: 1000px;
    float: right;
    color:white;
    background-image:url(Assets/background1.png);
    background-size: cover;
    background-repeat: no-repeat;
    margin:0 auto;
}
.tile{
    width: 25%;
    height: 100%;
    display:inline-block;
    margin: 0 auto;
    color:white;
    background-color:rgba(0,0,0,0.7);
    transition: opacity 1s;
    opacity: .3;
    float: left;
}
.tile:hover {
    opacity: 1; 
}
.tile h1{
    font-family: 'Francois One', sans-serif;
    background-color:rgba(110,0,1,0.77);
    width: 100%;
    height: 50px;
    text-align:center;
    line-height: 50px;
}
.tile p{

}
#item {
    padding-left: 25px;
    font-family: 'Francois One', sans-serif;
}
a {
    color:white;
    text-decoration:none;
}

這是我在純CSS中完成的布局。 我的問題是我希望側邊欄菜單的寬度固定為400px,並緊靠填充“ x”像素剩余空間的內容。 如果我將頁面縮小得足夠小,則側邊欄div上的文字會重疊幾圈。 這就是為什么我希望將其修復。

我對這個問題的解決方案是使用jQuery:

function widthAdjuster(wrapper, sidebar, content, tile){
    var wrapperWidth = $(wrapper).width();
    var sidebarWidth = $(sidebar).width();
    var newWidth = wrapperWidth - sidebarWidth;
    var newWidgetWidth = newWidth/4;

    $(content).css("width", newWidth);
    $(tile).css("width", newWidgetWidth);
}

function masterFunc(){
    widthAdjuster('.wrapper','.sidebar','.content','.tile');
    $(window).resize(widthAdjuster('.wrapper','.sidebar','.content','.tile'));
}

基本上,這將自動為我設置內容的寬度以及調整每個圖塊的寬度。 然后在調整大小時,它將重新運行該功能並調整每個圖塊的大小。 現在,我的問題是,我可以在CSS中執行此操作,以便沒有人為我大喊大叫而使用JQuery在“代碼審閱”中調整寬度嗎?

我之前已經做到了,將您的CSS更改為:

<style>
body {
background-color:#191919;
margin: 0 auto;
overflow:hidden;
font-family: 'Ubuntu', sans-serif;
}
.wrapper{
width: 100%;
min-height: 900px;
margin: 0 auto;
}
.sidebar{
width: 20%;
height: 100%;
float: left;
color:white;
background-color:#191919;
margin:0 auto;
display: block;

}
.content{

height: 1000px;
float: right;
color:white;
background-image:url(Assets/background1.png);
background-size: cover;
background-repeat: no-repeat;
margin:0 auto;
}
.tile{
width: 25%;
height: 100%;
display:inline-block;
margin: 0 auto;
color:white;
background-color:rgba(0,0,0,0.7);
transition: opacity 1s;
opacity: .3;
float: left;
}
.tile:hover {
opacity: 1; 
}
.tile h1{
font-family: 'Francois One', sans-serif;
background-color:rgba(110,0,1,0.77);
width: 100%;
height: 50px;
text-align:center;
line-height: 50px;
}
.tile p{

}
#item {
padding-left: 25px;
font-family: 'Francois One', sans-serif;
}
a {
color:white;
text-decoration:none;
}
.sidebar{
width:400px;
float:left;
margin:0 -400px 0 0;

}
.content{
position:absolute;
right:0;
left:400px;
}
</style>

不確定您的瀏覽器要求是什么,但是使用CSS Flexbox,您可以使用純CSS實現此功能。

http://caniuse.com/#feat=flexbox https://developer.mozilla.org/zh-CN/docs/Web/Guide/CSS/Flexible_boxes

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM