简体   繁体   中英

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

So I've been around the block with my issues (not to sound like I'm some sort of whiny hoodlum); the thing that I'm having a hard time with is trying to figure out the best method to float two divs next to each other, one with a fixed width, and the other with a percentage width to fill the remaining amount.

My HTML Code is like this:

<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>

And my 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;
}

This is my layout done in pure CSS. My problem is that I want my sidebar menu to have a fixed width of 400px and be next to content that fills the remaining space of 'x' pixels. If I shrink my page down enough, the text from the sidebar div over laps. That's why I want it to be fixed.

My solution to this problem was with 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'));
}

This will basically automatically set the width of content for me as well as adjust the width of each tile as well. Then on resize, it will re-run that function and size each of the tiles. Now, my question is, can I do this in CSS so that I don't have people yelling at me for using JQuery to adjust the width at Code Review?

I've achieved this before, change your css to this:

<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>

Not sure what you browser requirements are, but with css flexboxes you can achieve it with pure css.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM