Low
Low

Reputation: 461

2 Background Colours - CSS only

How do I add 2 background colours to a container div. I've seen some solutions that work but only with 50% height for each colour. I need one however to have a set height (see image).

My current solution is for background 1 to be an 1x260px background image with background 2 being a background colour. This however leaves you as you open the page with a flash of background colour 2 until background 1 is finished loading, I would like to avoid this flash. Here is the structure of the page:

Page Structure

Thanks for the help in advance!

UPDATE:

I couldn't get any of the solutions working properly in my context, but eventually solved it myself (I realise now my brief might've been slightly incomplete).

Here's my JSFiddle

Here's the Code:

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

.other-content {
    background-color:lightblue;
    width:100%;
    height:20px;
}

.page-content {
    width:100%;
    background-color:lightgray;
}

.container {
    width:600px;
    height:700px; /* This height is flexible and can change to whatever value you want */
    background-color:gray;
    margin-top:-50px;
    margin-left:auto;
    margin-right:auto;
}

.white-bg {
    background-color:dodgerblue;
    height:50px;
    width:100%;
}
<div class="other-content"></div>
<div class="page-content">
    <div class="white-bg"></div>
    <div class="container"></div>
</div>
<div class="other-content"></div>

Upvotes: 1

Views: 430

Answers (5)

Abhitalks
Abhitalks

Reputation: 28387

You could use CSS color gradients. Color stops can be specified in pixels for the first band, and then 100% for the rest. This way you won't have to calculate auto-height depending on the container.

Here is an example based on your use-case. I have taken 60px as the height of first band to make it fit neatly in the below snippet. You would make it 260px as required. (Click full-page for better view).

html, body {
    height: 100%;
}
.container {
    width: 70%;
    margin: auto;
    height: 100%;
    background-image:
        linear-gradient(
          to bottom, 
          #0f75bc,         /* Start with color of top band  */
          #0f75bc 60px,    /* Top-band color stops at 260px */
          #00aeef 60px,    /* Bottom-band color starts at 260px */
          #00aeef 100%     /* Bottom-band color continues to 100% i.e. remaining height */
        );

}
<div class="container"></div>

Upvotes: 1

rishiAgar
rishiAgar

Reputation: 168

Here, I have done only using css and background color.

Working JsFiddle

HTML:

<div class="part-b">
   <div class="background"></div>
   <div class="container">
      <div class="row">
         Content
      </div>
   </div>
</div>

CSS:

.container {
   width: 960px !important;
   position: relative;
}

.part-b {
   background: yellow;
   overflow: hidden;
   position: relative;
 }

.part-b .background {
   width: 100%;
   height: 100px;
   background-color: green;
}

.row {
   height: 50px;
}

Upvotes: 0

chrona
chrona

Reputation: 1911

You can add a pseudo-element like ::after on your page.

* { margin: 0; padding: 0; }    

body {
  background: #00AEEF;
}

body:after {
  background: #0F75BC;
  content: "";
  display: block;
  height: 50px;
  left: 0;
  top: 0;
  width: 100%;
}

Upvotes: 1

Rohit Azad Malik
Rohit Azad Malik

Reputation: 32182

used to this

css

.nice{
    
    width:500px;
    height:500px;
    margin:auto;
    position:relative;
    background:red;
    color:white;
    z-index:1;
    

}
.nice:after{
    content:"";
    position:absolute;
    left:0;
    right:0;
    top:0;
    bottom:70%;
    background:green;
    z-index:-1;
}
<div class="nice">helo helo helo </div>

Upvotes: 0

Zhenya
Zhenya

Reputation: 281

Try this

background: rgba(47,102,179,1);/* Old Browsers */
background: -moz-linear-gradient(top, rgba(47,102,179,1) 0%, rgba(255,255,255,1) 260px, rgba(255,255,255,1) 100%); /* FF3.6+ */
background: -webkit-gradient(left top, left bottom, color-stop(0%, rgba(47,102,179,1)), color-stop(260px, rgba(255,255,255,1)), color-stop(100%, rgba(255,255,255,1)));/* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, rgba(47,102,179,1) 0%, rgba(255,255,255,1) 260px, rgba(255,255,255,1) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, rgba(47,102,179,1) 0%, rgba(255,255,255,1) 260px, rgba(255,255,255,1) 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, rgba(47,102,179,1) 0%, rgba(255,255,255,1) 260px, rgba(255,255,255,1) 100%); /* IE 10+ */
background: linear-gradient(to bottom, rgba(47,102,179,1) 0%, rgba(255,255,255,1) 260px, rgba(255,255,255,1) 100%);/* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#2f66b3', endColorstr='#ffffff', GradientType=0 );/* IE6-9 */

its gradient, if you don't like it, change 0% to 100% or play with parameters

Upvotes: 1

Related Questions