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

The initial navigation pill content failed to display in Bootstrap

I am experiencing an issue with my pills that have 4 tabs. Initially, when I reload the page, the content of the first tab does not show up. However, if I navigate to another tab and then back to the first tab, the content appears. Can anyone suggest a fix ...

What are the best methods for maintaining the appearance of my website when resizing my browser window?

I am replicating the Twitter profile page, but I am facing an issue where the text and images get jumbled together whenever I resize my browser. This results in a messy appearance of the page. How can I ensure that the text and images stay in the same rela ...

Changing the bootstrap popover location

I'm looking to customize the position of a Bootstrap popover that appears outside of a panel. Here's my setup: HTML: <div class="panel"> <div class="panel-body"> <input type="text" id="text_input" data-toggle="popover ...

Menu toggle vanishes suddenly

Whenever I click the toggle button, I notice that the menu (which I have set to appear on toggle) only shows for a brief moment before disappearing. As a beginner in bootstrap, I am sure I might have made some obvious mistakes. Any assistance would be gr ...

Aggregated Mvc Razor components

After creating a layout in ASP.NET MVC, I utilized a script bundle to load the jQuery library. Unfortunately, it seems that the jQuery library is not being loaded properly. The structure of my layout is as follows: <!DOCTYPE html> <html> < ...

Adding a new row to a Bootstrap table while maintaining the consistent style

Is there a way to dynamically add a new table row with different styling using jQuery? I'm facing this particular issue and need help in solving it. Below, I have included some screenshots of my code and the view for better understanding. Here is the ...

Tips for creating a filter that meets multiple conditions in HTML

Click here for image descriptionI have the following code where I am utilizing story owner and story state. ng-repeat="item in IterationStories | filter: {Story: {storyOwner: filterRequestor}} | filter: {Story: {state: filterKey}} " //works fine when ...

When using margin-right and a width of 100% in the WordPress backend, a scrollbar may be triggered

I have been working on developing a configuration page for my WordPress plugin. In order to display the content, I included an <ul> element inside a <div> container and added it to the configuration page. However, I encountered an issue where a ...

Maximizing the functionality of PHP switch to create clickable list items

I have successfully created a simple switch for my website, but I am encountering an issue with clickable list items. The concept is that when I click on any of the list items, the switch should display the appropriate greeting (whether it's Night or ...

Hidden Navigation Bar in AngularJS

Is there a way to hide the navBar when the route is "/"? I managed to successfully hide/show the navbar, but the issue arises when the navbar is hidden. The "app-body" div then has 50px of padding top (where the navbar should be). Here is my HT ...

Issue with jQuery centering in IE browsers with jquery-ui-1.10.3.custom.js and jquery-ui-1.9.2.custom.js not working as expected

Are there any other alternatives you can recommend? Here is the dialog structure: <div id="dialogbox" title="message box"> <p>example content</P> </div> ...

Pricing determined by location on a website created with HTML

We are looking to customize our HTML5/CSS3 website by displaying different pricing tables based on the location of each visitor. Our goal is to have a fixed price for users from Singapore while showing a different price for visitors from other parts of th ...

Difficulty in altering the background of an input box

I am attempting to design a textbox that changes its background color to green when the correct answer is submitted, and to red for an incorrect answer. Unfortunately, nothing is happening for either option. document.querySelector('form').addE ...

Troubleshooting Problems with CSS Before Selector and Margins

Currently, I am attempting to utilize the before selector in order to replicate some list item behavior for elements. Due to constraints where I cannot use list items, it is crucial that I find a way to make styles work effectively. If you would like to s ...

Creating a smooth fading effect for an element within a react component

I am currently working on implementing a fade out warning/error message (styled with Bootstrap) in a React component, however, I am encountering some challenges with the timing of the fade-out effect. Up to this point, the fade out effect is functioning c ...

What could be causing my resize event to not trigger?

My goal is for the #sequence div to be full height of the browser window when the window size is greater than 920px in height. In such cases, I also want to trigger a plugin. If the window size is lower than 920px, then the height of the #sequence div shou ...

I am unable to apply CSS to style my <div> element

I've run into a snag with my coding project, specifically when attempting to style my div. Here is the code I have so far: All CSS rules are applying correctly except for the .chat rule. Can someone help me figure out what I'm doing wrong? var ...

Achieving CSS transition effects using the .appendChild function in JavaScript

When using the mouseenter event, I have a JavaScript function applied to an svg path element: const fadeIn = (event, locale) => { event.target.style.fill = 'hwb(' + (120 - score[locale] * 1.2) + ' 0% 0% / 1)' } This function wor ...

Copy and paste content from Outlook, Word, or any Office software directly into the embedded browser

Our application is performing well, but there is an issue with some users copying text to Word before pasting it into the app. The HTML gets parsed out somewhat correctly, but it often includes tags from outlook or word that our XHTML engine doesn't r ...

Customizing the size of dateBox icons in JQuery Mobile

Is there a way to increase the button size within the input box of a dateBox? Currently, it appears small and difficult to click accurately. Additionally, it would be beneficial if clicking anywhere in the input field could open the calendar, instead of ju ...