Code block in Next.js not shrinking below longest code line, causing full-page horizontal scrollbar

I’m developing a blog using Next.js where I use rehype-pretty-code for syntax highlighting. I have a code block inside a demo blog post.

When I resize the window to check the responsiveness, the code block does not shrink below the length of the longest line of code, instead triggering a horizontal scrollbar across the entire page.

Expected Behavior

The code block should adjust its width with the viewport and introduce a scrollbar within the pre element itself when the content overflows.

Current Behavior

When resizing the window, the code block width remains fixed to the longest line of code (as shown below), leading to a full-page horizontal scrollbar when it exceeds the viewport width.

enter image description here

Technology Stack

  • Next.js
  • Tailwind CSS
  • Next MDX
  • rehype-pretty-code

Code

Here is the CSS related to the styling of the code block:

@layer components {
  .prose a {
    @apply text-primary underline decoration-primary underline-offset-2 transition-all hover:no-underline;
  }

  .prose .anchor {
    @apply invisible absolute right-4 top-1/2 w-full -translate-y-1/2 text-lg text-muted-foreground no-underline after:content-['#'];
  }

  .prose .anchor:hover {
    @apply visible no-underline;
  }

  .prose :where(h2, h3):hover > .anchor {
    @apply visible;
  }

  .prose h2 {
    @apply mb-4 mt-16 text-xl font-bold tracking-tight lg:text-2xl;
  }

  .prose h3 {
    @apply mb-4 text-lg font-bold tracking-tight lg:text-xl;
  }

  .prose :where(h2, h3) {
    scroll-margin-top: 80px;
    position: relative;
  }

  .prose p {
    @apply mb-8 leading-7 text-slate-700;
  }

  .prose p code {
    @apply text-sm;
  }

  .prose strong {
    @apply font-semibold;
  }

  .prose ul {
    @apply mb-8 list-disc pl-8 leading-7;
  }

  .prose ol {
    @apply mb-8 list-decimal pl-8 leading-7;
  }

  /* Code block */
  .prose figure {
    @apply mb-8 rounded-lg border shadow-sm;
  }

  /* Code block title */
  .prose figure figcaption {
    @apply border-b p-3 text-sm text-muted-foreground;
  }

  .prose figure pre {
    @apply relative overflow-x-auto py-4;
    counter-reset: line;
  }

  [data-line] {
    @apply border-l-2 border-l-transparent;
  }

  [data-line]::before {
    counter-increment: line; /* Increment counter for each line */
    content: counter(line); /* Display the counter value */
    display: inline-block; /* Make line numbers appear inline */
    width: 1rem; /* Adjust width as needed */
    margin-right: 1rem; /* Adjust spacing between numbers and code */
    margin-left: 1rem; /* Adjust spacing between numbers and code */
    text-align: right; /* Align numbers to the right */
    color: #a1a1aa; /* Set line number color (adjust if needed) */
  }

  [data-line-numbers-max-digits="2"] > [data-line]::before {
    width: 2rem; /* Adjust width for codes with max 2 digits */
  }

  [data-line-numbers-max-digits="3"] > [data-line]::before {
    width: 3rem; /* Adjust width for codes with max 3 digits */
  }

  .prose figure pre code {
    @apply text-sm;
  }

  [data-highlighted-line] {
    @apply border-l-2 border-blue-600 bg-blue-50;
  }
}

Attempts to Resolve

I’ve attempted to manage overflow with overflow-x-auto on the pre element, expecting it to contain the overflow within the code block itself, but the entire page still scrolls.

Code is open-source

The code of the Next.js project I am building is open source. Here are the links to relevant pages:

globals.css

https://github.com/sundaray/hemantasundaray/blob/main/styles/globals.css

The demo blog post (/app/blog/posts/first-post/page.mdx)

https://github.com/sundaray/hemantasundaray/blob/main/app/blog/(posts)/first-post/page.mdx

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật