Select Page

Formatting Text

Paragraphs and Divs

Keeping Content together is a big part of writing your site! Both paragraphs and divs are meant to be containers. Both <p> and <div> put a space after themselves. Like paragraphs in books, we want to keep them separated. The only major difference between them is that people often use <div> as a catchall and write special classes for it in CSS, which we’ll get to soon!

<!doctype html>
<html>
  <body>
    <div>
      I'm inside of a div tag.
​
      I'm also inside of the div tag.
    </div>
    <p>
      I'm inside of a paragraph tag.
    </p>
    <p>
      I'm inside of another paragraph tag.
    </p>
  </body>
</html>

Did you notice that when the code was rendered, the second line of our <div> didn’t actually show up as a second line? That’s because no matter how many line breaks we enter on our keyboard, HTML will not render a line break. For that we need to add a <br />.

<!doctype html>
<html>
  <body>
     <div>
      I'm inside of a div tag.
      <br />
      I'm also inside of the div tag.
    </div>
    <p>
      I'm inside of a paragraph tag.
      <br />
      I'm inside of the same paragraph tag.
    </p>
  </body>
</html>

The <br />

Italic Lettering:

To emphasise or stress words in your HTML, you can use italics! <i> and <em> both look like italics, but <i> shows greater emphasis to a screen reader than does <em>. Take a look at the code:

&lt;!doctype html>
&lt;html>
  &lt;body>
    &lt;div>
      I make some pretty bad jokes sometimes, so I need to &lt;i>italicize&lt;/i> and &lt;em>emphasize&lt;/em> my words a lot so people know I'm telling a joke!!
    &lt;/div>
  &lt;/body>
&lt;/html>

Bold Lettering:

To boldly letter something in our code, we need to use the <strong> or the <b> elements. They both look the exact same to us, but for screen readers for visually impaired people <strong> shows a little bit less emphasis. Take a look and how this code renders:

&lt;!doctype html>
&lt;html>
  &lt;body>
    &lt;div>
      Sometimes when you want some text to be bolder than other text, you can use the &lt;b>bold&lt;/b> or &lt;strong>strong&lt;/strong> tags!
    &lt;/div>
  &lt;/body>
&lt;/html>

Underlining

Sometimes bold and italics don’t get the point across as much as we’d want, that’s when we’d want to start underlining our text! To do this, we use the <u> element. This Underlines all text between the <u> and </u>. Check out how underlining works:

&lt;!doctype html>
&lt;html>
  &lt;body>
    &lt;div>
      &lt;u>I FEEL EXTREMELY IMPORTANT&lt;/u>!!
​
      &lt;div> Also, you &lt;b>might&lt;/b> think we're crazy but you can &lt;b>&lt;i>&lt;u>ADD ALL THREE TOGETHER&lt;/u>&lt;/i>&lt;/b>&lt;/div>
    &lt;/div>
  &lt;/body>
&lt;/html>


Headers

Paragraphs and divs are great and all, but how should we title them? Luckily, we can do that with our header elements! Headers range in size from <h1> to <h6>. H1 headers are the biggest, and H6 headers are the smallest. Take a gander:

&lt;!doctype html>
&lt;html>
  &lt;body>
    &lt;h1> H1. Welcome to my site! &lt;/h1>
    &lt;h2> H2. This is where I can introduce you to myself &lt;/h2>
    &lt;h3> H3. I really like to write code &lt;/h3>
    &lt;h4> H4. I also really like to take pictures of bugs &lt;/h4>
    &lt;h5> H5. It would be super neat if I could make a photo blog for all of my bug pictures &lt;/h5>
    &lt;h6> H6. Maybe once we learn how to include images into our site we can start posting bug pictures? &lt;/h6>
    &lt;div>
       I'm normal sized text, by the way. Just for reference.
    &lt;/div>
  &lt;/body>
&lt;/html>

Headers get consecutively smaller as the numbers go! Be sure not to go too small, or you might end up with a header smaller than your actual text!

Horizontal Lines

Sometimes you might need to visually differentiate parts of your websites. One way to do this is with the <hr /> tag. Notice that it does not have a closing tag, that is because nothing goes inside of it!

&lt;!doctype html>
&lt;html>
  &lt;body>
    &lt;div>
      I'm above the line!
      &lt;hr />
      I'm below the line!
    &lt;/div>
  &lt;/body>
&lt;/html>

Superscripts and Subscripts

Suppose you wanted to properly format some math on your site? You can do that with superscripts <sup> and subscripts <sub>! Look at that beautiful math:

&lt;!doctype html>
&lt;html>
  &lt;body>
    &lt;div> MATH IS NEAT! &lt;/div>
    &lt;div> E = MC&lt;sup>2&lt;/sup>&lt;/div>
    &lt;div> Later, when we work with lists and arrays, you can denote a sub-element by writing array&lt;sub>n&lt;/sub> where `n` is the element number!&lt;/div>
  &lt;/body>
&lt;/html>


Code and Preformat

We’re getting pretty thick in the weeds for formatting, so this is it for the time being. The last two we have are for when we want to display ‘code’ or ‘preformatted’ text! Code <code> will render our font to look more like a monospaced text (programmers love this!), and preformat <pre> will render our text with the spaces and new lines EXACTLY as we left them! The main difference is that <pre> is a block element that makes its own section, while <code> is an inline element, like all of the previous formatters. Check out how it renders:

&lt;!doctype html>
&lt;html>
  &lt;head>
  &lt;/head>
  &lt;body>
    &lt;div> When we want to talk about a certain &lt;code>line of code&lt;/code> in our HTML, we can use the code element!&lt;/div>
    &lt;div> If we have a whole giant block of code with special spacing,
        &lt;pre> we can
                        use preformatted
     text
       with
         pre &lt; &lt;/div>
  &lt;/body>
&lt;/html>
4084