Bootstrap Utilities: Code

Bootstrap 4 allows for single lines of code to be formatted using the code element or code class. By applying this class, Bootstrap gives the element some extra padding, decreases the font-size of the text contained within the element, applies a border radius, and changes the foreground and background color:

<code>

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

</code>

Refer to Figure 8.11 to see the class in action:


Figure 8.11: Text rendered using the code element; note how the class is intended only to be used with single lines of code

To format blocks of code (that is, multiple lines), the pre element or class should be used. This class does the following:

  • Sets the display property to block
  • Reduces the font size
  • Changes the foreground color to #212529
  • Adjusts the element’s margin

<pre>

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

</pre>


Figure 8.12: A block of code rendered using the pre element; note how the class is intended only to be used with multiple lines of code, forcing all lines to be rendered on one

Code variables can be denoted using the var element or class (refer to Figure 8.13):

<var>c</var> = <var>a</var> + <var>b</var>

 

Figure 8.13: Displaying variables using the var tag

When to use <code> instead of <pre>?

The code tag should be used for displaying wrapped inline code.

Source: Jakobus Benjamin, Marah Jason (2018), Mastering Bootstrap 4: Master the latest version of Bootstrap 4 to build highly customized responsive web apps, Packt Publishing; 2nd Revised edition.

Leave a Reply

Your email address will not be published. Required fields are marked *