0

So I am working on a coding tutorial site and I would like to have code snippets put directly in HTML. I made a HTML and CSS code snippet and it works great. However when I put it inside a table, all of a sudden the overflow stops working and instead causes the whole page to scroll horizontally. Please help me solve this problem.

Thanks

Without table:

<html>
<body >
<style>
pre {
    overflow: auto;
    color: white;
    background: #000000;
    padding: 0;
    font-size: 0.8rem;
    font-family: Menlo, Monaco, monospace;
    -moz-tab-size: 2;
    -o-tab-size: 2;
    tab-size: 2;
    position: relative;
}

pre[rel] {
    padding-top: 33px;
}

pre[rel]:before {
    content: attr(rel);
    color:#000000;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    background: #CCCCCC;
    font-family: 'Gotham Rounded A', 'Gotham Rounded B', sans-serif;
    font-weight: 500;
    font-style: normal;
    padding: 5px 0;
    text-indent: 15px;
}

pre code {
    font-family: Menlo, Monaco, monospace;
    background: none;
    padding: 15px;
    white-space: pre;
    overflow: auto;
    display: block;
}
</style>
<pre rel="HTML"><code class="language-none">  
&lt;html&gt;
  &lt;head&gt; 
    &lt;title&gt;Hello World&lt;/title&gt; 
  &lt;/head&gt; 
  &lt;body&gt; 
   Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello 
  &lt;/body&gt; 
&lt;/html&gt;
</code></pre>
</body>

</html>

with table:

<html>
<body>
<style>
pre {
    overflow: auto;
    color: white;
    background: #000000;
    padding: 0;
    font-size: 0.8rem;
    font-family: Menlo, Monaco, monospace;
    -moz-tab-size: 2;
    -o-tab-size: 2;
    tab-size: 2;
    position: relative;
}

pre[rel] {
    padding-top: 33px;
}

pre[rel]:before {
    content: attr(rel);
    color:#000000;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    background: #CCCCCC;
    font-family: 'Gotham Rounded A', 'Gotham Rounded B', sans-serif;
    font-weight: 500;
    font-style: normal;
    padding: 5px 0;
    text-indent: 15px;
}

pre code {
    font-family: Menlo, Monaco, monospace;
    background: none;
    padding: 15px;
    white-space: pre;
    overflow: auto;
    display: block;
}
</style>
<table>
  <tr>
    <td>
<pre rel="HTML"><code class="language-none">  
&lt;html&gt;
  &lt;head&gt; 
    &lt;title&gt;Hello World&lt;/title&gt; 
  &lt;/head&gt; 
  &lt;body&gt; 
   Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello 
  &lt;/body&gt; 
&lt;/html&gt;
</code></pre>
</td>
</tr>
</table>
</body>

</html>

2 Answers 2

1

Specify a width for <pre>

Have an example!

pre {
    width: 500px;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you that works, however I would prefer to specify a percentage width and that doesn't seem to work. Any idea how to make it fill 100% of the screen however adjust the overflow as the screen is resized smaller?
I figured it out, if you apply a set width to the td element (that is style="max-width:100px") then give the td an id and apply css through an external style sheet you could give it a percentage width of 85% and thus fix the problem.
0
public partial class WUCHeader : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["UserCode"] == null)
        {
            PnlHome.Visible = true;
            PnlLogin.Visible = false;
        }
        else
        {
            PnlLogin.Visible = true;
            PnlHome.Visible = false;
            LblUserName.Text = Request.Cookies["Name"].Value;
        }

    }
    protected void Logout_Click(object sender, EventArgs e)
    {
        if (Request.Cookies["UserCode"] != null)
        {
            Response.Cookies["UserCode"].Expires = DateTime.Now.AddDays(-1);
            Response.Cookies["Name"].Expires = DateTime.Now.AddDays(-1);
        }
    }
}

1 Comment

A small description on your answer would help understand this better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.