What is the best way to enable a CSS table column to overflow the table while having a width of 100%?

I am faced with a CSS table dilemma where I have two columns, but one of them is hidden by setting its width to 0. The second column takes up the entire table by being set to a width of 100%. However, when the first column is shown (controlled by a checkbox and CSS), the second column resizes instead of staying the same size and appearing like its content is being pushed to the side. I was able to achieve the desired effect by giving the column an explicit width in pixels, but I am wondering if it is possible to accomplish this using a percentage width solely through CSS.

#container {
  width: 400px;
  border: 2px solid red;
}

.table {
  margin-top: 16px;
  display: table;
  table-layout: fixed;
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
}

.col {
  display: table-cell;
  overflow: hidden;
}

.col1 {
  width: 0;
  transition: width .5s ease;
}

.col2 {
  width: 100%;
  width: px;
  position: relative;
}

.col3 {
  width: 400px;
  position: relative;
}

.marker {
  position: absolute;
  right: 2px;
  top: 0;
}

input[type="checkbox"]:checked ~ div > div > .col1 {
  width: 100px;
}
<label for="show-col">Show extra col</label>
<input type="checkbox" id="show-col">
<div id="container">
  <div class="table">
    <span class="col1 col">Column 1</span>
    <span class="col2 col">
      Column 2 - with width as 100%
      <span class="marker">Edge</span>
    </span>
  </div>
  <div class="table">
    <span class="col1 col">Column 1</span>
    <span class="col3 col">
      Column 2 - with explicit width in pixels
      <span class="marker">Edge</span>
    </span>
  </div>
</div>

Answer №1

If you want to set a minimum width on an element without using percentages, one way is to use jQuery to get the parent element's width and apply it within a resize function.

An update has been made where the first table now utilizes position absolute on .col2 with a 100% width, along with CSS transition on the left property.

You can view the fiddle here: https://fiddle.jshell.net/Jim_Miraidev/pa734h2y/

$(document).ready(function(){
  var resizeTimer;
  var $col3 = $('.col3');
 
  $(window).on('resize load', function(e) {
    clearTimeout(resizeTimer);
    resizeTimer = setTimeout(function() {
      // get parent width and set
      var $pw = $col3.parent().width();
      $col3.css({'width':$pw});    
    }, 100);

  });

});
#container {
  width: 100%;
  border: 2px solid red;
  position: relative;
  overflow:hidden;
}

.table {
  margin-top: 16px;
  display: table;
  table-layout: fixed;
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  box-sizing: border-box;
}
.col {
  display: table-cell;
  overflow: hidden;
}
.col1 {
  width: 0;
  transition: width .5s ease;
}
.col2 {
  position: absolute;
  width: 100%;
  left:0;
  transition: left .5s ease;
}
.col3 {
  position: absolute;
  width: 100%;
}
.marker {
  float: right;
}
input[type="checkbox"]:checked ~ div > div > .col1 {
  width: 100px;
}
input[type="checkbox"]:checked ~ div > div > .col2 {
  left: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label for="show-col">Show extra col</label>
<input type="checkbox" id="show-col">
<p>Using css transition left width set to 100%</p>
<div id="container">
  <div class="table">
    <span class="col1 col">Column 1</span>
    <span class="col2 col">
      Column 2 - using postion absolute and
      animating the left
      <span class="marker">Edge</span>
    </span>
  </div>
</div>
<p>Using the jQuery</p>
<div id="container">
  <div class="table">
    <span class="col1 col">Column 1</span>
    <span class="col3 col">
      Column 2 - with explicit width in pixels
      <span class="marker">Edge</span>
    </span>
  </div>
</div>

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 functionality of changing the category in the second carousel slider on click using JavaScript appears to

Creating a bootstrap carousel slider that dynamically changes based on the selected category. For example, clicking on the hammer category will display only hammer images, while selecting the compass category will show compass images. Everything worked sm ...

How can you vertically center an icon button in Material UI?

Looking for help with aligning elements in this code snippet: <TextField id="outlined-basic" label="22Keyword" defaultValue={"test123"} variant="outlined" /> <IconButton aria-label="delete&q ...

The functionality of the jQuery .click method appears to be malfunctioning within the Bootstrap

It seems like my JQuery.click event is not functioning as expected when paired with the corresponding button. Any ideas on what might be causing this issue? Here's the HTML CODE snippet: <button id="jan7" type="button" class="btn btn-dark btn-sm"& ...

What is the best way to retrieve all form input values by utilizing the blur event within the `addEventListener` method

I'm currently attempting to retrieve form values dynamically using addEventListener, but I'm only able to access the first input field and not the others. Below is my current code snippet: $(document).ready( function() { const user_inpu ...

"Exploring the concept of odd and even numbers within the ng-repeat

Can anyone help me with detecting odd or even numbers in an ng-repeat? I have created a fiddle that displays some dots randomly showing and hiding. Now, I want to change the background color so that odd numbers are green and even numbers are red. function ...

Ensuring the next tab is not accessible until all fields in the current tab are filled

I am working on a form with a series of questions displayed one at a time, like a slide show. I need help with preventing the user from moving to the next set of questions if there is an empty field in the current set. Below is the script I am using to nav ...

Having dual fixed navigation bars on a single webpage

I am looking to create two fixed navigation bars, one at the top and another in the center of the page. The idea is that when scrolling reaches the second nav bar, the first one should hide (or become relative) and the second one should become fixed. Then, ...

Encountering a problem while verifying pattern using regular expressions

I'm facing an issue when manually checking if my inputs match the specified patterns. Below is the function I am using for this check: if (!$element.attr("pattern")) return true; let pattern = $element.attr("pattern"); le ...

I want to create CSS columns that stretch to the full height of the window, positioned between a header and a footer, while also

I am looking to create a basic HTML layout with CSS that includes a header and two columns. The right column should have a fixed width of 100px, while the left column may have varying content lengths. I want both columns to stretch to the footer. Below is ...

How to Align Navigation Bar Items to the Right in Bootstrap 4

I need some help with bootstrap. I've looked at various discussions on this topic and tried numerous solutions, but I still can't get it right. I want the logo to be on the left and the navigation links aligned to the right. Here's what I&ap ...

Retrieve data from a different div, perform a subtraction operation, and dynamically update yet another div using jQuery

Looking to extract a specific div class value, subtract 500 from it, and display the result in another div. Unclear about the steps needed to show the subtraction outcome on the other div. Consider this scenario: <div class="main-value">6000</d ...

Arranging rows and columns in Bootstrap 3 for small screens

My layout is structured as follows: <div class="container"> <div class="row"> <div class="col-md-8> <div class="row"> <div class="col-md-12"> box 1 & ...

Removing HTML formatting from JSON data within AngularJS

//Reviewing JSON Field [{"image":" <a href=\"http:\/\/docroot.com.dd:8083\/sites\/docroot.com.dd\/files\/catalogues\/2016-09\/images\/Pty%20Prs.compressedjpg_Page1.jpg\">Property Press.compress ...

The styling of divIcons in React Leaflet Material UI is not applied as expected

When using divIcon in React Leaflet to render a custom Material UI marker with a background color prop, I noticed that the background style is not being applied correctly when the marker is displayed in Leaflet. Below you can find the code for the project ...

Exploring the possibilities of PHP

I have a code that checks whether a string is a palindrome. <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" charset="utf-8"> <title>Document</title> </head> <body> <form action=pal.p ...

Tips for increasing the dimensions of a modal in Bootstrap

<div class="popup zoom" id="_consultant"> <div class="popup-window " > <div class="popup-content" style="background-color:#F5F5F5" > </div> </div> </div> After extensive research, I have not found ...

Eliminating unnecessary CSS from the codebase of a website

Currently, I am making adjustments to a website template that I downloaded for free online. I have noticed that even if I delete a div from the code, the corresponding CSS styles remain in one or more files. Is there any tool available that can automatic ...

What steps can I take to ensure the reset button in JavaScript functions properly?

Look at this code snippet: let animalSound = document.getElementById("animalSound"); Reset button functionality: let resetButton = document.querySelector("#reset"); When the reset button is clicked, my console displays null: resetButton.addEvent ...

Creating a dynamic input box that appears when another input box is being filled

I have a form set up like this: <FORM method="post"> <TABLE> <TR> <TD>Username</TD> <TD><INPUT type="text" value="" name="username" title="Enter Username"/><TD> </TR> <TR> <TD>A ...

Tips for successfully passing a Prop using the Emotion CSS function

Passing props through styled() in Emotion is something I'm familiar with. For example: const Container = styled("div")<{ position: string }>` display: flex; flex-direction: ${({ position }) => (position === "top" ? "column" : "row ...