CIP 1101  ·  Integrative Programming and Technologies 1

Text Formatting & CSS Selectors

Charles Iñigo G. Desuyo  ·  Lecture 5

Paragraphs & Headings Italic & Highlight Super & Subscript Monospaced Text Blockquote & pre CSS Selectors
Lecture Notes & Examples
Lecture 5

HTML Text Formatting

Paragraph — <p>

The <p> element defines a paragraph. Browsers automatically add margin before and after each paragraph. A paragraph is composed of two or more sentences.

Code

<!--Paragraph Example-->
<p>
    This is a sentence. A paragraph is composed of two or more sentences.
    So now, I am a pragraph! Wow!
</p>

Output

This is a sentence. A paragraph is composed of two or more sentences. So now, I am a pragraph! Wow!

Headings — <h1> through <h6>

HTML defines six heading levels. <h1> carries the most weight and is used for main titles; <h6> is the smallest. Proper heading hierarchy improves both accessibility and SEO.

Code

<!--Headings Example-->
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<h4>Heading 4</h4>
<h5>Heading 5</h5>
<h6>Heading 6</h6>

Output

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
Heading 6
Italicized Text — <i>, <dfn>, <cite>

All three tags render text in italic but have different semantic meanings: <i> is used for stylistic italics, <dfn> marks a term being defined, and <cite> references a creative work (book, film, etc.).

Code

<i>This is a sentence.</i>   <!-- stylistic italic -->
<dfn>This is a sentence.</dfn> <!-- definition term -->
<cite>This is a sentence.</cite><!-- citation/work title -->
<i>A phrase.</i>
<dfn>A phrase.</dfn>
<cite>A phrase.</cite>

Output

This is a sentence.
This is a sentence.
This is a sentence.
A phrase.
A phrase.
A phrase.

Notice the color difference: <dfn> renders in purple and <cite> in blue in this styled preview, highlighting their distinct semantic roles even though they visually appear as italic.

Highlighted Text — <mark>

The <mark> element highlights text, similar to using a highlighter on paper. It is used to mark text of special interest or relevance in a given context.

Code

<mark>This text is highlighted.</mark>

Output

This text is highlighted.
Superscripts & Subscripts — <sup> & <sub>

<sup> renders text as superscript (raised above baseline), commonly used for exponents or ordinals like X2. <sub> renders text as subscript (below baseline), used in chemical formulas like H2O.

Code

<sup>This is a superscript text.</sup>
<sub>This is a subscript text.</sub>

Output

Normal text This is a superscript text. and This is a subscript text.
Monospaced Text — <code>, <kbd>, <samp>

All three render in a monospace font but serve different purposes: <code> represents a fragment of computer code, <kbd> represents keyboard input, and <samp> represents sample output from a program.

Code

<code>This is a sentence.</code>
<kbd>This is a sentence.</kbd>
<samp>This is a sentence.</samp>
<code>A phrase.</code>
<kbd>A phrase.</kbd>
<samp>A phrase.</samp>

Output

This is a sentence.

This is a sentence.

This is a sentence.

A phrase.  A phrase.  A phrase.

Underlined Text — <u> & <ins>

<u> renders text with an underline for stylistic purposes (e.g., proper names in Chinese). <ins> marks text that has been inserted into the document — it also underlines but carries semantic meaning for document revision tracking.

Code

<u>This text is underlined.</u>
<br>
<ins>This line is inserted.</ins>

Output

This text is underlined.
This line is inserted.
&nbsp; — Non-breaking Space for Indentation

The HTML entity &nbsp; inserts a non-breaking space character. Unlike a normal space, it will not collapse or wrap. It can be chained together to add indentation inside a paragraph.

Code

<p>&nbsp;&nbsp;&nbsp;This sentence is indented.</p>

Output

   This sentence is indented.

The <div> Element with CSS Class

The <div> element is a generic block-level container. When combined with a CSS class attribute, it can be targeted with style rules. Below, the .myDiv class applies a red background and black outset border.

CSS (style.css)
.myDiv {
  border: 5px outset black;
  background-color: red;
  text-align: center;
}
HTML
<div class="myDiv">
  <h2>Heading in div</h2>
  <p>Some text in a div.</p>
</div>

Output

This is a heading in a div element

This is some text in a div element.

Blockquote — <blockquote>

The <blockquote> element defines a section quoted from another source. Browsers typically indent blockquotes. The cite attribute specifies the URL of the source.

Code

<blockquote cite="http://www.worldwildlife.org/...">
  For 50 years, WWF has been protecting the future of nature...
</blockquote>

Output

For 50 years, WWF has been protecting the future of nature. The world's leading conservation organization, WWF works in 100 countries and is supported by 1.2 million members in the United States and close to 5 million globally.
Preserve Formatting — <pre> vs <p>

The <pre> element displays text exactly as written in the HTML — preserving spaces, line breaks, and indentation. A <p> collapses all whitespace into a single space.

<pre> — preserves formatting
You ask about the cows, wearin' my sweater
It's somethin' 'bout the weather that makes them lie down
The only time I feel I might get better
is when we are together, oh, together
<p> — collapses whitespace
You ask about the cows, wearin' my sweater It's somethin' 'bout the weather that makes them lie down The only time I feel I might get better is when we are together, oh, together

Live Output — <pre>

You ask about the cows, wearin' my sweater
It's somethin' 'bout the weather that makes them lie down
The only time I feel I might get better
is when we are together, oh, together
CSS

CSS Selectors (via style.css)

id Selector — #id

The CSS id selector targets a single unique element using the # prefix. An id must be unique on the page. Below, #para1 styles the paragraph with blue centered text.

CSS (style.css)
#para1 {
  text-align: center;
  color: blue;
}
HTML
<p>An example for id Selector</p>
<p id="para1">
  This paragraph is an example
  for id Selector.
</p>

Output

An example for id Selector

This paragraph is an example for id Selector.

class Selector — .class

The CSS class selector targets any element with a matching class attribute using the . prefix. Unlike id, the same class can be applied to multiple elements. Here, .right right-aligns and colors the text red.

CSS (style.css)
.right {
  text-align: right;
  color: red;
}
HTML
<p class="right">
  Styled using a class selector.
</p>
<h2 class="right">
  Also using the same class.
</h2>

Output

An example for class Selector

This paragraph is styled using a class selector.

Grouping Selector — h3, h4

The CSS grouping selector applies the same rules to multiple selectors at once, separated by commas. This reduces code repetition. Here, both h3 and h4 are centered and colored green.

CSS (style.css)
h3, h4 {
  text-align: center;
  color: green;
}
HTML
<h3>
  Styled using a group selector.
</h3>
<h4>
  Also using the same group.
</h4>

Output

An example for group Selector

This heading is styled using a group selector.