Skillversitas

Print Styles

Print styles let you change how a website looks when it is printed or previewed as a PDF. Many elements are not needed when printing, such as the navbar, sidebar, and footer. The print media query lets you remove those elements and optimize the layout for paper.

Print-specific properties include page-break-before/after/inside to control page breaking, @page for paper margins, and orphans/widows to prevent text from being cut off at the end of a page.

It is important to make sure a website remains readable when printed. Background colors are usually removed, text is guaranteed to have enough contrast, and the layout is simplified into a single column.

Code Example

<!DOCTYPE html>
<html lang="en">
<head>
<style>
  /* Screen Styles */
  body {
    font-family: Georgia, 'Times New Roman', serif;
    line-height: 1.8;
    color: #333;
    max-width: 900px;
    margin: 0 auto;
    padding: 20px;
  }

  /* Navigation - Hide on Print */
  .navbar {
    background: #1a1a2e;
    color: white;
    padding: 15px 25px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-radius: 8px;
    margin-bottom: 30px;
  }

  .navbar .logo {
    font-weight: bold;
    font-size: 1.2em;
  }

  .navbar nav a {
    color: white;
    text-decoration: none;
    margin-left: 20px;
    opacity: 0.8;
  }

  /* Sidebar - Hide on Print */
  .layout {
    display: grid;
    grid-template-columns: 1fr 250px;
    gap: 30px;
  }

  .article h1 {
    font-size: 2em;
    margin-bottom: 10px;
    color: #1a1a2e;
  }

  .article .meta {
    color: #888;
    font-size: 0.9em;
    margin-bottom: 25px;
    padding-bottom: 15px;
    border-bottom: 1px solid #eee;
  }

  .article p {
    margin-bottom: 18px;
    text-align: justify;
  }

  .article blockquote {
    margin: 25px 0;
    padding: 15px 20px;
    border-left: 4px solid #667eea;
    background: #f8f9ff;
    font-style: italic;
    color: #555;
  }

  .article h2 {
    margin-top: 35px;
    color: #1a1a2e;
    page-break-after: avoid;
  }

  .article img {
    max-width: 100%;
    border-radius: 8px;
    margin: 20px 0;
  }

  .sidebar {
    background: #f8f9fa;
    padding: 20px;
    border-radius: 8px;
    border: 1px solid #eee;
  }

  .sidebar h3 {
    margin-top: 0;
    font-size: 1em;
    text-transform: uppercase;
    color: #888;
  }

  .sidebar ul {
    list-style: none;
    padding: 0;
  }

  .sidebar li {
    padding: 8px 0;
    border-bottom: 1px solid #eee;
  }

  .sidebar a {
    color: #667eea;
    text-decoration: none;
  }

  /* Footer - Hide on Print */
  .footer {
    margin-top: 40px;
    padding: 20px;
    background: #1a1a2e;
    color: #aaa;
    text-align: center;
    border-radius: 8px;
    font-size: 0.9em;
  }

  /* Print Button */
  .print-btn {
    position: fixed;
    bottom: 30px;
    right: 30px;
    padding: 12px 24px;
    background: #667eea;
    color: white;
    border: none;
    border-radius: 8px;
    font-size: 1em;
    cursor: pointer;
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
  }

  .print-btn:hover {
    background: #5a6fd6;
  }

  /* ============ PRINT STYLES ============ */
  @media print {
    /* Hide non-printable elements */
    .navbar,
    .sidebar,
    .footer,
    .print-btn {
      display: none !important;
    }

    /* Reset body for print */
    body {
      max-width: 100%;
      padding: 0;
      margin: 0;
      font-size: 12pt;
      color: black;
      background: white;
    }

    /* Layout adjustments */
    .layout {
      display: block;
    }

    /* Article print styles */
    .article h1 {
      font-size: 24pt;
      page-break-after: avoid;
    }

    .article h2 {
      page-break-after: avoid;
      font-size: 18pt;
    }

    .article h3 {
      page-break-after: avoid;
    }

    .article p {
      orphans: 3;
      widows: 3;
    }

    .article blockquote {
      border-left: 2px solid #999;
      background: none;
      page-break-inside: avoid;
    }

    /* Images print styles */
    .article img {
      max-width: 100% !important;
      page-break-inside: avoid;
    }

    /* Links */
    .article a {
      color: black;
      text-decoration: underline;
    }

    .article a[href]::after {
      content: " (" attr(href) ")";
      font-size: 0.8em;
      color: #666;
    }

    .article a[href^="#"]::after,
    .article a[href^="javascript"]::after {
      content: "";
    }

    /* Page setup */
    @page {
      margin: 2cm;
      size: A4;
    }

    /* Page breaks */
    .page-break {
      page-break-before: always;
    }

    /* Tables */
    table {
      border-collapse: collapse;
      width: 100%;
      page-break-inside: avoid;
    }

    table th,
    table td {
      border: 1px solid #ccc;
      padding: 8px;
      text-align: left;
    }

    table th {
      background: #f0f0f0 !important;
      font-weight: bold;
    }
  }
</style>
</head>
<body>
  <div class="navbar">
    <div class="logo">SkillVersitas</div>
    <nav>
      <a href="#">Home</a>
      <a href="#">Tutorial</a>
      <a href="#">Tentang</a>
      <a href="#">Kontak</a>
    </nav>
  </div>

  <div class="layout">
    <article class="article">
      <h1>Memahami CSS Print Styles</h1>
      <div class="meta">Written by Admin | July 13, 2026 | 5 min read</div>

      <p>Print styles let you optimize how a website looks when printed or exported to PDF. With a dedicated print media query, you can hide unneeded elements and adjust the layout for paper.</p>

      <p>Many developers ignore print styles, but they are important for articles, documents, or invoices that might be printed by users. A print-friendly website provides a better experience.</p>

      <blockquote>
        "Every website should have print styles. You never know when someone wants to print your page."
      </blockquote>

      <h2>Mengapa Print Styles Penting?</h2>
      <p>Users may want to print an article to read offline, save a page as a PDF for reference, or print an invoice or ticket. Without print styles, the printed output becomes cluttered with navigation elements and sidebars.</p>

      <h2>Best Practices</h2>
      <p>Hide non-essential elements such as the navbar, sidebar, and footer. Make sure the text has enough contrast without background colors. Use page-break properties to control page breaks. Include link URLs in the printout so readers can visit the source.</p>

      <p>By applying good print styles, your website will stay professional and readable in print format.</p>
    </article>

    <aside class="sidebar">
      <h3>Table of Contents</h3>
      <ul>
        <li><a href="#">Mengapa Print Styles?</a></li>
        <li><a href="#">Media Query Print</a></li>
        <li><a href="#">Page Break Properties</a></li>
        <li><a href="#">@page Rule</a></li>
        <li><a href="#">Tips Praktis</a></li>
      </ul>
    </aside>
  </div>

  <div class="footer">
    &copy; 2026 SkillVersitas. All rights reserved.
  </div>

  <button class="print-btn" onclick="window.print()">Print Page</button>
</body>
</html>

Code Explanation

@media print: A media query that is only active when printing or previewing the print output. All styles inside this block replace the screen styles when printing.

display: none !important: Hides the navbar, sidebar, footer, and print button in the printed output. !important is required to override the specificity.

@page rule: Sets the paper margin and size. margin: 2cm gives a uniform margin on all sides. size: A4 sets the paper size.

page-break-after: avoid: Prevents a heading from being separated at the end of a page. The heading is moved to the next page if there is not enough space.

page-break-inside: avoid: Prevents a blockquote or image from being cut in the middle.

orphans: 3: At least the last 3 lines of a paragraph must stay on the same page. Prevents a paragraph from leaving only 1-2 lines at the end of a page.

widows: 3: At least the first 3 lines of a paragraph must stay on the same page.

a[href]::after: Displays the URL after a link in the printed output, using content: " (" attr(href) ")". Useful so readers can visit the source.

.article a[href^="#"]::after: Empties the content for anchor links because they are not relevant in print.

Everyday Use Cases

Blog article: Hide the navbar, sidebar, and comments. Show the URL of every link in the article body.

Invoice: Remove the navigation and include the full order details in a neat format on paper.

Recipe: A clean single-column layout without distracting ads or a sidebar.

Online resume: Make sure the resume looks professional when printed directly from the browser.

Conclusion

Print styles ensure a website remains readable and professional when printed. Hide non-essential elements, control page breaks, and make sure colors have enough contrast for black-and-white printing. Use @page for paper margins, orphans/widows for paragraphs, and page-break properties for headings and images. Always test print styles with the browser's print preview.