I have a simple table :
table {
border: 1px solid grey
}
th,
td {
padding: .5rem
}
th {
text-align: right
}
<table>
<tbody>
<tr>
<th scope="row">Feed in Braids</th>
<td>20 / two braids</td>
</tr>
<tr>
<th scope="row">Waves / Curls / Straightening</th>
<td>30</td>
</tr>
<tr>
<th scope="row">Hairstyle for special occasions</th>
<td>45-60</td>
</tr>
</tbody>
</table>
I would like to squeeze the data in one column, which would probably have to look something like this:
table {
border: 1px solid black;
padding: 1rem;
text-align: center;
}
th,
td {
padding: .5rem
}
<table class="table table-hover">
<tr>
<th>Feed in Braids</th>
</tr>
<tr>
<td>20 / two braids</td>
</tr>
<tr>
<th>Waves</th>
</tr>
<tr>
<td>25</td>
</tr>
<tr>
<th>Special</th>
</tr>
<tr>
<td>40 </td>
</tr>
</table>
I have doubts if the "squeezed" table would be a correct construct, in terms of possibly unclear scope of the headings.
Concerning accessibility, a table header (th) can only have either "row" or "column" as its scope, and this is always valid for the whole row or column. So in this way your "one-column table" doesn't really meet accessibility standards and isn't semantically correct.
But if you have everything in one column, you could as well use alternating headers (like h3, h4, h4, whatever) instead of th and paragraphs (or simply contents following the headers) instead of td. And once you are that far, a table itself wouldn't make that much sense – the wparent element might as well be a div...
You also might want to consider a definition list instead, maybe (depending on what your actual usecase looks like, or which function it should fulfill): https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dl
Or you use nested tables, i.e. multiple tables consisting of one th and one td, nested either in the cells of a larger table (if that semantically makes sense at all) or simply inside a div or section element.
Related
I am using HTML and CSS to display sentences aligned with word-by-word IPA transcriptions:
<table>
<tr class="eng">
<td>This </td>
<td>is</td>
<td>an</td>
<td>example</td>
<td>sentence.</td>
</tr>
<tr class="ipa">
<td>ðɪs</td>
<td>ɪz</td>
<td>ən</td>
<td>ɪɡˈzæmpl̩</td>
<td>ˈsɛntəns</td>
</tr>
</table>
tr.eng {
font-weight: bold;
}
tr.ipa {
font-style: italic;
}
td {
white-space: nowrap;
text-align: center;
}
.eng td:first-child {
text-align: left;
}
.eng td:last-child {
text-align: right;
}
http://jsfiddle.net/2ab9pgmd/
If a sentence is too long for the screen, I would like both rows to wrap to a new line, so that it could look like following hard-coded example:
<table>
<tr class="eng">
<td>This </td>
<td>is</td>
<td>an</td>
<td>example</td>
</tr>
<tr class="ipa">
<td>ðɪs</td>
<td>ɪz</td>
<td>ən</td>
<td>ɪɡˈzæmpl̩</td>
</tr>
</table>
<table>
<tr class="eng">
<td>sentence.</td>
</tr>
<tr class="ipa">
<td>ˈsɛntəns</td>
</tr>
</table>
Is it possible to set up an HTML table to wrap multiple rows to new lines dynamically depending on the screen size, rather than manually defining the line lengths in advance?
I found a similar example of what I would like to do here, but this solution does not keep the columns aligned for tables with more than one row.
If possible, I would rather not use JavaScript so the text can be viewed as an eBook.
I prefer you go with the CSS Grid cause when you use table row for wrapping it wont get wrapped up and wont provide you the desired output . You can add wrapping rules but I don't think it will work.
I have a standard table in which the cells appear left-to-right in the same row, but I'd like to force the third cell to appear on a new line. Is there a CSS tweak that can do this?
<table><tr>
<td class="col-1">One</td>
<td class="col-2">Two</td>
<td class="col-3">Three</td>
<td class="col-4">Four</td>
</tr></table>
This displays as: One Two Three Four
Can I force the third column to display on a new row thus:
One Two
Three Four
Not exactly sure why you wouldn't just make it a new row, but I suppose if you had to use CSS, you could do something like this:
table tr td {
display: block;
width: 50%;
box-sizing: border-box;
float: left;
}
JSFiddle
You can make a new row, by wrapping around your td with a tr as below
<table>
<tr>
<td class="col-1">One</td>
<td class="col-2">Two</td>
</tr>
<tr>
<td class="col-3">Three</td>
<td class="col-4">Four</td>
</tr>
</table>
Hope this helps!
Ok, here's a graphic to explain what I'm talking about:
The first table would be what my html currently produces and the second table is what I'd like it to produce.
#animalTable{
display: table;
}
.animalRow{
display: table-row;
}
.animalCell{
display: table-cell;
width: 33%;
}
<div id="animalTable">
<div class="animalRow">
<div class="animalCell">Dog</div>
<div class="animalCell">Milton</div>
<div class="animalCell">1/2/1998</div>
</div>
...
</div>
What would be the best/easiest way to get my desired table? I know I could brute force it by creating sub-tables inside the main table but I was wondering if there was a better way?
Also, sorry if this is a dumb question.
I suggest using tables instead of div for this specific case. Here is why you should Use tables for what they are meant to and div's for what they are meant to.
What you are looking for is called Colspan and Rowspan, both are HTML td and th attributes.
Example:
<table>
<tr>
<th>Month</th>
<th>Savings</th>
<th>Savings for holiday!</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
<td rowspan="2">$50</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</table>
You can read further on w3schools.
Hope this helps.
Simple, add different class or id selectors and in the css, remove all borders and re-add the ones you want to keep. Example,
div{
border: none;
border-top: solid 1px black;
border-left: solid 1px black;
}
Correct me if I am misunderstanding. Otherwise, if you have any questions don't hesitate to ask!
I am trying to understand tables in html / css, but I don't understand anything of it. I have a lot of questions about it, but i will start with the first one.
Here:
http://www.w3schools.com/cssref/pr_tab_table-layout.asp
they are saying in case of "table-layout: fixed;":
"The horizontal layout only depends on the table's width and the width of the columns, not the contents of the cells"
Okay....so it does not depend on the contents of the cell? Let's test it:
I made a table with table-layout: fixed and I gave one column a width of 200px (inline css in the html):
.myTable {
border: 1px solid black;
}
.myTable th,
.myTable td {
border: 1px solid black;
}
<table class="myTable" style="width: auto; table-layout: fixed;">
<tr>
<th>Column A</th>
<th style="width: 200px">Column B</th>
</tr>
<tr>
<td>A1</td>
<td>B1: This is a sentence with a very long word in it, to check what the behavior is of the table in a case like that. Will long words be broken and wrap onto the next line? Thisisaverylongwordyesitisyesitisyesitisyesitisyesitisyesitisyesitisyesitisyesitisyesitisyesitisyesitisyesitisyesitisyesitisyesitisyesitis</td>
</tr>
<tr>
<td>A2</td>
<td>B2</td>
</tr>
</table>
Why that column with width 200px is now bigger than 200px? If the width would not depend on the contents of the cell then I would expect a width of 200px. In this case i would expect that the long word could overflow, but now the table / column is adjusting its width.
Can anyone tell me how this is working and why it's working like that? I know I can solve it with things like word-wrap: break-word; but I want to understand it.
Because you included a long line of unbroken text. By default the browser won't break up that text unless you tell it to with a rule like word-break:break-all;. So it does work as expected, you're just giving it text that it doesn't want to break because that's the default behavior. You'll also see if you remove that long unbroken string of text that the column is 200px wide.
.myTable {
border: 1px solid black;
}
.myTable th,
.myTable td {
border: 1px solid black;
word-break:break-all;
}
<table class="myTable" style="width: auto; table-layout: fixed;">
<tr>
<th>Column A</th>
<th style="width: 200px">Column B</th>
</tr>
<tr>
<td>A1</td>
<td>B1: This is a sentence with a very long word in it, to check what the behavior is of the table in a case like that. Will long words be broken and wrap onto the next line? Thisisaverylongwordyesitisyesitisyesitisyesitisyesitisyesitisyesitisyesitisyesitisyesitisyesitisyesitisyesitisyesitisyesitisyesitisyesitis</td>
</tr>
<tr>
<td>A2</td>
<td>B2</td>
</tr>
</table>
I have a <table> of data where consecutive rows are conceptually related and need to stay together. I've group each pair of rows in a <tbody> tag. When it comes time to print the table, I want to make sure that page breaks only happen between <tbody> tags.
I've tried some variations of page-break-inside: avoid and page-break-after: auto, but can't seem to get it to work in Chrome 42 (see screenshot below)
However, it does seems to work as expected in Firefox 40 and IE 11 though. It looks like page-break-* might only apply to block level elements. Is there a good way to accomplish this in html/css?
Example code:
<!doctype html>
<html>
<head>
<style>
table {
width: 70%;
border-collapse: collapse;
}
thead {
display: table-header-group;
text-align: left;
border-bottom: 2px solid #000;
}
tbody {
page-break-inside: avoid;
border-bottom: 1px solid #ccc;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>Project #</th>
<th>Owner</th>
<th>% Complete</th>
</tr>
</thead>
<tbody>
<tr>
<td>HR-123</td>
<td>Arther Dent</td>
<td>42%</td>
</tr>
<tr>
<td colspan='3'>Description: Find travel guide to get me back to earth.</td>
</tr>
</tbody>
<tbody>
<tr>
<td>RD-123</td>
<td>Frodo Baggins</td>
<td>9%</td>
</tr>
<tr>
<td colspan='3'>Description: Find a better way to get the ring to Mordor.</td>
</tr>
</tbody>
<!-- repeat tbody sections as necessary to get onto the second page -->
</table>
</body>
</html>
Here's a JSFiddle that'll give you a bit of an idea of what I'm trying to accomplish.
Edit: I considering not using a table but didn't since (i) I want my columns to line up, and (ii) I really don't want to hard-code column widths to make sure they're all the same.
Try wrapping it all in a
make that specific a block element (http://learnlayout.com/inline-block.html)
then use page-break-*