2011-01-08 9 views
2

Ich habe eine HTML-Tabelle mit 3 'Gruppen' Spalten, links, Mitte und rechts.Wie man eine Spalte in eine Tabelle einfügt, mit Tabellengröße zum Browserfenster

 
+----------------------------------+ 
| L-1 | L-2 | M   | R1 | R2 | 
+-----+-----+------------+----+----+ 
| x | z | aaaaaaa... | 1 | 2 | 
| y | w | bbbbbbb... | 3 | 4 | 
+-----+-----+------------+----+----+ 

Ich mag die Tabelle auf die Breite des Browserfensters beschränken, wenn es eine sehr lange Zeichenfolge in der Mitte („M“) Spalte ist.

Ich habe versucht, CSS-Wortbruch in dieser Spalte zu verwenden, wie in wordwrap a very long string beschrieben.

Unten ist das HTML (Beispiel). Das CSS enthält meine Gedanken darüber, wie dies funktionieren sollte (aber anscheinend nicht).

Was mache ich falsch?

 
<html> 
<head> 
    <style type='text/css'> 
    table { /* nothing here - table is block, so should auto expand to as large as it can get without causing scrollbars? */ } 
    .left { text-align:center; } 
    .right { text-align:right; } 
    .middle { text-align:left; width:100%; /* expand this column to as large as it can get within table? */} 
    .wrap { word-wrap:break-word; width:100%; /* use up entire cell this div is contained in? */ } 
    </style> 
</head> 
<body> 
    <table> 
    <tr> 
    <th class=left>L-1</th> 
    <th class=left>L-2</th> 
    <th class=middle>M</th> 
    <th class=right>R-1</th> 
    <th class=right>R-2</th> 
    </tr> 
    <tr> 
    <td class=left>argle</td> 
    <td class=left>bargle</td> 
    <td class=middle><div class=wrap>wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww</div></td> 
    <td class=right>glyp</td> 
    <td class=right>glof</td> 
    </tr> 
    </table> 
</body> 
</html> 

Das Ergebnis ist im Moment, dass kein Zeilenumbruch stattfindet, statt die Tabelle unwantedly auf der rechten Seite schießt aus und verursacht eine Scrollbar im Browser-Fenster.

Antwort

0

Einschlägige Änderungen:

  • Added table { table-layout:fixed } zu Ihrem CSS.
  • Hinzugefügt width="100%" zu Ihrem table.
  • Das externe div wurde entfernt und die .wrap Klasse zu td mit der Klasse .middle hinzugefügt.
  • Das Attribut width=100 wurde zu Ihren anderen Spalten hinzugefügt.

Full HTML:

<html> 
<head> 
    <style type='text/css'> 
    table { table-layout:fixed; /* nothing here - table is block, so should auto expand to as large as it can get without causing scrollbars? */ } 
    .left { text-align:center; } 
    .right { text-align:right; } 
    .middle { text-align:left; /* expand this column to as large as it can get within table? */} 
    .wrap { word-wrap:break-word; /* use up entire cell this div is contained in? */ } 
    </style> 
</head> 
<body> 
    <table width="100%"> 
    <tr> 
    <th class=left width=100>L-1</th> 
    <th class=left width=100>L-2</th> 
    <th class=middle>M</th> 
    <th class=right width=100>R-1</th> 
    <th class=right width=100>R-2</th> 
    </tr> 
    <tr> 
    <td class=left>argle</td> 
    <td class=left>bargle</td> 
    <td class="middle wrap">wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww</td> 
    <td class=right>glyp</td> 
    <td class=right>glof</td> 
    </tr> 
    </table> 
</body> 
</html> 
+0

Danke, dass Sie es versuchen! Funktioniert nicht ganz, die Spalten L und R enthalten Benutzereingaben, daher kann ich sie nicht auf 100 Pixel fixieren. – rosenfield

0

Dies funktioniert auf den ersten Blick:

<!DOCTYPE html> 
<html> 
<head> 
    <style type='text/css'> 
    td { vertical-align:top; } 
    .left { text-align:center; } 
    .right { text-align:right; } 
    .middle, 
    .middle iframe { width:100%; } 
    </style> 
</head> 
<body> 
    <table> 
    <tr> 
    <th class=left>L-1</th> 
    <th class=left>L-2</th> 
    <th class=middle>M</th> 
    <th class=right>R-1</th> 
    <th class=right>R-2</th> 
    </tr> 
    <tr> 
    <td class=left>argle</td> 
    <td class=left>bargle</td> 
    <td class=middle><iframe style='border:0' src='data:text/html,%3Cbody style=%22margin:0;word-wrap:break-word%22%3Ewwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww%3C/body%3E'></iframe></td> 
    <td class=right>glyp</td> 
    <td class=right>glof</td> 
    </tr> 
    </table> 
</body> 
</html> 

Aber bricht, wenn Sie eine weitere Zeile hinzufügen, weil das eingebettete iframe statische Höhe hat.

1

Dies scheint perfekt zu arbeiten: hin und wieder über

<!DOCTYPE html> 
<html> 
<head> 
    <style type='text/css'> 
    th,td { vertical-align:top; padding:0 5px; } 
    .left { text-align:center; } 
    .right { text-align:right; } 
    .middle { text-align:left; width:100%; } 
    </style> 
</head> 
<body> 
    <table> 
    <tr> 
    <th class=left>L-1</th> 
    <th class=left>L-2</th> 
    <th class=middle>M</th> 
    <th class=right>R-1</th> 
    <th class=right>R-2</th> 
    </tr> 
    <tr> 
    <td class=left>argle</td> 
    <td class=left>bargle</td> 
    <td class=middle>w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;w&#x200b;</td> 
    <td class=right>glyp</td> 
    <td class=right>glof</td> 
    </tr> 
    </table> 
</body> 
</html> 

Der Trick ist, nur einen nicht sichtbaren Leerzeichen einfügen (&#x200b;).

Ich hasse es aber, meine eigenen Fragen zu beantworten, also warte ich darauf, dass jemand etwas Schöneres findet, bevor ich das schließe.

+0

Danke Mann U habe einen tollen Job gemacht, um mein Problem zu lösen. –

Verwandte Themen