👀 Live Preview
Highlight a single word inside a sentence:
This is an example of styled text using the span tag.

The <span> tag is a versatile inline container for styling portions of text. This guide covers syntax, global attributes, common use cases, span vs div, and best practices for beginners.
Highlight words or phrases inside a sentence without breaking the line.
Use class and id to target text with stylesheets.
Write valid opening and closing span tags.
Wrap inline elements together for consistent styling.
Know when to use inline vs block containers.
Prefer CSS classes over excessive inline styles.
<span> Tag?The <span> tag is a generic inline container used to group text or inline elements and apply styles to a specific portion of content. Unlike block-level elements, span does not add line breaks — it stays on the same line as surrounding text.
span is a styling hook, not a semantic tag. Use strong, em, or code when the content itself carries meaning; reach for span when you need a neutral wrapper for CSS or JavaScript.
In the vast landscape of HTML elements, span is one of the most frequently used tags for precise inline formatting in tutorials, blogs, and production websites.
Wrap the desired content within opening and closing span tags:
<span>Your text here</span><p>
This is
<span class="highlight" style="color: #FF5733;">
styled text
</span>
inside a paragraph.
</p>span is phrasing content — use inside block elements like p, li, or td.<span />) is not valid in HTML.span as a substitute for block layout — use div for sections.class over repeated inline style attributes for maintainability.| Topic | Code Snippet | Notes |
|---|---|---|
| Basic span | <span>...</span> | Inline wrapper |
| With class | <span class="highlight"> | CSS hook |
| Inline style | style="color: red;" | Quick tweaks |
| Bold word | style="font-weight: bold;" | Emphasis |
| vs div | span = inline | div = block |
| Browser support | Universal | All browsers |
<span> vs <div>| Feature | <span> | <div> |
|---|---|---|
| Display | Inline (stays on same line) | Block (starts new line) |
| Typical use | Style a word or phrase | Layout sections and containers |
| Semantic meaning | None by default | None by default |
| Can wrap block elements? | No (invalid nesting) | Yes |
| Example | <span class="price">$9</span> | <div class="card">...</div> |
The <span> tag has no tag-specific attributes. Combine it with global attributes like class and style to apply CSS rules.
<span class="highlight" style="color: #FF5733;">Styled text here</span>class / id GlobalPrimary hooks for CSS selectors and JavaScript targeting.
class="highlight"style GlobalInline color, font-weight, or spacing for quick one-off styling.
style="color: #FF5733;"title OptionalAdvisory tooltip text on hover for extra context.
title="Sale price"lang OptionalDeclare language when the wrapped text differs from the page.
lang="fr"data-* ScriptingStore custom values for JavaScript without extra markup.
data-role="price"hidden OptionalHide the span from display when toggling visibility in scripts.
hiddenInline styling, bold emphasis, and grouped inline elements with copy-ready code and live previews.
Highlight a single word inside a sentence:
This is an example of styled text using the span tag.
Use <span> for inline styling, highlighting words, grouping inline elements, and attaching CSS classes to portions of text.
The primary purpose of span is to apply inline styling to specific portions of text. This is useful for highlighting or emphasizing certain words or phrases:
<p>
This is an example of
<span class="highlight" style="color: #FF5733;">Styled Text Here</span>
using the span tag.
</p>Use span with inline font-weight to emphasize a single word inside a line of text:
<p>
In this example, we use the
<span style="font-weight: bold;">bold</span>
style for emphasis.
</p>The span tag is commonly used to group and style inline elements together. This helps apply consistent styling to code snippets or grouped text:
<p>
Here is a JavaScript variable:
<span class="code">const variable = 'Hello, World!';</span>
</p>strong, em, or code when meaning matters; use span only as a styling wrapper.<span></span> with no content or purpose.A word, phrase, or inline element is placed inside span.
Styles apply via class, id, or inline style.
No line break is added; styled text flows with surrounding content.
Only the wrapped portion changes appearance while the sentence stays on one line.
The <span> tag is supported in all major browsers, including Internet Explorer.
All browsers render <span> as a generic inline container.
Bottom line: Use <span> confidently for inline styling in any browser.
Mastering the <span> tag empowers you to apply precise styling to specific portions of your HTML content. Whether emphasizing text or grouping inline elements, span is a valuable tool in your web development toolkit.
span when styling a small portion of text within a larger blockspan for block-level layout (use div instead)style attributes everywherespan for accessible semantic elementsspan<span>Bookmark these before you style inline text on your pages.
No line breaks.
DisplayCSS hooks.
AttributesStyle words.
Use caseInline sets.
PatternInline vs block.
ComparisonAll browsers.
Compatibilityspan is inline and stays on the same line. div is block-level and starts a new line for layout sections.class, id, style, and data-*.strong, em, or code when meaning matters. Use span as a neutral styling hook.Practice <span> for highlights, emphasis, and grouped inline content in the Try It editor.
6 people found this page helpful