Fancy Colored Boxes with CSS
If you like to play with colors, padding and borders, it’s easy to get boxes like these:
I use these boxes when I need to highlight some links or make a header stand out. One just has to get a hang of what the code should be, then one can tweak it for one’s own needs. You can use these at the top of a post or as a box to highlight links.
Learning the Code
Let’s take a look at the code for that top box. You will notice two div tags, one wrapped inside the other:
The above is done with inline code. A better approach is to separate the HTML from the CSS – so it’s easier for you to see what is going on. You can then use the CSS code over and over again. And you will also discover that the two boxes have similar HTML markup:
In the second box, the yellow with red borders, I inserted a <ul> tag with <li> links in the middle:
<li><a href=”http://www.somedomain.com/link2.htm”>Link Two</a></li>
<li><a href=”http://www.somedomain.com/link3.htm”>Link Three</a></li>
</div></div>
The CSS Code
Here is the CSS code for the blue box, which you should save to your stylesheet (if you use WordPress, it’s style.css):
========================================================= */
.myouter {
border: 1px solid #ccc;
background-color: #666;
width: 400px;
margin-left:auto;
margin-right: auto;
}
.myinner {
background-color: #69c;
padding: 5px;
color: #fff;
text-align:center;
font-weight: bold;
font-variant:small-caps;
}
The code for the red and yellow box with links is only a slight variation of the blue box. You will need to add some code for the ul and li tags:
========================================================= */
.myouter {
background-color: #fff;
width: 400px;
border: 1px solid #900;
margin-left:auto;
margin-right: auto;
}
.myinner {
background-color: #ffcc66;
padding: 5px;
color: #900;
font-weight: bold;
}
.myinner ul {
margin-bottom: 5px;
}
.myinner ul li a{
}
.myinner ul li a:hover{
text-decoration:underline;
}
If you want both box versions in your stylesheet, name the classes for one of them something other than myinner and myouter.
Note: if you want this to work in your WordPress theme’s style.css, you may have to put the post classes before the .myinner and .myouter to get this to work. This is what worked in my style.css:
…code here…
}
.post .entry .myinner {
…code here…
}
Curves: The Next Challenge
If you want to add curves, read some of these wonderful posts:
- CSS 3 and The Future: Image-free Rounded Corners, Drop Shadows and Gradients
- 5 Methods for CSS Curvy Corners
- 12 Really Useful CSS3 Tips And Techniques
Need More Basic? Intro to CSS Links







Thanks. Very good post with proper examples and code.
Thanks, Dhaval. I learned that it takes a while to write a post with code – you have to check your work in several places.
[...] This post was Twitted by purplehayz [...]
I only see one blue box in this post. The links don’t appear in a red box, to me.
Hannah @A Mother in Israel´s last [type] ..Links on RivkA z”l and what z”l means- Books- Carnivals and More- 12
Thanks, Hannah, I fixed it. The danger in upgrading a theme is sometimes styles don’t get transferred properly. I just added them back in.
You should be able to see the red boxes now.
Also, in the code for the red box, it lists padding twice in the same paragraph. Is that right?
Hannah @A Mother in Israel´s last [type] ..Links on RivkA z”l and what z”l means- Books- Carnivals and More- 12
I deleted one of them. Thanks for proofreading… the second padding didn’t do any harm, but it didn’t do any good, either.