Enhance your inline <pre> text by adding a unique background color with CSS

How can I achieve the gray background color, like in this word, which is contained within an inline <pre></pre>?

This is what I have attempted so far. The code snippet shows that the inline style works fine after the </pre> but not before <pre>.

pre.inline {
    display: inline;
    background-color: #80b3FF;
    border-radius: 4px;
    padding: 3px
}
<p>We utilize <pre class="inline">model</pre> to define the statistical model.</p> 

Answer №1

Another method for resolving the issue involves utilizing 'div' and 'span'

.inline {
    background-color: #80b3FF;
    border-radius: 4px;    
    padding: 3px
}

<div>We utilize <span class="inline">model</span>
to define the statistical model.

Answer №2

This issue appears to be caused by the pre tag creating a new line even though it is set to inline display. One way to resolve this is by using a span instead:

span{
    background-color: #80b3FF;
    border-radius: 4px;
    padding: 3px
}
<p>We utilize the <span>model</span> to define the statistical model.</p> 

Alternatively, you can use the code tag for a monospace font style by default:

code{
    background-color: #80b3FF;
    border-radius: 4px;
    padding: 3px
}
<p>We employ the <code>model</code> to specify the statistical model.</p> 

Answer №3

The reason for this is that the pre element is not considered a phrasing content. For more information, please refer to this link. It is recommended to use a div instead.

pre.inline {
    display: inline;
    background-color: #80b3FF;
    border-radius: 4px;
    padding: 3px
}
<div>We use <pre class="inline">model</pre> to specify the statistical model.</div>

Answer №4

Displayed below is an image of how your code appears in the browser, illustrating the issue at hand.

https://i.sstatic.net/0vRkS.png

To address this problem, consider implementing the following solutions: - Substitute <div> for <p>

pre.inline {
    display: inline;
    background-color: #80b3FF;
    border-radius: 4px;
    padding: 3px
}
<div>We use <pre class="inline">model</pre> to specify the statistical model.</div> 

Use <code> instead of <pre>

code.inline {
    display: inline;
    background-color: #80b3FF;
    border-radius: 4px;
    padding: 3px;
}
<p>We use <code class="inline">model</code> to specify the statistical model.</p>

This link provides further insight on the usage of <code> and <pre>

Additionally, you can refer to this link for more information on the <p> element

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

"Unusual bug in IE causing cell borders to disappear when colspan is used

Below is a sample table layout that I am working with: <table> <thead> <tr> <th>t1</th> <th>t2</th> <th>t3</th> <th>t4</th> ...

Lean/Tilt just the lower portion of the division

I have been experimenting with adding a unique slant to the bottom of a div. Progress has been made, as shown in the example below where I successfully applied the slant to some elements on the page. Currently, the slant is visible on both the top and bot ...

My JavaScript seems to be having an issue with .className - can anyone help me troubleshoot

I'm currently working on a navigation menu, and my objective is to have the class change to .nav-link-active when a link is clicked, while also reverting the current .nav-link-active back to .nav-link. Here's the code snippet I am using: <he ...

Troubleshooting a Selected Menu Problem in jQuery CSS

Hello, I have implemented a jQuery script to change the color of a menu item when it is selected: .selected{ background-color: red; } $("#nav-container>li").click(function(){ $(this).addClass('selected') .siblings() ...

Can Xpath be condensed into a more compact form?

Currently, I'm immersed in a project that involves utilizing XPath on HTML. The lengthy XPath is being passed to the client-side using JavaScript. Due to the verbose nature of XPath strings, my curiosity has been piqued about the existence of a more c ...

Which programming language is best suited for this task?

Seeking assistance with changing an image inside a div to another image with a simple mouse click. Wondering if this can be achieved through CSS or if jQuery would be the better option. Here is the code snippet: <div id="slideshow"> <img ...

Update the less mixin

I attempted to eliminate the border radius from all Bootstrap elements by creating a custom-mixins.less file with the following lines in it. My intention was for these lines to overwrite the original .border-radius mixin, but unfortunately, it did not work ...

Issue with PHP retrieving initial value of post data

Hi there, I am facing an issue with my PHP code where the first value of the input field is not being displayed. However, when I check the console.log, it shows correctly. Here is my console.log output: https://i.sstatic.net/eZvg6.png PHP Output: https ...

Boosting Your SCSS (SASS) Productivity

I'm curious if there are any ways to make the code below more efficient or dynamic. I've already achieved the desired result, but I'm interested in optimizing it further. The Script Any suggestions would be highly appreciated, Cheers, ...

The print preview displays a single div in an incorrect location

I have successfully created a javascript plot and wanted to incorporate a legend that overlaps the plot. Unfortunately, the function I used does not support directly overlapping legends, but it does allow for placing the legend in a separate div. To work a ...

Placing information into a table cell using d3 library

Looking to insert text into a cell using d3? I have a function with the necessary code, which I am calling in the body of an HTML page. Here is a sample of the code that I am trying to get working: <!DOCTYPE html> <html> <head> <link ...

What are some methods to identify the cause of a click not registering?

While browsing through the page, I noticed a link that looks like this: <a href="/go/some/where.htm">...</a> This link is situated within a google.maps.InfoWindow box that appears when a user hovers over a map pin. Oddly enough, when a user t ...

Showing Predefined Date on an HTML Form in an iOS Application

Is there a method to dynamically show the current date within the button that triggers the date picker in an HTML form on an iOS device? This is what currently appears: This is what I want it to automatically display: Below is the code I have implemente ...

What is the best way to use canvas to precisely draw a line on the display at specific absolute coordinates?

As a newcomer to working with canvas and SVG, I am trying to create a line between two specific coordinates. For example, if I have two rectangles, I need to draw a line connecting their centers: https://i.sstatic.net/Rc6qA.png Here is what I have tried ...

Checkbox: Activate button upon checkbox being selected

On my webpage, I have a modal window with 2 checkboxes. I want to enable the send button and change the background color (gray if disabled, red if enabled) when both checkboxes are selected. How can I achieve this effectively? HTML: <form action="" me ...

Activate a .click() event on a hyperlink exclusively in mobile view

Currently, I am working on a WordPress website that utilizes a Table of Contents plugin. The plugin simply identifies headings and adds them to the table of contents. I am aiming to achieve a specific functionality on my website (). When the window width ...

Tips for getting the jQuery accordion to return smoothly to its original position (instead of stacking on top of each other)

I'm a complete beginner at this and could really use some assistance. Here's my JS Fiddle: http://jsfiddle.net/hAZR7/ The problem I'm facing seems to be more related to logic or math, as the animation works fine when starting and moving to ...

Displaying a subset of categories based on the user's selection

I have been trying to find a solution to automatically display a subcategory select drop-down only when a user selects a category. If no category is selected, the subcategory drop-down should remain hidden. I have searched online tutorials and videos for ...

How to position and center a div layer over another div

I have been struggling to place a div over another div that contains an image. The div needs to have a simple HTML link just like the example shown in this picture: However, when the page loads, the URL is not centered on the image as you can see on the l ...

Spacing in CSS between the menu and its submenu elements

My current challenge involves creating space between the menu and the submenu. However, I've noticed that when there is this space, the submenu doesn't function correctly. It only works when the design has no gap between the menu and the submenu. ...