What is the best way to center images horizontally in CSS and HTML?

I'm looking to create a driver's page where the desktop view displays images horizontally instead of vertically. I've tried adjusting the display attribute with limited success, and so far, the grid display is the closest I've come to achieving my desired layout. I'm still relatively new to this, so any assistance would be greatly appreciated. Thank you! Here is a snippet of my code:

Html Code:

<div class="drivers-container">
  <div class="driver-row">



    <!--Drivers from Spain-->
    <h2><a id="spain" href="#spain">Spain</a></h2>
    <div class="image2">
      <img src="Drivers/EFREN LLARENA (Spain) 2.jpg" alt="Efren Llarena" width="500">️;
      <div class="details">
        <h2><span>Efren Llarena</span></h2>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Nesciunt explicabo perferendis mollitia iste libero magni ab! Ipsam id beatae nesciunt?.</p>
      </div>
    </div>

    <div class="image2">
      <img src="Drivers/Sara Fernandez (Spain).jpg" alt="Sara Fernandez" width="500" height="400">️;
      <div class="details">
        <h2><span>Sara Fernandez</span></h2>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Nesciunt explicabo perferendis mollitia iste libero magni ab! Ipsam id beatae nesciunt?.</p>
      </div>
    </div>
 </div>

Sara Fernandez (Spain) Efren-Llarena (Spain)

CSS Code;

.driver-row h2 a{
    color: #fff;
    text-decoration: none;
    font-size: 70px;
    font-weight: 300;
}
.drivers-container{
    margin: 30px;
}
.driver-row{
    width: 100%;
    display: grid;
    justify-content: center;
    flex-wrap: wrap;
}
.image2{
    background: #000000;
    position: relative;
    flex: 1;
    max-width: 460px;
    height: 300px;
    margin: 20px;
    overflow: hidden;
}
.image2 img{
    opacity: 0.8;
    position: relative;
    vertical-align: top;
    transition: 0.6s;
    transition-property: opacity;
}
.image2:hover img{
opacity: 1;
}
.image2 .details{
    z-index: 1;
    position: absolute;
    top: 0;
    right: 0;
    color: rgb(0, 0, 0);
    width: 100%;
    height: 100%;
}
.image2 .details h2{
    text-align: center;
    font-size: 35px;
    text-transform: uppercase;
    font-weight: 300;
    margin-top: 70px;
    transition: 0.4s;
    transition-property: transform;
}
.image2 .details h2 span{
font-weight: 900;
}
.image2:hover .details h2{
    transform: translateY(-30px);
}
.image2 .details p{
    margin: 30px 30px 0 30px;
    font-size: 18px;
    font-weight: 600;
    text-align: center;
    opacity: 0;
    transition: 0.6s;
    transition-property: opacity,transform;
}
.image2:hover .details p{
    opacity: 1;
    transform: translateY(-40px);
}

Answer №1

My suggestion is to separate the heading and additional content from the driver-container

<div class="drivers-container">
    <h2>
        <a id="spain" href="#spain">Spain</a>
    </h2>
    <div class="driver-row">
        <div class="image2">
            <img src="Drivers/EFREN LLARENA (Spain) 2.jpg" alt="Efren Llarena" width="500">
             <div class="details">
                 <h2><span>Efren Llarena</span></h2>
                 <p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Nesciunt explicabo perferendis mollitia iste libero magni ab! Ipsam id beatae nesciunt?.</p>
             </div>
        </div>

        <div class="image2">
             <img src="Drivers/Sara Fernandez (Spain).jpg" alt="Sara Fernandez" width="500" height="400">
              <div class="details">
                  <h2><span>Sara Fernandez</span></h2>
                  <p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Nesciunt explicabo perferendis mollitia iste libero magni ab! Ipsam id beatae nesciunt?.</p>
             </div>
        </div>
   </div>

For your CSS, I'll provide essential rules only. Feel free to customize with your own designs

.driver-row{
     display: flex;
     flex-wrap: wrap;
     width: 100%;
}
.image2{
     width: 50%;
     box-sizing: border-box;
     padding: 20px;
     position: relative ;
}
.image2 img{
     width:100%;
     height: 400px;
     object-fit: cover;
}
.image2. details{
     position: absolute ;
     top: 0;
     left: 0;
     width: 100%;
     height: 100%; 
}

This layout utilizes flexbox.

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

Tips for repairing a button using a JavaScript function in an HTML document

I am currently working on extracting titles from body text. To achieve this, I have created a button and linked my function to it. The issue I am facing is that when I click on the button, it disappears from its original position. I intend to keep it in pl ...

Leveraging the Xpath Contains function to locate an element with specific text content

For my Selenium Xpath locators, I have always used the Contains function successfully. However, I recently encountered an issue where it does not work for a TD element in a table, despite sending the correct text to the function. To replicate the problem ...

Unable to retrieve input using jquery selector

I am attempting to detect the click event on a checkbox. The Xpath for the element provided by firebug is as follows, starting with a table tag in my JSP (i.e. the table is within a div). /html/body/div[1]/div/div/div[2]/div/div[2]/div[1]/div/div[2]/table ...

How to customize the appearance of an HTML checkbox using custom images for a unique

Does anyone know how to change the default style for: <input type=checkbox...> I want it to use my own styled pictures instead of the default style. Can anyone help? Thank you! ...

Having trouble with the anchor tag not functioning properly

I'm looking to create a unique video experience by having an iframe video play automatically and opening a third-party video player in a lightbox style when clicked. Currently, the video autoplays, but I want it so that when the user clicks on the vi ...

The header and sub-navigation are in disarray and require some help

Dear web experts, After six months of learning to code websites, I'm tackling a big project with some challenges. The recent changes to the header and subnav have thrown everything off balance, making it look wonky and not quite right. Specifically, ...

How do I print a QTableWidget using QT5 and resize the table to fit perfectly on one side of an A4 sheet?

My QT5 application is designed to generate a monthly rota/timesheet in csv format, which can be easily read and printed using Excel or LibreOffice. However, I want to enhance the functionality by printing the table directly from Qt to the printer. I have ...

Creating a scrollable nested div on a mobile website

Is there a way to achieve a scrollable nested div on a jQuery mobile site? I am aiming for a fixed header and footer with the middle section being scrollable. Despite my attempts to set overflow:scroll (along with specifying the width and height of the div ...

"Troubleshooting a CSS Bug on a PHP Dynamic Web

I have a PHP web page that is dynamic. localhost/ctd/index.php Everything is working fine, except when I add a forward slash like this: localhost/ctd/index.php/ The CSS does not load. Is this a CSS bug or PHP issue? Here is the full code of index.php: ...

ECharts - Version 3.1.6 Event Plugin

I am looking for advice regarding the management of two charts with reference to echars from Baidu (version 3.1.6). Is there a way to control both charts by engaging in a click event on one of them? In simpler terms, how can I capture and respond to the c ...

`Changing the iframe content dynamically with next and previous buttons: a step-by-step guide`

function displayPDF(fileSlNo){ var data = { pageMode: "PDF_DISPLAY", fileSlno: fileSlNo }; var d = $.param(data); var request = $ .ajax({ type: "GET", url:"folderNavigation.do?"+d, dataType : "TEXT", success: functio ...

Is one round the limit for my JavaScript image slider?

After studying the JavaScript Chapter on W3Schools, I attempted to create an image slider. Initially, everything worked perfectly for the first cycle. For instance, when I clicked the next button, the slides continued to slide until the end of the slidesho ...

Incorporating jQuery to seamlessly add elements without causing any disruptions to the layout

I'm looking to enhance the design of my website by adding a mouseenter function to display two lines when hovering over an item. However, I've encountered an issue where the appearance and disappearance of these lines cause the list items to move ...

Is there a way to ensure that the 'pointermove' event continues to trigger even after the dialog element has been closed?

I am working on a project that involves allowing users to drag elements from a modal dialog and drop them onto the page. I have implemented a feature where dialog.close() is called once the user starts dragging. This functionality works perfectly on deskto ...

Using Vue to display a Div inside a TD element of a table does not result in a reactive behavior

I am encountering an issue with a table where the last column contains a div with three options (View, Edit, and Delete). This submenu is initially hidden, but when clicking on the options button in the last column of the table, the array used to control i ...

Shifting the position of an HTML page to one direction

I'm currently working on adding a sidebar to my Twitter Bootstrap 3 project. The goal is to have a fixed positioned nav nav-pills nav-stacked show up on the left side of the page when a button is clicked. I've set its z-index to 1000 so it appear ...

Issue: The function _lib_getAllUsers__WEBPACK_IMPORTED_MODULE_2___default(...) is not recognized as a valid function

I'm currently following the YouTube series on Next.js by Dave Gray. My goal is to import a function from the file "lib/getAllUsers" https://i.sstatic.net/1SQMg.png However, I'm encountering the following error: Unhandled Runtime Error Error: ...

Enhance your website's accessibility by using JavaScript to allow the down and up arrow keys to function

Thank you for taking the time to read this, any feedback is greatly appreciated. The current script on my website is only partially functional (click "i" in the bottom right corner). Currently, the script will focus on the first link AFTER pressing the T ...

Issue with submitting form using Jquery on select action

There seems to be an issue with this jquery code that is supposed to trigger a form submit when a drop down select is changed. It works fine on our test site, but once we move it to the live website, it breaks. Can someone help us troubleshoot and identify ...

As a useful tool in XML processing, xsl:value-of function effectively filters out any

When creating XML that contains HTML tags, the challenge arises when trying to convert all the XML data to HTML using XSLT. The issue lies in handling HTML tags included within the XML. Specifically, I need to retrieve the content within the summary tag wi ...