HTML <q> Element

Publish in HTML Tutorial el 25/05/2025 15:35

The <q> tag in HTML is used to define a short inline quotation. Browsers usually render the content inside this element with quotation marks around it, making it an ideal choice for including quoted text within paragraphs or other inline elements without breaking the flow of the document.


This element also supports the cite attribute, which allows you to specify the source URL of the quotation. While the quotation marks are automatically added by the browser, developers can further style the <q> element using CSS for more visual control.


Basic Syntax

To use the <q> element, simply wrap the quoted text inside the opening and closing tags:

This will display as a short quote wrapped in quotation marks by default:

Play Code


Examples Using <q>

Example 1: Basic Inline Quote

This example shows how to use the <q> element inside a paragraph to denote a short quote.

Play Code


Example 2: Styled Quote with Custom Styling

In this example, we apply some custom CSS to make the quote stand out visually.

Play Code


Example 3: JavaScript Dynamic Quote Display

This example demonstrates how to dynamically insert a quote using JavaScript inside a <q> element.

Play Code


Example 4: Dynamic Source Attribution with JavaScript

This example adds a dynamic attribution link based on the quote shown, demonstrating how JavaScript can interact with the <q> element's cite attribute.

Play Code


Example 5: Random Quote Generator

This final example uses JavaScript to randomly select and display a quote from a predefined array of quotes each time the page loads.

Play Code


Tips and Tricks

  • Use the cite attribute to reference the source of the quote, even if it's just a placeholder.
  • Combine <q> with <blockquote> for longer citations that require paragraph-level structure.
  • Style the <q> element using CSS to enhance readability or match your site's design language.
  • Avoid nesting multiple <q> elements unless necessary; use sparingly to maintain semantic clarity.
  • You can dynamically change the content of a <q> element using JavaScript, just like any other HTML element.

The <q> element is a powerful tool for marking up inline quotations with semantic meaning. By understanding its usage and styling options, you can enhance both the accessibility and presentation of quoted content on your website.

También te puede interesar

HTML <style> Element
HTML <style> Element

The <style> HTML element contains style information for a document, or part of a document. ...

HTML <strong> Element
HTML <strong> Element

The <strong> element is used to indicate that its content has strong importance, seriousnes...

HTML <span> Element
HTML <span> Element

The <span> element is an inline container used to mark up a part of a text or document. Unl...

HTML <source> Element
HTML <source> Element

The <source> element is used to specify multiple media resources for media elements like &l...

HTML <small> Element
HTML <small> Element

The <small> element is used to represent side-comments and small print, typically for discl...

HTML <section> Element
HTML <section> Element

HTML <section> Element: The Complete Guide The <section> element is a semantic HTML ...