0

I have (probably) very basic HTML/CSS question:

I'm using asp.net MVC 4 I supposed to develop simple website, which looks as the following:

  1. Page example 1: enter image description here

  2. Page example 2: enter image description here

On every page, there should be a header icon (this is a website logo) and links in the bottom. The only thing that changes between pages is a content area. Content width is different from page to page.The whole layout should be centered (as shown on design examples).

Since there are common header and footer for all pages on the website, I decided to create Layout file, like this:

...
    <body id="index" class="home">

        <header id="header" class="body">
            here comes a logo
        </header>

        <section id="content" class="body">
            @RenderBody()
        </section>

        <footer id="footer" class="body">
            <nav>
                <ul id="menu">
                   here the links
                </ul>
            </nav>
        </footer>

        @Scripts.Render("~/bundles/jquery")
        @RenderSection("scripts", required: false)
    </body>
</html>

Problem: I don't know how to achieve desired look. Did I made right decision about putting header and footer into common layout file?

I don't know HTML, but I do know well XAML. In XAML, I would do the following:

<Grid>
 <Grid.ColumnDefinitions>
   <Column Width="*"/>
   <Column Width="auto"/>
   <Column Width="*"/>
 </Grid.ColumnDefinitions>
</Grid>

Putting content into Column 1 would give me the desired behavior (middle column will take width of it child, while left and right columns will take all the left space on the screen divided equally, thus centering content on the middle).

Thanks

4 Answers 4

2

Wrap your elements within a div that has a set width and the following styles: margin:0 auto;

HTML:

<div id="widthContainer">
    <div id="header">
        <div id="logo">Logo</div>
    </div>
    <div id="mainContent">mainContent</div>
    <div id="footer">footer</div>
</div>

CSS

#widthContainer{width:800px;height:1000px;background-color:red;margin:0 auto;}
#header{width:100%;height:200px;background-color:blue;margin-bottom: 50px;}
#logo{width:200px;height:200px;background-color:green;}
#mainContent{position:relative;width:100%;height:500px;background-color:pink;margin-bottom: 50px;}
#footer{position:relative;width:100%;height:300px;background-color:green;margin-bottom: 50px;}

Here's an example fiddle.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried to do what you suggesting, but it not gives me desired effect. I wanted that the width of widthContainer will be according to width of the content inside mainContent. In your sample (unless I missing something), the width is always 800px. BTW - thank you for showing jsfiddle - looks very useful.
What is the width of mainContent? Just make that the width of widthContainer and make mainContent width 100% (Which is the width you would want.) You can edit the JSFiddle to whatever your needs are, it's just an example and not the end product that you'll use.
I edited my question to make it more clear. Thank you
My example still works in this situation. Just change the width and the container contents from page to page. Easy peasy.
1

If you're not familiar with HTML and CSS then I would suggest starting with a framework like Foundation. Foundation will give you a CSS grid which will make page layouts very easy to understand.

There is a NuGet package with simple installation instructions. You can find out more here: http://responsivemvc.net/foundation

You can get templates for many different layouts here: http://foundation.zurb.com/templates.php

Comments

0

create a wrapper around all containers with style

  width:960px; /* or whatever you want */
  margin:0 auto;

every div inside this wrapper would be centre aligned.

2 Comments

I edited my question to make it more clear. Thank you
This is exactly what I understood from your earlier question. May answer was too brief.
0

As far as I know, your CSS would be something basic along the lines of this. You would need to change them from classes to whatever CSS type you are wanting to use (I like classes, if that is what they are called :| ). If you were going to use CSS "classes", you would change the "class" on your elements also to match each name.

Also, the float property on the header you may be able to do without, it might distort the page. That I'm unsure of.

.body {
    display:block;
    width: 1080px; /* your choice*/
    margin: 0 auto;
    padding: 0;
}
.header {
    display:block;
    float:left;
}
.content {
    display:block;
}
.footer {
    display:block;
}

1 Comment

why display block for content and footer? aren't they already block. also you missed clearing float.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.