Arranging Tabs in an Uneven Stack: jQuery and CSS

Looking to stack 5 tabs on mobile with the odd tab at the top instead of the bottom? Currently set to 50% width and floated left, but they fill up the top first.

<div class="tab-row">
  <button class="tab active">A</button>
  <button class="tab">B</button>
  <button class="tab">C</button>
  <button class="tab">D</button>
  <button class="tab">E</button>
</div>

Code for handling tab selection:

$(".tab").click(function() {


  // All versions
  $(".tab").removeClass("active");
  $(".tab").show();
  $(".clone").remove();

  // Mobile version clones rather than moves $(this) to preserve order

  if(window.matchMedia("(max-width: 1100px)").matches) {
     $(this).clone().addClass("clone active").insertAfter(".tab-row button:last-of-type");
     $(this).hide();
  }
  // Full width
  else { $(this).addClass("active"); }

  // Code to load tab content not shown

  });

(Separate code puts Tab A at the bottom when loading on mobile.)

Previously tried setting a clear: but the clone js messes it up. Is there another feasible route?

Struggling with this, any help is appreciated!

Illustration available at https://i.sstatic.net/ttNwW.png.

Answer №1

To achieve the desired layout, utilize display:flex in conjunction with flex-wrap: wrap-reverse;

$(".tab").click(function() {


  // All versions
  $(".tab").removeClass("active");
  $(".tab").show();
  $(".clone").remove();

  // mobile version does clone thing rather than moving $(this) in order to preserve original order of inactive tabs

  if(window.matchMedia("(max-width: 1100px)").matches) {
     $(this).clone().addClass("clone active").insertAfter(".tab-row button:last-of-type");
     $(this).hide();
  }
  // full width
  else { $(this).addClass("active"); }

  // code to load the tab content elided

  });
.tab-row{
    max-width:100px;
  background-color: DodgerBlue;
  display: flex;
  flex-wrap: wrap-reverse;
}

.tab{
  width:35px;
  margin:5px;
  align-self:flex-end;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="tab-row">
  <button class="tab active">A</button>
  <button class="tab">B</button>
  <button class="tab">C</button>
  <button class="tab">D</button>
  <button class="tab">E</button>
</div>

For a demonstration and testing, visit this link: https://jsfiddle.net/kt39Lp71/1/

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

Managing various names of child objects in a recursive manner

I'm currently facing a challenge while working on a nested JSON feed using D3.js Although my code is functioning properly when the child object is named children, I am interested in displaying nodes for other objects as well, not just the ones labele ...

Ways to input a string into a field discreetly without displaying it?

I'm currently developing a Rails app that includes a form for users to input their age. When they enter `20`, I want to display it on the next page as `20 years old`. <%= form_tag age_path, method: "POST", id: 'age', remote: true do %> ...

Navigating to a precise location on initial page load using Angular 11

Within the structure of my app, I have a parent component that displays a large image from a child component. When the parent component loads, I want the browser window to automatically scroll to a specific position in order to center the image. Currently ...

Ensure that the contents of the upper and lower divs are appropriately aligned, while maintaining the centered positioning of the

As a complete beginner in the world of HTML and CSS, I am facing a challenge with my code. Here's the HTML snippet: <section class="instructions"> <div class="row"> <div class="instructions-col" ...

Highchart tip: How to create a scrollable chart with only one series and update the x-axis variable through drilldown

Before I pose my question, here is a link to my jsfiddle demo: http://jsfiddle.net/woon123/9155d4z6/1/ $(document).ready(function () { $('#deal_venue_chart').highcharts({ chart: { type: 'column' ...

Tips for guiding users to a different page based on whether a linkid is associated with a specific Cat

I created this script, but I am facing an issue with the redirect. Can someone please assist me as soon as possible? <?php include('config.php'); $number = intval($_POST['catid']); $query = mysql_query("SELECT * FROM sound ...

The browser fails to implement styling prior to a demanding workload

Is it possible to refresh a section of my webpage that contains numerous DOM elements? My ideal approach would be to hide the section using visibility: hidden;, then redraw it, and finally set visibility back to visible;. However, I've encountered an ...

jQuery method for implementing alternating row colors that are not sequential

Starting from scratch here. I've fetched a table from a remote server where odd rows are white and even rows are grey. Some rows have been hidden: $("td").filter(function(){ return $(this).text()=='R1';}).text('Row1'); //white $(" ...

What steps do I need to take to retrieve data in a JSON array based on its

Hello, I could really use your assistance with a problem I'm having. Here is the scenario: I have a JSON array that looks like this: "category" : [ { id: 1, product: [{id : product_1, type : ball}] }, { id : 2, product :[{id : prod ...

The gap separating the three columns in the DIVs structure

Here is an example that I need The images are being loaded from a MySQL while loop, and I want the spacing between them to be such that the left column and right column touch each side with the middle image centered. Just like in the picture :D This is m ...

Using Selenium Webdriver to automate a webpage featuring jQuery drag and drop functionality

It appears that the css is not updating correctly when using Selenium to drag and drop elements. $driver.action.click_and_hold(item).move_by(0 , distanceToDrop).release.perform The dragging and dropping functionality seems to be working, but it beh ...

Building a custom form layout using Bootstrap v4 with three input fields and a button all lined up on one

I am struggling to create a form with 3 input fields and a button on the same row using Bootstrap v4 for the design. Can anyone help me figure this out? Take a look at my current design: Click here My Code <div class="col-sm-7 mx-auto bg-faded"&g ...

Inquiries regarding jQuery's functionality and efficiency

I was wondering about the best way to improve performance? Would it be more efficient to have two images written in HTML, one visible and one hidden, and then use jQuery to toggle their display (hiding the visible one and showing the hidden one)? <img ...

Is it possible to insert links for SVG images after a fadeIn effect in jQuery?

Welcome and greetings to my first question. I am eager to answer more questions as I gain experience, but for now, I am indebted to someone. Upon the initial page load, users will be presented with a variety of logos. Depending on which logo they choose, ...

jQuery fadeIn effect happening at rapid speed

I am currently working on integrating ajax with WordPress. After the ajax call is completed, I am successfully fading out a specific div slowly. However, when trying to fade in the new data using jQuery's fadeIn() method, I noticed that regardless of ...

Using AngularJS to execute jQuery code after the page has finished loading

I have a carousel of images on my website: Here is the code I am using: <div id="carousel"> <ul class="bjqs"> <li ng-repeat="image in dealer.images"><img src="{{image}}" alt="{{image}}" /></li> </ul& ...

When you click on a sublevel in the vertical CSS and JavaScript onclick menu, it disappears automatically

I'm a beginner when it comes to Javascript, and I'm still getting the hang of CSS/HTML. Currently, I am working on creating a vertical menu using CSS and javascript. My goal is to have the sublevels appear on the right side of the menu when someo ...

Using table.column as a function in jQuery datatabbe is not supported

When using jquery datatable, I encountered an error stating "table.column is not a function." <script> $(document).ready(function() { var table = $('#lsotable').dataTable(); $("#lsotable thead th").each( function ( i ) { ...

Issue with Setting Value in JQuery Select Box

I am in the process of designing a webpage that allows users to edit listings they have created on my website. These listings fall under three main categories, each with its own set of subcategories. To streamline this process, I am implementing AJAX to dy ...

Is the drag-and-drop feature not working properly?

I'm new to coding in Javascript and I'm attempting to insert an image inside a border created with CSS. Unfortunately, the script doesn't seem to be working properly. It's possible that there is a small error in the code causing the iss ...