阿里云主机折上折
  • 微信号
Current Site:Index > Text emphasis tags (strong, em)

Text emphasis tags (strong, em)

Author:Chuan Chen 阅读数:25098人阅读 分类: HTML

Basic Concepts of Text Emphasis Tags

In HTML, <strong> and <em> are semantic tags specifically designed for text emphasis. They not only change the visual appearance of text but, more importantly, assign specific semantic meaning to content. <strong> indicates the importance of content, while <em> denotes emphasis or special tone.

<p>This text contains <strong>important content</strong> and <em>parts that need emphasis</em>.</p>

Detailed Usage of the strong Tag

The <strong> tag is used to indicate the importance or urgency of content. Browsers typically render its content in bold by default, but this is merely a visual representation—its core purpose lies in semantics.

<p><strong>Warning:</strong> Please back up data before proceeding.</p>

It is particularly suitable for the following scenarios:

  • Security warnings or important notices
  • Critical operation steps
  • Information that requires special attention
<div class="notice">
  <p><strong>System Maintenance Notice:</strong> The server will undergo upgrades from 23:00 to 24:00 tonight.</p>
</div>

In-Depth Analysis of the em Tag

The <em> tag is used to indicate text that requires emphasis, and browsers typically render it in italics. Unlike <strong>, it is more about tonal emphasis rather than the importance of content.

<p>Are you <em>sure</em> you want to delete this file?</p>

Typical use cases include:

  • Changes in tone in dialogue
  • Words that need special attention
  • Definitions of terms in technical documentation
<p>In CSS, <em>selectors</em> are used to specify HTML elements to style.</p>

Visual Presentation Differences Between the Two

Although browsers have default styles for these tags, their actual display can be fully customized with CSS:

strong {
  font-weight: bold;
  color: #d9534f;
}

em {
  font-style: italic;
  text-decoration: underline;
}

Nesting Usage Scenarios

These tags can be nested to express more complex semantic layers:

<p>This <em><strong>particularly important</strong> notice</em> requires careful reading.</p>

Differences from b and i Tags

While <b> and <i> can produce similar visual effects, they lack semantic meaning:

<!-- Not recommended -->
<p>This is <b>bold</b> and <i>italic</i> text.</p>

<!-- Recommended -->
<p>This is <strong>important</strong> and <em>emphasized</em> text.</p>

Practical Applications in Forms

In form validation, these tags can effectively mark required fields and error messages:

<div class="form-group">
  <label for="username">Username <em>(required)</em></label>
  <input type="text" id="username" required>
  <p class="error"><strong>Error:</strong> Username cannot be empty</p>
</div>

Accessibility Considerations

Screen readers adjust their tone based on these semantic tags:

  • <strong> typically results in a heavier voice
  • <em> may cause changes in intonation
<p>Please <strong>immediately</strong> save your work, <em>or</em> data may be lost.</p>

Best Practices in Technical Documentation

When writing API documentation, judicious use of these tags can enhance readability:

<article>
  <h2>fetch() Method</h2>
  <p><strong>Note:</strong> This API requires network permissions.</p>
  <p>The <em>options</em> parameter is an optional configuration object.</p>
</article>

Style Adjustments in Responsive Design

On different devices, the styles of emphasis tags can be adjusted to optimize the reading experience:

@media (max-width: 768px) {
  strong {
    font-size: 1.1em;
    background-color: #fffde7;
  }
  
  em {
    letter-spacing: 0.5px;
  }
}

Combining with CSS Pseudo-Elements

Additional decorations can be added to emphasized text via CSS:

strong::after {
  content: "!";
  color: red;
}

em {
  position: relative;
}

em::before {
  content: "";
  position: absolute;
  left: -5px;
  top: 0;
  bottom: 0;
  width: 3px;
  background: #ffeb3b;
}

Applications in JavaScript Dynamic Content

Dynamic addition of emphasis tags via JavaScript can enhance interactive feedback:

function showError(message) {
  const errorDiv = document.createElement('div');
  errorDiv.innerHTML = `<strong>Error:</strong><em>${message}</em>`;
  document.body.appendChild(errorDiv);
}

Considerations in Internationalization Scenarios

Emphasis methods may vary across different language environments:

<!-- Chinese -->
<p>This feature is <strong>very important</strong>, so please <em>be sure</em> to read the instructions carefully.</p>

<!-- Japanese -->
<p>この機能は<strong>非常に重要</strong>ですので、<em>必ず</em>説明をお読みください。</p>

Special Handling in Print Styles

Emphasized text can be optimized specifically for print media:

@media print {
  strong {
    font-weight: 900;
    text-decoration: underline;
  }
  
  em {
    font-style: normal;
    border-bottom: 1px dotted #000;
  }
}

本站部分内容来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn

Front End Chuan

Front End Chuan, Chen Chuan's Code Teahouse 🍵, specializing in exorcising all kinds of stubborn bugs 💻. Daily serving baldness-warning-level development insights 🛠️, with a bonus of one-liners that'll make you laugh for ten years 🐟. Occasionally drops pixel-perfect romance brewed in a coffee cup ☕.