Can we limit the text in a specific cell to one line on a desktop for a responsive table?

I am facing a challenge with making the second cell in my table have a width of 100% while ensuring that the text stays on one line.

While the mobile version is functioning correctly, I am struggling to find a solution for the desktop version as the last cells are not collapsing the text in the second cell.

If you'd like to view the source code, please visit this CodePen link.

I did try the following approach:

table {
  table-layout: fixed;
  width: 100%;
}

However, this ended up making all cell widths even. My goal is to have the second cell occupy full width while allowing the rest of the cells to adjust based on their content.

Thank you!

Answer №1

Utilize the th element to specify the width of the columns:

*,
*:before,
*:after {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
table {
  border-collapse: collapse;
  margin: 20px auto;
  table-layout: fixed;/* will also fix width to cells if specified */
  width: 100%;
}
table thead tr th {
  text-align: left;
  font-weight: bold;
  text-transform: uppercase;
  background: #f8f8f8;
}
table thead tr th,
table tbody tr td {
  padding: 10px;
}
table tbody tr td {
  border: 1px solid #e5e5e5;
}
table tbody tr,
table thead tr {
  border: 1px solid #e5e5e5;
}
/* set th width */
table thead tr th:first-child {
  border-right: 1px solid #e5e5e5;
  width: 2em;/* should be enough*/
}

/* LAST TWO CELLS */

table thead tr th:nth-child(2) ~th {
  width: 4em;/* should be enough*/
}
/* end th width */

table tbody tr td:first-child {
  text-align:right;
  padding:0 0.25em 0 0
}

/* SECOND CELL */

table tbody tr td:nth-child(2) {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
table tbody tr td:nth-child(2) ~td {
  background: red;
  text-align:center;
}
/* RESPONSIVE */

@media screen and (max-width: 600px) {
  table {
    width: 100%;
  }
  table thead {
    display: none;
  }
  table tbody tr {
    display: block;
  }
  table tbody tr td {
    display: block;
    text-align: center;
    border: 0;
    border-bottom: 1px solid #e4e5e7;
  }
  table tbody tr td:first-child {
    background: #f8f8f8;
  }
  table tbody tr td:last-child {
    border-bottom: 0px;
  }
  table tbody tr td:before {
    content: attr(data-th);
    font-weight: bold;
  }
}
<table>
  <thead>
    <tr>
      <th>ID</th>
      <th>Post Title</th>
      <th></th>
      <th></th>
    </tr>
    <tbody>
      <tr>
        <td data-th="POST ID: ">1</td>
        <td data-th="TITLE: ">Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</td>
        <td><a href="#">Edit</a>
        </td>
        <td><a href="#">Delete</a>
        </td>
      </tr>
      <tr>
        <td data-th="POST ID: ">01</td>
        <td data-th="TITLE: ">Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</td>
        <td><a href="#">Edit</a>
        </td>
        <td><a href="#">Delete</a>
        </td>
      </tr>
      <tr>
        <td data-th="POST ID: ">150</td>
        <td data-th="TITLE: ">Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</td>
        <td><a href="#">Edit</a>
        </td>
        <td><a href="#">Delete</a>
        </td>
      </tr>
</table>

Answer №2

When faced with challenges like this, I prefer to create pseudo-tables using div elements instead of traditional tables. This approach offers greater flexibility in manipulating block positioning compared to utilising table cells.

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

Whenever the download bar emerges at the bottom, the slideshow content on the page suddenly shifts upwards

Each time the download bar shows up at the bottom, the slideshow content on the Homepage suddenly moves up. It returns to its original position after I close the download bar.https://photos.app.goo.gl/F482eMfkXyfEZkdA9. My assumption is that this issue is ...

CSS transitions/transforms are failing to impact the necessary elements

I'm currently grappling with advanced transitions and transforms, and encountering some challenges with a section I'm developing. I've managed to get everything working as needed, except for the fact that there is an h5 element positioned ov ...

What is the best way to align text at the center of a circular animation?

I stumbled upon this fascinating animation and I'm looking to incorporate it into my project. However, I'm facing a challenge in centering text inside the circle without resorting to using position absolute. My goal is to have the text with the ...

Manipulating the DOM in AngularJS Directives without relying on jQuery's help

Let's dive right in. Imagine this as my specific instruction: appDirectives.directive('myDirective', function () { return{ restrict: 'A', templateUrl: 'directives/template.html', link: functio ...

A fixed header for tables that shifts along with horizontal scrolling

Seeking assistance with a persistent issue - I have a table with a fixed header on vertical scroll (see CSS and Javascript below), but the header fails to move horizontally when I scroll. window.onscroll = functi ...

Implementing the rotation of an element using 'Transform: rotate' upon loading a webpage with the help of Jquery, Javascript, and

A div element designed to showcase a speedometer; <div class="outer"> <div class="needle" ></div> </div> The speedometer animates smoothly on hover; .outer:hover .needle { transform: rotate(313deg); transform-origin: ce ...

Instructions on creating a "ripple" effect on a webpage using CSS

I am attempting to create a wavy border effect where two sections meet on a page (refer to the image below). What is the most effective way to achieve this? The waves should be uniform in size. EDIT: To whoever flagged this as 'already answered' ...

Trick for adjusting padding on top and bottom for responsive blocks of varying widths

Hello there, I've been working on a project where I need to create a responsive grid of cards, like news articles, that maintain their proportions even when the browser's width changes. To achieve this, I decided to use an aspect-ratio hack by s ...

Create animated changes in height for a mdDialog as elements are shown or hidden

Utilizing Angular Material, I have implemented tabs within an md-dialog. The dialog smoothly adjusts its height based on the content of the active tab during navigation. However, when utilizing an ng-if directive to toggle visibility of content, there is n ...

Material-UI Grid in Full Width Mode is malfunctioning

I've been delving into the Material-UI@next grid layout system to grasp its intricacies. What I'm aiming for is to have two papers that stretch across the entire width of the screen and collapse into one paper when the screen size shrinks. The d ...

What could be causing the lack of animation in W3schools icons?

I've followed the steps provided and even attempted to copy and paste them. However, I'm experiencing an issue where the animation doesn't fully execute. Instead of the bars turning into an X shape, they reset halfway through the animation. ...

Ways to center vertically aligned buttons within cards in a React application with Material-UI

I've encountered an issue with my ReactJS project using material-ui. I created 3 cards, each with a paragraph of varying lengths. This caused the buttons to be misaligned vertically in each card, as the position differs due to paragraph size differenc ...

Concealing a Div on Click Outside of Child Div in ReactJS

I have a parent div with a child div in the center. I am trying to figure out how to close the parent div only when clicking outside of the child div. My current code is structured like this, using triggerParentUpdate to control whether the div should be d ...

Unusual HTML Structure (content misplaced or out of order?)

Recently, I started learning CSS/HTML in school and we just delved into Javascript. Currently, we are working on a website project. However, while trying to integrate the content with the navbar, I encountered a strange issue. When resizing to 620px or le ...

The parent container is not properly wrapping its content in Internet Explorer only

Check out this code snippet I have here: https://jsfiddle.net/kimwild/mtxgtwr5/2/ main#wrapper { z-index: 1; width: 100%; position: relative; overflow: hidden; background-color: red !important; border: 10px dotted #4D37DB; } figure#about-im ...

"Exploring the world of CSS3: Arrow shapes and animated effects

Looking at the image provided, my task is to create and animate it. I was considering using CSS3 border-radius instead of an actual image. Below are the HTML and CSS code that I have so far. The animation I envision involves the arrow gradually appearing ...

Struggling with the transition of a CSS navigation menu from hover to focus

I'm attempting to transition a "mega menu" from 'hover' to 'focus'. My goal is to have the dropdown menus appear "on click" and remain visible until another top-level selection is clicked. Ideally, I am looking for a CSS-only solut ...

How can custom selectors be created in LESS?

Let me provide an example of my desired approach. :all() { &, &:link, &:visited, &:active, &:focus } The concept above involves a 'custom selector' that encompasses all pseudo-classes of an anchor tag, e ...

jQuery selector unable to locate the specified class in the table row

After receiving an array of strings as a response from an SQL query, I am using JavaScript to dynamically add this data as rows to an HTML table: loadUsers: function() { . . . function displayUsersOnTable(response) { for(var i = ...

Arranging divs with CSS positioning

I struggle with positioning divs, especially in this particular situation. I am attempting to position boxes in the following layout: _ ___ _ |_|| ||_| _ | | |_||___| Is there a way to avoid manually defining pixel positions for each box and ins ...