Coloured content boxes are a great technique for creating visually-pleasing CSS layouts and content. All it takes is assigning a background colour to a <div> element or setting an image as the background if you want a sleeker look.

To begin create a <div> element and assign it an ID.

<div id='contentBox'></div>

We then need to define the styles for the contentBox ID. Let's start by setting the font type, size and colour.


#contentBox 
{
  font-family: sans-serif;
  font-size: 80%;
  color:# 2E2E2E;
}

Next we are going to add some filler text to the <div>.


<div id='contentBox'>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas  feugiat neque sit amet ante tincidunt hendrerit. Vestibulum eleifend erat  et enim. Cras nisi arcu, malesuada malesuada, accumsan non, venenatis  eget, purus. Mauris pulvinar tincidunt turpis. Aliquam enim. Vestibulum  tristique risus eget elit. Fusce urna arcu, dictum sit amet, luctus non,  bibendum eu, leo. Vestibulum at diam. Nunc pretium. Maecenas dapibus  laoreet leo. Mauris pretium quam. Nulla ultrices. Donec iaculis laoreet  diam. Suspendisse a felis quis mauris porttitor mattis. Duis aliquet  lacinia odio. 
</div>

So far we have some text in a <div>. Let's make this look more like a content box, by adding a background colour and a border around it. Add the code below to #contentBox.


background-color:#FFFF99;
border-style: solid;
border-width:2px;
border-color:#FF9933;

Set the width and height attributes of the box, like this:


width:20%;
height:35%;

We're going to add some padding around the text to make it look better.

padding:1%;

Our text fits nicely within the box at the moment, but if we added another paragraph it would flow over the box. To prevent this we need to add a scrollbar.

overflow: scroll;

Alternatively, you can set the overflow property to auto to display a scrollbar only when needed.

We're going to position the box 10 pixels from the start of the page:


position:absolute;
left:10px;

You can position the box anywhere on the page where you see fit, by adjusting the position attribute.

And tying it all in:


<html>
<head>
<title>Content Box</title>
<style type="text/css">
#contentBox 
{
  position:absolute;
  left:10px;
  font-family: sans-serif;
  font-size: 80%;
  color: #2E2E2E;
  background-color:#FFFF99;
  border-style: solid;
  border-width:2px;
  border-color:#FF9933;
  width:20%;
  height:35%;
  padding:1%;
  overflow: scroll;
}
</style>
</head>
<body>
<div id='contentBox'>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas  feugiat neque sit amet ante tincidunt hendrerit. Vestibulum eleifend erat  et enim. Cras nisi arcu, malesuada malesuada, accumsan non, venenatis  eget, purus. Mauris pulvinar tincidunt turpis. Aliquam enim. Vestibulum  tristique risus eget elit. Fusce urna arcu, dictum sit amet, luctus non,  bibendum eu, leo. Vestibulum at diam. Nunc pretium. Maecenas dapibus  laoreet leo. Mauris pretium quam. Nulla ultrices. Donec iaculis laoreet  diam. Suspendisse a felis quis mauris porttitor mattis. Duis aliquet  lacinia odio. </p>

<p>Nullam cursus erat id magna. Sed eget lectus. Phasellus dignissim. In  purus. Duis scelerisque. Sed suscipit risus nec velit. Sed et neque.  Maecenas eget nunc. Ut turpis tortor, varius ut, dapibus quis, lobortis 
et, erat. Aliquam vitae odio vitae tellus ultricies adipiscing. In ut  diam. </p>
</div>
</body>
</html>

This is how it will render in a browser:

Do you need help with CSS? Gain advice from Builder AU forums

Related links

Comments

1

sweetangel - 11/06/08

css lessons - css scrollbar examples code
-- http://css-lessons.ucoz.com/scroll-css-examples.htm --

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

1

sweetangel - 06/11/08

css lessons - css scrollbar examples code -- http://css-lessons.ucoz.com/scroll-css-examples.htm -- ... more

Log in


Sign up | Forgot your password?

  • Renai LeMay Startup Camp Sydney: The review

    Three new Australian technology start-ups, uTag, TrafficHawk.com.au and LinkViz, were conceived and launched over the weekend in a lightning initiative dubbed "Startup Camp Sydney". Read more »

    -- posted by Renai LeMay

  • Staff Apple to developer: Fart jokes aren't funny

    When Apple announced it would be vetting every application submitted for inclusion in the App Store, this was just the kind of question that entered many a mind: just how arbitrary would the company be in wielding that veto power? Read more »

    -- posted by Staff

  • Staff Chrome is just another browser

    Hands up if you missed the Chrome release -- didn't think anyone did. Google's browser arrived with all the fanfare and hype that only Google can produce. Read more »

    -- posted by Staff

What's on?