Optimal methods for arranging images in a list with CSS

I am trying to create a grid layout for a list of images using CSS. Here is the HTML code I have:

<ul id='feat-products'>
    <li><img id='feat-product-1'></li>
    <li><img id='feat-product-2'></li>
    <li><img id='feat-product-3'></li>
    <li><img id='feat-product-banner'></li>
</ul>

Here is the CSS I am using:

#feat-products ul {
    width: 1000px;
    height: 400px;
}    
#feat-products li {
    display: inline-block;
    border: solid 1px;
}
#feat-products li:nth-child(1) {
    width: 500px;
    height: 400px;
}
#feat-products li:nth-child(2) {
    width: 250px;
    height: 200px;
}
#feat-products li:nth-child(3) {
    width: 250px;
    height: 200px;
}
#feat-products li:nth-child(4) {
    width: 500px;
    height: 200px;
}

However, the layout I am getting does not look as expected. I have tried using negative margins but I believe there might be a better way to achieve the desired layout. Any help would be appreciated!

Thank you!

DB

Answer №1

Make a modification to your CSS by adding the following code to float the first child of the li element to the left:

#feat-products ul {
    width: 1000px;
    height: 400px;
}    
#feat-products li {
    display: inline-block;
    border: solid 1px;
}
#feat-products li:nth-child(1) {
    width: 500px;
    height: 400px;
  float:left; //Include this line in your CSS
}
#feat-products li:nth-child(2) {
    width: 250px;
    height: 200px;
}
#feat-products li:nth-child(3) {
    width: 250px;
    height: 200px;
}
#feat-products li:nth-child(4) {
    width: 500px;
    height: 200px;
}

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

Display a popover when the button is clicked using jQuery

Is there a way to show a notification message using popover? I've managed to make it work, but I'm facing an issue where the popover doesn't appear on the first click of a button after the page loads. Here is the code snippet: <button ty ...

What could be the reason that the accordion is failing to expand or collapse when clicked?

I am facing an issue where the div is not expanding or collapsing upon clicking the + sign. I have tried removing unnecessary scripts but it still doesn't work. Additionally, there is an error in the console: Uncaught TypeError: $(...).slideReveal is ...

An alternative method for storing data in HTML that is more effective than using hidden fields

I'm trying to figure out if there's a more efficient method for storing data within HTML content. Currently, I have some values stored in my HTML file using hidden fields that are generated by code behind. HTML: <input type="hidden" id="hid1 ...

Use HTML input to enter numbers that are not written in the English language

I am facing an issue with my ASP .Net core web application where I am using Devextreme controls. The main requirement for my application is to support both Persian and Arabic languages, including numbers and dates. I have set up my local windows keyboard ...

Is there a way for me to adjust the window's placement?

Currently utilizing Jquery to launch a new Telerik window, as shown below: var win = $('#myWindow').data('tWindow'); win.open(); With the knowledge of the mouse position (x,y), my goal is to initiate the window at that exact location. ...

Easy ways to align sprite images and text in the center

I am trying to utilize css sprites for my website. I have a basic image and some text that I would like to display together. My goal is to center the image on the page, and then vertically center the text next to it. As a newcomer to css, I am struggling t ...

The Hamburger Menu Opens Smoothly But Refuses to Shut Down

Below is the HTML code for my website. I have managed to open the hamburger menu with the code, but I am facing an issue where it won't close. I have checked the HTML structuring using a validator and found no errors. Additionally, I have reviewed my ...

Update the HTML page when switching between tabs

Currently, I am experiencing an issue with tab navigation in HTML. Within my code, I have two tabs named #tab1 and #tab2, each containing a form. The problem arises when I input data into #tab1, switch to #tab2, and then return to #tab1 - the information I ...

How to remove an extra horizontal scroll bar from a fixed-header HTML table?

While working on updating my HTML tables to have fixed headers, I followed online examples that suggest making table-layout fixed, thead and tbody blocks, setting height constraints for tbody, and applying overflow-y to tbody. The result is functional as m ...

Adding a custom source to the script tag in Angular 7

I am currently using angular cli to develop my web application. The app is being built in the dist folder as of now. This is the index.html file: <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Adm ...

Display HTML content from a JSON array with the help of ng-bind-html

I am looking to display HTML tags stored in a JSON object property within a modal window. Below is an example of the JSON object: { "fileUploadCategories": [ { "id": "Bread", "title": "GrandmaBread", "info": "Some info text.", "restService": "emp ...

What could be causing the alignment issue with cells in Bootstrap?

I'm currently facing an issue with Bootstrap where two columns that are supposed to appear side-by-side on medium resolution end up displaying one below the other. You can view a screenshot of the problem here, and the code can be found at this link. ...

The performance of the animation on http://responsive-nav.com/ becomes erratic when viewed on Android devices

Recently, I came across a fantastic plugin that works perfectly on regular computer browsers. However, when I tested it on my android phone, the css3 animation for the dropdown appeared choppy and seemed to be dropping frames. Does anyone have suggestions ...

What could be the reason for the body background color refusing to change

I'm currently working on a JavaScript calculator project and I'm using this code snippet from codepen. The issue I'm facing is that despite setting the body background color to black, it still appears white. Could there be a spacing problem ...

The content has spilled over from the div and leaked into the adjacent div

When the left div's content exceeds the right div's, it spills over into the next container div. Sample HTML: <div class="musictemplate_container"> <div class="musictemplate_left"> something<br> omething< ...

Is your email showing up with an unknown sender when using the display function?

I'm facing an issue where the name entered in the input field on a contact form is not being displayed after submitting the email. The headers in PHP were changed and now it shows "unknown sender" instead of the name that was entered. How can I fix th ...

By utilizing the "order" property, I can arrange divs to start at the bottom and ascend as additional elements are added

Exploring the use of display: flex and order:, I noticed a curious behavior. When I set the order for my first div to 0 (order: 0;), it appears at the top of the page. However, as I add more divs with different orders, they initially show up in unexpected ...

Looking for assistance with changing the class of a span when a radio button is selected

I'm facing an issue with toggling the class of a <span> when a radio button is clicked. <html> <head></head> <style type="text/css"> .eHide { display:none; } </style> <script type="text/javas ...

What is the best way to incorporate JSON data into an HTML document?

Currently, I am working on a project involving the creation of a playable ad for Facebook. Per Facebook's ad specifications (), only a single HTML file under 2mb is accepted for uploading. This necessitates the inlining of all external references into ...

Capture the onclick attribute with jQuery and then reapply it

I am facing a challenge with a page that has several calendars composed of HTML tables where each day is represented by a td. The td elements have an onClick attribute which I need to manipulate using jQuery. Specifically, I need to remove the onClick attr ...