Mark & kbd for pre

Mark & kbd for pre

In the world of HTML, semantic elements play a crucial role in defining the structure and meaning of your content. Two such elements, <mark> and <kbd>, often get overlooked but can significantly enhance the user experience, especially when used within the <pre> tag. In this blog post, I'll dive deep into how you can effectively use these elements to highlight important information and improve code readability.

As a seasoned HTML developer, I've found that using these elements judiciously can make your documentation and code examples much clearer and more accessible. You might be surprised to know how much of a difference a simple <mark> tag can make in drawing attention to key parts of your code. Let's explore the possibilities together.

Over my 5 years of experience, I've seen countless ways developers present code snippets. Some are clear and concise, while others leave you scratching your head. The key is to guide your users through the code, highlighting the important bits and explaining the context. That's where <mark> and <kbd> come into play.


Helpful tip: Use <mark> sparingly to avoid overwhelming the reader.

The <mark> element is used to highlight parts of your text. It's a simple yet powerful way to draw attention to specific words or phrases. Think of it as a digital highlighter. You can use it to emphasize keywords, important instructions, or any other text you want to stand out.

<p>This is a <mark>highlighted</mark> word.</p>

In the example above, the word "highlighted" will be displayed with a background color, making it stand out from the rest of the text. You can customize the appearance of the <mark> element using CSS to match your website's design.

I remember once working on a project where we had to display a lot of technical documentation. We used the <mark> tag extensively to highlight important parameters and function names. The feedback from our users was overwhelmingly positive; they found the documentation much easier to follow.

Now, let's talk about the <kbd> element. This element represents user input, typically from a keyboard. It's commonly used to display keyboard shortcuts or commands. For example:

<p>Press <kbd>Ctrl</kbd> + <kbd>S</kbd> to save.</p>

This will display "Ctrl + S" in a monospace font, indicating that it's a keyboard shortcut. The <kbd> element helps users quickly identify the keys they need to press to perform a specific action.


One of the most effective ways to use <mark> and <kbd> is within the <pre> tag. The <pre> tag is used to display preformatted text, typically code snippets. By combining these elements, you can create highly readable and informative code examples.

<pre>
  <code>
    <span class="keyword">function</span> <span class="function">greet</span>(<span class="parameter">name</span>) {<br>
      <mark><span class="keyword">return</span> <span class="string">"Hello, "</span> + <span class="parameter">name</span> + <span class="string">"!"</span>;</mark><br>
    }
  </code>
</pre>

In this example, I've used <mark> to highlight the return statement, which is the most important part of the function. This helps users quickly understand what the function does. I once forgot to properly escape characters within a <pre> block and spent a frustrating hour debugging why my code examples were rendering incorrectly. Always double-check your code!

Here's an example using <kbd> within a <pre> tag to show keyboard shortcuts for a command-line tool:

<pre>
  <code>
    <strong>Commands:</strong><br>
    <kbd>--help</kbd>: Shows the help menu.<br>
    <kbd>--version</kbd>: Displays the version number.<br>
    <kbd>Ctrl + C</kbd>: Exits the program.
  </code>
</pre>

This provides a clear and concise way to display the available commands and their corresponding keyboard shortcuts.


Let's look at some more advanced styling options for the <mark> tag using CSS. You can change the background color, text color, and even add custom effects. Here are some examples:

<mark class='red'>This is red.</mark>
<mark class='blue'>This is blue.</mark>
<mark class='green'>This is green.</mark>
mark.red {
  background-color: red;
  color: white;
}

mark.blue {
  background-color: blue;
  color: white;
}

mark.green {
  background-color: green;
  color: white;
}

These examples demonstrate how you can use CSS classes to create different styles for the <mark> element. Remember to always provide sufficient contrast between the background and text colors to ensure readability.

You can also use the data-* attributes to add custom prefixes or suffixes to the <mark> element. This can be useful for adding context or units to the highlighted text. For example:

<mark data-before='$'>100</mark>
mark[data-before]::before {
  content: attr(data-before);
}

This will display "$100", with the dollar sign appearing before the number. This is a great way to add context without cluttering the code.

Information alert: Remember to test your code examples on different browsers and devices to ensure they render correctly.

Here's how you can use the provided <mark> tag customizations:

<mark>مرحبا بالعالم!</mark>
<mark class='block'>مرحبا بالعالم!</mark>
<mark class='gray'>مرحبا بالعالم!</mark>
<mark class='red'>مرحبا بالعالم!</mark>
<mark class='orange'>مرحبا بالعالم!</mark>
<mark class='blue'>مرحبا بالعالم!</mark>
<mark class='green block'>مرحبا بالعالم!</mark>
<mark class='gold'>مرحبا بالعالم!</mark>
<mark class='purple'>مرحبا بالعالم!</mark>
<mark data-before='نص بعد التمييز'>مرحبا بالعالم!</mark>
<mark class='block red' data-after='قبل التمييز'>مرحبا بالعالم!</mark>
<mark class='green' data-before='$'>مرحبا بالعالم!</mark>
<mark data-after='$'>مرحبا بالعالم!</mark>
<mark class='block green' data-before='+'>مرحبا بالعالم!</mark>
<mark class='block red' data-after='-'>مرحبا بالعالم!</mark>

These examples show how you can use CSS classes and data-* attributes to customize the appearance and behavior of the <mark> element.


Important warning: Avoid using <mark> for purely decorative purposes. It should always be used to highlight important information.

In conclusion, the <mark> and <kbd> elements are valuable tools for enhancing the readability and usability of your HTML content, especially when used within the <pre> tag. By using these elements judiciously, you can guide your users through your code examples and documentation, making it easier for them to understand and learn.

Experiment with different styles and techniques to find what works best for your specific needs. And remember, always prioritize clarity and readability. With a little creativity, you can create code examples that are both informative and visually appealing.

When should I use the <mark> tag?

I've found that the <mark> tag is most effective when highlighting specific keywords, important instructions, or any text that needs to stand out. Avoid using it excessively, as it can become distracting.

How can I customize the appearance of the <mark> tag?

You can customize the <mark> tag using CSS. I often use CSS classes to define different styles for different types of highlighted text. For example, you can change the background color, text color, and font style.

What's the best way to use <kbd> within a <pre> tag?

I recommend using <kbd> within a <pre> tag to display keyboard shortcuts or commands. This provides a clear and concise way to show users how to interact with your application or tool. Make sure to use a monospace font for the <kbd> element to ensure readability.

About the author

Jamal El Hizazi
Hello, I’m a digital content creator (Siwaneˣʸᶻ) with a passion for UI/UX design. I also blog about technology and science—learn more here.
Buy me a coffee ☕

Post a Comment