0

I am working on a backend API that contains an endpoint that returns markdown converted into HTML and this HTML is rendered on the client.

Problem:

Main content on the page is wrapped in a div with the css class .contentContainer. When the HTML generated on the server contains code blocks, .containerContainer stops being responsive. At a particular window width, .codeContainer stops shrinking with the window width and I get a horizontal scroll. (See the code snippet below)

What I have tried so far:

I have tried setting overflow: auto on .contentContainer. That fixed the horizontal scroll problem BUT causes other layout issues (e.g. footer moving to the middle of the page).

I have identified that display: grid on body is the cause of the issue here. Removing that fixed the issue but I have used CSS Grid on the body element to achieve a layout that I want. In short, I can't remove CSS Grid from the body.

Question:

Why is CSS Grid causing the problem here with code blocks? How can this problem be fixed?

Minimal Reproducible Example:

Try shrinking / expanding the window with to see the problem.

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  background-color: red;
  margin: 0;
  display: grid;
  grid-template-rows: auto 1fr auto;
  align-items: start;
  gap: 30px 0px;
}

.contentContainer {
  max-width: 800px;
  width: 95%;
  background-color: white;
  margin: 20px auto;
  padding: 25px 35px;
}

@media (max-width: 550px) {
  .contentContainer {
    width: 100%;
    padding-inline: 25px;
  }
}

main {
  font-size: 1.1rem;
}

pre {
  overflow: auto;
  padding-top: 30px;
  line-height: 170%;
  border-radius: 6px;
}

code .line {
  padding-inline: 30px;
}
<div class="contentContainer">
  <main>
    <!-- Following html is generated on the server after parsing the markdown --> 
    <p>This is a test note to test the code highlighter.</p>
    <pre class="shiki" style="background-color: #292d3e;"><code><span class="line"><span style="color: #C792EA">const</span><span style="color: #A6ACCD"> age </span><span style="color: #89DDFF">=</span><span style="color: #A6ACCD"> </span><span style="color: #F78C6C">20</span><span style="color: #89DDFF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #89DDFF; font-style: italic">if</span><span style="color: #A6ACCD"> (age </span><span style="color: #89DDFF">&lt;</span><span style="color: #A6ACCD"> </span><span style="color: #F78C6C">18</span><span style="color: #A6ACCD">) </span><span style="color: #89DDFF">{</span></span>
<span class="line"><span style="color: #F07178">  </span><span style="color: #A6ACCD">console</span><span style="color: #89DDFF">.</span><span style="color: #82AAFF">log</span><span style="color: #F07178">(</span><span style="color: #89DDFF">"</span><span style="color: #C3E88D">you are not eligible to drive</span><span style="color: #89DDFF">"</span><span style="color: #F07178">)</span><span style="color: #89DDFF">;</span></span>
<span class="line"><span style="color: #89DDFF">}</span><span style="color: #A6ACCD"> </span><span style="color: #89DDFF; font-style: italic">else</span><span style="color: #A6ACCD"> </span><span style="color: #89DDFF">{</span></span>
<span class="line"><span style="color: #F07178">  </span><span style="color: #A6ACCD">console</span><span style="color: #89DDFF">.</span><span style="color: #82AAFF">log</span><span style="color: #F07178">(</span><span style="color: #89DDFF">"</span><span style="color: #C3E88D">you can drive!</span><span style="color: #89DDFF">"</span><span style="color: #F07178">)</span><span style="color: #89DDFF">;</span></span>
<span class="line"><span style="color: #89DDFF">}</span></span>
    <span class="line"></span></code></pre>
  </main>
</div>
Yousaf
  • 27,861
  • 6
  • 44
  • 69

0 Answers0