In the previous post, we’ve talked about HTML basics. Today, I’d like to expand on this topic by introducing the basic of using CSS for styling. CSS stands for Cascading Style Sheets and is used to apply certain style to specific HTML elements. For example, you could make only the second paragraph turned green by adding some codes to the second paragraph only. But first, we’ll continue our discussion from the first post by talking about formatting text.
If you need refresher on the previous topic, click here to go to the first post.
List of Contents
Click the title to jump to specific topic.
Formatting Text: Italicize Text – the <i> and <em> tags
Formatting Text: Bold Text – the <b> and <strong> tags
Formatting Text: Underline Text – the <u> tag
Add Styling Codes – how to add style to your post
CSS Property: Color – changing text color
CSS Property: Font-Size – changing text size
CSS Property: Text-Decoration – underline, overline, and strikethrough
CSS Property: Font-Variant – small-caps
CSS Property: Text-Align – left, right, justify, and center
CSS Property: Border – create and modify border style
CSS Property: Background – change background color or use image as background
Formatting Text: Italicize Text
WordPress editor use the em tag (<em>…</em>) when you italicize text.
For example:
1 |
<em>This text is italic.</em> And this part is not. |
And the output:
However, there’s actually another way to achieve similar visual presentation, using the tag <i>….</i>. Check this out:
1 |
<i>This text is italic.</i> And this part is not. |
And the output:
The i tag slightly differs from em tag in the sense that i tag is used to format text that has different semantic meaning (such as different language, term) while em tag is used to emphasis something.
For more detailed explanation on the difference between <i> and <em>, read this article from MDN.
Formatting Text: Bold Text
Like italicize, there are two tags that could accomplish this task: the <b> and the <strong> tags.
Wordpress editor in default uses the <strong> tag if we press the b button. There’s a slight difference between the two:
- the <b> is used to give a different presentation to a specific part of text, for example when creating a list of products, you might want to bold the name to differentiate it from the description. It does not mean that the product name is more important, rather it aims to help the visitors to quickly recognize that these bold texts are the names and the rest are the description.
- <strong> is used to highlight an important part of the text. For example, when you write a book review, you might put SPOILER WARNING in all caps and in bold to make sure the visitors don’t miss it.
Here’s how they look:
1 2 |
Hello, I am <b>bold</b> with <b>b</b>. Hello, I am <strong>strong</strong>. |
The output:
Hello, I am strong.
Not much difference, right? In most browsers, it looks exactly the same! You might ask, why do I care?
The simple answer is because search engine these days could read CSS and they value text in order of their importance. Using b and strong might achieve the same visual presentation, but googlebot will value strong more than b.
Formatting Text: Underline Text
Underline used to be very popular when many webpages are written with almost no CSS and all hyperlinks are blue and easy to differentiate from other texts. These days though, it might be best to use underline very sparingly. Not only people might mistaken it for hyperlinks, underline doesn’t have a specific semantic meaning. You see, when you use strong, you mean to show its importance, when you use em it’s for emphasis, b is to differentiate some part of the text, and i has its place to mark special terms. But u? What does underline mean it’s not really clear. Some people avoid using it altogether, but I still use it now and then. Just be careful not to make it look similar to your hyperlinks.
The code:
1 |
I cannot spell mis<u>s</u>pelling. |
And the output:
Next, we’re going to start using CSS. This can be accomplished by applying style attribute on HTML element.
Add Styling Codes
In general, you can style HTML element in either one of these ways:
- External CSS file
- Internal CSS embedded in HTML document
- Inline CSS applied directly on the specific HTML element
Free user of wordpress.com (like me) could only use the third method, so I’m only going to talk about the third method. Bear in mind, however, that the recommended method is actually number 1 (external CSS file). Inline styling (as in method 3) is usually frowned upon by web developer guru. However, the free version of wordpress does not give us access to modify the stylesheet, hence, mixing them we must. If you use paid version or in any way have access to your CSS file, I highly encourage you to put your stylesheet in your CSS file (the one that ended with .css). Not only it’s reusable and easier to maintain, it also has many benefits related to readability and loading time that I will not mention in details here.
How to add inline CSS
We can add style to an HTML element using style attribute. Here’s an example on how to add style attribute to <p> or paragraph element:
1 |
<p style = "…………">Adding style attribute to this paragraph. All the styling rules will go between the quotation marks.</p> |
All the style rules will go inside the style attribute, between the two quotation marks, separated by semicolon.
Here’s how you make a paragraph text turned red and 20% larger:
1 |
<p style = "color: red; font-size: 120%;">This is a sample paragraph. It's red and slightly bigger than other text in this post. It was styled using color and font-size properties.</p> |
The result:
This is a sample paragraph. It’s red and slightly bigger than other text in this post. It was styled using color and font-size properties.
Notice the semicolon between each rule and the colon between the property and the value. Note: example of properties are color, font-size, background, etc, while value is the value of the property, for example red, 120%, etc.
Next, I’m going to discuss some of the common CSS properties that might be useful to bloggers. All the properties and their values must go inside the style attribute.
CSS Property: Color
Color property is used to modify the color of the text.
Therefore,
1 |
<p style = "color: blue;">Blue text</p> |
will turn the paragraph text blue. You can use hexcode or any acceptable color keywords as value for the color property.
Example:
1 |
<span style = "color: red;">Red</span> and <span style="color: #88e05f;">green</span> |
Output:
CSS Property: Font-Size
Font-size, as you can probably guess, is used to modify the size of the text. You can use either absolute number, in pt or px to declare the value, or use percentage. Personally, I prefer to use percentage so that it’ll always be proportional to the base textsize. Otherwise, if you change theme that use different font size, your text might look weird and out of proportion. Like this:
1 2 3 |
<p style = "font-size: 18px;">The base text size of this paragraph is 18 px. <span style = "font-size:21px;">I use bigger text size of 21px to highlight this part</span></p> <p style = "font-size: 14px;">The base text size of this paragraph is 14 px. <span style = "font-size:21px;">This part is now look too big because I use absolute value.</span></p> |
Output:
The base text size of this paragraph is 18 px. I use bigger text size of 21px to highlight this part
The base text size of this paragraph is 14 px. This part is now look too big because I use absolute value.
Compare to this:
1 2 3 |
<p style = "font-size: 18px;">The base text size of this paragraph is 18 px. <span style = "font-size:120%;">I use bigger text size of 120% to highlight this part</span></p> <p style = "font-size: 14px;">The base text size of this paragraph is 14 px. <span style = "font-size:120%;">When the base text size is reduced, the size of this part is also automatically decreased</span></p> |
Output:
The base text size of this paragraph is 18 px. I use bigger text size of 120% to highlight this part
The base text size of this paragraph is 14 px. When the base text size is reduced, the size of this part is also automatically decreased
CSS Property: Text-Decoration
Alright, let’s do more fun things, shall we?
Text-decoration property is mostly used to put a line under, above, both above and under, or through text.
I rarely put a line over text, but you might find some use for it. Strikethrough, however, is quite useful.
Acceptable values for text-decoration are underline, overline, and line-through.
Here are the codes so you can see them in action
1 2 3 4 |
<p style="text-decoration:underline;">Below the text</p> <p style="text-decoration:overline;">Above the text</p> <p style="text-decoration:underline overline;">Line above and below text</p> <p style="text-decoration:line-through;">Line through the text</p> |
The output:
Below the text
Above the text
Line above and below text
Line through the text
CSS Property: Font-Variant
Technically, there’s only one use of font-variant property, that is to set some text to small-caps.
But what on earth is small-caps? Well, it looks like this when attached to a h4 element (of course, you can attach it to other HTML elements like p or span):
1 2 |
<h4 style = "font-variant:small-caps;">Font Variant</h4> <p>Acceptable value for font-variant is small-caps</p> |
Output:
Font Variant
Acceptable value for font-variant is small-caps
Pretty neat, right?
I might or might not have abused small-caps in the past just because I like how it looks. :p
CSS Property: Text-Align
Like its name suggested, text-align property is used to set the alignment of specific text. Acceptable values are: left, right, center, and justify.
The codes:
1 2 3 4 5 6 |
<h4 style="text-align:center;">Subheading</h4> <p style="text-align:left;">Turnip greens yarrow ricebean rutabaga endive cauliflower sea lettuce kohlrabi amaranth water spinach avocado daikon napa cabbage asparagus winter purslane kale. Celery potato scallion desert raisin horseradish spinach carrot soko.</p> <p style="text-align:justify;">Nori grape silver beet broccoli kombu beet greens fava bean potato quandong celery. Bunya nuts black-eyed pea prairie turnip leek lentil turnip greens parsnip. Sea lettuce lettuce water chestnut eggplant winter purslane fennel azuki bean earthnut pea sierra leone bologi leek soko chicory celtuce parsley jÃcama salsify. <p style="text-align:right;">Celery quandong swiss chard chicory earthnut pea potato. Salsify taro catsear garlic gram celery bitterleaf wattle seed collard greens nori. Grape wattle seed kombu beetroot horseradish carrot squash brussels sprout chard.</p> |
The output:
Subheading
Turnip green yarrow ricebean rutabaga endive cauliflower sea lettuce kohlrabi amaranth water spinach avocado daikon napa cabbage asparagus winter purslane kale. Celery potato scallion desert raisin horseradish spinach carrot soko.
Nori grape silver beet broccoli kombu beet greens fava bean potato quandong celery. Bunya nuts black-eyed pea prairie turnip leek lentil turnip greens parsnip. Sea lettuce lettuce water chestnut eggplant winter purslane fennel azuki bean earthnut pea sierra leone bologi leek soko chicory celtuce parsley jÃcama salsify.
Celery quandong swiss chard chicory earthnut pea potato. Salsify taro catsear garlic gram celery bitterleaf wattle seed collard greens nori. Grape wattle seed kombu beetroot horseradish carrot squash brussels sprout chard.
CSS Property: Border
Border property is one that allows you to be very creative. There are many things you can play with and modify.
It’s actually a shorthand of different properties: the first one is border width, the second is border style, and the third is border color.
For example:
1 2 3 4 5 6 7 8 9 |
<blockquote style="border:2px dotted blue;"> <h4>Quote</h4> <p>Then a strange thing happened.</p> <p>The house whirled around two or three times and rose slowly through the air. Dorothy felt as if she were going up in a balloon.</p> <p>The north and south winds met where the house stood, and made it the exact center of the cyclone. In the middle of a cyclone the air is generally still, but the great pressure of the wind on every side of the house raised it up higher and higher, until it was at the very top of the cyclone; and there it remained and was carried miles and miles away as easily as you could carry a feather.</p> </blockquote> |
will create a blockquote with dotted blue border with 2px width, it will look like this:
Quote
Then a strange thing happened.
The house whirled around two or three times and rose slowly through the air. Dorothy felt as if she were going up in a balloon.
The north and south winds met where the house stood, and made it the exact center of the cyclone. In the middle of a cyclone the air is generally still, but the great pressure of the wind on every side of the house raised it up higher and higher, until it was at the very top of the cyclone; and there it remained and was carried miles and miles away as easily as you could carry a feather.
You can modify any of these three (width, style, and color). For width, you can specify the unit or simply use “thin”, “medium”, or “thick”. Acceptable values for border style are dashed, dotted, solid, and double. For colors, you can use hexcode or any acceptable keywords.
Furthermore, you can even have different value for top, right, bottom, and left border. Simply divide the border property into 4 properties: border-top, border-right, border-bottom, and border-left then use the shorthand to specify width, style, and color for each.
Here’s an example of a blockquote with 4 different borders on each side:
1 2 3 4 5 6 7 |
<blockquote style="border-top: 2pt solid red; border-right: 3pt dashed blue; border-bottom: 5pt double #cccccc; border-left: 1pt dotted #000000;"> <p>"No matter," replied Forster; "I think that by putting on the very highest speed we might have a chance of getting over."</p> <p>"The devil!" muttered Passepartout.</p> <p>But a number of the passengers were at once attracted by the engineer's proposal, and Colonel Proctor was especially delighted, and found the plan a very feasible one. He told stories about engineers leaping their trains over rivers without bridges, by putting on full steam; and many of those present avowed themselves of the engineer's mind.</p> </blockquote> |
The output:
“No matter,” replied Forster; “I think that by putting on the very highest speed we might have a chance of getting over.”
“The devil!” muttered Passepartout.
But a number of the passengers were at once attracted by the engineer’s proposal, and Colonel Proctor was especially delighted, and found the plan a very feasible one. He told stories about engineers leaping their trains over rivers without bridges, by putting on full steam; and many of those present avowed themselves of the engineer’s mind.
CSS Property: Background
For background, you can use either color or image. However, if you use image it’s better to also specify a color so that if the image fail to load, the browser can fallback to the background color.
Example of using background color on blockquote:
1 2 3 4 |
<blockquote style="background: mistyrose;"> <p>It was lucky the Scarecrow and the Woodman were wide awake and heard the wolves coming.</p> <p>"This is my fight," said the Woodman, "so get behind me and I will meet them as they come."</p> </blockquote> |
Output:
It was lucky the Scarecrow and the Woodman were wide awake and heard the wolves coming.
“This is my fight,” said the Woodman, “so get behind me and I will meet them as they come.”
Example of using background-image:
1 2 3 |
<blockquote style="background-image: url('.........');"> <p>We remained at Weybridge until midday, and at that hour we found ourselves at the place near Shepperton Lock where the Wey and Thames join. Part of the time we spent helping two old women to pack a little cart. The Wey has a treble mouth, and at this point boats are to be hired, and there was a ferry across the river.</p> </blockquote> |
The output:
We remained at Weybridge until midday, and at that hour we found ourselves at the place near Shepperton Lock where the Wey and Thames join. Part of the time we spent helping two old women to pack a little cart. The Wey has a treble mouth, and at this point boats are to be hired, and there was a ferry across the river.
Background is quite a complicated and interesting property, you can set it to repeat, set the position, etc. However, those topics are outside the scope of this introduction post. If you’d like to learn more about background, you can visit the background page on MDN.
There you go!
This post ended up much longer than I anticipated it to be. As usual, let me know if my explanation confuses you or if you have any correction/suggestion/question. You can also hit me up on twitter if you have any question/suggestion/rant.
Part 3 of this HTML Tips for Bloggers will (hopefully) be posted within the next 2 weeks.
See you next time!
Twitter | Follow my blog with Bloglovin
Windie
Twitter | Instagram | Follow my blog with Bloglovin
Very informative for noobs like me who have no idea how to code 😂
Thank you Jesse! Glad if it can help. 😀
We all start as noobs. I still remember when I ran my first blog (on blogspot) and had no idea how to upload image. lol.
I should get my coder friends to do everything for me 😉
xD
OMG Windie! Thank you so so much for sharing this! I love that photo background thing. I feel like I will be using that on my future posts A LOT! Haha!
Awesome! I hope you can put it to good use. I’m awful at making graphics and background so I’ll just leave it to you guys. Hahaha.