Tips for positioning a hero image perfectly using HTML and CSS

I am looking to display two hero images on the index page and wondering how to align them. My goal is to have both images centered with 50px separation under the header, ensuring they are of equal height.

Will using the flex property work for hero images?

.hero-image1 {
    /* Add a darkened background effect using linear-gradient for better text readability */
    background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url("https://galacticsabers.co/wp-content/uploads/2023/08/mattgallodesigns_the_planet_naboo_from_star_wars_hyper_realism__92ca62a7-f8f0-458f-a6a7-49c3120150de-1024x574.png");
    padding-left: 20px;
    margin: 20px;
    justify-content: left;
    align-items: left;
  }

  .hero-image2 {
    padding-left: 20px;
    margin: 20px;
    justify-content: right right;
    align-items: right right;
    background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url("https://i.pinimg.com/originals/00/84/89/0084892bd1326bdcb2cc597820af2fdf.png")
  }

  .hero-image1, .hero-image2 {
    width: 300px;
    height: 200px;
    border-radius: 15px;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    position: relative;
  
  /* Center text within the image */
  .hero-text1, .hero-text2 {
    text-align: center;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: white;
   }
<div class="hero-image1">
  <div class="hero-text1">
    <h1>Our Bestsellers</h1>
    <a href="bestsellers.html"><button>Bestsellers</button></a>
  </div>
</div>

<div class="hero-image2">
    <div class="hero-text2">
        <h1>Our New Arrivals</h1>
        <a href="newarrivals.html"><button>New Arrivals</button></a>
    </div>
</div>

I attempted to use the "justify-content" and "align-items" properties as suggested by Google, but it did not produce the desired results.

I also tried aligning the images using padding and margins in an unconventional way, but that only caused issues with the rest of the page layout.

Answer №1

Place your content within a flex container.

For instance, consider the following simplified example:

.flex-parent {
  display: flex;
  align-content: center;
  flex-wrap: wrap;
  gap: 20px 5px;
}

.flex-parent div {
  background-color: gray;
}
<div class="flex-parent">
    <div class="hero-image1">
      <div class="hero-text1">
        <h1>Our Bestsellers</h1>
        <a href="bestsellers.html"><button>Bestsellers</button></a>
      </div>
    </div>

    <div class="hero-image2">
       <div class="hero-text2">
         <h1>Our New Arrivals</h1>
         <a href="newarrivals.html"><button>New Arrivals</button></a>
       </div>
   </div>
</div>

Visit Mozilla's website for detailed information on CSS flex properties here

Answer №2

.hero-image1 {
    /* Apply a darkened background effect to the image (photographer.jpg) using "linear-gradient". This enhances text readability */
    background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url("https://galacticsabers.co/wp-content/uploads/2023/08/mattgallodesigns_the_planet_naboo_from_star_wars_hyper_realism__92ca62a7-f8f0-458f-a6a7-49c3120150de-1024x574.png");
    padding-left: 20px;
    margin: 20px;
    justify-content: left;
    align-items: left;
  }

  .hero-image2 {
    padding-left: 20px;
    margin: 20px;
    justify-content: right right;
    align-items: right right;
    background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url("https://i.pinimg.com/originals/00/84/89/0084892bd1326bdcb2cc597820af2fdf.png")
  }

  .hero-image1, .hero-image2 {
    width: 300px;
    height: 200px;
    border-radius: 15px;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    position: relative;
  }
  
  /* Center text within the image */
  .hero-text1, .hero-text2 {
    text-align: center;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: white;
  }
<div style="display:flex;justify-content:center;">
  <div class="hero-image1">
    <div class="hero-text1">
      <h1>Our Bestsellers</h1>
      <a href="#"><button>Bestsellers</button></a>
    </div>
  </div>

  <div class="hero-image2">
      <div class="hero-text2">
          <h1>Our New Arrivals</h1>
          <a href="#"><button>New Arrivals</button></a>
      </div>
  </div>
</div>

Simply add a parent div to encompass both hero-image divs for better alignment control.

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

Leverage AngularJS to dynamically load CSS stylesheets using ng-repeat

I have organized my stylesheets into separate modules for each include, and I am trying to dynamically load them. However, I am facing some difficulties as when they are rendered, all I see is the following markup for each sheet that should be loaded: < ...

Display or conceal the location where the click event occurs within a single div

progress $(".button-toggle").click(function(e){ $(this).parents.find('#second').toggle(); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="first"> <a href="#" class= ...

Text located incorrectly beside image within container box

Below is the HTML code that defines a box containing title text and an image. <div id="about"> <div id="title"> <h3><b>About</b></h3></div> <div id="text"><p>Text</p></div> <div id="img ...

What is the best way to eliminate the border in IE edge that surrounds special characters?

Here is the current appearance of the cross button in IE/Edge: And here is how it is supposed to look: Upon inspecting the CSS, it reveals that the #10006 html character has a color code of FFF: I am seeking assistance on how to remove the black border ...

What is the best way to prevent the table from being added again once it has been displayed?

I'm faced with the task of displaying an HTML table in a popup window when a button is clicked, using Bootstrap modal functionality. This scenario is similar to a preview function where user input is displayed in a table when a preview button is click ...

Guide on utilizing multiple ng-apps alongside multiple controllers

Having trouble accessing controller values in different ng-apps? Here's a setup with two ng-apps and their controllers, where you may encounter errors when trying to access the value of one controller in another. Need some assistance with this issue. ...

Convert HTML content to a PDF file with Java

Welcome to the community! My project involves extracting data from a website containing information on various chemical substances and converting it into a PDF while preserving the original HTML formatting, including CSS styles. For example, here is a li ...

Turn off slider trace animation?

Check out the slider component in MUI here: https://mui.com/material-ui/react-slider/ I'm currently exploring how to disable the animation on the nub so it moves instantly to the new position. Any advice on how to achieve this? ...

The div is refusing to align to the left within the same row

I need assistance in creating a footer layout with an h3 and two div elements, all floated to the right with a percentage margin. The issue I'm facing is that the second div element (div2) is not floating to the left as expected but instead aligns be ...

The random quote generator or current tweet quote feature is malfunctioning

I am having trouble with a jQuery on click function that is not working to tweet the current quote. I am working on building a random quote machine and tweeting the current quote is essential. Despite successfully implementing the JSON API, I cannot seem t ...

Avoiding Overlapping DIVs: Tips to Keep Your Elements in Place

Instead of using table-based layout, I have decided to go with a DIV-based layout in order to streamline the markup and improve page load speed. However, as I am not an expert in CSS, I find it quite challenging. Below are the CSS classes I am currently us ...

HTML - Align 3 tables horizontally with text below

I have successfully floated 3 tables next to each other using <table style="float:left;">. However, I am facing an issue where some text is being wrapped to the right side of the right-most table instead of being left-justified at the very ...

Using Jquery .ajax to Populate Select Dropdown with JSON Data

I have put in a lot of effort but I'm not seeing any results. My JSON data, created with PHP, looks like this (with the header sent before the data): {"users":[ {"id":"3256","name":"Azad Kashmir"}, {"id":"3257","name":"Balochistan"}, {"id":"3258","na ...

Ensuring a DIV remains fixed in place for a specific number of scrolls

I'm looking to create a 'Page section' that remains in place while scrolling for a specific distance and then smoothly transitions to the next section. I've attempted to implement this within the child theme without success... Any sugge ...

Understanding the positioning of HTML elements using CSS

If you have the HTML snippet below: ... <fieldset> <div>above or below</div> <div>content</div> </fieldset> ... Is there a way to use CSS to position the <div>above or below</div> above or below the < ...

Leveraging a common element throughout various partial views

In my ASP.Net MVC 3 application, I have various controllers and actions that display pop-up dialog windows for user input. One important aspect of these dialogs is the faded mask that appears behind them. Each dialog window control resides in a separate P ...

Change the value of the material slide toggle according to the user's response to the JavaScript 'confirm' dialogue

I am currently working on implementing an Angular Material Slide Toggle feature. I want to display a confirmation message if the user tries to switch the toggle from true to false, ensuring they really intend to do this. If the user chooses to cancel, I&ap ...

jQuery parent() Function Explained

checkout this code snippet - https://jsfiddle.net/johndoe1994/xtu09zz9/ Let me explain the functionality of the code The code contains two containers: .first and .second. The .first container has two default divs with a class of .item. The .second contai ...

"Implement a feature that allows for infinite scrolling triggered by the height of

Looking for a unique solution to implement a load more or infinite scroll button that adjusts based on the height of a div. Imagine having a div with a height of 500px and content inside totaling 1000px. How can we display only the initial 500px of the div ...

Using SQL to Insert Values into Select/Option

I am attempting to create a select form element with predetermined options. <select name="select1"> <option value="value1">Value 1</option> <option value="value2">Value 2</option> <option value="value3">Value 3 ...