Shifting two out of three lists to the right while preserving the order of the markup

I am struggling with aligning my three inline-block lists on the screen. I want the last two lists to be moved to the right, while still maintaining their original markup order.

The problem arises when the third list appears before the second list in the layout.

What could be causing this issue?

ul {
  display: inline-block; 
}

ul:nth-child(2),
ul:nth-child(3) {
  color: red;
  float: right
}
<ul>
  <li>1</li>
  <li>2</li>
</ul>

<ul>
  <li>3</li>
  <li>4</li>
</ul>

<ul>
  <li>5</li>
  <li>6</li>
</ul>

Answer №1

Applying the style float:right will shift the flow to begin from the right, resulting in the first element in the DOM being displayed on the right side.

Alternatively, you can achieve a similar effect by floating the first element to the left and adjusting the text-align property for the remaining elements:

body {
  text-align:right;
}

ul {
  display: inline-block; 
}

ul:nth-child(2),
ul:nth-child(3) {
  color: red;
}
ul:nth-child(1) {
  float:left;
}
<ul>
  <li>1</li>
   <li>2</li>
</ul>

<ul>
  <li>3</li>
    <li>4</li>
</ul>

<ul>
  <li>5</li>
  <li>6</li>
</ul>

To achieve a cleaner and more contemporary layout, consider using flexbox:

body {
  display:flex;
}

ul:nth-child(2),
ul:nth-child(3) {
  color: red;
}
ul:nth-child(1) {
  margin-right:auto;
}
<ul>
  <li>1</li>
   <li>2</li>
</ul>

<ul>
  <li>3</li>
    <li>4</li>
</ul>

<ul>
  <li>5</li>
  <li>6</li>
</ul>

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

What steps should I take to troubleshoot the JavaScript dropdown list on my webpage

I'm having some trouble with my dropdown list code. My goal is to be able to select an option from the list and have it display an image with some text, but I seem to be missing something. Here's what I have so far: <form action=""&g ...

Why do my navigation bar items in Bootstrap get highlighted in blue and underlined?

<!Doctype html> <html> <head> <meta charset="utf-8"> <title>IBAE-Information Library</title> <link rel="stylesheet" href="/Users/jeevabharathi/Desktop/Project.css"> <script sr ...

Tips for rotating individual letters within a sentence on popular web browsers, including Chrome

Can you make a sentence appear with each individual letter rotating and getting larger? This is my approach: I decided to split the sentence into separate span elements, one for each letter. Then I used CSS transform -webkit-transform to animate the lette ...

Accessing a JavaScript variable in another <script> section

I'm facing a challenge where I need to access a JavaScript variable that is declared in one block of an HTML page from another block on the same page. This is crucial for me to be able to halt an AJAX call that is currently ongoing. However, I'm ...

Tips For Making Your Bootstrap 4 Page Fit Perfectly on Mobile Screens

Currently in the process of developing . The frontpage has been created and tested using Chrome's dev console's device toolbar. Ensured that the correct viewport is set and utilized Bootstrap's column system for stacking elements. However, ...

Tips for safeguarding registration forms against spamming

The ASP.NET/Mono MVC2 E-shop includes a registration form that requests mandatory customer name, address, phone password (entered twice) and some optional fields. Should we add spam protection to the form? The current setup verifies that the password and ...

"Exploring the best locations for storing CSS, JS, and image files in Spring

Having trouble figuring out where to place my public files. I attempted the solution mentioned in this thread on Stack Overflow but it didn't work for me. I've tried putting my public folder in various locations within the WEB-INF directory, but ...

What is the best way to attach a function to an href attribute for creating a dynamic link?

I've been struggling to pass a function to the href attribute in my link tag, but no matter what I try, it just doesn't seem to work. It either redirects to a 404 page or is completely unclickable. What could be causing this issue? This link app ...

What is the best way to retrieve the initial cell from a row that the user is currently hovering over?

Is there a way to extract the first cell from a row when a user hovers over it and then clicks a specific key combination? (considering using jQuery) I have a table set up similarly where placing the mouse over a tr and pressing ctrl+c will copy the ID t ...

jQuery Mobile - Struggling to hide a button with traditional jQuery techniques

Looking for help hiding a simple button? <input type="button" id="logoutBtn" value="logout" data-theme="d" data-inline="true" aria-disabled="false"> Attempted to hide it with this code but it didn't work: $('#logoutBtn').hid ...

The tale of a div's mysterious empty room

Once upon a time, in the world of HTML and CSS, there existed a lonely div. This lonely div longed for companionship, and one day, an inline img came to visit. They formed a bond and became good friends. But, no matter how hard the img tried, it couldn&apo ...

How can HTML5 canvas keep its aspect ratio and maximize its size within a container using CSS?

My goal is to resize a canvas to a specific aspect ratio that occupies the maximum available space within its parent container. Below is the CSS and HTML code I am using: .grid-container { display: grid; grid-template-rows: min-content 1fr min-conte ...

What are the steps to design a navigation bar inspired by the Angular Material 2 website?

In my quest to master the fusion of Material and Angular for applying the Material Design pattern to my applications, I stumbled upon Material2 for Angular. Embarking on this journey, I dove into exploring its components, starting with the navigation secti ...

Transfer information to a different html page and store it

Seeking guidance on transferring the entire content of input.html to approve.html for validation and database storage. Any tips or advice would be greatly appreciated as I am new to this and have been struggling with this issue for a week. <form action= ...

Fonts fail to load on Internet Explorer 10

Link to the website in question: In my fonts.css file, I am loading multiple fonts as shown below: @font-face { font-family: GothamBook; src: url('../fonts/Gotham-Book.otf'); src: url( ../fonts/Gotham-Book.otf ); /* IE */ } @font-face ...

Is it possible to modify a Facebook post in editing mode using JavaScript?

I am trying to edit the content of a Facebook post dynamically using JavaScript. https://i.sstatic.net/pyO9h.png My goal is to be able to select the text in edit mode and make changes to it programmatically. The structure of the HTML code for a Facebook ...

What strategies can I use to enhance the efficiency of my code using AngularJS?

After conducting extensive research, I am still unable to find a solution or comprehend the code written by someone else - it simply does not work. As a result, my codebase has grown significantly. I am interested in optimizing my code using AngularJS func ...

Converting a data type into a CSS format

Currently, I am in the process of creating a table and would like to implement a 4-color scheme randomly selected from a pool of 10 schemes. Below is my CSS/PHP code: <?php header('Content-type: text/css'); // 0 grey $color00 = '#95 ...

Strange rendering in CSS JQuery Datatable

I am encountering an issue with a DataTable from datatables.net. Each row can be selected by clicking on a checkbox, which triggers a click event. In response to the event, I add or remove a CSS class to change the appearance of the selected rows. $(".cbD ...

Enhance text by hovering over it

I am currently working on implementing a unique feature using jQuery and CSS. Rather than just inheriting the width, I want to create a magic line that extends to the next index item. Scenario: 1: Hover over Element one ELEMENT ONE ELEMENT TWO ELEM ...