HTML: arranged <pre> with fixed positioning

I currently have a centered column of text with a fixed width. However, I am looking to break that fixed width for certain tags like <pre> so that they can fill the full screen width while maintaining flow with the rest of the text.

My CSS snippet so far looks like this:

pre {
  float: left;
  left: 0;
  padding: 1em;
  position: absolute;
}

In this HTML fragment, the paragraphs are correctly displayed in a centered column on the page.

<div class="bodice">
  <p>Some text.  Some more text.</p>
  <pre>Here's some code!</pre>
  <p>Yet more text.  And some more text.</p>
</div>

However, the content inside the <pre> tag does not flow properly and overlaps, which is not the intended effect. It does appear aligned against the left side of the screen as desired.

You can view a more complete example on jsFiddle at: http://jsfiddle.net/Jashank/VbKDP/

How can I make the content within the <pre> tag flow seamlessly with the rest of the paragraphs while keeping it aligned to the left?

Answer №1

After engaging in some conversation, I have finally grasped the concept of what you were seeking.

html

<div class="header">
    <h1>[jashank] / diary</h1>

    <p><small>
        <!-- &#187;&nbsp;<a href="#xkcd">xkcd</a>&nbsp;&nbsp; -->

        </small></p>
</div>

<div class="bodice">
    <div id="post">
        <p>Having retrieved LyX's SVN sources and compiled them with the CMake build
            system, everything was functioning well. However, upon attempting to launch LyX:</p>

        <pre>
<strong>jashank@jashank</strong> <span style="color: #0000ee;">(pts/33)</span> <span style="color: #cd0000;">~/Software/LyX-build</span> <span style="color: #0000ee;">[0 jobs]</span> <u>!3027</u>
2011-07-03 10:49:15 &gt;&gt;&gt;&gt;&gt; bin/lyx2.1
zsh: segmentation fault (core dumped)  bin/lyx2.1
        </pre>

        <div style="clear: both;"></div>

        <p>
            Even tracing the core dump did not provide much insight; it seems like the stack
            had been corrupted.
        </p>

        <p>
            After encountering an aspell issue and making a few corrections to address
            erratic malfunctions:
        </p>

        <pre>
11:11:07 &lt;nox&gt; wow it built! :)
11:11:28 &lt;jashank&gt; nox: Try running bin/lyx2.1
11:12:02 &lt;nox&gt; segfaults in ucs4le_mbtowc
        </pre>

        <p>
            Essentially, it does not seem to be related to bitness, as the issue occurs
            in both 32- and 64-bit modes. This is not a positive sign.
        </p>

    </div>;

</div>;

css

body {
font-family: sans-serif;
margin: 0;
text-align: center;
}

div.header {
text-align: center;
}

div.bodice {
margin: 0;
text-align: center;
width: 100%;
}

.bodice p {
margin: 0 auto;
text-align: justify;
width: 50ex;
overflow: auto;
}

div#post pre {
text-align: left;
width: 100%;
padding: 1em;
}

body {
text-align: center;
}

h1, h2, h3, h4, h5, h6 {
text-align: center;
}

example

A demonstration can be viewed on jsfiddle.

Answer №2

I found this code to be quite helpful. Take a look at the demo here.

<style type="text/css>
    .body {
        text-align: center;
    }
    pre {
        text-align: left;
        width: 100%;
        clear: both;
        padding: 1em;
    }
</style>

<div class="body">
    <p>Adding some text. Including more text.</p>
    <pre>Presenting you with some code!</pre>
    <p>Even more text. Plus additional text.</p>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Best practices for refreshing the HTML5 offline application cache

My website utilizes offline caching, and I have set up the following event handler to manage updates: applicationCache.addEventListener('updateready', function () { if (window.applicationCache.status == window.applicationCach ...

Make the width of a table column match the width of the header

I am currently using Primeng2 datatable and I am looking to adjust the column width to match the header size. This is my HTML code: <p-dataTable #dtselfcollectmonthly [exportFilename]="exportname" [rows]="10" [value]="rawdatatrucks"> <ng-con ...

Struggling to close modal popup after submitting form

click here for imageCurrently, I am working with the bootstrap modal plugin to create an inquiry form. The issue I am facing is that even though I am using the "ng-submit" directive to save the form details, when I click submit, the form submits successful ...

Utilizing Flexbox for Nested Columns

I am attempting to nest flexbox columns inside a flexbox layout. This is the HTML code I have: <div class="container"> <div class="row flex height"> <div class="col-md-6 red"> </div> <div class="col-md-6 orange"> ...

Unable to Display Bootstrap 4 Navbar on Hamburger Menu

I am having an issue with my mobile-sized site. Whenever I click the hamburger menu, the Navbar does not appear. I have double-checked and my data-target matches the nav id, so I am not sure why it is not working. <section id="nav"> <na ...

What is the best way to replicate the orange outline when focused?

I am in the process of creating a series of divs that can be navigated by the user using the tab key. I would like to incorporate the standard orange focus outline for these elements. Is there anyone who can provide guidance on how to achieve this? I unde ...

Python for Asynchronous HTTP Requests

I am currently working on a Python script to extract the value of the href attribute from an anchor element on a web page. The challenge is that the div element containing the anchor element's content is loaded through AJAX jQuery calls when the web p ...

Struggling to locate text within the confines of the meta viewport

So I'm new to this whole blogging thing and I wanted to make sure my site was responsive. I added the meta viewport tag, but when I tested it out on Google Chrome with different window sizes, my text just disappeared! Here's a snippet of the CSS ...

Utilize jQuery and PHP to populate an HTML Select Field with data retrieved from a MySQL Database in JSON format

I am exploring how to Transfer Database Data into an HTML Dropdown Selection Field using jQuery. I came across a useful example that seems promising. Although I am new to jQuery and JavaScript, I am eager to learn. My goal is similar to the approach descr ...

Maintain user input within the table following a page refresh

I have been working on developing a table that allows users to edit it. I successfully created a script that adds comments to the table, but unfortunately, these comments disappear when the page is refreshed. I understand that I may need some additional to ...

After deploying DRF, the CSS in Django admin is not displaying

After developing a web application using Django Rest Framework and React, I faced an issue during deployment on IIS. While the deployment is successful, I encountered a problem with the Django Admin where the styles were not displaying properly. This led t ...

Jquery problems impacting every element rather than only one

I'm currently experimenting with a small project where I have multiple div elements containing an image, h1 tag, and p tag all sharing the same class name. My goal is to add CSS effects that make the h1 tag and p tag slide into view and become visible ...

Incorporate user input into Alert Dialog Boxes

Would you be able to assist me in displaying the input value from the "email" field in my alert box? The code seems to be working fine, but I'm having trouble getting the alert box to show the email form value. I decided to use Bootstrap for som ...

Arrange the labels and input fields within nested elements in a synchronized manner

My data is structured semantically as shown below: <div class="inputs"> <div class="top"> <h4>Top</h4> <label for="top-1">Label 1</label> <input id="top- ...

The radio button that disables other inputs only functions correctly for a single element when using Query Selector, but does not work with Query

I have attempted to develop a form section that is disabled when the user selects option A and enabled when they choose option B. document.getElementById('delivery').onclick = function() { var disabled = document.querySelectorAll(".dis ...

Searching for specific text within HTML href tags involves parsing the HTML document and

[I'm having trouble isolating links that contain the specific text '/Archive.aspx?ADID='. Even though I have tried to filter for these specific links, I end up retrieving all of the links from the webpage. Once I am able to extract the desir ...

The svg line is drawn with a width that is half of the specified width

I am looking to create a horizontal line that is 10px wide. I tried using the code below <svg width="500" > <line x1="100" x2="460" y1="0" y2="0" stroke="red" stroke-width="10px&qu ...

Combine two languages into a single <p>

I'm dealing with a WordPress website where I have a paragraph that contains two different languages. What I want to achieve is to display each language in a distinct font-family (font-face). For example: <p>למרות הכל its worth it</p& ...

What could be causing the issue with my linear gradient not displaying correctly on top of my background image in Rails and Sass?

I am struggling to get a linear gradient to display over a background image. Despite trying various solutions from other questions, I still can't get it to work with my current code... .bottom-pic { width: 100%; height: 425px; ...

Prevent page refresh when submitting a form using PureCSS while still keeping form validation intact

I'm currently implementing PureCSS forms into my web application and facing a challenge with maintaining the stylish form validation while preventing the page from reloading. I discovered that I can prevent the page reload by using onsubmit="return f ...