- How to Create a Coloured («Colored») Box in HTML/CSS
- How to put text into a box that has a background colour
- How to Create a Coloured («Colored») Box in HTML/CSS
- Prerequisites
- Creating a Coloured Box
- Demo
- Browser Compatibility
- thesitewizard™ News Feed (RSS Site Feed)
- Please Do Not Reprint This Article
- Related Articles
- New Articles
- Popular Articles
- How to Link to This Page
- Create Colored Boxes
- Invalid Short Codes & CSS
- 3 Ways to Make Colored Boxes
- Using Pure CSS Code
- Creating Shortcodes for Boxes
- Difference Between Shortcodes & CSS
- Changing Themes Without Breaking Code
- Reader Interactions
- Leave a Reply Cancel reply
- Primary Sidebar
How to Create a Coloured («Colored») Box in HTML/CSS
How to put text into a box that has a background colour
How to Create a Coloured («Colored») Box in HTML/CSS
I was asked by a visitor how he could create a box, give it a background colour («color» if you use a different variant of English), and insert text in it, the way some printed magazines and books sometimes place additional information in a separate box or panel on a page.
Prerequisites
This article assumes that you know a bit of HTML and CSS. Otherwise you will be at a loss as to where to put the code I supply below or how to adapt it for your purpose.
Creating a Coloured Box
The box itself can be any block tag in HTML. Many, if not most, webmasters use a for this purpose.
Let’s say that you have the following HTML snippet that you want to make into a box.
The CSS to give the DIV block a background colour is, predictably:
The background-color rule above specifies the HTML colour value of #cfc . You can of course use any other colour you like. Most web editors and plain text editors (other than the rudimentary Notepad that comes with Windows) have a colour picker, allowing you to visually select a colour to get the appropriate numerical value. Alternatively, if you prefer to do things the hard way, you can also consult the list of colours and their values on Wikipedia.
Unfortunately, although the above code is correct, if you use it as it stands, you will find that the background colour will hug the text you place very closely, causing the entire thing to seem barely like a box. To make things more box-like, you will probably want to add some space to the area around your text, and perhaps even give it a border.
The padding rule adds 10 pixels to the space between your text and the margins of the box, and the border rule creates a 1-pixel thick solid green border. You can of course change the values given here (ie, the number of pixels and the colour) to suit your page’s design.
If you’re posting to a blog that allows you to insert HTML, but does not make it easy for you to change the style sheet, you can even put those rules into your DIV tag.
Demo
The above code produces the following box.
This is a demo box to illustrate the code given in thesitewizard.com’s tutorial on creating coloured boxes.
Browser Compatibility
The CSS given above should work in all current browsers. It will probably even work in most older browsers too, including Internet Explorer 6 (which is most likely extinct today).
Copyright © 2017-2018 Christopher Heng. All rights reserved.
Get more free tips and articles like this, on web design, promotion, revenue and scripting, from https://www.thesitewizard.com/.
thesitewizard™ News Feed (RSS Site Feed)
Do you find this article useful? You can learn of new articles and scripts that are published on thesitewizard.com by subscribing to the RSS feed. Simply point your RSS feed reader or a browser that supports RSS feeds at https://www.thesitewizard.com/thesitewizard.xml. You can read more about how to subscribe to RSS site feeds from my RSS FAQ.
Please Do Not Reprint This Article
This article is copyrighted. Please do not reproduce or distribute this article in whole or part, in any form.
Related Articles
New Articles
Popular Articles
How to Link to This Page
It will appear on your page as:
Copyright © 2017-2018 Christopher Heng. All rights reserved.
thesitewizard™, thefreecountry™ and HowToHaven™ are trademarks of Christopher Heng.
This page was last updated on 20 December 2018.
Create Colored Boxes
Have you ever landed on a website for the very first time which is all the same color?
How hard is it to find the most important content on that page?
On your own site you know where everything is but put yourself in your readers shows for a minute.
Around 80% of visitors to your site are new.
They have never seen your site before and when they leave, most will never come back.
So make it easy for them and more will come back.
One way to attract your readers attention is to use colored boxes. You can create colored boxes by creating shortcodes or using pure CSS code.
Invalid Short Codes & CSS
Highlight your most important: content, links and calls to action using colored boxes.
But this can cause a massive problem down the track for you.
The reason for this is most themes include this feature built into the core files.
When you change themes, you end up with invalid shortcodes, css code or html that doesn’t work.
This can be a nightmare to remove manually.
Invalid code will also look ugly, unprofessional and worst of all, the content you highlighted to grab your readers attention, is no longer highlighted.
This is called theme lock and sometimes used to try and lock you into staying with the same theme developer.
3 Ways to Make Colored Boxes
The solution to this is to either:
- Install a plugin with shortcodes which moves with you from theme to theme.
- Using CSS only – Insert a small code block into your child themes css file which works on all themes.
- Using CSS and php to create shortcodes for colored boxes.
Using Pure CSS Code
Here’s some code which you can easily paste into your child themes style.css file.
You can easily edit this code to change:
- The background color – background: #eee ;
- The border color – border: 1px solid #ddd ;
- The border thickness – 1px solid
- The padding between your text and the border – padding: 10px ;
- Create different types of boxes ( .alert, .note, .info) – .alert
Once you’ve created different colored boxes and styled them, simply wrap your content using this code in your HTML editor.
If you want 3 types of boxes, simply use the code block above and name them:
- .note – Use this to make a note box
- .alert – Use this to make an alert box
- .info – Use this to create an info box
Different themes use different code for displaying boxes
- Woo themes uses shortcodes built into the framework
- Genesis doesn’t offer any code built into the core files so you’ll need to install a plugin or use the code above.
- Thesis uses this code which won’t work in Genesis or StudioPress themes
Creating Shortcodes for Boxes
You can also create great looking boxes using shortcodes which won’t break when you move themes.
Add some PHP code and css to your files like this. Learn more about how to create short codes in WordPress to make colored boxes in my next post.
Difference Between Shortcodes & CSS
Shortcodes will enable you to include content with paragraphs within your colored boxes.
Pure CSS will only allow you to wrap the content within one paragraph.
Changing Themes Without Breaking Code
One of the big advantages of adding the code above is that you may find your old boxes start working again.
This depends on which theme you where using previously but in my case it worked after moving from Thesis.
If you do use code like this which is flexible, you won’t suffer any problems when you change themes.
How about yourself, what do you use to draw attention to your most important content?
Reader Interactions
Leave a Reply Cancel reply
You must be logged in to post a comment.
Primary Sidebar
Code written by Brad Dalton specialist for Genesis, WooCommerce & WordPress theme customization. Read More…