CSS Tricks: Setting Dynamic Height Limits Based on Content Size

Imagine a scenario where there is a table set to have a height of 100vh. How can you establish the minimum height for the thead based on its content and the maximum height for the tbody?

<table style="height: 100vh;">
    <thead>...</thead>
    <tbody>...</tbody>
</table>

https://i.sstatic.net/aJ4xt.png

Answer №1

Are you searching for this information?

Updated

I conducted several tests and found that assigning height to the body was not working correctly across different browsers. The updated solution below resolves this issue (tested on Chrome, Firefox, Edge, IE11)

html, body {
  margin: 0;
}
table {
  width: 100%;
  height: 100vh;
}
table thead tr {
  height: 80px;      /*  on table elements, height works kind of like min-height  */
  background: yellow;
}
table tbody tr {
  background: lime;
}
<table>
  <thead>
    <tr>
      <td>
        HEAD<br>
      </td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        BODY
      </td>
    </tr>
  </tbody>
</table>

Answer №2

For reaching your goal, try this clever hack. Keep the height of the table header minimal, but not zero - opt for 1px instead. Set the body to occupy 0% height.

html,body{
  margin:0;
  }
table{
  width:100%;
  height: 100vh;
  border: 1px solid black;
}
thead{
  background: red;
  height:1px;
}
tbody{
  background: blue;
  height:0%;
}
<table>
    <thead>
      <tr>
        <td colspan="4">
          <img src="http://dummyimage.com/100x100/eb00eb/fff">
        </td>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>BODY</td>
        <td>BODY</td>
        <td>BODY</td>
        <td>BODY</td>
      </tr>
    </tbody>
  </table>

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

How to make text dynamically shrink with TailwindCSS class 'flex-shrink-0'

I've designed an 'Album' (React) component to showcase album artwork, name, and release date in a card-like format. This component consists of two divs - one for the photo and the other for text. Each artist's discography displays multi ...

Center your wordpress site title next to your logo

My logo and website title are not aligned correctly on my site. The current setup looks like this: https://i.sstatic.net/3dUiN.png After examining the HTML generated by WordPress, I found the following snippet: <div id="logo" class="clearfix"> ...

Using X-Editable to send information through Ajax requests

Trying to submit data via Ajax using X-Editable has presented a challenge when attempting to run the php script defined in the url parameter. Starting with a basic example that works: Html: <a href="#" id="username" data-type="text" data-pk="1" data-n ...

Staggered Drop-Down Menus

I've created a custom CSS drop-down menu that works well, but I've realized that updating this menu across all the HTML pages on my website would be a tedious task. If I ever need to add a new section and tier to the menu, I'd have to update ...

The Bootstrap Carousel fails to function properly when applied to elements that are dynamically generated through JavaScript

I'm attempting to design a feed similar to LinkedIn on social media where each div acts as a carousel displaying images just like a social media feed. Using JavaScript, I dynamically generate the following element with images for the carousel: &l ...

Attempting to serialize a form using Ajax and jQuery

I am facing an issue with submitting user input from a modal using jQuery and AJAX. The problem is that the AJAX call is not capturing the user input. Even though the AJAX call is successful, when I check on the server side, the user input appears to be bl ...

Position text above an image using CSS

.content { float: left; width: 33.33%; font-weight: bold; text-align: center; margin-top: 5px; margin-bottom: 2px; overflow-wrap: break-word; } .content_div { position: absolute; width: 100%; left: 0; top: 100%; font-size: 16px } ...

Maintain the aspect ratio of an image using CSS and flexbox styling

I have a gallery of images that looks great on a full-size PC screen. However, when I resize it for viewing on a phone, the aspect ratio is not maintained and the images only stretch in width. Currently, this is how it appears: https://gyazo.com/a1f605bb4 ...

What makes the AngularJS ng-repeat search term filter fail to function properly?

I am attempting to create a dynamic table that filters and displays only rows containing the search term entered in the search box. Following a basic example from the w3schools tutorial, I am trying to get it to work properly: https://www.w3schools.com/ang ...

Issue with showing error message to the user using AJAX

Using AJAX code to manage errors from the backend. Everything is functioning properly, but I want to display the errors individually to the user in a numbered list. Desired Output 1. Please enter a valid phone number 2. Please provide your email address ...

Materialize CSS is throwing an error: 'velocity is not a function'

Encountering an issue with Materialize CSS. A JavaScript error appears 'I('.velocity is not a function', for certain actions. For instance, clicking the collapse icon on the sidenav results in e.velocity is not a function error. Similarly, u ...

Styling Image Components for Mobile Devices on Next.js

Utilizing the Image Nextjs Component to center my image on the page has proven challenging. Despite aiming for the picture to maintain its true size, it appears pixelated. Additionally, when reducing the screen width, the image fails to display correctly f ...

Looking to display dropdown menus on a new line along with a text input field using jQuery?

<script type="text/javascript"> var arr = [{ val: 1, text: 'Option 1' }, { val: 2, text: 'Option 2' }]; $(function () { $('a').click(function () { var sel = $('<select>&apo ...

Transitioning images in JQuery by using a rollover effect

Is there a way to use Jquery for a hover effect that transitions images with a slide up animation? I've looked everywhere online, but I can't seem to find a code that works. All the examples I've found use fadeIn or fadeOut. Thank you for yo ...

Having difficulty locating the identification number of an item within the HTML coding

I'm currently trying to locate the ID of the "proceed to checkout" button for a bot that I am developing for my mother on the Best Buy website. However, it seems like the button's ID is hidden and I'm unable to determine how to uncover it. A ...

Tips for maintaining consistent styles in CSS for multiple websites

I am currently working on developing a customizable chatbot widget using react. The goal is to create a chatbot widget that can be easily integrated into any website, similar to the functionality of rasa-webchat. During testing on some websites, I encount ...

Using the paragraph element within a flexbox forces the parent element to expand to the full width

I am attempting to design a straightforward layout where a heading, paragraph, and button are centered both horizontally and vertically on the page. My goal is for the .centered-div to match the width of the .heading. The issue arises when the paragraph t ...

Tips for choosing the parent's parent and applying a width of 100%

I am currently facing some challenges while creating a simple navigation bar from scratch. I am struggling with setting specific widths, especially wanting .navbarDropBtn to align its width with .navbarMain. However, it seems to only match the width of the ...

Restricting the size of the component to match the height of its containing div

I set up a layout with a toolbar at the top, a sidenav on the left, and the remaining space for content. The sidenav consists of a searchbox and a list of items. My goal is to have the sidenav's height match the row it's placed in, allowing the ...

Horizontal compression applied to background image

After following a suggestion ("css only technique number 1" found here http://css-tricks.com/perfect-full-page-background-image/), I experimented with using an inline img element and CSS to create a background image that would occupy the entire browser win ...