Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

align a div next to one that uses margin: 0 auto

Tags:

html

css

This is my first time on this forum and ill try to be clear as possible, i have a problem with creating a small website for my own, specifically with the header. Im trying to create a page which has a wrapper of 1024px center (margin: 0 auto;) and i would like 2 divs, on both sides of this wrapper where i can use another picture as background. My current css looks like this:

body, html      
background: url(../images/bg.jpg);
background-repeat: no-repeat;
background-position: top center;
margin: 0;
padding: 0;
}
#wrapper
margin: 0 auto;
width: 1024px;
}
#header {
width: 1024px;
height: 254px;
background-image: url(../images/header2.png);
background-repeat: none;
position: relative;
}
#header_right {
width: 50%;
right: 0;
background-image: url(../images/header_right2.png);
position: absolute;
height: 254px;
}
#header_left {
width: 50%;
left: 0px;
background-image: url(../images/header_left.png);
position: absolute;
background-position: right;
margin-left: -512px;
height: 254px;
}

and my html looks like:

<body>
<div id="header_right"></div><!--End header right!-->
<div id="header_left"></div><!--End header right!-->
<div id="wrapper">
<div id="header"></div><!--End header!-->
<div id="content"></div><!--End Content!-->
</div><!--End wrapper!-->
</body>

What i'm trying to accomplish is to have a header that continues on both left and right (both headers use different backgrounds), in this case it does work on the left, because im using a negative margin, since i use 50% width and exactly the half of the wrapper (-512px), this works, but if i would try to use a negative margin on the right (margin-right: -512px) this will extend the page on the right with an extra 512px, which is not my intention.

I've been googling all day but can't seem to find any answer to my question, also tried to make 3 divs with float: left , but couldnt figure out how to make 1 in the center with a width of 1024px and the rest 100% width, if anyone could help me out that would be really appreciated.

Kind regards

like image 438
Geo Avatar asked Nov 13 '22 19:11

Geo


1 Answers

I am not entirely sure how you want it to look like, but I'll give it a shot.
If I'm way off, perhaps you could provide me with a schematic of sorts?

In any case, the example given below does not use your specific code, but it should give you an idea of how it's done.


Result: first example

The left and right headers are "infinite", in that they always fill the entire page's width.
The middle header covers up the rest. If you've got background images you can use background-position to position them so that they align with the middle header's left and right edges.


Code | JSFiddle example

HTML

<div class='side_wrapper'>
    <div class='left_header'></div><div class='right_header'></div>
</div>
<div class='header'></div>
<div class='content'>
    Content here
</div>

CSS

.header, .side_wrapper, .left_header, .right_header{
    height: 100px;
}

.header, .content{
    width: 400px;
    margin: 0 auto;
}

.side_wrapper{
    width: 100%;
    white-space: nowrap;
}

.left_header, .right_header{
    width: 50%;
    display: inline-block;
}

.left_header{
    background-color: blue;
}

.right_header{
    background-color: lightblue;
}

.header{
    position:absolute;
    top: 0;
    left: 50%;
    margin-left: -200px;
    background-color: red;
}

.content{
    background-color: green;
    text-align: center;
}
like image 129
ShadowScripter Avatar answered Nov 15 '22 12:11

ShadowScripter