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

Learn the process of covering the entire screen with a video

I'm attempting to achieve this: IMG Below is the code snippet I've been using: <div class="container" id="containervideo"> <div id="video"> <div class="box iframe-box"> <div class="container"> ...

Having trouble with GSAP CDN not functioning properly in your JavaScript or HTML document?

After watching a tutorial on YouTube by Gary Simon, I attempted to animate text using GSAP. Despite following the instructions meticulously and copying the CDN for GSAP and CSSRulePlugin just like him, nothing seems to be happening. Even setting my border ...

Guide on making a persistent sidebar using CSS and JavaScript

I'm in the process of developing a website that features a main content area and a sidebar, similar to what you see on Stack Overflow. The challenge I am facing is figuring out how to make the sidebar remain visible as a user scrolls down the page. T ...

What is the best way to hide the black icons (x) and bars on larger screens and which specific CSS code should I use to achieve this?

I'm attempting to display menu icons only on smaller screens like phones or tablets, and text on larger screens such as laptops or desktops. I've experimented with adjusting the media query and CSS, but haven't had any success. What specific ...

Using CSS and Bootstrap together in the Yii2 framework

Hey there, I'm new to Yii2 and recently put together a layout page. However, I seem to be having some trouble with my CSS - it's not fully loading, only parts of it are displaying correctly. Is there anyone out there who can lend a hand? This is ...

Guide on transferring the td id value to a different page

document.getElementById('scdiv').innerHTML=Quantity <td id="scdiv"> </td> document.getElementById('quantitydiv').innerHTML=mrp <td id="quantitydiv"> </td> I am currently facing an issue where the code is being d ...

Using Jquery to enhance navigation tabs with pointers

I am facing an issue where the class .hover and .over are added to the entire list whenever I mouseover any list item. I understand that this is happening due to my jQuery code, but I would like to find a way for jQuery to only add the class to the specifi ...

Creating a personalized layout for your WordPress posts with custom card designs

As a new developer diving into the world of WordPress, I am aiming to create a sleek and minimalistic grid/card layout for my website. Struggling to achieve this, I seek guidance on how to replicate the design seen in the following link: desired result H ...

CSS opacity is currently not working as intended

Why doesn't the opacity work here? I've tried using hex and rgba colors, but nothing changes. Even adding classes to tr and td didn't help... First attempt: .done{ background-color: rgba(0,175,51,5); color:white; opacity: 50; ...

When you hit a certain point on the website, the scrolling momentarily pauses

Upon refreshing the page and scrolling down, I notice that the website experiences a brief lag for a few milliseconds before continuing as normal. Oddly enough, this issue only occurs after refreshing the page. Any suggestions on how to resolve this? Th ...

Having issues with customizing the design of MaterialUI Accordion header

Link to my code sandbox project I am encountering issues with the styling in my Accordion Heading. My Accordion Contents are set up like this: <Accordion heading1= { <DishItem name="Döner Teller Spezial" price="13,60€"/> ...

CSS: Centralizing a Div Element with Fluid Width

I've created a popup that appears in the center of the screen using: transform: translate(-50%, -50%); and position: absolute. However, I'm facing an issue where the width of the popup remains fixed instead of adjusting dynamically based on the c ...

Toggling the visibility of a div using JavaScript

When I click the button, I want to show and then hide a div. However, it doesn't work on the first click - only on the second click. How can I make it work on the first click? HTML <p>Click the button</p> <button onclick="myFu ...

Tips for creating a responsive CSS "drawing"

I've created an "aquarium" using HTML and CSS, Now, I want to make the entire page responsive so that the elements shrink and grow based on the browser resolution. The first step is determining how the container should be structured. Is it acceptabl ...

Is it possible to stack divs horizontally in a liquid layout while allowing one of the widths to be dynamic?

This is the code snippet and fiddle I am working with: http://jsfiddle.net/hehUt/2/ <div class="one"></div> <div class="two">text here text here text here text here text here text here text here text here text here text here text here te ...

Arrangement containing 4 separate div elements - 2 divs are fixed in size, 1 div adjusts dynamically (without scroll), and the remaining div fills the

I am trying to implement a specific layout using HTML and CSS on a webpage. So far, I have successfully created the black, red, and blue areas, but I am facing challenges with the scrollable green content. It currently has a static height, but I need it to ...

designing a responsive form in the mobile environment

I have implemented a search form on my website with a button inside the text box. It works fine on desktop, but when I switch to mobile view, the positioning is off. I am currently using absolute positioning for the button to give it a fixed position. Is t ...

When the child element's "aria-expanded" attribute is set to true, a CSS class should be dynamically

Is there a way to apply a grey background to the first div when the dropdown is open? I've managed to add CSS based on the child elements' aria-expanding attribute, but I'm unsure how to do it for a parent element. Since I am using Vue, I pr ...

Using Percentage-Based Margin in React-Native

I've encountered a problem in my React Native project where using a percentage value for the margin style attribute seems to shrink the height of one of my View components. However, if I switch to using an absolute value, the issue disappears and ever ...

Is there a way to extract a list of words from a designated element using selenium?

I have been trying to scrape the content of this webpage 10 Fast Fingers in order to extract the words that need to be typed into a list. My intention is to then input these words into a text box for typing practice. The code seems to be running fine, but ...