Using percentage-based widths to ensure responsive design

Suppose I specify that the container (div) holding four items should be 100% wide, and each element (li) should be 25% with no margins or padding. Shouldn't each item perfectly fit on one line within the container? Why does it not align as expected, causing the last one to spill onto the next line?

Review the HTML structure on jsFiddle

<div>
    <ul>
        <li><img src="http://static.comicvine.com/uploads/original/2/29462/582674-kenny.png" /></li>
        <li><img src="http://static.comicvine.com/uploads/original/2/29462/582674-kenny.png" /></li>
        <li><img src="http://static.comicvine.com/uploads/original/2/29462/582674-kenny.png" /></li>
        <li><img src="http://static.comicvine.com/uploads/original/2/29462/582674-kenny.png" /></li>
    </ul>
</div>

The accompanying CSS style rules are listed below:

* {
    margin: 0;
    padding: 0;    
}

div {
    width: 100%;
}

li {
    display: inline-block;
    list-style: none;
    width: 25%;
}

img {
    width: 100px;
}

Answer №1

Check out the fiddle here: http://jsfiddle.net/arshadmuhammed/n4e1ncvh/1/

Include float:left in your li styling

li {
    display: inline-block;
    list-style: none;
    width: 25%;
    float:left;
}

Your answer to the second question:
2) Rather than adding margin, apply border & box-sizing to your li for equal spacing between them.

li {
    box-sizing:border-box;/*new*/
    list-style: none;
    width: 25%;
    float:left;
    background:red;
    border:20px solid white;/*new*/
}

Answer №2

According to your explanation, the reason the last image is going to the next line when the width is set to 100% and the image width is 25% is because you have not specified the position of the image. Try adding float:left; to your code for better alignment.

Answer №3

The reason for this issue is due to the presence of unnecessary whitespaces between inline-blocks in your HTML code. To resolve it, simply remove these spaces:

<div>
    <ul>
        <li><img src="http://static.comicvine.com/uploads/original/2/29462/582674-kenny.png" /></li><!--
        --><li><img src="http://static.comicvine.com/uploads/original/2/29462/582674-kenny.png" /></li><!--
        --><li><img src="http://static.comicvine.com/uploads/original/2/29462/582674-kenny.png" /></li><!--
        --><li><img src="http://static.comicvine.com/uploads/original/2/29462/582674-kenny.png" /></li>
    </ul>
</div>

For an updated version of the JSfiddle, click here: http://jsfiddle.net/somj9pq2/4/

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

I am attempting to dynamically align the images of two articles. The exact heights of the containers are not specified. Can anyone provide guidance on how to achieve this?

My code snippets: <article class="featured"> <img src="http://www.placehold.it/300x100/ff0000"> <p> <!--Text--> </p> </article> <article class="sub-featured"> <img src="http://www.placeh ...

Can the current website navigation be integrated into the blog script?

People might see me as a spoil sport for not using Wordpress, but I prefer a flatfile blog system for my small site. Currently, I am using the ozjournals-3.2 blog system. I need assistance in installing a header and site navigation on top of the blog page ...

`Unable to automatically redirect to new pages on the website`

After transitioning my website from plain HTML to WordPress, I encountered an issue with old links still using the .html extension. To address this problem, I decided to upload all the old HTML files to the new server and create redirects by removing the ...

When the AJAX function is called again, the previous loading process is interrupted, without the use of jQuery

I have created a function that loads a URL into an element, but it seems to be encountering issues when called repeatedly in a loop to load multiple elements. The current load operation stops prematurely: function loadDataFromURL(url, elementId) { var ...

Tips for adding a click event to a random "div" within a parent "div" element?

Upon clicking a main "Div", it splits into an n X n matrix with random colored divs. I now want to implement a click function for these random colorful divs that are scattered throughout the main "div". $(window).load(function() { var no = 1, $m = ...

Circular graphs displaying percentages at their center, illustrating the distribution of checked checkboxes across various categories

Looking for a JavaScript script that displays results in the form of circles with percentage values at their centers, based on the number of checkboxes checked in different categories. The circle radius should be determined by the percentage values - for e ...

What is the best way to relocate an image or link using CSS?

I've been attempting to adjust the position of this image using CSS, but no matter how many times I try, it just refuses to budge. Can someone please help me troubleshoot what might be causing this issue? #yahoo1 { position: absolute; top: 100p ...

Issue with the image posting function in Laravel - why isn't it functioning properly?

Looking for Assistance I've been working on a simple web application using Laravel 6.0 and have encountered an issue with the image post function I developed. Despite not receiving any error messages, the functionality doesn't seem to be work ...

Having a tough time navigating the Bootstrap upgrade from 2.x to 3.x - now my design is all out of whack

Upgrading my site to the newest version of Bootstrap has been quite the challenge. I'm noticing that despite the window size being the same, version 1 and version 2 of my site are displaying different @media sizes. What's going on? On the left i ...

Navigating the screen reader with the cursor位

Site Design Challenge I recently discovered that the master/detail design of my website is not very accessible. The main view features a column chart where each column represents a different month. Clicking on one of these columns reveals details in a nes ...

Setting the CSS property `overflow-y: scroll;` does not make the element scrollable and causes it to exceed 100% of the block

I am seeking a solution to create a y-axis scrollable block within the left-navbar. The height of this block should occupy all available space within the left-navbar, ensuring that the left-navbar itself remains at 100% of the page height. The issue arise ...

Strategies for handling divisions while resizing the browser界面 manipulating divisions during

Here's the HTML/CSS code I'm working with: .container { max-width: 900px; margin: 0 auto; margin-top: 30px; } .divisions { border: 1px solid Black; } .Fisrt-Line { height: 180px; } .First { background-color: Green; width: 32.2% ...

AngularJS initial scroll down functionality

I am currently working with this div container setup: <div class="col-md-8 grid-wrapper-div chatHistory"> ... </div> Here is the CSS associated with it: .chat .chatHistory { overflow-y:scroll; height: 550px; } If the page loads, I w ...

Achieve horizontal centering of modal without the use of jQuery

Currently, I am developing a modal within a React application. The modal's width is set to auto and its position is set to fixed. My goal is to center it horizontally in the middle of the screen. .Modal { position: fixed; width: auto; z- ...

In instances where the text exceeds the width of the container, a horizontal scrollbar will appear

When looking at the custom content section on my website, you will find paragraphs and lists. One of the list items contains the following text: Track your package online: The link provided is quite lengthy and lacks white spaces, causing it to extend ...

Struggling to align my image in the center while applying a hover effect using CSS

Hey there, I'm having an issue centering my image when I add instructions for it to tilt on mouseover. If I take out the 'tilt pic' div, the image centers just fine. Can anyone help me identify what I might be doing wrong? Thanks in advance! ...

What is the best way to increase the size of a specific text style?

Recently, I created a post in WordPress that included some code samples. To ensure the code was displayed neatly, I utilized the "preformatted" style. However, upon previewing it, I noticed that the text within the code samples was incredibly small, almost ...

Is there a way to dynamically add <td> based on the quantity of values in a JSON object?

I have developed a program that retrieves values from JSON and I aim to display these values in a table. Currently, only the last array value is being displayed in the table even though all values are available in the console. The objective now is to dynam ...

SassError: Variable not defined

I have a file containing variables with different themes that I need to override: _vars.scss body { $bg-color: #fff } body.theme-dark { $bg-color: #333 } In my Angular button component, I am referencing these variables: button.scss @import '_va ...

Guide to integrating various HTML files into a single HTML file with the help of Vue.js

Although I am familiar with using require_once() in PHP, unfortunately, I am unable to use PHP in my current project. I attempted to use w3-include from W3Schools as an alternative, but encountered issues with loading my scripts. Even utilizing JavaScript ...