Ensure that all panels are set to the same width as the widest panel

Is there a way to make all the panels in my jQuery tabs have the same width as the widest panel? I know that heightStyle:"auto" works for adjusting the height, but is there a similar option like widthStyle:"auto"?

I want to show you the current look compared to how I would like it to appear:

<div id="tabs" class="tab">
  <ul>
    <li><a href="#f1"><span>One</span></a></li>
    <li><a href="#f2"><span>Two</span></a></li>
    <li><a href="#f3"><span>Three</span></a></li>
  </ul>
  <div id="f1">aaaaaaaaaaaaaaaaaaaaaaaaaa</div>
  <div id="f2">bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb</div>
  <div id="f3">c<br>c<br>c<br>c<br>c<br></div>
</div>
<div style="clear:both">
</div>
<div id="tabs2" class="tab" >
  <ul>
    <li><a href="#f1"><span>One</span></a></li>
    <li><a href="#f2"><span>Two</span></a></li>
    <li><a href="#f3"><span>Three</span></a></li>
  </ul>
  <div id="f1">aaaaaaaaaaaaaaaaaaaaaaaaaa</div>
  <div id="f2">bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb</div>
  <div id="f3">c<br>c<br>c<br>c<br>c<br></div>
</div>
<script>
$("#tabs").tabs({
            heightStyle: "auto",
        }).css("float", "left");

$("#tabs2").tabs({
            heightStyle: "auto",
        }).css("float", "left").width(485);
</script>

http://jsfiddle.net/knj73r9o/2/

Instead of hardcoding the width, I am wondering if there is a way to automatically adjust the width based on the content. Is this possible?

Answer №1

In order to tackle this issue, the best approach would be to go through each panel and ascertain the largest width value. Remember that the width is collapsed, so you must activate each panel as well. Below is a snippet that can guide you:

For instance: https://jsfiddle.net/Twisty/knj73r9o/24/

JavaScript

$(function() {
  function findMaxWidth($tab) {
    var mw = 0;
    var oInd = $tab.tabs("option", "active");
    $tab.find(".ui-tabs-panel").each(function(ind, el) {
      $tab.tabs("option", "activate", ind);
      console.log("Activate Tab " + ind);
      if ($(el).width() > mw) {
        mw = $(this).width();
        console.log("Tab " + ind + " Width: " + mw + "px");
      }
    });
    $tab.tabs("option", "active", oInd);
    return mw;
  }

  $("#tabs").tabs({
    heightStyle: "auto",
  }).css("float", "left");

  var widthStyle = findMaxWidth($("#tabs"));
  $("#tabs div").each(function() {
    $(this).css("width", widthStyle + "px");
  });

  $("#tabs2").tabs({
    heightStyle: "auto",
  }).css("float", "left").width(485);
});

Once you determine the width, you can utilize .css() to adjust the width accordingly for each item.

If you find yourself needing to do this frequently or across numerous items, it might be beneficial to leverage $.widget() to establish your custom widthStyle option or function.

Trust this explanation proves helpful.

Answer №2

To achieve full width tabs, CSS can be utilized for setting the tab width to 100% of its parent container. This will make the tabs expand horizontally to fill the entire space available.

.tab{
 width: 100%;
}

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

PHP Newsletter Creator

Recently, I created a unique php newsletter script in CakePHP that allows users to utilize an in-place editor to generate HTML content for newsletters. While the functionality works well, I have encountered some challenges with a new newsletter design that ...

Is it possible to alter the color of the text "midway" within a character on a website?

In some desktop applications, I've noticed a cool feature where the color of text changes as the background evolves. This allows for multiple colors on a single character, which is especially useful when displaying progress bars with percentages insid ...

Positioning the label for a Material-UI text field with an icon adornment when the shrink property is set

https://i.stack.imgur.com/E85yj.png Utilizing Material-UI's TextField, I have an Icon embedded within. The issue arises when the label "Your good name here" overlaps the icon instead of being placed beside it. This problem emerged after I included ...

What is the best way to incorporate text or characters into your website using CSS only, without using

Will it be able to function in IE? I am aware that IE does not support the content property for anything outside of what? ...

Issue with aligning CSS in Internet Explorer 7

Everything looks great on Firefox and IE8, but on IE 7 the middle "search input text field" is not aligned properly with the other two fields. It seems to be dropping down slightly. Here is the code - thank you for your help: <!DOCTYPE html PUBLIC "-/ ...

Enhance the "text" attribute of IXMLDOMElement to enable functionality in Chrome

The web application I am currently working on was developed approximately 10 years ago and is only compatible with Internet Explorer. My goal is to make it functional in Chrome as well. I am facing a challenge regarding the "text" property of IXMLDOMEleme ...

Fixing the mobile display issue with react-responsive-carousel

I am relatively new to ReactJS and I am looking to develop a responsive Carousel. Here is the code snippet that I have currently: To achieve a responsive Carousel for both desktop and mobile devices, I utilized the react-responsive-carousel library. The ...

Action of Submit Button Based on Field Matching Another Form

I currently have a single page that contains two forms, with only one form being displayed at the moment. The concept is that if the user selects a specific state from the drop-down menu and clicks on the submit button, they will be redirected to either a ...

Image carousel with interactive buttons

I've taken over management of my company's website, which was initially created by someone else at a cost of $24,000. We recently made some edits to the slideshow feature on the site, adding buttons that allow users to navigate to corresponding p ...

Utilizing SASS for customizable color schemes

I am in the process of creating a website using Rails 3 which will allow users to customize their profiles with different layouts and color schemes. I have already implemented SASS, and I believe it would be incredibly useful if I could achieve something l ...

Start up a server-side JavaScript instance utilizing Express

My journey into web programming has led me to learning JavaScript, Node.js, and Express.js. My ultimate goal is to execute a server-side JavaScript function (specifically a function that searches for something in a MySQL database) when a button is pressed ...

Tips for displaying live data in the rows of Element UI's Table

Recently, I've been working with a table that looks like this. <v-table :data="data"> <v-table-column prop="data.attribute" label="Attribute"> </v-table-column> </v-table> Instead of displaying data.attribute in the co ...

Twitter Bootstrap 3 - Change column order for extra small screens

My Twitter Bootstrap layout consists of two columns structured like this: <div class="col-md-4 col-sm-6 col-xs-12"> <div class="post"> Content here </div> </div> <div class="col-md-8 col-sm-6 col-xs-12"> <di ...

Django - tallying timer in action

Looking to add a timer that begins when accessing this template: {% block content %} <h3>C-Test</h3> <p>In the text below, some word endings are missing. The gap is about half the length of the word, so you need to fill in the rest ...

Overlap caused by padding between two elements

I'm encountering an issue where adding padding to a block element is causing it to overlap with the padding of other elements. Below is the code snippet showcasing this problem. <section class="hero"> <h1>Studying REDEFIN ...

Unleash the power of WordPress Revolution Slider captions with exclusive CSS styling

Struggling with adding custom captions? I'm using a Multisite WordPress setup and trying to avoid editing the captions.css file in the plugins folder. Here's what I attempted: .rev_slider .tp-caption .my_head{ position: absolute; ...

What is the best way to center and personalize slider arrow placements vertically?

I applied CSS to customize the appearance of the previous and next arrows for .customer-logos, but I'm still seeing the default button style along with my changes. How can I resolve this issue and position the buttons on either side of the slider inst ...

Is it possible to nest clicks for ajax calls within the results of a previously appended call?

As I dive into learning about AJAX to enhance page loading speed by avoiding reliance on PHP for displaying results, I've encountered a challenge. I have three tiers of data distributed across three database tables: The first tier of data is fetche ...

The submit button is not reading the JavaScript file for validation and instead goes directly to my PHP file

**Hello, I'm new to Stack Overflow. My submit button is not reading the JavaScript file I created; instead, it goes straight to the PHP file without validating the input fields first in the JavaScript file. I've been stuck here for hours and can& ...

Adjust the active carousel item to 0 within onsen-ui (displaying a list of carousel items in a sliding menu)

In my sliding menu, each menu item contains a carousel with two items. I am trying to make the first carousel item show after closing and reopening the menu, or by clicking a button outside of the list on the menu page. This is my current setup: <ons- ...