Text Formatting with CSS

Introduction

This article outlines some of the basics of text formatting with CSS. For now, we will deal with changing styles inline. Later on, we will discuss how to use CSS at a more global level.

Bare Minimum

Suppose you have a div of content, if you wanted to make all of the text within the div bold, you would use the following tag: <div style="font-weight:bold;"> Your Text Goes Here </div>. If I wanted to also underline the text, I would write: <div style="font-weight: bold; text-decoration: underline;"> Your Text Goes Here </div>. Notice that the semicolon (;) separates each of the attributes and that the colon (:) is used as an equals almost. Here is a list of some of the most basic attributes of text formatting. The vertical (|) is used as a divider for possible values of the attribute. Most of the values for the attributes are self-explanatory, and thus not all are fully explained.

Borders

Borders can be defined very specifically in CSS, they draw a box around the text they are associated with. The most direct way to define a border is using the following syntax:

There is also a short hand way of defining a border: <div style="border: red 2px solid;">Text Here</div>. All of the attributes above change every side of a border. However, you can also specify a particular border, as noted in the example following: <div style="border-bottom: red 2px solid; border-left: blue 3px dotted;">Text Here</div>. It is also possible to specify all the values of color for each specific side of a border box, explained in the xample following: <div style="border-color: red red blue blue;">Text Here</div>. The first value is attributed to the top value, the second to the right, the third to the bottom, and the last to the left.

Margins and Paddings

Margins and paddings allow for white space to surround text, giving it a more aesthetic appeal. Margins surround a div, while paddings are within a div. For example, if a div had a background color of pink, you could see the difference, as shown below.

This is a div with padding of 10 px on each side.
This is a div with margins of 10 px on each side.

Margins are defined just like borders are. Margin-left refers to the left border, margin-right to the right border, etc. And just in how borders are defined, there is a shorthand for margins as well, as shown in this example: <div style="padding: 10px 5px 10px 10px;">Text Here</a> . Again the sequence of padding values would be top, right, bottom, and left, respectively. Paddings are defined in the same way, except as padding-left, padding-right, and overall padding as the shorthand method.

Sources / For More Information