Unexpected behavior with the background property in CSS

Visit the live webpage

Searching for a solution to showcase code snippets on my site, I'm faced with a challenge as a CSS beginner. My struggle lies in creating a dark background for displaying these code snippets.

Upon trying to implement a class named "test" with color blue and background black properties, I noticed that while the text appears in blue within pre tags, the background remains unchanged from default.

Switching to span tags resolves this issue but goes against best practices due to non-semantic tag usage. It seems like there might be an inherent property of pre tags that requires special handling.

Any suggestions or insights? If necessary, I can share the code snippet here for better analysis.

Edit: Here's a condensed version of the code with the same problem persisting. Hopefully, by simplifying it, we can pinpoint the root cause more effectively.

Here's the HTML:

<!DOCTYPE html>
  <head>
    <title>Test</title>
    <link type="text/css" rel="stylesheet" href="stylesheets/test.css">
  </head>
  <body>
    <p>
      <pre class="test">test</pre>
    </p>
  </body>
</html>

And now, the CSS:

.test {
  color: blue;
  font-weight: bold;
  background: black;
}

pre {
  font-family: monospace;
  font-size: 1.25em;
  text-align: left;
  line-height: 0px;
  background: black;
}

Answer №1

A parent class or id can be overridden by a more specific rule. Let's try this one out...

.examples div{
  font-weight: bold;
}
.examples div.highlight{
  font-weight: normal;
}

<div class="examples">
   <div>&nbsp;</div>
   <div class="highlight">&nbsp;</div>
   <div>&nbsp;</div>
   <div>&nbsp;</div>
   <div>&nbsp;</div>
</div>

Answer №2

Have you experimented with the background-color property of the pre tag?

Please have a look at my codepen link: http://codepen.io/user123/pen/abc123

HTML:
  <body>
    <p>
      <pre>sample</pre>
    </p>
  </body>

CSS:
.sample {
  color: blue;
  font-weight: regular;
  background: white;
}

pre {
  font-family: monospace;
  font-size: 1em;
  text-align: center;
  line-height: 20px;
  border: 2px solid #333;
  height: 60px;
  background-color:#FFDDDD;
  padding: 0 10px 10px 0;
}

Answer №3

In order to override the color property defined in another class, you must use !important on your class.

.code{
    background-color: #000 !important;
}

.box{
  width: 250px;
  height: 20xp;
  background-color: red;
  display: inline-block;
  margin: 5px;
}
.code{
    background-color: #000 !important;
  }
<div class="box code">&nbsp;</div>
<div class="box code">&nbsp;</div>
<div class="box code">&nbsp;</div>
<div class="box no_code">&nbsp;</div>
<div class="box code">&nbsp;</div>
<div class="box no_code">&nbsp;</div>
<div class="box no_code">&nbsp;</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

Guide for adding a button in HTML table with event creation using ASP.NET and C# language

I am trying to dynamically add a button in each row of an HTML table column using C#. The button is being created for each row, but the click event is not being triggered. Here is the code snippet from the code behind and ASP.NET: public string ge ...

Navigating to a different page in Ionic 2 when a link or button is clicked

When I click on the link provided below, it should take me to the home page. However, I am facing issues and it is not redirecting me as expected. I want to be taken to the home page when clicking on the specified link or button. Do I need to add any Ang ...

What could be the reason for the child elements not being centered in a bootstrap navbar in this basic code example

Take a look at the display output by clicking here -> http://www.bootply.com/VtPTgKeCLO Below is the code snippet: <nav class="navbar navbar-fixed-bottom top-main-navbar"> <div class="container"> < ...

Guide to dynamically add tr element using CSS to a table

While using the $.each function in jQuery, I am retrieving data and adding it to a table in the following manner: $.each(segment, function (index, innerSegment) { var tr; tr = $('<tr/>'); tr.append("<td>" + segment[i].Airline.Air ...

Adjust the transparency of an image within an overlay when clicked on the overlay

Having an overlay on a Google map with two images contained within it, I am looking to adjust the opacity of the images when a user clicks on the overlay or on top of an image within the overlay. I attempted to utilize domlistener, but it did not yield the ...

Dynamic font sizing in CSS allows text on a webpage to

I am working on creating a dynamic screen using AngularJS. Within this screen, there are objects with a specific size: .item { margin: auto; margin-bottom: 10px; width: 11vw; height: 11vw; text-overflow: ellipsis; overflow: hidden; } These i ...

Pagination in the style of a book using HTML

Currently, I am faced with a unique challenge. I have a large HTML file that contains images and links, and I am looking to parse it in order to display it like a book with interactive paging (using swipes). Has anyone encountered a similar task in iOS bef ...

Could multiple values be stored in the option tag in Laravel after selection?

I have encountered an issue where I am able to display all the attribute drop-down options and obtain the attribute id in the value="{{$play->id}}" after selection, which is then stored in the select with name="attribute_id". This allows me to easily fe ...

Can CSS be utilized to choose every second set of three elements?

Is there a way in CSS to style every second group of three elements? If so, how can this be achieved? For example, in the code snippet below, how can we style the 4th-6th and 10th-12th li elements? <ul> <li>1</li> <li>2< ...

Step by step guide to creating individual counter sections

I have set up a counter section where the numbers go from 0 to a specific value. However, currently all three counters start counting simultaneously. Is there a way for the first counter to count up first, then once it's done, move on to the second c ...

Flex items maintaining their size when the window decreases

My goal is to arrange two plotly plots side by side within a CSS flexbox, with the ability to resize them as the window size changes. The issue I'm facing is that while the plots expand correctly when the window is enlarged, they fail to shrink when t ...

Is there a way to adjust text animations so they appear separately in their designated space?

During my project, I decided to incorporate some CSS animations onto the site. However, I encountered a problem with the overflow: hidden attribute not functioning as anticipated. Here is the code snippet I used: .jumbotron { height: 100%; heigh ...

Using jQuery to modify the collapse behavior in Bootstrap accordion from the "+" sign to the "-" sign

Currently, I am attempting to display the 'minus (hyphen)' sign when a user clicks the button. Despite trying various codes, none seem to be working as expected. Can anyone identify where the mistakes may be? <a role="button" data-toggle="col ...

Strategies to avoid red squiggle lines in a contenteditable div that has lost focus

While the div is focused, spell checking is enabled which works well. However, once the focus is removed and there are spelling mistakes, the red squiggle lines for spell checking remain visible. After following the advice provided in this query: spellch ...

What is the recommended method for removing CSS classes in the Sencha ExtJS framework during a component event? (Those pesky red lines)

Occasionally, an aggravating red line will appear unpredictably when adjusting the size of this component using its split bar located at the top edge of the component. I managed to find a way to permanently eliminate the red line by removing its CSS class ...

Display v-select options by clicking a button

I am trying to figure out a way to make the vuetify v-select component appear when I click on an icon button. The v-select should remain hidden when closed, only visible when triggered by clicking the button. It's like having the button toggle the vis ...

By utilizing the <tr> element, one can enhance the border thickness by 1 pixel

Is it possible to ensure that row selection in a table is consistent in terms of border alignment on both sides? https://i.stack.imgur.com/qmZiQ.png If you have any suggestions or solutions, please share them. Thank you! table { margin: 10px; bord ...

Display the footer once the user has reached the end of the page by scrolling

Below is the footer code I am working with. <div class="row"> <div class="col-md-12"> <div> this section always remains at the bottom </div> </div> <div class="col-md-12"> <div> only v ...

Tips on adjusting the font size of headers in a Vuetify v-data-table?

I've been struggling to change the font size of the headers in my v-data-table. No matter what I try, the font doesn't seem to update. I even experimented with the Chrome developer tool and managed to make it work there. However, when I attempt t ...

Strategies for handling divisions while resizing the browser界面 manipulating divisions during

Here's the HTML/CSS code I'm working with: .container { max-width: 900px; margin: 0 auto; margin-top: 30px; } .divisions { border: 1px solid Black; } .Fisrt-Line { height: 180px; } .First { background-color: Green; width: 32.2% ...