petehallw
petehallw

Reputation: 1014

Centering body content HTML and CSS

I am having difficulty centering the content of my web page. Currently it looks like this on my mobile device:

enter image description here

I wish to center all the content of this page on the screen and have the width fill the page.

My HTML is as follows:

<html>
    <head>
    <link rel="stylesheet" media="screen and (max-device-width: 799px)" type="text/css" href="stylesheet_small.css" />
    <link rel="stylesheet" media="screen and (min-device-width: 800px)" type="text/css" href="stylesheet_large.css" />

    <div class ="container_header">
        <div class="header">
            <div class="nav">
                <a href="index.php" class="body">HOME</a>.....etc.
            </div>
        </div>
    </div>
    </head>

    <body>
    <p id="content">
        <div class="container_top">
            <div id="logo"><img border="0" src="images/introspect_logo_container.png" alt="INTROSPECT PICTURES" align="center"></div>
        </div>
    <div class ="container_body">
        <br><div class ="current_page"><b>INTROSPECT</b> HOME</div>
        <hr>
        ....
    <div id="featured_pic"><img border="0" src="images/index_fliers.jpg" alt="FLIERS" align="center"></div>
    </div>
    </p>
    </body>

    <div class="footer">
    ....
    </div>
</html>

The CSS for stylesheet_small is shown below:

body {
text-align: center;
}

.header {
width:auto;
text-align: center; 
padding:10px;
border-radius: 5px;
}

.container_top {
text-align: center;
width:auto;
border-radius: 5px;
}

.container_body {
text-align: center;
width:auto;
padding:10px;
border-radius: 5px;
}

#logo {
width: auto;
}

#featured_pic {
width: auto;
}

I have center-aligned the text and images and set the widths of each class to auto. What else do I need to do? Thanks.

Upvotes: 1

Views: 2601

Answers (1)

AnnieMac
AnnieMac

Reputation: 862

Try adding

margin: 0 auto;

to the divs you want centered

Also, your navigation bar is (container_header) is too wide for smaller viewports and pushing your layout out of whack. Take a look at collapsing the navbar for smaller devices to avoid this.

Upvotes: 2

Related Questions