Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make div scale to 100% of browser width?

<div id="sidebar">sidebar</div>
<div id="content">content</div>

The sidebar is a fixed width of 100px. How do I make the width of content to 100% of the browser width?

http://jsfiddle.net/chickendance/qQQTU/1/

like image 916
Derek Avatar asked May 23 '13 20:05

Derek


People also ask

How do I change the width to 100% in CSS?

If you want a block-level element to fill any remaining space inside of its parent, then it's simple — just add width: 100% in your CSS declaration for that element, and your problem is solved.

Is a div 100% width by default?

So, by nature, it doesn't matter how much content the element contains because its width is always 100%, that is, until we alter it. Think of elements like <p> , <article> , <main> , <div> , and so many more.

How can I make my body 100% height?

Try setting the height of the html element to 100% as well. Body looks to its parent (HTML) for how to scale the dynamic property, so the HTML element needs to have its height set as well. However the content of body will probably need to change dynamically. Setting min-height to 100% will accomplish this goal.


1 Answers

You can do this with simple CSS:

#sidebar {
    float: left;
    width: 100px;
}

#content {
    margin-left: 100px;
}

Updated with wrapper div:

HTML

<div id="wrapper">
    <p>wrapper</p>
    <div id="sidebar">sidebar</div>
    <div id="content">content</div>
    <p>wrapper</p>
</div>

CSS

* {
    /* reset */
    margin: 0;
    padding: 0;
}
html,
body {
    /* for demo purposes only */
    height: 100%;
}
#wrapper {
    /* for demo purposes only */
    background: rgba(200, 200, 200, 0.6);
    height: 100%;
}
#sidebar {
    float: left;
    width: 100px;

    /* for demo purposes only */
    background: rgba(200, 200, 200, 0.6);
    height: 50%;
}
#content {
    margin-left: 100px;

    /* for demo purposes only */
    background: rgba(200, 200, 200, 0.9);
    height: 50%;
}

Working example: http://jsfiddle.net/kboucher/FK2tL/

like image 51
Kevin Boucher Avatar answered Oct 02 '22 03:10

Kevin Boucher