Expanding container in table-cell to occupy full height of its parent element

I have encountered some issues with using flexbox for vertical alignment in our specific use case, so I am attempting to achieve it using a table instead. While the table method works well for vertical alignment, I am facing an issue where the content inside does not fill the remaining height. Is there a way to accomplish this using CSS?

Link to example

<html>

  <body>
    <div class="row">
      <div class="cell">
        <div class="container">
          Hello world
        </div>
      </div>
      <div class="cella">
        Cell2
      </div>
    </div>

  </body>

</html>


.container {
  background-color: red;
  height: 100%; 
}

.cell {
  display: table-cell;
  vertical-align: middle;
  border: 1px solid black;
  min-width: 100px;
  max-width: 100px;
  min-height: auto;
  max-height: none;
}

.cella {
  display: table-cell;
  vertical-align: middle;
  border: 1px solid black;
  min-width: 100px;
  max-width: 100px;
  height: 30px;
  max-height: none;
}

.row {
  display: table
}

Answer №1

To ensure proper rendering, it is important to establish the height of the parent element. Currently, your .cell does not have a defined height, causing the .container to lack a reference for calculating its dimensions based on a percentage value. Consider setting a specific height for the .cell, such as .cell {height: 30px;} similar to how it is done for .cella.

If both .cella and .cell are meant to be identical in appearance but one requires unique styling, it may be helpful to follow this suggestion. Apply the same class to all cells, then assign an id to the one requiring distinct styling. You can then customize the style for that particular cell using its assigned id. Keep in mind that the cell with both a class and id will inherit CSS properties from both selectors.

Answer №2

I found this solution to work perfectly for me. Give it a go and let me know if it meets your requirements.

<html>
  <body>
    <div class="row">
      <div class="cell" id="cell1">
        <div class="container">
          Hello world
        </div>
      </div>
      <div class="cell" id="cell2">
        Cell2
      </div>
    </div>
  </body>
</html>

<style>
.container {
  background-color: red;
  position: absolute;
  inset: 0; /*shorthand for top:0, left:0, bottom:0, right:0*/
  height: 100%;
  width: 100%;

  /* for centering content within container
  display:flex;
  justify-content:center;
  align-items:center;
 */
}

.cell {
  display: table-cell;
  vertical-align: middle;
  position: relative;
  border: 1px solid black;
  height: min-content;
  width: 100px;
}

.row {
  display: table
}
</style>

My approach involved setting the .container to position absolute and making the parent .cell relative, thus creating a containing block for the .container. Using inset:0 ensures that the absolute .container stretches across the entire relative parent .cell. I hope this solution works for you!

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

Leveraging JavaScript to change the input value by appending the character "#"

I am looking to modify the input value by adding a "#" in front of each word, but I am facing issues with handling spaces properly. function addHash(input) { var text = input.value.replace('#', ''); var words = text.split(" "); ...

What is causing the initial activation of the <button> within a form?

Within my <form>, I have included 2 submit buttons. The first button looks like this: <button class="regular" id="geocodesubmit" style="height:40px;">Set Location</button> The second button looks like this: <button type="submit" ...

What sets apart li.parent from ul?

I am facing some confusion with this parent keyword. Are li.parent and ul the same thing? If so, can someone please explain what this means? Thank you. (nav and sidebar are classes). .sidebar ul.nav .active > a:focus, .sidebar ul.nav li.parent a.active ...

Using JavaScript or JQuery, is there a way to retrieve the favicon icon, Apple-touch icon, and title of a webpage?

Is it possible to extract just the favicon icon and title from a webpage using only an IFrame? For example, for a site like www.stackoverflow.com, I want to retrieve only its title and favicon icon/Apple-touch-icon. Are there any plugins or parsers avail ...

Find all relevant employee information at once without the need for iteration

I have structured two JSON arrays for employee personal and company details. By inputting a value in the field, I compare both tables and present the corresponding employees' personal and company information in a unified table. <html> ...

Implementing inline scrolling using CSS

My objective is to have each article added to the container div, "inlineExample", load to the right without any vertical overflow. The CSS should be able to handle each additional article dynamically as they are added to the right. <div id="inlineExamp ...

Preventing scrollbar-induced content shifting in Material-UI components: A guide

My browser app displays content dynamically, which can vary in length compared to the screen size. This causes a vertical scrollbar to appear and disappear on desktop browsers, leading to the content shifting left and right. To prevent this, I apply style ...

Make the table in a bootstrap design appear with a border when you hover over a cell using the table

Looking for a solution to make the border work as expected when hovering over each td in a table like this example: https://jsfiddle.net/nmw82od1/ Here is the CSS code: .table1 td:hover { border: 1px solid black; } .table2 td:hover { border: 1px do ...

What is the process for inserting an image into a table using el-table and el-table-column components in Vue.js while utilizing ui-elements?

I'm new to Vue.js and successfully built a basic table using the ui-element. The el-table element was utilized for constructing the table, with columns displayed using el-table-column and prop (see code below). Now, I want to incorporate images/avatar ...

Tips for maintaining a stationary background during scrolling movements

Check out the following CSS code: #bigwrapper{ background-image: url('http://www.w8themes.com/wp-content/uploads/2013/09/Water-Backgrounds.jpg'); background-repeat: no-repeat; background-position: fixed; width: 100%; } Here is t ...

How to position collapsible buttons in Twitter's Bootstrap framework

I have successfully created two buttons on the same line. The second button is collapsible and when clicked, it adds a row of two more buttons to the view. However, the newly collapsed row of buttons appears aligned with the second button. I would like th ...

Use a CSS media query to elevate the third inline-block div above the initial two divs without utilizing Bootstrap

I am trying to style three colored HTML div tags displayed inline-block by using CSS only. The challenge I am facing is getting the blue div to appear on top of the red and green divs. My goal is to achieve a layout where the blue div overlaps the other tw ...

What's the best way to trigger an alert popup after calling another function?

Here are some HTML codes I've been working with: <button id="hide" onclick="hide()">Hide</button> <p id="pb">This paragraph has minimal content.</p> My goal is to have the paragraph hide first when the button is clicked, foll ...

Achieving Vertical Alignment of Text with CSS

Can you figure out what's wrong with the placement of content outside the <footer> tag in this HTML? Also, why is the text not perfectly vertically middle-aligned? <html> <head> <style> body { ...

What's the best way to add a border of 1 pixel solid #ddd to a select box with the help of jQuery?

I'm in need of some assistance, Many websites utilize the jQuery framework to add stylish effects to their selectboxes. Unfortunately, I am limited to using ie7 at my workplace and I want to include a 1px solid #ddd border around my selectbox (which ...

The website displays perfectly in Atom, however, it is not functional in any web browser

My website is currently in the development phase, and I'm experiencing issues with certain div elements not positioning correctly once the site goes live. Specifically, the "Follow Us" tab at the bottom of the site, among other divs, has been affecte ...

Activating Anchor's Pseudo Content on Hover of Another Element

I've encountered a straightforward issue - I have an anchor in a navigation menu that generates an arrowhead character as ::after pseudo content in CSS when hovering over it: https://i.sstatic.net/Yhd9A.jpg However, the arrowhead character disappear ...

The CSS animation initially encounters a glitch before ultimately running smoothly as planned

I am currently working on a basic webpage that will showcase a list of disciplines. When a discipline is selected, relevant information will be displayed below. The code snippet available here demonstrates the functionality I am aiming for. However, I hav ...

Create a grid or flex layout with Bootstrap 4 that features a sticky first row and column

Does anyone have any suggestions on how to make the attached grid/table layout responsive using Bootstrap 4's flex/grid system? The layout should include a sticky first row and column for navigation items. I attempted using w- & h- classes, but it do ...

Issue with React-Route not displaying components

Below is the code snippet from my app.js file: <Router> <Header title="My Todos List" /> <Routes> <Route path="/about" element={<About />} /> <Route path="/" ...