Show the table within the flex container

I have a question about CSS. Is it possible to apply display: table to a flex item?

Whenever I try to do this, the layout doesn't behave as expected - specifically, the second flex item does not fill the available vertical/horizontal space.

.container {
  min-height: 200px;
  border: 1px solid black;
  display: flex;
  flex-direction: column;
  text-align: center;
}

.header {
  background-color: gray;
}

.content {
  flex: 1;
  display: table;
  background-color: red;
}

.content > span {
  display: table-cell;
  vertical-align: middle;
}
  
<div class="container">
  <div class="header">
    <span>Header</span>
  </div>
  <div class="content">
    <span>Main content</span>
  </div>
</div>

Answer №1

Sure, it is possible to achieve the desired layout using different techniques.

One approach could be utilizing flex properties extensively for better control over layout.

.wrapper {
  height: 300px;
  border: 1px solid black;
  display: flex;
  flex-direction: column;
  text-align: center;
}

.header {
  background-color: lightblue;
}

.main-content {
  flex: 1;
  background-color: lightgrey;  
  display: flex;
  align-items: center;
  justify-content: center;
}
<div class="wrapper">
  <div class="header">
    <span>Header</span>
  </div>
  <div class="main-content">
    <span>Main content</span>
  </div>
</div>


On a side note:

The behavior of a table element is unique and requires special handling compared to regular block or inline elements. To integrate a table within a flex layout, specific height settings are necessary for both parent and child elements, as demonstrated in this example: http://jsfiddle.net/LGSon/0bzewkf4.

Although display: table may seem like a feasible solution, it's essential to acknowledge that the limitations of flex can impact height restrictions, leading to unexpected results. In such cases, it is not the display: table property causing conflicts but rather the intrinsic behaviors of flex.

For additional insights into addressing potential issues with flexbox, feel free to explore my response on managing overflow concerns in IE11: Normalizing Flexbox overflow in IE11

Answer №2

It's often questioned why one would use a table within a flexbox layout... However, a solution is to set a specific width for the table and have it inherit the minimum height from its parent element.

.parent {
  min-height: 200px;
  border: 1px solid black;
  display: flex;
  flex-direction: column;
  text-align: center;
}

.header {
  background-color: gray;
}

.content {
  display: table;
  flex:1;
  background-color: red;
  width:100%;

min-height:inherit; 
}

.content > span {
  display: table-cell;
  vertical-align: middle;
  height: 100%;
}
<div class="parent">
  <div class="header">
    <span>Header</span>
  </div>
  <div class="content">
    <span>Main content</span>
  </div>
</div>

Answer №3

To achieve the desired layout, avoid using a table layout and instead apply align-self: center; to .content- > span {.... Additionally, make sure to turn the span element into a flex item by adding display:flex to the .content element. It's important to note that combining a flex layout with a table layout may not yield the desired results, as vertical-align does not impact the alignment of flex items.

Properties not affecting flexible boxes

Due to the different layout algorithm used by flexible boxes, certain properties do not apply to flex containers:

column-* properties from the multiple column module do not affect flex items. float and clear have no impact on flex items. Implementing float causes the element's display property to compute to block. vertical-align does not affect the alignment of flex items.

.parent {
  min-height: 200px;
  border: 1px solid black;
  display: flex;
  flex-direction: column;
  text-align: center;
}

.header {
  background-color: gray;
}

.content {
  flex: 1;
  display: flex;
  background-color: red;
}

.content > span {
  flex: 1;
  align-self: center;
}
<div class="parent">
  <div class="header">
    <span>Header</span>
  </div>
  <div class="content">
    <span>Main content</span>
  </div>
</div>

Answer №4

When working with tables in a flex-column, you may encounter unexpected results due to their horizontal orientation. To avoid this issue, I made the necessary adjustments to ensure they align perfectly in a vertical column flow.

To center the .content, I utilized the following CSS properties: position: relative;, top: 50%;, translateY(360%); for vertical alignment, and text-align: center; for horizontal alignment. Additionally, I transformed the span into a block element for better functionality.

I updated the styling as follows:

.content {
  flex: 1;
  background-color: red;
}
.content > span {
  display: block;
  position: relative;
  top: 50%;
  transform: translateY(360%);
  text-align: center;
}

Instead of using display: table, I changed it to table-row. Is this the desired outcome?

.parent {
  min-height: 200px;
  border: 1px solid black;
  display: flex;
  flex-direction: column;
  text-align: center;
}
.header {
  background-color: gray;
}
.content {
  flex: 1;
  background-color: red;
}
.content > span {
  display: block;
  position: relative;
  top: 50%;
  transform: translateY(360%);
  text-align: center;
}
<div class="parent">
  <div class="header">
    <span>Header</span>
  </div>
  <div class="content">
    <span>Main content</span>
  </div>
</div>

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 CSS for the ajax call functions correctly on desktop devices but is not displaying properly on mobile devices

My ajax call is facing an issue where the CSS classes for a loader and overlay are only being applied in desktop view, not mobile view. I am unsure of what mistake I may be making. Can someone help me resolve this problem? Here is the ajax call: function ...

Thrilling visual loops in motion with CSS animations

Currently, I am working on developing a captivating 'pulsating rings' animation that revolves around a circular image with the use of CSS animations. The image itself is a circle measuring 400px in width. Although I have successfully implemented ...

Which selector in CSS is applicable for targeting Bootstrap tooltips?

After some experimentation, I've mastered the functionality of Twitter Bootstrap Tooltips and am now focused on customizing their appearance. Previous inquiries into other plugins revealed specific CSS selectors - for example, jScrollPane had a track ...

Moving the final item in the list to the end

I've chosen to implement the Vali Admin theme and am currently attempting to move the last list item in the left sidebar to the bottom. While I have limited experience with flexbox, I decided to change the menu to display: flex and followed the steps ...

Centered text in <td> with CSS aligned to the right

https://i.sstatic.net/UrB5f.jpg Is there a way to align the 3 input fields so that their right edges are all vertical? I still want the text+Input to be centered within the column but aligned vertically... Using Bootstrap 3.3.7 HTML: <link href="h ...

Extracting the URL of the @font-face declaration in CSS with the use of JavaScript

Currently, my CSS file contains numerous @font-face tags. Each tag specifies a unique font-family and src URL. @font-face{ font-family: Garamond; src: url('ePrintFonts/pcl_91545.ttf'); } @font-face{ font-family: C ...

What steps should I follow to create a cohesive background design using CSS?

Is there a way to combine two separate div elements in CSS? Currently, the image Row and col are not connected to the above SVG blob. How can I utilize the blob as a background? Based on the code provided below, they appear separated and I'm unsure ho ...

Designing php/mysql data in a container

After successfully converting an ordered list output into a div output, I now face the challenge of stacking arrays on top of each other (and side by side with the two divs) like they would in an ordered list. Here is the original code snippet: <?php ...

What is the correct way to integrate a HTML/CSS/JS theme into a Vue project effectively?

As a newcomer, I recently acquired a bootstrap theme that comes with HTML, CSS, and JavaScript files. My goal now is to integrate this theme into Vue in order to make it fully functional. The challenge I am facing is how to successfully incorporate the the ...

Ensuring a @keyframe animation remains constant for the entire duration of the user's visit to the webpage using CSS

How can I create a continuous @keyframe animation that stays active for the duration of the user's visit to my webpage? I've implemented several color animations on my website, each with a specific duration. Is there a way to make these color ch ...

Challenges with Implementing Background Images on my Website with HTML and CSS

Being new to the world of coding, I recently created a website for a college presentation. However, after uploading the website onto the internet, I noticed that the background images from my files are not displaying on the website. It's likely that I ...

Which particular files should be included such as CSS and JavaScript?

After downloading bootstrap4, I'm unsure which css, js, or other files are necessary to include in order to make my form Bootstrap ready. My current task involves creating a form in CakePHP 3. Click here for more information on how to create a form ...

Dynamic sidebar that adheres to the content and is placed alongside it using JavaScript

I am in need of creating a sticky sidebar that will float beside my content column. The purpose of this sidebar is to contain jump links on the page, similar to what can be seen on this page, but with the navigation buttons positioned directly next to the ...

Strange height problem detected in embedded YouTube video

Currently, I have a page with an embedded YouTube video that is intended to be placed in a container with a variable width. When the video is outside of the container, the aspect ratio appears normal. However, once it is placed inside the container, the he ...

Is the WordPress error message "wp_register_style was improperly called" showing up on

I am facing an issue while trying to incorporate this code into my initial Wordpress template. It seems that the libraries for Bootstrap and my custom styles are not functioning as expected. Here is the code snippet in question. Any insights would be great ...

Tips for resizing the dimensions of two div elements to fit the screen dimensions

Take a look at this template. It's interesting how the left content div and right sidebar divs adjust themselves based on the browser window size, zoom level, or viewing device such as a mobile phone. When viewed on a mobile, only the content div is d ...

Navigation problems resolved: The fixed-top class causes issues with navbar-right and obscures the logo

I have implemented a bootstrap Nav on this page To achieve a sticky top navigation, I used the fixed-top class. However, this caused the navbar to align left instead of right as intended. It also ended up covering my logo, which was originally aligned to ...

Images in the JavaScript menu are not remaining fixed in place

Can someone help me with my website? I'm having trouble with the menu for different pages, it should stay gray for the active page. It was working fine before I switched to Wordpress, but now I'm not sure what the issue is. Could someone take a ...

Exploring the varying treatment of the noscript tag across different web browsers

I've been searching everywhere, but I can't find any information on this topic. What I really want to understand is what browsers do with the content inside a noscript tag when JavaScript is enabled. Let's consider an example: According t ...

show items in UL LI elements as "inline" within a bootstrap column

Struggling with organizing UL LI items in a bootstrap setup? Trying to make them align horizontally instead of vertically but can't figure it out. Here's my code: <div class="container-fluid" style="margin-bottom:45px;"> <div cl ...