When using the `display: block;` property within a `code` tag nested inside a `pre` tag, it results in unexpected line breaks and incomplete styling when scrolling

I am trying to create a zebra striped code sample using HTML and CSS. However, when I run the code snippet below, it adds unwanted new lines and does not apply the styling properly when using the scroll bar. How can I resolve this issue?

<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<style>
body {
max-width: 650px;
margin: 0 auto;
}
pre {
counter-reset: code-line;
overflow-x: auto;
}
code {
counter-increment: code-line;
display: block;
}
code::before {
content: counter(code-line) ": ";
}
code:nth-child(odd) {
background-color: #aaa;
}
</style>
<body>

<pre>
<code>Quisque quis congue lectus. Nullam ornare quis magna sit amet volutpat. Nam quis magna id ex venenatis rutrum.</code>
<code>Quisque quis congue lectus. Nullam ornare quis magna sit amet volutpat.</code>
<code>Quisque quis congue lectus.</code>
</pre>
</body>
</html>

Answer №1

To achieve this effect, all you need is a basic background style and you can eliminate the display: block

body {
  max-width: 650px;
  margin: 0 auto;
}

pre {
  counter-reset: code-line;
  overflow-x: auto;
  line-height:1.2em;
  background:repeating-linear-gradient(#aaa 0 1.2em,transparent 1.2em 2.4em);
}

code {
  counter-increment: code-line;
}

code::before {
  content: counter(code-line) ": ";
}
<pre>
<code>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</code>
<code>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</code>
<code>In hac habitasse platea dictumst.</code>
</pre>

<pre style="font-size:20px;">
<code>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium.</code>
<code>Totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.</code>
<code>Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.</code>
</pre>

Answer №2

According to the information found on the MDN documentation page, the purpose of the <pre> tag is to display text exactly as it appears in the HTML source file.

In reviewing the code snippet provided, it seems that the initial code tag contains a sentence that exceeds the maximum width set for the body element. This results in the content overflowing due to the sentence being displayed with formatting applied.

To address this issue, you may consider using the white-space property to control how white space is handled within the element.

pre {
    counter-reset: code-line;
    overflow-x: auto;
    white-space: pre-wrap;
}

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

EU directive on cookies: What is the best way to store your preferences?

While there are numerous third-party solutions available to comply with the cookie EU directive, I prefer to learn how to implement it myself. My specific requirement is that when a user clicks on "Accept", their choice should be remembered for the entire ...

How does Facebook access the new hashtags when they are clicked on?

I've been developing a website recently and I'm really impressed with the way a popup page appears when you click on a hashtag. I've been wanting to incorporate a similar feature for a while now, but I'm not sure how to do it. Could thi ...

Eliminate unnecessary gaps by using HTML to position images side by side without vertical space

I am just starting out with html and css coding and recently I built my first website using wordpress. On one of the pages in wordpress, I wanted to display images next to each other with a small space between them. I achieved this successfully, but unfor ...

Deactivate a button within a label container with the help of Javascript

Is there a way to deactivate an 'inner-button' located within a label tag? The button in question is constructed within a label: <label for="UploadFile" class="inner-button" data-toggle="tooltip" data-placement="top" title="Upload File" oncl ...

Within the body class, text appears with a subtle shadow effect, however, the links within the body also inherit this shadow

I'm encountering a problem in my CSS: body.transparent {background-color: transparent;color:#ffffff;text-shadow: 0 -1px #000, 1px 0 #000, 0 1px #000, -1px 0 #000;} The text-shadow was intended to apply only to simple, non-formatted text. However, it ...

The server is throwing a 403 error when trying to submit an AJAX request

I am currently working on a project similar to jsfiddle and encountering an issue during development. Whenever I attempt to make an ajax request with the JS alert() function in a text box, the server is returning a 403 error. Can anyone provide assistance ...

Modifying the dimensions of mat-card in Angular Material

https://i.stack.imgur.com/CP16N.png I am struggling to make both components the same height, similar to the left form. I have tried adjusting margins and padding but nothing seems to work. Below is the HTML code for the parent element: <mat-tab label= ...

What is the code to make an arrow symbol in CSS that represents 'greater than' symbol?

Can these arrows be created using CSS while maintaining compatibility with all browsers, including IE 6, 7, and 8? Or is using an image the only solution? Home > Products > Digital camera ...

The issue with the smooth scrolling feature in next/link has not been resolved

I am currently facing an issue where smooth scrolling is not working when using next/Link, but it works perfectly fine with anchor tags. However, the downside of using anchor tags is that the page reloads each time, whereas next/link does not reload the pa ...

JS on-click function is not functioning

Here is the HTML code I have for my navigation menu: <nav role="navigation" class="nav"> <ul class="nav-items"> <li class="nav-item"> <a href="#" class="nav-link"><span>About Us</span></a> ...

What is the best way to retrieve the root binding node from a viewmodel in order to apply jQuery.blockUI when performing an AJAX post request?

Within my code, I have a designated DIV element that serves as the root node for applying knockout bindings like so: ko.applyBindings(viewModel, document.getElementById('myContainerDiv')); In all of my viewmodel types, there is a generic post m ...

extracting the value of a chosen radio button in AngularJS using HTML

When attempting to retrieve the selected value from a radio button, I am encountering an issue where choosing "teacher" still shows as "student." Here is the HTML file: <!DOCTYPE html> <html ng-app="phase2"> ... (HTML code continues) Rige ...

Scrollable Tailwind components

I am working on creating a page layout similar to this design: here is what I want to achieve The goal is to have a simple homepage with a navbar and split screen. The challenge is to prevent the entire page from scrolling normally and enable independent ...

Creating a list of definitions in the form of an HTML table using a multidimensional array in

Currently, I am tackling the following challenge: I must create a public static buildDefinitionList() method that produces an HTML list of definitions (using <dl>, <dt>, and <dd> tags) and then returns the resulting string. If the array c ...

Creating a web form with the ability to select multiple images using Javascript

Currently, I am in the process of developing an HTML form that enables users to select an image, a URL, and text that will be inserted as a <li> into a webpage. However, I am facing an issue where I want users to create multiple <li> per input. ...

Responsive Button Sizing with Bootstrap 4

Would it be possible to use Bootstrap 4 to automatically apply the 'btn-sm' class (small button) when the media breakpoint is xs? I want the buttons to remain their default size unless the screen size is xs, where they should switch to btn-sm au ...

Troubleshooting Bootstrap 4 problem with varying sizes of div content

I have encountered a problem with the script below, where the content in a div is of variable size and does not always occupy the entire horizontal space allotted by the col-6 class from one row to the next: <div class="card-body " > <div c ...

jQuery and ajax not functioning properly with disabled button

After a user clicks the submit button in an HTML form, I am trying to disable the button. However, the form redirects to the next page without disabling the button. The ideal process should be: User clicks the submit button Validation is performed Submi ...

What is the best way to fill out a secondary drop

Is it possible to dynamically populate a secondary selection based on the User's choice in the first selection? For example, if the user selects "Silver," how can I automatically remove "Silver" from the second selection options? var b = { img: ...

bxSlider adds in an empty slide after deleting an image

Whenever I click a link in my navigation, I want to remove certain images from the page. I tried implementing a solution, but now I have two empty spaces where the images used to be. How can I remove those as well? I searched on Stack Overflow for a soluti ...