Introduction to <caption>tag

The <caption>tag in HTML is used to provide a descriptive title for a table. It is placed directly after the <table>tag and serves as a summary or explanation of the data contained within the table. The <caption>element enhances accessibility by helping screen readers convey the context of the table to visually impaired users, thereby making it an important element for web accessibility.

Copy to clipboard
<!DOCTYPE html>
  <html lang="en">
  <head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>title of <caption> tag</title>
  <link rel="stylesheet" href="styles.css">
  </head>
  <body>
  
  <!-- First Table -->
  <table class="styled-table with-borders">
      <caption class="enhanced-caption">
        Sales Report 2024
        <div class="caption-underline"></div>
      </caption>
      <thead>
        <tr>
          <th>Product</th>
          <th>Sales</th>
          <th>Revenue</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Product A</td>
          <td>500</td>
          <td>$10,000</td>
        </tr>
        <tr>
          <td>Product B</td>
          <td>300</td>
          <td>$6,000</td>
        </tr>
      </tbody>
    </table>
  
    <!-- Second Table -->
    <table class="styled-table-alt with-borders">
      <caption class="enhanced-caption-alt">
        Customer Feedback
        <div class="caption-underline-alt"></div>
      </caption>
      <thead>
        <tr>
          <th>Customer</th>
          <th>Feedback</th>
          <th>Rating</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>John Doe</td>
          <td>Great service!</td>
          <td>★★★★★</td>
        </tr>
        <tr>
          <td>Jane Smith</td>
          <td>Good product quality.</td>
          <td>★★★★☆</td>
        </tr>
      </tbody>
    </table>
    
    <!-- Third Table -->
    <table class="styled-table-dark with-borders">
      <caption class="enhanced-caption-dark">
        Inventory Status
        <div class="caption-underline-dark"></div>
      </caption>
      <thead>
        <tr>
          <th>Item</th>
          <th>Stock</th>
          <th>Status</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Laptop</td>
          <td>20</td>
          <td>In Stock</td>
        </tr>
        <tr>
          <td>Smartphone</td>
          <td>0</td>
          <td>Out of Stock</td>
        </tr>
      </tbody>
    </table>
  
  </body>
  </html>
Copy to clipboard
body {
        background-color: #323645;
    }
    
    * {
        color-scheme: dark;
    }
    
    /* General table styling */
    table {
        width: 100%;
        border-collapse: collapse; /* No space between table borders */
        margin-bottom: 30px;
        background-color: #2b2f3a; /* Darker background color to match image */
    }
    
    /* Table borders */
    .with-borders th, .with-borders td {
        border: 1px solid #3b3f4a; /* Slightly lighter dark gray for borders */
    }
    
    /* Enhanced styling for the table captions */
    .enhanced-caption, .enhanced-caption-alt, .enhanced-caption-dark {
        font-size: 1.8em;
        font-weight: bold;
        text-align: center;
        color: #ffffff;
        padding: 20px 30px 10px; /* Reduced bottom padding to make space for underline */
        margin-bottom: 10px;
        border-radius: 12px;
        display: inline-block;
        position: relative;
        overflow: hidden;
        transition: transform 0.4s ease, box-shadow 0.4s ease;
    }
    
    /* Specific styles for each caption */
    .enhanced-caption {
        background-image: linear-gradient(135deg, #ff416c 0%, #ff4b2b 100%);
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 0 15px rgba(255, 75, 43, 0.5);
    }
    
    .enhanced-caption-alt {
        background-image: linear-gradient(135deg, #36d1dc 0%, #5b86e5 100%);
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 0 15px rgba(91, 134, 229, 0.5);
    }
    
    .enhanced-caption-dark {
        background-image: linear-gradient(135deg, #2c3e50 0%, #4ca1af 100%);
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 0 15px rgba(76, 161, 175, 0.5);
    }
    
    /* Hover effects for captions */
    .enhanced-caption:hover, .enhanced-caption-alt:hover, .enhanced-caption-dark:hover {
        transform: scale(1.1);
        box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3), 0 0 25px rgba(255, 75, 43, 0.8);
    }
    
    /* Decorative line under the caption */
    .caption-underline, .caption-underline-alt, .caption-underline-dark {
        width: 80%;
        height: 4px;
        margin: 8px auto 0; /* Centered and spaced below the caption text */
        border-radius: 2px;
    }
    
    /* Different line colors to match each table's style */
    .caption-underline {
        background-color: #ff4b2b;
    }
    
    .caption-underline-alt {
        background-color: #5b86e5;
    }
    
    .caption-underline-dark {
        background-color: #4ca1af;
    }
    
    /* Alternating row colors */
    .styled-table-alt tbody tr:nth-child(even) {
        background-color: #2f3340; /* Slightly lighter than the table background */
    }
    
    .styled-table-alt tbody tr:hover {
        background-color: #3b3f4a; /* Darker shade for hover effect */
    }
    
    /* Dark themed table */
    .styled-table-dark {
        background-color: #2b2f3a; /* Matches the main table background */
        color: #ffffff;
        border: 1px solid #3b3f4a;
        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
    }
    
    .styled-table-dark th, .styled-table-dark td {
        border: 1px solid #3b3f4a;
    }
    
    .styled-table-dark tbody tr:hover {
        background-color: #3b3f4a; /* Hover effect for dark-themed table */
    }
    
    /* General cell styling */
    th, td {
        padding: 12px;
        text-align: center;
        color: #ffffff; /* White text for better contrast */
        background-color: #2b2f3a; /* Matches table background */
    }
    
    /* Header styling */
    th {
        background-color: #323645; /* Slightly darker for headers */
        font-weight: 600;
    }
    
    .styled-table-dark th {
        background-color: #323645; /* Same color for dark-themed headers */
    }
    
    /* Keyframes for the fade-in and slide-up animation */
    @keyframes fadeInSlideUp {
        from {
            opacity: 0;
            transform: translateY(20px);
        }
        to {
            opacity: 1;
            transform: translateY(0);
        }
    }

Global Attributes

AttributesDescription
accesskeySpecifies a shortcut key to activate or focus an element.
autocapitalizeControls whether and how text input is automatically capitalized.
classSpecifies one or more class names for the element, allowing CSS styling and JavaScript manipulation.
contenteditableSpecifies whether the content of an element is editable or not.
contextmenuSpecifies a context menu for the element.
dirDefines the text direction. Possible values are "ltr" (left-to-right), "rtl" (right-to-left), and "auto".
draggableSpecifies whether an element is draggable. Possible values are "true" or "false".
enterkeyhintSpecifies the action label or icon to be shown for the enter key on virtual keyboards.
hiddenIndicates that the element is not yet, or is no longer, relevant. The browser does not display elements that have the `hidden` attribute set.
idSpecifies a unique identifier for the element.
inputmodeProvides a hint to browsers for which virtual keyboard configuration to use when editing this element or its descendants.
isAllows you to specify the type of custom element.
langSpecifies the language of the element's content.
nonceA cryptographic nonce ("number used once") that can be used by Content Security Policy (CSP) to determine whether or not a given fetch will be allowed to proceed.
partSpecifies the element’s part in the shadow DOM.
slotAssigns a slot in a shadow DOM shadow tree.
spellcheckIndicates whether the element is subject to spell checking.
styleProvides inline CSS styling for the element.
tabindexSpecifies the tab order of the element.
titleAdds extra information about the element, displayed as a tooltip when hovering over the element.
translateSpecifies whether the content of the element should be translated. Possible values are "yes" or "no".
HTML AttributesGlobal AttributesEvent Attributes
To view the full list
To view the full list
To view the full list
elementsChrome BrowsersMicrosoft Edge BrowserFirefox BrowsersSafari BrowserOpera Browser
<caption>YesYesYesYesYes