I have three rows of primary divs in my template. I have a header, a content wrapper, and a footer. The header butts right up against the top and the footer against the bottom. I have a content wrapper div in between the two. I basically want to center the content wrapper vertically within the two divs. The content div will always be a dynamic height, so the line-height method won't work for me. What would be the best method for me to use for this?
相关问题
- Adding a timeout to a render function in ReactJS
-
Why does the box-shadow property not apply to a
- Add animation to jQuery function Interval
- jQuery hover to slide?
- Issue with star rating css
Vertically centering content
Without using JavaScript, there's a limited number of ways to vertically center dynamic content:
In a case like this, the modern approach is to use CSS tables, which is equivalent to HTML tables. But if support for IE7 or earlier is needed, then use HTML tables instead. Flexbox isn't a good fit in this particular case, and it's not supported by IE9 and earlier. CSS grids are only supported by IE10 at present, and the standard hasn't been finalized.
The basic usage of CSS tables is:
HTML
CSS
Demos (tested in: IE8/9/10, FF, Chrome, Safari, Opera)
Choosing between HTML tables and CSS tables
For tabular data (e.g., a grid of data), favor using HTML tables. It's more semantically correct in this case, and the nature of the source code can be more easily understood, which may help with accessibility and SEO (and the maintainability of the code).
For non-tabular data (e.g., general layout), favor using CSS tables (if floats and CSS positioning are not adequate). It's more semantically correct, since the use of HTML tables creates an expectation of tabular data, and it may be less confusing to screen readers and search engines. Specific uses for layout include vertically-centered content and equal-height columns.
One particular advantage of CSS tables over HTML tables is the support for
visibility:collapse
, which allows rows or columns to collapse (something not doable with HTML tables). If there's a need for that specific feature for tabular data, use CSS tables.Save yourself some pain and just break them out into three separate containers.
The overflow hidden attribute will make a div expand automatically to show any additional content that is added to it.. making it behave more like a table cell.
Here is an example enter link description here
You can do this using the following code:-