Creating a tab resizing effect similar to Chrome using only CSS

I am attempting to design tabs that can dynamically resize similar to how Chrome tabs behave. However, I am uncertain if achieving this functionality is possible without relying on JavaScript.

Browser compatibility is not a concern for me; my main goal is to determine if it can be accomplished using only HTML and CSS/CSS3.

Here are the requirements:

  • The tabs should never expand wider than the text they contain
  • When the rightmost tab reaches the edge of the window during resizing, the tabs should begin to shrink (ideally starting with the widest tab)
  • The active tab must maintain its size and not shrink
  • As the tabs decrease in width, the text should be truncated with an ellipsis
  • The tabs should stop shrinking once they reach a minimum width that I specify (preventing them from disappearing entirely)

Is it feasible to achieve this using properties like display: box; and box-flex: 1;? I have been unable to implement it successfully thus far. While I have managed to accomplish this with JavaScript, the performance isn't as optimal as desired for our large web application.

So, any CSS experts out there willing to take on the challenge? If there's a way to achieve this with minimal reliance on JavaScript, I am also open to exploring those options.

Thank you everyone!

Answer №1

If you want to closely mimic Chrome's behavior, consider using flexbox...

body {
  font: 14px/140% 'Arial', sans-serif;
}

ul {
  background-color: #f0f0f0;
  display: -webkit-flex;
  padding: 15px 10px 0 10px;
}

ul li {
  -webkit-flex: 1;
  background: #ccc;
  padding: 12px 18px 8px 18px;
  white-space: nowrap;
  overflow: hidden;
  max-width: 170px;
  min-width: 60px;
  border: solid #999 1px;
  border-bottom: none;
  border-radius: 6px 6px 0 0;
  text-overflow: ellipsis;  
}

http://jsfiddle.net/b23lp/

​​However, achieving your specific requirements with CSS alone may not be feasible. Additionally, maintaining the 'active' tab in an expanded state may lead to a non-standard user experience as the tab positions will change upon each click.

Answer №2

By adding an extra <span> element and utilizing the CSS property display: table-cell, I enhanced Duopixels' answer. This approach ensures better cross-browser support and fallback options. Check out the demo at http://jsfiddle.net/w34nm/ (Compatible with IE8+, Firefox 10, Chrome 17)

Additionally, it is advisable to employ JavaScript for dynamically setting the appropriate width values for the list items.

Answer №3

Initially, it's important to understand that tabs in Chrome do not adjust size individually but rather uniformly based on available space. (As per MDN, achieving your desired layout can be done by:

To ensure XUL elements within a box are of equal size, set the equalsize attribute of the container to always. This attribute does not have a CSS equivalent.

It would be beneficial to provide a visual representation of your requirements as some aspects were unclear to me.

In any case, I've put together a demonstration here. Please let me know if it meets your needs.

ul {
    display: -webkit-box; 
    width: 500px; 
    background: lightgray; 
    text-align: left;
}
li {
    background: gray; 
    text-align: center; 
    height: 24px; 
    -webkit-box-flex: 1; 
    min-width: 30px; 
    max-width: 100px; 
    overflow: hidden; 
    text-overflow: ellipsis; 
}

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

The appearance of the drop-down menu in IE is not the same as in other

I have implemented css bootstrap 3.2.0 on my website. However, I have noticed some discrepancies when viewing the site in Internet Explorer. The menu options have a thick black border and the dropdown menu button appears different compared to other browse ...

Ways to remove the line under a logo in HTML using CSS and Bootstrap

While working on a website design, I encountered an issue with the logo where it has a line of the background color under it. This is frustrating and affects the overall design. Does anyone have a straightforward solution to remove this line? I need someth ...

A guide on expanding an HTML table dynamically with MVC razor syntax

My goal is to dynamically add new rows to a table by following the advice given in this answer on Stack Overflow: Add table row in jQuery I have successfully implemented it for one of my table requirements as seen below: function onAddItem() { $( ...

Modifying SASS variable values based on the presence of specific text in the page URL

How can I utilize the same SASS file for two different websites with similar functionality but different color schemes? My goal is to dynamically change the color based on the URL of the page. However, I am facing challenges in extracting the page URL from ...

Color-matching cells must be positioned in the same column within the gridbox consecutively

In my current project, I am dealing with a large number of sections. Each section contains one or two rows with columns ranging from 2 to 15, all of equal width. The cells of the same color are placed in the same column, and the layout looks like this: To ...

What are the steps to switch multiple CSS styles for creating a dark mode?

I'm currently using a combination of HTML, CSS, and JavaScript to construct my website. One feature I'd like to implement is a Darkmode switch button that toggles between Dark and Light mode when clicked. However, the issue I'm facing is tha ...

Is it possible to make the mat-menu-item the same size as the mat-menu button in Angular Material v.12?

How can I ensure that the mat-menu-item button is the same size as the mat-menu itself? For example, click here <div class="container d-block d-md-none"> <div class="row"> <div class="col d-flex justify-content-cent ...

How to eliminate a hyperlink from an HTML element with the help of JQuery

Recently, I was assigned to revamp a website for the company I work for. However, upon closer inspection, I realized that the website is quite messy and relies heavily on templates, resulting in certain elements being auto-generated as active links. The i ...

Using percentages for CSS alignment and adjusting element widths

Greetings everyone! I have been struggling to align two divs - one on the right with a width of 30% and the other on the left with 70%. Despite my efforts, I have not been able to find a solution. I am hopeful that someone here can assist me. My setup inv ...

Is there a way to adjust the label size for a material ui TextField component?

My TextField element is defined like this: <TextField id="standard-with-placeholder" label="First Name" className={classes.textField} margin="normal" /> It currently appears as shown in the image below: However, I would like it to look mor ...

Overflow on flex layout causes line breaks

I am facing an issue with two divs in a flex container where I want them to be displayed on separate lines when they overflow .container { background: wheat; padding: 10px; width: 300px; display: flex; justify-content: space-betwee ...

"Implementing a search bar feature using HTML, CSS, and

My attempt to implement a search bar CSS from CodePen is not working properly in React. Even though I used the exact code provided, the search bar seems to be buggy. If you want to take a look at the code sandbox for better reference, you can visit: https ...

When you zoom in or out, HTML5 elements styled with CSS will dynamically adjust

Hey everyone, I have a question about a problem I'm facing with my HTML and CSS page. The issue is that when I zoom in, the "Section" part moves underneath the nav bar, and when I zoom out, the footer moves next to the section part... Below is a sni ...

How can I customize the color of a date range in the jQuery date picker?

Currently, I am using the following method to set a different color for a specific date range - April 5th to April 7th. You can view it here: http://jsfiddle.net/sickworm/dpvz52tf/ var SelectedDates = {}; SelectedDates[new Date('04/05/2015') ...

Ways to conceal a grid item in Material UI framework

My goal is to hide a specific grid item for a product and smoothly slide the others in its place. Currently, I am using the display:none property but it hides the item instantly. I have already filtered the products and now I want to animate the hiding of ...

Equal dimensions for all cards in the TailwindCSS gallery display

I am currently working on an image gallery in React with Tailwind CSS. Here's a glimpse of how it looks: https://i.stack.imgur.com/ABdFo.png Each image component is structured like this: <div className="flex items-center"> < ...

When text is added to div elements, they are mysteriously pushed downward. This strange phenomenon occurs with inline-block elements

Why does the first element get pushed down when its child contains text? What causes inline-block to behave this way? Is there a way to align divs side-by-side while still allowing them to have children? For example, I want the grey box to contain a list ...

Eliminating the final space in CSS flexbox layout

When I set the display of an element to flex, I noticed that the last space in a text string gets removed. <div class="has_flex"> Some text <a href="link">Link</a></div> After setting display to flex: <div class="has_flex"> ...

Difficulty encountered with changing font color on CSS

I'm having trouble fixing the text color, it's currently white and blending in with the background. I've set the color to #1a6eb6 for both the TEXT element and the submenu list items, but it's not working. Can someone assist me with thi ...

The Microsoft Booking feature is not being shown in the iframe

Instead of the booking website, this is what is being displayed. Below is the code snippet: index.js import React from 'react' const Appointment = () => { return ( <iframe src='https://outlook.office365.com/owa/calendar/<a ...