Sidetracking
Sidetracking

Reputation: 229

Div filling entire width of screen

I've been trying to add a header to my website, but I cannot get the container to fit the full width of the screen, despite the width being set to 100% or auto. It always has about a ~5px margin on both the left and right, even with margin and padding both set to 0.

HTML:

<div id="header">
    <h7>
        <p>Lorem Ipsum</p>
        <p>Lorem Ipsum</p>
        <p>Lorem Ipsum</p>
        <p>Lorem Ipsum</p>
    </h7>
</div>

CSS:

body div#header{
    width: 100%;
    margin: 0;
    padding: 0;
    background-color: #c0c0c0;
}

Upvotes: 7

Views: 22549

Answers (7)

Palak Jain
Palak Jain

Reputation: 683

Try out the following code:

<!DOCTYPE html>
    <html>
    <head>    
        <style>
            body, html{
                   margin:0;
            } 
        </style>

    </head>

Upvotes: 0

Newbie
Newbie

Reputation: 249

body, html {
    margin: 0;
    padding: 0;
}

div { width: 100%;
    margin: 0;
    padding: 0;
    background-color: #c0c0c0;
}

OR

html, body, div {
    margin:0;
    padding:0;
    border:0;
    outline:0;
    background:transparent;
}

Upvotes: 2

GOK
GOK

Reputation: 2428

Try this, i hope this helps:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Header & Footer</title>
        <style type="text/css">
            /* Global */
            * {
                margin: 0;
            }
            html, body {
                height: 99%;
            }
            /* Header */
            .container{
                min-height: 100%;
                height: auto !important;
                height: 100%;
                margin: 0 auto -4em;
                width:100%;
                font-family:Segoe UI;
                color:#fff;
            }
            .container-header{
                padding-top:5px;
                padding-left:20px;
            }

            /* Footer */
            .footer{
                background-color:#333030;
                width:100%;
                font-family:Segoe UI;
                color:#fff;
            }
            .footer img{
                padding-left:15px;
            }
            /* Page Content */
            .content{
                height: auto !important;
            }
            .container p{
            font-size:12pt;
            font-weight:bold;
            }
        </style>
    </head>
    <body>
        <!-- Header Div -->
        <div class="container">
            <table width="100%" style="background-color:#333030;color:#FFFFFF;padding:10px">
                <tr></tr>
                <tr>
                    <td></td>
                    <td>
                        <div style="padding-left:100px;font-size:36px;">Header</div>
                    </td>
                </tr>
                <tr></tr>
            </table>

            <!-- Page Content Div -->
            <div class="content">
                Blah Blah
            </div>
        </div>

        <!-- Footer Div -->
        <div class="footer">
            <table width="100%" style="background-color:#333030;color:#FFFFFF;padding:10px">
                <tr></tr>
                <tr>
                    <td></td>
                    <td>
                        <div style="padding-left:100px;font-size:36px;">Footer</div>
                    </td>
                </tr>
                <tr></tr>
            </table>
        </div>
    </body>
</html>

Upvotes: 0

Tom
Tom

Reputation: 311

This is because it's being padded by it's parent. Do you have it contained in another div? Or maybe you have a padding/margin property set on the document's body? Please supply your full CSS. If you don't it's because the browser is adding it for you, so explicity set it using:

body {
    margin: 0;
    padding: 0;
}

Upvotes: 1

Paul Dessert
Paul Dessert

Reputation: 6389

You'll need to add a reset such as:

html, body, div {
margin:0;
padding:0;
border:0;
outline:0;
font-size:100%;
vertical-align:baseline;
background:transparent;
}

All browsers have a default stylesheet. You need to override it with a reset.

Upvotes: 0

AceCorban
AceCorban

Reputation: 2103

put a zero margin on your body/html tags in your CSS. See if that helps.

Upvotes: 0

Horen
Horen

Reputation: 11392

Add

body, html{
  margin:0;
  padding:0;
}

to your CSS.

Browsers put default margins and/or paddings when rendering websites. With this you can avoid that.

Upvotes: 18

Related Questions