Fieldset containing a table with a horizontal scrollbar

One issue I encountered in my application was dealing with a fieldset that contained table data with potentially many columns.

To prevent the browser window from acquiring a horizontal scroll bar due to the wide table, I attempted to wrap the table in a container with properties like width: 100% and overflow: auto.

Unfortunately, this method failed as the fieldset still extended beyond the inner width of the browser, resulting in a scroll bar. I tried applying different styles like width: 100% directly to the fieldset, and even experimented with position: absolute using left:0;right:0; without success.

After numerous attempts, I eventually added min-width and max-width properties, which seemed to work well in Chrome, Internet Explorer, and Opera, but not in Firefox.

It appears that in Firefox, it's challenging to restrict the fieldset's width to the body width. Are there any workarounds for this issue?

Below, you can find a snippet of the CSS code that was used:

* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
body {
  margin: 1em;
}
fieldset {
  width: 100%;
  max-width: 100%;
  min-width: 100%;
  overflow: auto;
}
table {
  display: block;
  width: 290em;
  table-layout: fixed;
  border-collapse: collapse;
}
table input {
  border: 1px solid transparent;
  width: 100%;
}
td,
th {
  border: 1px solid black;
  width: 10em;
}
.container {
  width: 100%;
  overflow: auto;
}

And here is the corresponding HTML code:

<fieldset>
  <legend>
    Table Data
  </legend>
  <div class="container">
    <table>
    <!-- table content goes here -->
    </table>
  </div>
</fieldset>

Answer №1

The inclusion of the 'min-width: inherit' attribute in the fieldset is the key to its functionality.

Answer №2

After trying different techniques, I found a workaround that solved my issue:

By replacing the fieldset and legend elements with section and h3 respectively, I was able to display the table as intended, complete with a scroll bar. To maintain the appearance of fieldsets, I created a custom class called .fieldset, which looked like this:

.fieldset {
  border: 1px solid silver;
  display: block;
  position: relative;
  margin-top: 1em;
  padding: 1em;
}

.fieldset > h3 {
  position: absolute;
  background-color: white;
  font-weight: normal;
  font-size: 0.8em;
  padding: 0;
  margin: 0;
  top: -0.5em;
}

/* additional content styling, not directly related to the workaround */
label {
  display: block;
}
<section class="fieldset">
  <h3>Fieldset legend</h3>

  <!-- some content -->
  <p>
    <label>Foo</label>
    <input />
  </p>
  <p>
    <label>Bar</label>
    <input />
  </p>
  <p>
    <label>Baz</label>
    <input />
  </p>

</section>

Although this workaround resolved the display issue, I realized that the semantics of fieldset and legend are important for organizing elements within a complex web form. These elements are specifically designed for that purpose (refer to MDN).

Moreover, I wanted to avoid altering the HTML structure in case I needed to revert back to using fieldsets. To address this, I implemented a server-side solution to automatically replace fieldsets with my custom fieldsets. Alternatively, one could utilize features of a template engine like "mixins" in jade. However, in my scenario, replacing the elements using a DOM manipulation library was the most efficient approach, especially considering the need for WYSIWYG editor compatibility.

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

What is the correct way to reset the styling of a web browser element, such as a <button>?

I have come across many sources advising me to reset HTML by manually resetting numerous individual properties, as demonstrated here: https://css-tricks.com/overriding-default-button-styles/ Another approach I discovered in CSS is simply using: button: {a ...

Alter the style attribute using JavaScript

I have a calendar table row that needs to be displayed or hidden based on the month. HTML: <tr id="bottomrow" class="week" valign="top" style="display:none;"> <td class="day" id="d36"> <div class="daynumb" id="x36">& ...

IE9: Enforce the browser and document modes of IE9

Currently in IE9, my webpage is rendering with the Browser Mode set to 'IE9 Compat View' and Document Mode as 'IE standards', causing issues with all canvas elements on the page. I have already ensured that I have included '' ...

Is there a way to locate a file by inputting a partial name?

How can I create a search bar that allows me to find files in a folder (songs) by typing only part of the song name? For example, if I type "he," it should show results like "hello" and "hey." Is there a way to take my input and search for it within file n ...

Display the Ajax response in a designated div

I am facing an issue with my ajax code. It doesn't print any output, but when I use alert() to print the output, it shows up fine. $(".grp-ans").each(function(e){ $.ajax({ type: 'POST', url: 'show-answ ...

HTML - Centered layout with a constant header and footer

Imagine a layout where there is always a header at the top, a footer at the bottom, and the space between them should be filled by the content 100% (image 2). Additionally, everything in the header, content, and footer should be horizontally centered, usin ...

Is there a way to convert HTML into a structured DOM tree while considering its original source location?

I am currently developing a user script that is designed to operate on https://example.net. This script executes fetch requests for HTML documents from https://example.com, with the intention of parsing them into HTML DOM trees. The challenge I face arise ...

Receiving an absence of string or a null value from a text box

My goal is to test the functionality of an <input> element by entering text into a textbox and then displaying that data in the console. However, I am encountering issues with retrieving and printing the content. Additionally, I am interested in test ...

Is it possible to display a pdf.js page as actual HTML elements rather than as canvas or SVG?

I am in the process of creating a user-friendly mobile interface for pdf reading. However, I want to incorporate more advanced features by developing my own pdf reader instead of relying on the pdf.js team's viewer. Is there a method available to rend ...

Refresh a page automatically upon pressing the back button in Angular

I am currently working on an Angular 8 application with over 100 pages (components) that is specifically designed for the Chrome browser. However, I have encountered an issue where the CSS randomly gets distorted when I click the browser's back button ...

Showing a div element with the power of JavaScript

I want to enhance the accessibility of my website for users who do not have JavaScript enabled. Content that will be visible if the user has JavaScript enabled. Content visible when JavaScript is disabled. By default, DisableJS is set to Display:none; ...

Make sure a div is displayed on top of any content currently in fullscreen mode

I recently encountered an issue with my Chrome extension where a menu I inserted into the page would disappear whenever a flash or html5 video player went full screen. Is it possible to have two objects in full screen simultaneously, or is there another so ...

Tips for transferring data to the next page with JavaScript AJAX

I am working on a webpage that includes an html select element <pre> $query = mysql_query("select * from results"); echo "<select id='date' onchange='showdata()' class='form-control'>"; while ($arr = mysql_fetch_a ...

Linking the User Interface to the Controller and Database

My UI requires a text field where users can input the streamId (e.g. 1, 2, 3) and then click 'ok' to display a table on the screen from the database based on the streamId value. ...

In my PHP project, I am working on implementing a feature that will display the name of the logged-in user on the website, such as "Hello Mike" or "Welcome Mike"

When a user is logged into my website, I want it to display a greeting like "Hello" or "Welcome User." This is the code I am currently using: <?php $user = $_GET['session_is_registered']; echo ('$user'); ?> However, the PHP c ...

Can you explain the distinction between Declared Values and Specified Values?

I found myself confused by the distinction between Declared Values and Specified Values. Initially, I believed that the Declared Values were set by the author. These values then filter down to a single value, known as the "winning value", which becomes t ...

Error Handling in PHP Form with HTML and Image Upload

Let me give you some context first. Recently, I've delved into the world of php and have been at it for a few days now. My code might be a bit messy, but I've done my best to markup as much as possible. I have a form where three things are expec ...

Having trouble with the dropdown feature on AngularJs?

Encountering an issue while trying to display the dropdown list. Upon inspecting in Chrome, it seems like the data is loading correctly but when clicked, the dropdown menu does not show up. The data is fetched from the BooksController and all options are ...

Discovering the process of extracting a date from the Date Picker component and displaying it in a table using Material-UI in ReactJS

I am using the Date Picker component from Material-UI for React JS to display selected dates in a table. However, I am encountering an error when trying to show the date object in a table row. Can anyone provide guidance on how to achieve this? import ...

Issues regarding the CSS navigation bar

I'm currently working on creating a navigation bar using HTML and CSS. However, I'm facing an issue where the nav-bar doesn't extend to the full width of the page as intended. I've attempted adjusting the padding-left: 0px and padding-r ...