Adjust the order of list items based on the resolution change

Is there a way to rearrange the order of the list items in an ul when the screen resolution changes, specifically having the last li appear in the first place?

Edit: I am attempting to achieve this by using flexbox.

 .postpreview ul {
list-style: none;
display: inline-flex;

}

.postpreview ul li {
flex: 0 100px 30px;
}

@media(max-width: 1000px) {
.postpreview ul {
flex-direction: row-reverse;
justify-content: flex-end;
 }
}

Currently, on smaller screens, the layout is not as desired. How can I make the last item appear at the top instead of staying on the right? Check it out live here:

Answer №1

Effective CSS-only technique utilizing flexible boxes:

ul {
  display: flex;
}
li {
  flex: 0 0 30px;
}
@media(max-width: 1000px) {
  ul {
    flex-direction: row-reverse;
    justify-content: flex-end;
  }
}

Check out the Live example, with a screen width threshold set at 1000px.

Update: To switch from horizontal to vertical layout, simply use flex-direction: column in your media query. Providing specific examples and intentions would greatly assist in offering tailored solutions. The question could benefit from further clarification.

Answer №2

Here is some code for styling a navigation bar using CSS:

nav ul li { display: inline; margin: 0 100px; }

And here is the corresponding HTML code:

<nav>
<ul>
    <li><a href="#">Text</a></li>
    <li><a href="#">Text</a></li>
    <li><a href="#">Text</a></li>
    <li><a href="#">Text</a></li>
    <li><a href="#">Text</a></li>
</ul>
</nav>

There is also some JavaScript code included to adjust the margins dynamically:

function Func(){
var list = $('nav ul li'),
    firstLi = list.first().offset().top,
    lastLi = list.last().offset().top,
    mLeft = parseInt(list.css('marginLeft')),
    mRight = parseInt(list.css('marginRight'))
if(firstLi != lastLi){
    --mLeft
    --mRight
    list.css({
        'marginLeft': mLeft,
        'marginRight': mRight
     })  
     Func();
  }
}

 Func();

Another solution:

You can also consider using media queries to make your navigation bar responsive for different screen resolutions.

Answer №3

const myTag = $('ul');
const childTags = myTag.children('li');
myTag.append(childTags.get().reverse());

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

Reduce the combined character value of the title and excerpt by trimming the excess characters

I am seeking a solution to trim the total character count of a post title and excerpt combined. Currently, I can individually adjust the excerpt through theme settings and the post title using JavaScript. However, due to varying title lengths, the height ...

I'm encountering an issue where Bulma is taking precedence over the CSS of my Vue3

In my Vue CLI 3 project, I'm utilizing Bulma and have included the import in the 'index.js' file like so: import { createApp } from 'vue' import App from './App.vue' import router from './router' require('@ ...

Verify if the date string includes the day

Here's the scenario: a user clicks on one of two links, either 2012/10, or 2012/10/15. I am extracting the day value from the link to pass it onto an AJAX request that will change days on an archive page. Is using a regular expression like /\d{ ...

Display a pop-up window upon clicking anywhere on the webpage using jQuery and HTML

Is it possible for me to create a pop-up window that opens a specific website when a user clicks anywhere on the page, similar to pop-up companies? Can this be achieved using HTML, JavaScript, and jQuery? Any help would be greatly appreciated. ...

What are the various methods for incorporating icons and logos into website design?

Can anyone provide insights on the quality of logos and icons used in professional websites? I often create logos and icons using Illustrator, but when comparing them to icons on websites like those shown in the provided image and links, mine appear blurry ...

Is there a way to make the footer adapt to the varying heights of the grid sections as they grow in size?

Currently, I am facing an issue with the positioning of the page footer. Despite my efforts, it does not remain at the bottom as intended. Please refer to the image for a visual representation of the problem. How can this be rectified? It is worth noting ...

Is there a way to eliminate unknown white gaps in the content?

I have encountered a very perplexing issue. When accessing this site on Safari, an additional 4-500px of scrollable whitespace is added. This is clearly not the intended behavior, but I am struggling to understand what is causing this space :S Upon inspe ...

The radio button is displaying the text 'on' instead of its designated value

function perform_global(tablecounter) { for (index = 1; index <= 2; ++index) { var dnsname = "dns_name"+index; oRadio = document.getElementsByName(dnsname); alert (" radio ID " + dnsname + " " + index + "length " + oRadio.leng ...

The art of aligning text in CKEditor5

I am attempting to configure my page using the CK5 editor. So far, I have done the following: ClassicEditor.create(document.querySelector('#editor'), { language: 'pt-br', toolbar: ['heading', '|', 'bold&apo ...

At what point do browsers decide to round pixel fractions to whole pixels, and when do they choose not to do

I recall reading here on SO that using pixel fractions in CSS is generally advised against because browsers tend to round them to the nearest whole pixel. As shown in the example below, 0.3 pixels are indeed rounded to a full pixel: span { border: 0. ...

Tips for loading a webpage with optimal speed and efficiency

I am inspired by the sleek design of Apple's website and want to replicate some of its features in my project (best viewed on FF, Chrome, Safari): Initially, the page appears empty except for the header - but the final height of the page is already ...

Implementation of cascading selects using JQuery

As a newcomer to JQuery, AJAX, and PHP, I am attempting to create a form with linked dropdowns that will populate initial data for a new MySQL record. Despite using the JQuery chained select remote plugin, I am struggling to retrieve the data for the secon ...

Is it possible to monitor a section of a webpage's source code for any updates and notify about them?

This question may seem a bit confusing, but I am in need of some guidance. I do not require any code to be written for me, but rather assistance in finding the right direction to achieve my goal. The task at hand is to monitor a specific area of a web page ...

Utilize BotDetect in Angular js for advanced functionality

We are licensed to use BotDetect and now, we want to integrate it with Angular js. After searching on Google, I couldn't find a proper solution but stumbled upon angularjs-captcha, which seemed promising at first but didn't quite help me achieve ...

Save JSON data from Javascript to a file on the server without using a server-side application

I am currently working with a .js client and need to save an object to a file on the server. This is new to me as I am not familiar with file i/o using JavaScript. My plan is to utilize jquery and json for this task, and I am using java serverside. Althoug ...

Unable to showcase Mysql search outcomes in a distinct div section

I have been working on a project to showcase MySQL results by utilizing three different sections/divisions. In Division 1, there are radio buttons that allow for selecting and viewing the results based on different criteria. Division 2 contains text indica ...

I'm looking to create a chart similar to this using Bootstrap 4 - any advice on how to

Is there a way to implement animations in this chart? https://i.sstatic.net/YpabJ.png ...

The Chrome browser is not displaying the Ajax loader

I've been using a please-wait .gif image for my ajax loader, and it was working perfectly in Chrome until recently. There hasn't been any changes to the image itself, and it still displays correctly in IE, Mozilla, and other browsers. I'm p ...

Incorporating Angular module into the mean.io bundle

Need help with adding the angular-ui-grid module to a mean.io package: $ cd packages/custom/mypackage $ npm install angular-ui-grid --save To import the JS, add this line to packages/custom/mypackage/public/index.js: import 'angular-ui-grid'; ...

At a specific media query breakpoint, links are disabled while the rest continue to function without any

Today I encountered a puzzling issue on my website with a responsive layout that utilizes @media queries and breakpoints. At one specific breakpoint (or resolution, as I am still learning the terminology), the links in the navigation bar are inexplicably d ...