menu

html - 3 Topics

HTML5 APIs

DEEP DIVE INTO

html

Topic:mark

menu

The <mark> HTML element is used to highlight and emphasize text within a block of text. It is often rendered as a yellow background with black text, but the appearance can be styled using CSS. Let's take a deep dive into how you can style the <mark> element with CSS.

1. Understanding the <mark> Element:

  • The <mark> element is a semantic HTML5 element used to highlight or mark a specific portion of text within a block of content.

  • It is commonly used to draw attention to keywords, search results, or other important pieces of text on a webpage.

  • The default rendering for the <mark> element is typically a yellow background with black text.

2. Styling the <mark> Element with CSS:

To style the appearance of the <mark> element using CSS, you can apply various CSS properties:

1. Background and Text Color:

  • Use the background-color property to set the background color of the <mark> element.

  • The color property controls the text color within the <mark> element. You can change it to contrast with the background color effectively.

2. Font Styles:

  • Modify font properties like font-weight and font-style to adjust the appearance of the text inside the <mark> element.

3. Box Shadows and Borders:

  • If desired, you can add box shadows or borders to the <mark> element to make it stand out.

Here's an example of CSS to style the <mark> element:

cssmark {
    background-color: #ffcc00; /* Yellow background */
    color: #000; /* Black text */
    font-weight: bold;
    padding: 3px; /* Add some padding to the highlight */
}

mark:hover {
    background-color: #ff9900; /* Change the background color on hover */
}

This CSS code sets the background color to yellow, text color to black, adds some padding for spacing around the highlight, and makes the text bold. Additionally, it changes the background color to a slightly different shade on hover for a visual effect.

3. Applying <mark> in HTML:

To use the <mark> element in your HTML, simply wrap the text you want to highlight with <mark> tags, like this:

html<p>This is a <mark>highlighted</mark> word in a sentence.</p>

4. Accessibility Considerations:

When styling the <mark> element, ensure that your chosen colors and styles meet accessibility standards and provide sufficient contrast between the highlighted text and the background to make it easily readable by all users, including those with visual impairments.

In summary, the <mark> element is used to highlight and emphasize text within a web page, and you can style it using CSS to control its appearance. The specific styles you apply should match your design preferences and the overall aesthetics of your website while ensuring good accessibility and readability.

1280 x 720 px