<pre> HTML tag

Usually browsers ignore multiple spaces and tabs and display one single space break. We can retain the same space between words by using pre code.
<pre>Some text</pre>
This can be useful for displaying text that is formatted in a specific way, such as a poem or a song lyric.
Similarly some symbols like less than or greater than can be displayed without any conversion.
Another advantage of pre command is it retains the line breaks between lines.
Let us try to display some text having multiple lines and more than one space within words and having some symbols.


Welcome to plus2net, you can learn following scripts here. PHP Server side scripting language ASP Server side scripting language HTML Tags to use using web browser JavaScript Client side scripting language

When displayed along with pre tag then above lines will retain their extra space and line breaks
Welcome to plus2net,   you can learn  following scripts here. 
PHP                Server side scripting language
ASP                Server side scripting language
HTML             Tags to use using web browser
JavaScript    Client side scripting language 

Styling Preformatted Text with CSS

While the <pre> tag preserves whitespace and formatting, we can enhance its appearance using CSS. For instance, we can add borders, background colors, or padding to improve readability.

<style>
  pre.styled-pre {
    background-color: #f5f5f5;
    border-left: 3px solid #ccc;
    padding: 10px;
    overflow-x: auto;
  }
</style>

<pre class="styled-pre">
function helloWorld() {
  console.log("Hello, world!");
}
</pre>

Combining <pre> with Other Tags

The <pre> tag can be combined with other tags like <code>, <samp>, or <kbd> to semantically represent different types of text.

<pre><code>
SELECT * FROM users WHERE active = 1;
</code></pre>

Handling Long Lines within <pre>

By default, text inside a <pre> tag does not wrap, which can cause horizontal scrolling. To address this, we can use the CSS white-space property set to pre-wrap to allow long lines to wrap within the container.

<style>
  pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
</style>
<pre>
This is a very long line of text that will wrap within the pre tag container to prevent horizontal scrolling.
</pre>

Accessibility Considerations

When using the <pre> tag, it's important to consider accessibility. For example, when displaying ASCII art or diagrams, we should provide descriptive text alternatives for screen readers.

<pre role="img" aria-label="A simple smiley face made with ASCII art">
:-)
</pre>

Deprecation of the width Attribute

Previously, the <pre> tag supported a width attribute to specify the number of characters per line. This attribute is now deprecated and should be replaced with CSS for controlling layout and presentation.

<style>
  pre {
    width: 80ch; /* 80 characters wide */
  }
</style>

Use Cases for the <pre> Tag

  • Displaying Code Snippets: Preserving indentation and formatting for programming tutorials or documentation.
  • Showing Log Files: Presenting log entries where exact formatting is crucial for readability.
  • ASCII Art: Creating text-based art where spaces and line breaks define the visual structure.

By incorporating these enhancements and additional examples, we can provide a more comprehensive understanding of the <pre> tag and its practical applications in web development.


We can display multiple space by using html blank space
Learn how to display html tags in web pages.
Managing font size and style

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com










    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer