How to design an HTML table with columns of a set width using CSS

I need a table that has a fixed width but dynamic height to accommodate varying content lengths. Take a look at this example:

Despite setting a fixed width, my content is overlapping. What am I doing incorrectly? Here is my current code:

<table width="60%" align='center' cellpadding='2' 
        cellspacing='2' border='2' style='table-layout:fixed'>
    <thead>
        <tr>
            <th>#</th>
        </tr>
    </thead>
    <tbody>
        ...
    </tbody>
</table>

This is the css I am using:

table{
font-size:12px;
}

table td{
    padding:5px; height:auto;
    background:#f6f6f6;
}

table thead tr th{
    background:#d7dbe2; 
}

Any suggestions on how I can make the height adjust dynamically?

Answer №1

table td{
    overflow-wrap: break-word;
}

I found this solution to be very effective!

Answer №2

My belief is that there are no instances of 'breaking points' (like spaces) in the text, making it impossible to break the text into multiple lines. To resolve this issue, one potential solution would be to insert ­ where the text could potentially break.

Answer №3

Your issue could potentially stem from the implementation of table-layout: fixed. To address this, I recommend adding style="width: 150px;" to every <th> element while eliminating the use of table-layout.

Answer №4

When it comes to the width of <th> and <td>, the former takes precedence over the latter. By setting a width for the <th> element, like Kolink suggested, you can easily resolve any overflow issues.

table thead tr th{
    width:120px; 
}

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 mobile website is not adjusting to the viewport size as expected

I'm currently working on a website and experimenting with responsive design. Unfortunately, I'm facing an issue where every mobile browser is not scaling the site properly. Regardless of the browser, the site appears as 1000px or wider. I have at ...

Tips on customizing the appearance of JFoenix components within JavaFX Scene Builder using CSS

I'm struggling to customize the appearance of a JFXButton using CSS stylesheets. While some properties are working fine, such as background color and size, I can't seem to get font color and weight to apply: .eliminarBtn { -fx-background-col ...

Implementing a JQuery script that triggers every time there is a change in the scroll

This script creates a shrinking effect on a box followed by a fade-in/out transition when the scroll position changes. However, the issue is that it triggers every time there is a scroll position change. For example, if the scroll position is at 100px and ...

Learn how to efficiently apply styles to multiple objects within an array using the material-ui library

Currently, I'm utilizing the functional component of React.js. Within this component, I have an array of objects that contain specific styles. const data = [ { id: 1, color: '#000' }, { ...

Guiding the jQuery Document

I currently have a stylesheet in the head section with the following content: *, body { direction:rtl; } Upon inspection, I found that the dir variable of jQuery is set to ltr: $(function(){ alert(this.dir); }); How can I obtain the specific page di ...

Tips for implementing a watermark background image that appears in each section of a single-page website

Currently, I am exploring HTML, CSS, and Bootstrap and facing a particular issue. I am attempting to add a logo to a website as a background. I have already discovered how to do this by following the instructions here. This website is comprised of a sing ...

Row within table extending beyond boundaries of parent div table

Is there a way to make a table row wider than its parent table? I want the button at the bottom to extend beyond the borders of the table. https://i.sstatic.net/VH0HP.png Do I need to abandon the table styling to achieve this effect? Check out this JsF ...

Ways to conceal the player controls with mediaelementjs.com

Is there a way to display the control bar only when the user hovers over the video while using the mediaelementjs.com player? I feel like there might already be a function for this, but I can't seem to find it anywhere. Should I just implement a simpl ...

Creating a JSON file using an object to send requests in Angular

In my Angular 7 project, I am trying to send a multipart request to the server that includes a file (document_example.pdf) and a json file containing a data object (data_object_example.json). The content of data_object_example.json is as follows: { " ...

The computed style of a node in Chrome returns as null

In my javascript code, I have the following line: if(document.defaultView && document.defaultView.getComputedStyle){ strValue = document.defaultView.getComputedStyle(oElm).getPropertyValue(strCssRule); } // ... This code is used to retrieve a ...

Unattended HTML and head tags found unexpectedly

As a new programmer tackling my first RoR site with an integrated bootstrap theme, I've encountered some issues with the HTML code that are causing problems with image sharing on Facebook. The error message from the debugger indicates that there are m ...

Making poll everywhere items align horizontally using HTML: a step-by-step guide

Is there a way to display two different Poll Everywhere polls side by side on a Blackboard course, rather than having them stacked on top of each other? I have the embed codes for both polls ready to go. ...

Receive the outcome once the form is submitted

Can someone provide quick assistance? I am looking to allow users to upload a CSV file for me to parse and analyze. Once the processing is done, I need to display the results back to the users. While uploading the file, I also need to ensure that the file ...

Angular automatically scrolls to the top of the page when clicking on any part of the bottom

I am encountering an issue on a page where I have implemented NgFor. Whenever I click anywhere at the bottom of the page, it automatically jumps to the top. Additionally, when I try to click on a button at the bottom, the button's action does not exec ...

Creating a Distinct Interior Array Separate from the Exterior

I'm currently working on a project that involves creating a 2D array. I want the interior elements of this array to be black while the exterior elements should be white. However, my 2D array doesn't seem to be forming correctly - it looks more li ...

The overflow hidden function does not seem to be fully functional on the iPad

Struggling to prevent body scrolling when a modal pop-up appears? I've tried setting the body overflow to hidden when opening the modal and resetting it when closing, which worked fine on desktop browsers. However, mobile devices like iPod/iPhone pose ...

Issue of scrolling on Firefox 3.6 due to CSS page width set to 100%

Strange issue with my website on Firefox 3.6 - there is an unwanted horizontal scroll bar at the bottom even though nothing exceeds 100% of the body width. This problem does not occur in Chrome, Safari, or Firefox 4... Any suggestions? Check out the li ...

Updating the background image of a React app according to each component

After researching extensively and attempting various solutions, I am still struggling to make my app function correctly. My goal is to display a different background image depending on which component is being rendered on the screen. The app is built using ...

How can we invert codes in PHP?

I've been examining the source code of a PHP website, but I'm finding that all pages have a similar format with unreadable PHP and HTML codes encapsulated within PHP tags. How can I reverse engineer this to understand how it gets translated into ...

Manipulating HTML content with Javascript Array

Is there a way to retain Javascript context from an array? Any suggestions for improving this code? SAMPLE: <html> <script type="text/javascript"> var rand = Math.floor(Math.random() * 6) + 1; var i = Math.floor(Math.random() * 6) + 1; var ...