Center Align Images in HTML List Items

Hey there! I'm currently diving into the world of web development with responsive design, but I'm still a bit of a newbie. So please bear with me as I try to explain my issue in detail. If you need more information, just let me know!

My current challenge involves designing a page that features jQuery hover effects on images. While one might think the problem lies in the JavaScript, it's actually simpler yet extremely frustrating for me. I want my images to be centered on the page even though they are aligned to the left by default. These image boxes are contained within list items (li's) and I've attempted to wrap them in a div and center the div instead.

It's important to mention that responsiveness is key here, hence why simply adding margins or paddings won't suffice.

Below is the snippet of HTML code for the body of my webpage:

<body>
<div class="body">

   <div> <img src="images/logo.png" class="image"></div>

            <div class="imgs">
                <ul id="da-thumbs" class="da-thumbs">
                    <li>
                        <a href="http://dribbble.com/shots/502538-Natalie-Justin-Cleaning">
                            <img src="images/7.jpg" />
                            <div><span>Natalie & Justin Cleaning by Justin Younger</span></div>
                        </a>
                    </li>
                    <li>
                        <a href="http://dribbble.com/shots/501695-Cornwall-Map">
                            <img src="images/9.jpg" />
                            <div><span>Cornwall Map by Katharina Maria Zimmermann</span></div>
                        </a>
                    </li>

                </ul>
            </div>
</div>


        <script type="text/javascript">
            $(function() {

                $(' #da-thumbs > li ').each( function() { $(this).hoverdir({
                    hoverDelay : 75
                }); } );

            });
        </script>
</body>

And here is how I've styled the elements using CSS:

.body {
    width: 100%;
    height: 1000px;
    animation-name: colorChange;
    animation-duration: 10s;
    animation-iteration-count: infinite;
    text-align: center;
}

@keyframes colorChange {
    0% {
        background: #4BB4BF;    
    }
    20% {
        background: #306F7A;    
    }
    ...
    100% {
        background: #4BB4BF;    
    }


}

.button {
    padding: 10px;
    margin-top: 40px;
    font-size: 20px;
}



.da-thumbs {
    list-style: none;
    width: 100%;
    height: 100%;
    position: relative;
    margin: 20px auto;
    padding: 0;

}
...
...

.image {
 max-width: 100%;
    max-height: 100%;
    background-size: cover;
}

.imgs {margin: 0 auto;
align: center;
}

I would greatly appreciate any suggestions or thoughts on how I can successfully align these elements on the webpage. Thanks!

Fiddle Case: https://jsfiddle.net/eqyfm41r/3/

Answer №1

Here is the revised code snippet:

.da-thumbs li {
   display:inline-block;
   width:46%;
   margin: 5px;
   padding: 8px;
   position: relative;
}
.da-thumbs li img{
   box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

To view the updated code on JSFiddle, click here: https://jsfiddle.net/1zq95tzp/

The changes made include not floating the li's and applying box shadows to the images instead. This adjustment ensures that the box shadow appears close to the image edges, even when stacked on smaller screens. For responsiveness, consider adding the following style to the images:

max-width:100%;

This will prevent the images from overflowing on narrow screens. I hope this explanation clarifies things for you.

Answer №2

Have you considered:

margin-left: auto; margin-right: auto; 

Instead of a fixed margin, this code is responsive and adjusts based on the screen size.

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

Upon selecting the hamburger menu, the menu pops up; however, the homepage text is visible overlapping the menu

I'm in the process of developing a responsive portfolio page using ReactJS and Tailwind CSS. When the screen size is smaller than the medium breakpoint, a hamburger menu icon appears. However, when I click on the hamburger icon to view the menu items, ...

Easily validate the contenteditable attribute of <td> element

I am currently working on a table that displays data from a MYSQL database. Whenever a user makes changes to an element in the table, I want the database to be updated using AJAX. Below is my JavaScript code for sending data in an editable row. function s ...

Is it possible to update an anchor tag using BeautifulSoup directly?

I am aiming to retrieve all anchor tags from an HTML document, regardless of their location or depth within the structure. Once these links have been gathered, I intend to manipulate them and create new links. My goal is to update the original HTML conten ...

"Utilize HTML to make inline-block elements appear small in size while giving the illusion of being

I have a group of inline-block elements (4 total), all arranged neatly in a single row. As the window size changes, the elements wrap to the next line accordingly. The page does not display scrollbars at the bottom until the browser width reaches the minim ...

The <g> tag fails to properly render within the <svg> element in Firefox

When running an Angular 6 application with ES6-style D3js modules, there are some issues on Firefox (Chromium, Chrome, Safari, and IE Edge work fine). Below is a sample of pseudo code (full production code is available below): <svg width="500" height=" ...

Hovering over an image will not cause the floating header to appear

My attempt to create a header that fades with scroll and appears on hover is working perfectly, except for the cat pictures in the Portfolio section. Despite adjusting z-indexes and trying various solutions, the header just won't show up over the h4 e ...

Navigating a plethora of unpredictable identifiers using AngularJS

My goal is to display content from a json file that varies in type and consists of pages divided into chapters. Users can navigate through the content using an aside menu or next and previous buttons. Currently, I am able to display the content by inserti ...

Tips on prefixing Bootstrap 4 classes to prevent conflicts with CSS classes

Recently, I've been focusing on creating small applications for websites. The websites hosting these apps may or may not use a css framework. To prevent any potential conflicts, I have decided to add a unique prefix to all Bootstrap classes. For insta ...

Is it possible to retrieve the parent object?

My code snippet is as follows, and I'm struggling to access the data object from within the innerFn function. Is there a way to accomplish this? export default { data: { a: "a", b: "b" }, fn: { innerFn: () => co ...

Date selection tool malfunctioning - unexpected behavior

Struggling to identify where I'm making mistake. The dynamically created input fields with the "freeze-date" class seem to only function correctly on the initial datepicker input field that's generated. You can find the code over at http://jsfidd ...

Why isn't Gzip compression working in webpack? What am I missing?

After comparing the compression results of manual webpack configuration and create-react-app for the same application, it became clear that create-react-app utilizes gzip compression, resulting in a significantly smaller final bundle size compared to manua ...

I have a desire to use Javascript to control CSS styles

Within the CSS code below, I am seeking to vary the size of a square randomly. This can be achieved using the Math.random() property in Javascript, but I am uncertain how to apply it to define the size in CSS. #square { width: 100px; height: ...

Exploring the concept of global objects within the expressjs framework

Currently, I am working on building a school management system. One issue I encountered is related to the creation of global objects in my backend when a teacher sends a post request. I am wondering whether multiple teachers accessing the server will res ...

Thymeleaf: Automating the Adjustment of Content Fragments

In my project using Spring MVC and Thymeleaf, there is a default fragment that sets up the page structure. Here's what it looks like: <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/ ...

Interval function not initiating properly post bullet navigation activation

Currently, I am experiencing an issue with my custom slider where the auto sliding set interval function is not working after using the bullet navigation. Despite trying to implement "setTimeout(autoSlide, 1000);", it doesn't seem to be resolving the ...

JQuery Datatables: Struggling to make JQuery function work following AJAX update

Watch this screencast to get a clearer understanding of the issue.... I'm currently working on my first project involving AJAX, and I'm encountering some obstacles. The datatable is loading user details from a JSON output using AJAX. Each row ...

The scaling transformation is not accurate for uneven pixel widths

I'm attempting to resize a div while keeping the inner element in the same position and at the same size. To achieve this, I am using transform: scale(value) on the wrapper and transform: scale(1/value) on the inside div. The issue arises when the in ...

As the screen size decreases, stacked Font Awesome icons seem to vanish

Currently, I'm designing a form that requires the user to select a color scheme by clicking on circle font awesome icons. These icons are implemented using Bootstrap and when clicked, a stacked font-awesome icon appears. However, I've encountered ...

How can I trigger a JavaScript function when the ENTER key is pressed?

I'm attempting to use JavaScript to scroll down a page. The code below works perfectly when I use javascript:pageScroll() in a link or button: <SCRIPT LANGUAGE="Javascript"> function pageScroll() { window.scrollBy(0,50); // hor ...

What is the best way to retrieve a JSP parameter dynamically or how can one create a JSP parameter on the

Currently learning JSP and ajax simultaneously. In the process of creating a dynamic tab that can be added or removed using these steps. Looking to pass parameters from controller to the content area of the newly added tab. 1. When clicking on the &apos ...