Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

css - Force HTML Tables To Not Exceed Their Containers' Size

This question has been asked several times, but none of the answers provided seem to help me:

See this in action here: http://jsfiddle.net/BlaM/bsQNj/2/

I have a "dynamic" (percentage based) layout with two columns.

.grid {
    width: 100%;
    box-sizing: border-box;
}
.grid > * {
    box-sizing: border-box;
    margin: 0;
}
.grid .col50 {
    padding: 0 1.5%;
    float: left;
    width: 50%;
}

In each of these columns I have a table that is supposed to use the full column width.

.data-table {
    width: 100%;
}
.data-table td {
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
}

My problem is that some of the columns in that table have content that needs to be truncated to fit in the given width of the table. That does not happen, though. I get two tables that are overlaying each other.

Requirements:

  • Needs to be percentage based. I can't set absolute sizes.
  • Each rows' height must not grow beyond one text line (which would happen if I remove white-space: nowrap)
  • Must work in Chrome, Firefox and Internet Explorer 8+
  • Can't display tables below each other as it has to fit onto one sheet of paper when printing.

What I tried:

  • inside of and use width and overflow on that. Changed nothing.
  • "display: table;" on containing div - instead of having two columns the tables were displayed below each other
  • "table-layout: fixed;" - Forced all columns to have same width
  • I know that columns 2+3 have a total of 30% of width so I tried to manually set column 1 to 70% - Did not change anything
  • Zero-width spaces in content - didn't change anything, probably due to white-space: nowrap;

Related Questions:

question from:https://stackoverflow.com/questions/17969877/force-html-tables-to-not-exceed-their-containers-size

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you need to add the table-layout property:

table-layout: fixed;

also include width=100% in the table HTML tag, not just the style tag.

http://jsfiddle.net/reeK5/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...