What is the best way to show a background color on a heading?

I am new to website design and I have successfully created a slideshow. However, I am facing an issue with applying a background color to the heading so that it stands out over the image. Despite setting the background color in the CSS, it is not displaying on the heading.

Here is the HTML code:

<div class="slideshow">
      <div class="slideshow-item-container">
        <div class="slideshow-item">
          <a href="humans/embryonic-development.html">
          <img src="img/baby.jpg" alt="A newborn baby"/>
          <h2 class="item-text">The Qur'an on Human Embryonic Development</h2></a>
        </div>
        <div class="slideshow-item">
          <a href="humans/the-cerebrum.html">
          <img src="img/brain.jpg" alt="The Cerebrum and Its Parts"/>
          <h2 class="item-text">The Cerebrum in Quran</h2> </a>
        </div>
        <div class="slideshow-item">
          <a href="humans/the-cerebrum.html">
          <img src="img/ocean.jpg" alt="The Cerebrum and Its Parts"/>
          <h2 class="item-text">Where the oceans meet</h2> </a>
        </div>
        <div class="slideshow-item">
          <a href="humans/embryonic-development.html">
          <img src="img/baby.jpg" alt="A newborn baby"/>
          <h2 class="item-text">The Qur'an on Human Embryonic Development</h2></a>
        </div>
      </div>
    </div>

And this is the CSS code:

*{
  margin: 0;
  padding: 0;
}
.slideshow {
  overflow: hidden;
  margin-top: 20px;
}
.slideshow-item-container {
  position: relative;
  width: 400%;
  left: 0;
  animation: 20s slider infinite;
}
.slideshow-item {
  width: 25%;
  float: left;
}
h2.item-text {
  margin-top: -48px;
  margin-right: 20px;
  text-align: right;
  background-color: #2b5072;
  color: white;
}

Even though the background property is set in the CSS, the heading is still not displaying it. Here is a screenshot for reference: screenshot of site with no background color appearing on heading

Answer №1

Don't forget to insert the following code snippet after scrolling the text on the image in order to create an overlapping effect.

You can try out the code below to see how it works.

h2.item-text {
  .
  .
  .
  position: relative;
  z-index: 1;
}

* {
  margin: 0;
  padding: 0;
}

.slideshow {
  overflow: hidden;
  margin-top: 20px;
}

.slideshow-item-container {
  position: relative;
  width: 400%;
  left: 0;
  animation: 20s slider infinite;
}

.slideshow-item {
  width: 25%;
  float: left;
}

h2.item-text {
  margin-top: -48px;
  margin-right: 20px;
  text-align: right;
  background-color: #2b5072;
  color: white;
  position: relative;
  z-index: 1;
}
<div class="slideshow">
  <div class="slideshow-item-container">
    <div class="slideshow-item">
      <a href="humans/embryonic-development.html">
        <img src="https://placeimg.com/1640/380/any" alt="A newborn baby" />
        <h2 class="item-text">The Qur'an on Human Embryonic Development</h2>
      </a>
    </div>
    <div class="slideshow-item">
      <a href="humans/the-cerebrum.html">
        <img src="https://placeimg.com/1640/380/any" alt="The Cerebrum and Its Parts" />
        <h2 class="item-text">The Cerebrum in Quran</h2>
      </a>
    </div>
    <div class="slideshow-item">
      <a href="humans/the-cerebrum.html">
        <img src="https://placeimg.com/1640/380/any" alt="The Cerebrum and Its Parts" />
        <h2 class="item-text">Where the oceans meet</h2>
      </a>
    </div>
    <div class="slideshow-item">
      <a href="humans/embryonic-development.html">
        <img src="https://placeimg.com/1640/380/any" alt="A newborn baby" />
        <h2 class="item-text">The Qur'an on Human Embryonic Development</h2>
      </a>
    </div>
  </div>
</div>

Answer №2

Consider updating your images by setting them as the background image of your div element. Take a look at this example:

<div class="square" style="border:solid 1px red; height:150px;background- 

image:url('https://www.campsitecoders.com/img/google-seo-search-engines.jpg')"> Photo 4

https://codepen.io/susanwinters/pen/wvWjvRb

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

XSLT - Dividing table into three columns while maintaining continuity on the same page

I'm looking for a solution where I can display a long two column table in a three-column page layout. The table should seamlessly flow from one column to the next, maintaining its headers throughout. Current attempts show the entire table in just one ...

Creating HTML tables with PHP for dynamic web content

I am facing a minor issue while displaying my results in tables. Here is the code snippet: <?php //return data if any while ($client = mysqli_fetch_assoc($result)) { //output data form each client //var_dump($client); ?> < ...

What causes the size of an image to decrease when placed within an anchor tag?

Trying to add clickable social media icons, but when wrapped in an anchor tag the images shrink significantly. <span class="iconspan"> <a href="https://www.instagram.com/lil_ijoez/" target="_blank" ...

Transform the CSS to a Mat-Form-Field containing a search box within it

I am currently working with Angular, Angular Material, and SCSS for my project. Here is the front-end code snippet that I am using: <mat-form-field class="custom-form-field" style="padding-top: 5px;padding-bottom: 0px; line-height: 0px; ...

choose particular column in every row from the table's header class name

Every <th> in a table has a class, while the <td> elements do not. Is there a way to style all corresponding <td> elements with the same styles as their <th> counterparts using only plain css without any script? Furthermore, since ...

Images displayed on cards using BOOTSTRAP

I'm interested in creating cards with categories such as Men, Women, and Kids. I envision these cards to have a design similar to the ones shown here. My goal is for users to be able to click on one of the cards and be directed to either the Men or W ...

Add a new row to the table when a dropdown option is selected, and remove the row when deleted. Ensure that the row is only added

Here is my specific requirement: I need a table with a default row containing a dropdown menu in the first column. When an option is selected from the dropdown, a new table row should be added with the same content as the main row and a delete button for ...

Transitioning from LESS to SASS involves adapting responsive breakpoints

Adapting from Less to Sass has been my current focus. I have defined responsive breakpoints in Less variables: /* Breakpoints */ @break1:~"(max-width: 570px)"; @break2:~"(min-width: 571px) and (max-width: 1002px)"; @break3:~"(min-width: 1003px)"; These ...

Is it possible for the JavaScript DOM API to retrieve the exact casing of attribute and tag names?

Is it possible to retrieve the original casing of an attribute name or tag name from the source? The attribute name is in lowercase The tag name is in uppercase The local element name is in lowercase I am looking for a solution that doesn't require ...

Having trouble with the rendering of the Stripe Element Quickstart example

Currently, I am diving into the world of Stripe's Element Quickstart. Take a look at this fiddle that I have been working on. It seems to be quite different from the example provided. Although I have included the file, I can't seem to figure out ...

Issues with the styling of a CSS webpage within an AJAX application

I'm encountering an issue with my CSS style that works fine on a static HTML page. The problem arises when content is dynamically loaded into layer3 and then into layer 2. I'm struggling to understand the consistency as sometimes the layers are ...

Incorporating an Aspx Page into a Static Website

We currently have a static website consisting of all html files. In addition, we have a dynamic ASPX website with a specific page (RegisterAndBuy.aspx) that displays data based on user selections and collects personal information to send to the Admin via ...

Align an image in the center of a div without using absolute positioning

I'm currently facing an issue where I need to align an image at the center of a div without using absolute positioning. This is necessary because the image spills over into any adjacent divs on the page. Specifically, I have a previous div containing ...

Typescript input event

I need help implementing an on change event when a file is selected from an input(file) element. What I want is for the event to set a textbox to display the name of the selected file. Unfortunately, I haven't been able to find a clear example or figu ...

Despite being positioned absolutely, they are able to make z-index work effectively

Currently, I am facing an issue with two elements in my design. The first element consists of dotted circles that should have a z-index of -999 so they remain in the background entirely. The second element is a login form that needs to have a z-index of 99 ...

Refreshing the webpage upon submitting with Angular2 and Firebase technology

Yesterday, I came across an Admin HTML template that I wanted to integrate with Firebase. However, when I tried to register and clicked on Sign in, the page would reload instead of carrying out the Firebase createUserWithEmailAndPass process. Here is a s ...

Implementing checkboxes for each API data in JavaScript

I am fairly new to this and I am currently working on a to-do list. I want to include a checkbox next to each item from the API to indicate whether it has been completed or not. <script type="text/javascript"> fetch('http://localhos ...

Clicking on AngularJS ng-click to navigate to a different page within an Ionic framework application

My goal is to navigate to another page when clicking on a button in the Ionic navbar at the top. However, I am encountering an issue where upon clicking, all nav bar buttons disappear. Interestingly, using dummy codes triggers an alert successfully. But w ...

Troubleshooting Problem in Coursera's AngularJS Week 3 Exercise 4

Here's the issue I'm facing: After launching the index page with 'gulp watch', there's nothing visible on the screen. An error appears: Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.4.12/$injector/modulerr?p ...

Can someone provide guidance on creating a JavaScript function that locates an image within an <li> element and sets that image as the background-image property in the li's CSS?

Let's dive deeper into this concept: <li class="whatever"> <img src="/images/clients/something.jpg"> </li> <li class="whatever"> <img src="/images/clients/whatever.png"> </li> He ...