kylef
Gone
+1,352|6929|N. Ireland
Figured it would be best to post here:

I'm looking to create a two-tone background (bottom 50% and top 50% being different colours) in CSS. Can anyone lead me in the right direction?Having a bit of a hard time getting it right.

Thanks
Scorpion0x17
can detect anyone's visible post count...
+691|7202|Cambridge (UK)

kylef wrote:

Figured it would be best to post here:

I'm looking to create a two-tone background (bottom 50% and top 50% being different colours) in CSS. Can anyone lead me in the right direction?Having a bit of a hard time getting it right.

Thanks
Just to clarify, is that CSS as in Cascading Style Sheets?
Finray
Hup! Dos, Tres, Cuatro
+2,629|6224|Catherine Black

Scorpion0x17 wrote:

kylef wrote:

Figured it would be best to post here:

I'm looking to create a two-tone background (bottom 50% and top 50% being different colours) in CSS. Can anyone lead me in the right direction?Having a bit of a hard time getting it right.

Thanks
Just to clarify, is that CSS as in Cascading Style Sheets?
No it's Counter Strike: Source
https://i.imgur.com/qwWEP9F.png
kylef
Gone
+1,352|6929|N. Ireland

Scorpion0x17 wrote:

kylef wrote:

Figured it would be best to post here:

I'm looking to create a two-tone background (bottom 50% and top 50% being different colours) in CSS. Can anyone lead me in the right direction?Having a bit of a hard time getting it right.

Thanks
Just to clarify, is that CSS as in Cascading Style Sheets?
I didn't realize it could be perceived in any other manner Yes, cascading.
Zimmer
Un Moderador
+1,688|7192|Scotland

Like dis?
//
?

Look at the CSS
Probably not the best method, but it did the trick for Liquidat0rs site, and I couldn't be bothered doing it in a better way.
Scorpion0x17
can detect anyone's visible post count...
+691|7202|Cambridge (UK)


I gave up assuming TLAs are what I think they are years ago...

Anyhoo...

I think, if I'm interpreting the OP correctly, what you want to do is nigh on impossible, but there are workarounds.

Unless the div, or whatever, you want to apply the 2-tone background to is of a known, fixed, measured in pixels, height.

Then the best way is to use a repeating background image that is 1px wide, the correct height, and is coloured appropriately, thus:

Code:

div {
 background-image: 2-tone.png;
 background-repeat: repeat-x;
}
But, if the the div (or whatever) can be any height, then you need to make the container non-static, drop in two positioned (absolute) divs, and size and colour those, as appropriate.

Summat like:

in the HTML:

Code:

<div class='container'>
 <div class='backgroundTop'></div>
 <div class='backgroundBottom'></div>
 ... other content here...
</div>
then in the CSS:

Code:

div.container { position: relative; }
div.backgroundTop { position: absolute; top: 0; left: 0; bottom: 50%; right: 0; background-color: top_color; }
div.backgroundBottom { position: absolute; top: 50%; left: 0; bottom: 0; right: 0; background-color: bottom_color; }
(where, obviously, 'top_color' and 'bottom_color' should be replaced with the desired color # or RGB code)


Or there's the way zimmer did it... (why didn't I think of that?)

Last edited by Scorpion0x17 (2009-02-25 13:53:33)

Zimmer
Un Moderador
+1,688|7192|Scotland

Scorpion0x17 wrote:




Code:

div {
 background-image: 2-tone.png;
 background-repeat: repeat-x;
}
But, if the the div (or whatever) can be any height, then you need to make the container non-static, drop in two positioned (absolute) divs, and size and colour those, as appropriate.

then in the CSS:

Code:

div.container { position: relative; }
div.backgroundTop { position: absolute; top: 0; left: 0; bottom: 50%; right: 0; background-color: top_color; }
div.backgroundBottom { position: absolute; top: 50%; left: 0; bottom: 0; right: 0; background-color: bottom_color; }
(where, obviously, 'top_color' and 'bottom_color' should be replaced with the desired color # or RGB code)
That's wrong. It should be

Code:

background-image: url(2-tone.jpg);
And your CSS is completely wrong for what he wants.

First of all, when you absolutely position something, it needs to have a defined height. This will show absolutely nothing. Second, you positioned both DIVs at the exact same position : 50% on the browser. Third, creating a relative position DIV outside is completely useless if you aren't going to define a specific width for that and then move your absolutely positioned DIVs around that. An absolute position can only be used in conjuction with a height and width, and you also don't need to define each axis. "top:50%" should suffice. Not that it would make a difference.

Thing is, to do what you want, Kyle, it's a bit hard. It would have to be dynamic on all browsers. You will still need to have a specific height... Hmm...

"height" doesn't like percentages... I can make a background that splits halfway on the vertical end, but not horizontally.
Scorpion0x17
can detect anyone's visible post count...
+691|7202|Cambridge (UK)

Zimmer wrote:

Scorpion0x17 wrote:




Code:

div {
 background-image: 2-tone.png;
 background-repeat: repeat-x;
}
But, if the the div (or whatever) can be any height, then you need to make the container non-static, drop in two positioned (absolute) divs, and size and colour those, as appropriate.

Summat like:

in the HTML:

Code:

<div class='container'>
 <div class='backgroundTop'></div>
 <div class='backgroundBottom'></div>
 ... other content here...
</div>
then in the CSS:

Code:

div.container { position: relative; }
div.backgroundTop { position: absolute; top: 0; left: 0; bottom: 50%; right: 0; background-color: top_color; }
div.backgroundBottom { position: absolute; top: 50%; left: 0; bottom: 0; right: 0; background-color: bottom_color; }
(where, obviously, 'top_color' and 'bottom_color' should be replaced with the desired color # or RGB code)
That's wrong. It should be

Code:

background-image: url(2-tone.jpg);
And your CSS is completely wrong for what he wants.

First of all, when you absolutely position something, it needs to have a defined height. This will show absolutely nothing. Second, you positioned both DIVs at the exact same position : 50% on the browser. Third, creating a relative position DIV outside is completely useless if you aren't going to define a specific width for that and then move your absolutely positioned DIVs around that. An absolute position can only be used in conjuction with a height and width, and you also don't need to define each axis. "top:50%" should suffice. Not that it would make a difference.
1. I just wrote if of the top of my head and so, excuse me, I couldn't be arsed to type the url() bit.
2. Note in the code for the second method that a) it says "... other content here..." - the content defines the the height and width of the container and b) the styles of the two background divs are different and, therefore, do not put the inner divs in the same place.
3. Funny that it basically works, given that you think it's so very wrong (the only minor issue (that is easily solvable) being that the text doesn't show through):

https://img502.imageshack.us/img502/3602/2tone.png

Last edited by Scorpion0x17 (2009-02-25 14:12:02)

kylef
Gone
+1,352|6929|N. Ireland

Zimmer wrote:

Like dis?
//
?

Look at the CSS
Probably not the best method, but it did the trick for Liquidat0rs site, and I couldn't be bothered doing it in a better way.
Yep. From looking at liq's CSS, the real relative section I need to edit is:

Code:

body {
    background-color: #000000;
    background-image: url(img/bg-repeat.png);
    background-repeat:repeat-x;
    background-position:bottom;
    background-attachment:fixed;
    width: 100%;
}
Just so that I can confirm that I know what all of this is actually doing:

background-color: #000000 - black top section
background-image: url(img/bg-repeat.png) - this gives it the 'bottom colour section' as such?
background-repeat:repeat-x - referring to copy horizontally forever (or is this just width: 100%)
background-position:bottom - so that the image is placed underneath the black top section?
background-attachment:fixed - not sure what this section refers to..

Given the fact that liq has a white line running through his - seeing as I am only working with hex colours do I even need an image? (took a look at bg-repeat.png)

Thanks for the help ... total newbie in this area. I feared as much it would 'seem' so hard.

If I can crack this section then I should be fine for everything else.

Last edited by kylef (2009-02-25 14:25:05)

Zimmer
Un Moderador
+1,688|7192|Scotland

lol.

I wasn't saying it in a condescending tone. No need to bite back.

Take out the content ( the text ), you will see why it doesn't work. The container part is just an extra though. It needs to work without content. Even with content, it will never span the whole browser page simply because it has no defined height and will expand depending on the content.

The only way to get something close to a 2 tone bg is by doing a background attachment and shoving it at the bottom and keeping something at the top. Even then, once you resize the browser window to a size that is too small, one of the parts ( top or bottom, depending on which you made fixed with background attachment ) will start to get larger.
Scorpion0x17
can detect anyone's visible post count...
+691|7202|Cambridge (UK)
To make the text show through, in my 2nd solution, you need to add "z-index: -1;" to the two background div styles, thus:

Code:

div.container { position: relative; }
div.backgroundTop { position: absolute; top: 0; left: 0; bottom: 50%; right: 0; background-color: top_color; z-index: -1; }
div.backgroundBottom { position: absolute; top: 50%; left: 0; bottom: 0; right: 0; background-color: bottom_color; z-index: -1; }
Scorpion0x17
can detect anyone's visible post count...
+691|7202|Cambridge (UK)

Zimmer wrote:

lol.

I wasn't saying it in a condescending tone. No need to bite back.
Well, it read with a condescending tone.

Zimmer wrote:

Take out the content ( the text ), you will see why it doesn't work. The container part is just an extra though. It needs to work without content. Even with content, it will never span the whole browser page simply because it has no defined height and will expand depending on the content.
If you want it to apply to the entire page, you just make the <body>...</body> the container.

Zimmer wrote:

The only way to get something close to a 2 tone bg is by doing a background attachment and shoving it at the bottom and keeping something at the top. Even then, once you resize the browser window to a size that is too small, one of the parts ( top or bottom, depending on which you made fixed with background attachment ) will start to get larger.
Exactly. My 2nd solution solves that problem. And it works.
kylef
Gone
+1,352|6929|N. Ireland

Scorpion0x17 wrote:

To make the text show through, in my 2nd solution, you need to add "z-index: -1;" to the two background div styles, thus:

Code:

div.container { position: relative; }
div.backgroundTop { position: absolute; top: 0; left: 0; bottom: 50%; right: 0; background-color: top_color; z-index: -1; }
div.backgroundBottom { position: absolute; top: 50%; left: 0; bottom: 0; right: 0; background-color: bottom_color; z-index: -1; }
With regards to top_color and bottom_color do they require images or will hex do?
Scorpion0x17
can detect anyone's visible post count...
+691|7202|Cambridge (UK)

kylef wrote:

Scorpion0x17 wrote:

To make the text show through, in my 2nd solution, you need to add "z-index: -1;" to the two background div styles, thus:

Code:

div.container { position: relative; }
div.backgroundTop { position: absolute; top: 0; left: 0; bottom: 50%; right: 0; background-color: top_color; z-index: -1; }
div.backgroundBottom { position: absolute; top: 50%; left: 0; bottom: 0; right: 0; background-color: bottom_color; z-index: -1; }
With regards to top_color and bottom_color do they require images or will hex do?
Either is fine - as it is above, you need to use a color value (hex code or RGB(...) will work).

If you want to use images, make them "background-image: url(...);" instead (where '...' is replaced by the appropriate image name).
Zimmer
Un Moderador
+1,688|7192|Scotland

Scorpion0x17 wrote:

Zimmer wrote:

The only way to get something close to a 2 tone bg is by doing a background attachment and shoving it at the bottom and keeping something at the top. Even then, once you resize the browser window to a size that is too small, one of the parts ( top or bottom, depending on which you made fixed with background attachment ) will start to get larger.
Exactly. My 2nd solution solves that problem. And it works.
What works? The background colours will still never span across the entire browser.... I don't get what you solved.... Other than the text being infront of the divs.
Scorpion0x17
can detect anyone's visible post count...
+691|7202|Cambridge (UK)

Zimmer wrote:

Scorpion0x17 wrote:

Zimmer wrote:

The only way to get something close to a 2 tone bg is by doing a background attachment and shoving it at the bottom and keeping something at the top. Even then, once you resize the browser window to a size that is too small, one of the parts ( top or bottom, depending on which you made fixed with background attachment ) will start to get larger.
Exactly. My 2nd solution solves that problem. And it works.
What works? The background colours will still never span across the entire browser.... I don't get what you solved.... Other than the text being infront of the divs.
What do you mean?

Divs automatically span the width of the container.

Or do you mean the background doesn't cover the whole height of the browser window?

If so, no - it doesn't - it covers the entire height of the content.

Last edited by Scorpion0x17 (2009-02-25 14:44:20)

Zimmer
Un Moderador
+1,688|7192|Scotland

Scorpion0x17 wrote:

What do you mean?

Divs automatically span the width of the container.

Or do you mean the background doesn't cover the whole height of the browser window?

If so, no - it doesn't - it covers the entire height of the content.


That's why. I think he wants something that spans the height of the browser window, not just the content.
Scorpion0x17
can detect anyone's visible post count...
+691|7202|Cambridge (UK)

Zimmer wrote:

Scorpion0x17 wrote:

What do you mean?

Divs automatically span the width of the container.

Or do you mean the background doesn't cover the whole height of the browser window?

If so, no - it doesn't - it covers the entire height of the content.


That's why. I think he wants something that spans the height of the browser window, not just the content.
... of the container.

As I said before, to make it cover the entire window, you use the <body>...</body> as the container.

(i.e. just remove the container div)
Zimmer
Un Moderador
+1,688|7192|Scotland

Scorpion0x17 wrote:

... of the container.

As I said before, to make it cover the entire window, you use the <body>...</body> as the container.

(i.e. just remove the container div)
Nah man, that doesn't work.
Scorpion0x17
can detect anyone's visible post count...
+691|7202|Cambridge (UK)

Zimmer wrote:

Scorpion0x17 wrote:

... of the container.

As I said before, to make it cover the entire window, you use the <body>...</body> as the container.

(i.e. just remove the container div)
Nah man, that doesn't work.
So why does this look right?
Zimmer
Un Moderador
+1,688|7192|Scotland

Scorpion0x17 wrote:

Zimmer wrote:

Scorpion0x17 wrote:

... of the container.

As I said before, to make it cover the entire window, you use the <body>...</body> as the container.

(i.e. just remove the container div)
Nah man, that doesn't work.
So why does this look right?
lulz. Apologies, was prviewing the wrong index.html file. Stupid dreamweaver.
kylef
Gone
+1,352|6929|N. Ireland
Appreciate all the help, thanks! Karma all round ... thanks guys

Board footer

Privacy Policy - © 2025 Jeff Minard