Align the final inline-block to the right

Imagine having an unordered list with 5 list items. The list items have been styled to display: inline-block in order for them to stack horizontally, left aligned.

However, the challenge arises when you desire the very last list item to be right aligned. Is there a clever solution to achieve this without relying on float?

Answer №1

To easily clear float, you can utilize the pseudo-element ::before. Below is an example demonstrating the use of last-child:

ul li {
  width: 100px;
  height: 50px;
  display: inline-block;
  background: red;
}
ul li:last-child {
  float: right;
}
ul li:last-child:before {
  content: '';
  clear: both;
}
<ul>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
</ul>

Answer №2

To achieve the desired layout without using floats, you can try this approach:

li:last-child {
    position: absolute;
    right: 10px;
}

Check out the example on Fiddle to see it in action.

Feel free to adjust the value of "right" to customize the spacing between the last item and the other list items or border.

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

Adding an image to a div element dynamically with the help of JavaScript

I am in the process of creating a Baccarat game and I'm aiming to display an image of each card within a div that is nested in a li - list item. I have set up an empty image tag inside a div with an id tag, along with implementing a Card Object in Jav ...

Refreshing PHP Script

I have a PHP file that contains a combination of HTML elements, JavaScript functions, and PHP scripts. I need to rerun a specific part of the PHP script repeatedly. Let's consider the following example: <html> <?php $connection = ...

The paragraph fails to trigger the CSS :hover effect when I assign it an ID

Here is an example of a CSS3 transition with ease effect: HTML <div class="button"> <a href="#" onMouseOver="clicksound.playclip()"></a> <p id="myId" class="top"></p> </div> CSS * { pad ...

The functionality of react-waypoint's onEnter/onLeave event handlers seems to be malfunctioning

Recently, I experimented with react-waypoint to control the visibility of a div. The code works as intended by hiding the div when it reaches the waypoint inside onEnter. When the div is inside, the isInView state becomes true, which in turn triggers the d ...

MDBootstrap Datatable Failing to Apply Automatic Styling Properly

Working on my Laravel project, I decided to use MDBootstrap to style a datatable. However, after running the script at the bottom of my page to create the datatable, I noticed that the Search function and pagination were not styled and did not have classes ...

Keywords: <footer>, <aside> not encompassing all aspects of the HTML on the content page

Recently, I have been transitioning my codebase from .NET webform to .NET Core: In the _Layout.cshtml, elements like <hr> and <section> span the entire width (e.g. 100%) of the html page as expected. However, when I insert similar tags in the ...

Achieving alignment of header text and body text on the same line

I am dealing with a formatting issue where the first item in my paragraphs is not lining up properly. Here is an example of the problem: Header waspeen: 3304.07 ananas: 24 appel: 3962.0 Header waspeen: 3304.07 ananas: 30 appel: 3962.0 Does anyone k ...

"Creating multiple circles on an HTML5 canvas using an iPad: A step-by-step guide

My current code is only drawing one circle at a time on an iPad view, but I want to be able to draw multiple circles simultaneously. How can I achieve this? // Setting up touch events for drawing circles var canvas = document.getElementById('pain ...

What is the process for modifying the value of a concatenated string?

I have a scenario where I need to extract the date of a blog post from one page on a website and display it on another page within the same site. Initially, the date is shown as 2/22/18 on the first page. With the jQuery code provided below, I am able to ...

A step-by-step guide on using Jquery to fade out a single button

My array of options is laid out in a row of buttons: <div class="console_row1"> <button class="button">TOFU</button> <button class="button">BEANS</button> <button class="button">RIC ...

Deactivate the rest of the buttons by utilizing the e.target.id

I have ten expansion panels. When I click a button in one expansion panel, I want to disable other buttons in the remaining expansion panels. The issue is that when I try to target an id, it does not return e.target.id but instead returns as the value pass ...

Troubleshooting a text alignment problem with form-group in the Bootstrap framework

Currently, I am in the process of building a form that consists of several form-group elements. My main issue arises when dealing with long text as it causes alignment problems. Could you kindly take a look at the problem through this link: http://jsfidd ...

PHP - What is the reason for the output being a multi-dimensional array?

Hey there, I'm currently working with this code and for some reason, the variable records2 is returning a multi-dimensional array. Can anyone help me figure out why this is happening? I actually need it to be a simple, single dimension array. functi ...

Making sure all items in the top row of Flexbox columns have the same height

Within my flex wrapper, I have multiple column components with various flex items inside. Each column displays content adjacent to each other. The challenge is to ensure that the top item in each column has an equal height, creating a row-like effect. I&a ...

Position two elements side by side without utilizing the float property

<html> <head> <style> #success { color: #f0f0f0; position: relative; top: 3%; left: 50%; padding: 0; margin: 0; } #errors { color: #b81d18; ...

Choose the appropriate sprite based on the calculated result

I have a PNG file with 30 sprites in it. After converting it into CSS using , I am facing an issue with my JavaScript code. The script generates a random number between 1 and 30, and I need to select the corresponding sprite based on this number. .sprit ...

What separates $(document).ready() from embedding a script at the end of the body tag?

Can you explain the distinction between running a JavaScript function during jQuery's $(document).ready() and embedding it in the HTML within script tags at the bottom of the body? Appreciate your insights, DLiKS ...

Utilizing Selenium with Java, you can hover over a button and choose to click on any of the available options

Currently, I am utilizing Selenium to automate the user interface using Java. Within the UI, there is an action button that, when hovered over by the User, displays two clickable options - Create and Edit. For ease of use, I have stored CSS locators as E ...

Using a double border causes the div to appear bigger in size

I'm currently working with React and Tailwind CSS, and I have a specific design goal in mind: https://i.sstatic.net/rbVP1.png To achieve this, I added borders to both the + and - buttons, as well as a border on the parent div to encompass all elemen ...

Can media queries styles be applied to a window of random size using JavaScript?

Can JavaScript be used to apply media queries style based on random window sizes? I have 5 buttons that I want to switch styles for using JavaScript according to the media queries defined in my CSS stylesheet. Is there a method to achieve this? ...