I've got a simple table
<table>
<thead>
<tr>
<td>Name</td>
<td>Age</td>
<td>Photo</td>
</tr>
</thead>
</tbody>
<tr>
<td>John Doe</td>
<td>Eighteen</td>
<td>Img</td>
</tr>
<tr>
<td>Peter Stevens</td>
<td>Twenty</td>
<td>Img</td>
</tr>
<tr>
<td>Elizabeth Olsen</td>
<td>Twenty Six</td>
<td>Img</td>
</tr>
</tbody>
</table>
I'd like to hide the photos column if, say, width of the window is less than 1000 px. And I'd like the Age to show as a number ("Eighteen" -> "18") if the width is less than 800 px.
How do I do that? (preferably with HTML + CSS only)
I'd like to hide the photos column if, say, width of the window is less than 1000px.
Use a CSS Media query to check the screen size and hide it if the screen width is less than 1000px.
<td class="photo">Img</td>
#media (max-width:1000px) {
.photo {
display: none
}
}
And I'd like the Age to show as a number ("Eighteen" -> "18") if the width is less than 800px.
This also uses a media query, but it requires a CSS Pseudo Element and an HTML Custom Data Attribute as well.
<td class="age" data-age="18">Eighteen</td>
#media(max-width:800px) {
.age {
font-size: 0;
}
.age:before {
content: attr(data-age);
font-size: 12pt; /* Set this to your desired font size */
}
}
Full Demo:
#media(max-width:1000px) {
.photo {
display: none
}
}
#media(max-width:800px) {
.age {
font-size: 0;
}
.age:before {
content: attr(data-age);
font-size: 12pt; /* Set this to your desired font size */
}
}
<table>
<thead>
<tr>
<td>Name</td>
<td class="age" data-age="?">Age</td>
<td class="photo">Photo</td>
</tr>
</thead>
<tbody>
<tr>
<td>John Doe</td>
<td class="age" data-age="18">Eighteen</td>
<td class="photo">Img</td>
</tr>
<tr>
<td>Peter Stevens</td>
<td class="age" data-age="20">Twenty</td>
<td class="photo">Img</td>
</tr>
<tr>
<td>Elizabeth Olsen</td>
<td class="age" data-age="26">Twenty Six</td>
<td class="photo">Img</td>
</tr>
</tbody>
</table>
To hide photos column in window width < 1000px
simply apply media query like that:
#media(max-width : 1000px) {
td:nth-child(3) {
display:none;
}
}
To display age as number in window width < 800px
you'll need to add the age in numbers somewhere in HTML, so the table row may look like that
<tr>
<td>John Doe</td>
<td>
<span class="age-in-text">Eighteen</span>
<span class="age-in-numbers">18</span>
</td>
<td>Img</td>
</tr>
then you set your style / media query simply like:
.age-in-numbers {
display:none;
}
.age-in-text {
display:block;
}
#media(max-width : 800px) {
.age-in-numbers {
display:block;
}
.age-in-text {
display:none;
}
}
You can use a #media query to write CSS that only applies to screens of certain sizes.
Hiding the column is quite easy. You can just hide the nth-child. td:nth-child(3) matches each third cell.
Hiding the text is a bit harder. CSS cannot transform written text to numbers like that, but in your server side script (PHP?) you may be able to add those numbers already. Now, you could add an extra column with the numeric age, and hide one when you show the other, but personally I think it's a neat trick to add the age as an attribute, and use CSS to display the numeric age instead of the textual content.
Use can use attr() to get the attribute of an element. In the snippet below, I 'hide' the textual content by making the font size 0. Then, I show the data-age attribute in the ::before pseudo element.
The advantage of this method is that it requires no significant changes to the HTML. Semantically it's still just a simple table.
#media (max-width: 800px) {
/* Hide the third column */
td:nth-child(3) {
display: none;
}
/* Hide the text in the second column by setting the font-size to 0 */
td:nth-child(2) {
font-size: 0;
}
/* Show the numeric value from the attribute instead. */
td:nth-child(2)::before {
font-size: 17px;
content: attr(data-age);
}
}
<table>
<thead>
<tr>
<td>Name</td>
<td>Age</td>
<td>Photo</td>
</tr>
</thead>
<tbody>
<tr>
<td>John Doe</td>
<td data-age="18">Eighteen</td>
<td>Img</td>
</tr>
<tr>
<td>Peter Stevens</td>
<td data-age="20">Twenty</td>
<td>Img</td>
</tr>
<tr>
<td>Elizabeth Olsen</td>
<td data-age="26">Twenty Six</td>
<td>Img</td>
</tr>
</tbody>
</table>
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 want to add a class to a parent DIV that affects lots of inner elemnets. But when I do this with Bootstrap, the styling gets messed up.
This code shows the problem on 2 where you can see the bottom border is missaligned. If I just do a .toggle(".second") on the element instead, it will work.
https://www.bootply.com/KOJ4VKNbOa
.second {
display: none;
}
.show-inner .second {
display: block;
}
<div id="outer">
<table class="table">
<thead>
<tr>
<th class="first">A</th>
<th class="second">B</th>
<th class="third">C</th>
</tr>
</thead>
<tbody>
<tr>
<td width="98%" class="first">1</td>
<td width="1%" class="second">2</td>
<td width="1%" class="third">3</td>
</tr>
</tbody>
</table>
</div>
<button onclick="$('#outer').toggleClass('show-inner');">Toggle .second</button>
In my project I would like to have many dependencies of that outer DIV class, so getting it to work would save me lots of code.
use display: table-cell instead of block.
.show-inner .second {
display: table-cell;
}
I have class definition:
.small, td.small, td.small > nobr, td.small > a
{
font-size: 90%;
}
The purpose is to make text smaller. That should be applied to anything: text in anchor, text in cell, etc.
But in fact, style is applied TWICE if anchor is inside of the cell:
<table>
<tbody>
<tr>
<td class="small">
VERY small content
</td>
<td class="small">Smaller text - looks as required</td>
</tr>
</tbody>
</table>
Why? How to make sure that style is applied only once?
Thank you.
Just remove the last part of the style, td.small > a. Then it will get applied to everything inside the <td>. Note, I changed the size of the font to 60% just so that the size change is apparent.
.small, td.small, td.small > nobr
{
font-size: 60%;
}
<table>
<tbody>
<tr>
<td class="small">
Small content
</td>
<td class="small">Should be smaller as well</td>
</tr>
</tbody>
</table>
I'm trying to show movie info for a website, but I'm having issues with the CSS and HTML. I want the title (Director:, Cast:, etc) to be in line with the first item in the list, and then have all others follow below. I tried a definition list, but couldn't figure out the css to make it responsive. Now I have a table, but I'm starting to suspect a regular list might be better.
Here is my fiddle which looks how I want it to when the window is wide enough, but gets ruined when shrunk down to mobile (which is when I want the Cast: columns to become one column)
Any help would be super appreciated, I've been playing with this for months.
Here's my html:
<div class="movieinfo">
<table>
<tr class="spaceUnder">
<td class="workheader">Director:</td>
<td>John Carney</td>
</tr>
<tr class="spaceUnder">
<td class="workheader">Writer:</td>
<td>John Carney</td>
</tr>
<tr>
<td class="workheader">Cast:</td>
<td>Keira Knightly</td>
<td class="spacedRight">James Corden</td>
</tr>
<tr>
<td></td>
<td>Mark Ruffalo</td>
<td class="spacedRight">CeeLo Green</td>
</tr>
<tr>
<td></td>
<td>Hailee Steinfeld</td>
<td class="spacedRight">Catherine Keener</td>
</tr>
<td></td>
<td>Adam Levine</td>
</table>
</div>
and my CSS:
.movieinfo {
color: #333333;
font-size: 24px;
line-height: 1.1;
font-weight: 300;
padding: 20px;
background-color: rgba(255,255,255,.7);
-moz-border-radius: 20px;
-webkit-border-radius: 20px;
border-radius: 20px;
margin-bottom: 20px;
}
You are partially misusing the table for layout here by putting the cast names in separate cells. Don't let your design requirements influence your decision on how to markup the data. This includes using appropriate HTML elements such as <th> for header cells:
<div class="movieinfo">
<table>
<tr class="spaceUnder">
<th>Director:</th>
<td>John Carney</td>
</tr>
<tr class="spaceUnder">
<th>Writer:</th>
<td>John Carney</td>
</tr>
<tr>
<th>Cast:</th>
<td>
<ul>
<li>Keira Knightly</li>
<li>James Corden</li>
<li>Mark Ruffalo</li>
<li>CeeLo Green</li>
<li>Hailee Steinfeld</li>
<li>Catherine Keener</li>
<li>Adam Levine</li>
</ul>
</td>
</tr>
</table>
</div>
To format the cast in columns there are two options depending on if you want to order the items primely vertically or horizontally. For vertical ordering use columns:
.movieinfo ul {
-webkit-columns: 3 10em;
-moz-columns: 3 10em;
columns: 3 10em;
}
EDIT: When using both a width and a number form columns, then the number is the maxmium number of columns displayed. The gap between the columns can be set with column-gap.
More complete example: http://jsfiddle.net/owdz2m0m/
Or for horizontal use floats:
.movieinfo ul > li {
float: left;
width: 10em;
}
EDIT: Limiting the number of columns with floats is a bit trickier. You'll need to set the max-width of the surrounding element (in this case the ul) to the "column width * maximum Number of columns". So to limit the columns to 2 set .movieinfo ul { max-width: 20em; }
More complete example: http://jsfiddle.net/tr9e8bmv/
In either case you can use media queries at adjust the widths depending on screen size.
I think, You need #media queries. Something like this:
#media only screen and (max-width: 640px){ your style }
And You can build your HTML width <dl> and <ul>
DEMO
If You change window size on demo, You can see how 2 columns become 1 column. Screen 1, Screen 2
I have an html table with table-layout: fixed and a td with a set width. The column still expands to hold the contents of text that doesn't contain a space. Is there a way to fix this other than wrapping the contents of each td in a div?
Example: http://jsfiddle.net/6p9K3/29/
<table>
<tbody>
<tr>
<td style="width: 50px;">Test</td>
<td>Testing 1123455</td>
</tr><tr>
<td style="width: 50px;">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</td>
<td>B</td>
</tr>
</tbody>
</table>
table
{
table-layout: fixed;
}
td
{
border: 1px solid green;
overflow: hidden;
}
In the example, you can see that the column with AAAAAAAAAAAA... expands despite being explicitly set to 50px wide.
Specify the width of the table:
table
{
table-layout: fixed;
width: 100px;
}
See jsFiddle
Try looking into the following CSS:
word-wrap:break-word;
Web browsers should not break-up "words" by default so what you are experiencing is normal behaviour of a browser. However you can override this with the word-wrap CSS directive.
You would need to set a width on the overall table then a width on the columns. "width:100%;" should also be OK depending on your requirements.
Using word-wrap may not be what you want however it is useful for showing all of the data without deforming the layout.
Make the table rock solid BEFORE the css. Figure your width of the table, then use a 'controlling' row whereby each td has an explicit width, all of which add up to the width in the table tag.
Having to do hundreds html emails to work everywhere, using the correct HTML first, then styling w/css will work around many issues in all IE's, webkit's and mozillas.
so:
<table width="300" cellspacing="0" cellpadding="0">
<tr>
<td width="50"></td>
<td width="100"></td>
<td width="150"></td>
</tr>
<tr>
<td>your stuff</td>
<td>your stuff</td>
<td>your stuff</td>
</tr>
</table>
Will keep a table at 300px wide. Watch images that are larger than the width by extremes
You can add a div to the td, then style that. It should work as you expected.
<td><div>AAAAAAAAAAAAAAAAAAA</div></td>
Then the css.
td div { width: 50px; overflow: hidden; }
You can also use percentages, and/or specify in the column headers:
<table width="300">
<tr>
<th width="20%">Column 1</th>
<th width="20%">Column 2</th>
<th width="20%">Column 3</th>
<th width="20%">Column 4</th>
<th width="20%">Column 5</th>
</tr>
<tr>
<!--- row data -->
</tr>
</table>
The bonus with percentages is lower code maintenance: you can change your table width without having to re-specify the column widths.
Caveat: It is my understanding that table width specified in pixels isn't supported in HTML 5; you need to use CSS instead.
You can also work with "overflow: hidden" or "overflow-x: hidden" (for just the width). This requires a defined width (and/or height?) and maybe a "display: block" as well.
"Overflow:Hidden" hides the whole content, which does not fit into the defined box.
Example:
http://jsfiddle.net/NAJvp/
HTML:
<table border="1">
<tr>
<td><div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div></td>
<td>bbb</td>
<td>cccc</td>
</tr>
</table>
CSS:
td div { width: 100px; overflow-y: hidden; }
EDIT: Shame on me, I've seen, you already use "overflow". I guess it doesn't work, because you don't set "display: block" to your element ...
I would try setting it to:
max-width: 50px;
This works for me
td::after {
content: '';
display: block;
width: 30px;
}