> > ## This article is from [this link](http://mobifreaks.com/user-interface/responsive-and-seo-friendly-data-tables). This is a [demo](http://www.mobifreaks.com/wp-content/demos/Responsive-and-SEO-Friendly-Data-Tables/). > >
There was a time when we all used tables to design whole layout, later we started using div to style the layout.
It is because of search engine robot. As it divides the table into three parts like header, content and footer and only follows the data inside the content. That’s what I learned when I was using tables to create layouts.
Even though usage of tables is still a need to maintain data sheets, we have come up with simple table design. Which creates a table without using html elements like table, td, tr etc.
SEO Friendly Table
You might be thinking what element we would be using. It’s div element styled by CSS.
Before creating a table we need to know the basic CSS styling of the table elements.
Default CSS for table and its elements
-
table{
-
` display:table; /* — Defines a table — */`
-
}
-
tr{
-
` display:table-row; /* — Defines a table row — */`
-
}
-
thead{
-
` display:table-header-group; /* — Defines a table header group — */`
-
}
-
tbody{
-
` display:table-row-group; /* — Defines a table row group — */`
-
}
-
tfoot{
-
` display:table-footer-group; /* — Defines a table footer group — */`
-
}
-
col{
-
` display:table-column; /* — Defines a table column — */`
-
}
-
colgroup{
-
` display:table-column-group; /* — Defines a table column group — */`
-
}
-
td,
-
th{
-
` display:table-cell; /* — Defines a table cell — */`
-
}
-
caption{
-
` display:table-caption; /* — Defines a table caption — */`
-
}
Now from the above code we will be using table, tr, td and thead to create few classes
-
.table{
-
` display:table;`
-
}
-
.table-head{
-
` display:table-header-group;`
-
}
-
.row{
-
` display:table-row;`
-
}
-
.column{
-
` display:table-cell;`
-
}
By using the classes we will be creating tables which are SEO friendly.
Sleek CSS table
-
.table{
-
` display:table; /* Defines a Table */`
-
` font-family:Arial, Helvetica, sans-serif;`
-
` font-size:14px;`
-
` border-bottom:2px solid #dddddd;`
-
` color:#8d8d8d;`
-
` margin:10px 0;`
-
}
-
.table-head{
-
` display: table-header-group; /* Defines a table header group */`
-
}
-
.table-head .column{ /* Column inside the table-head */
-
` background:#333333;`
-
` color:#7d7d7d;`
-
` border-right:1px solid #5d5d5d;`
-
` border-bottom:none;`
-
}
-
.table-head .column:hover{ /* Column hover inside the table-head */
-
` background:#222222;`
-
}
-
.row{
-
` display:table-row; /* Defines a table row */`
-
}
-
.row .column:nth-child(1){ /* First column in a row */
-
` border-left:1px solid #eeeeee;`
-
}
-
.row:last-child .column{ /* column in a last row */
-
` border-bottom:none;`
-
}
-
.column{
-
` display:table-cell; /* Defines a table cell */`
-
` padding:10px 20px;`
-
` border-bottom:1px solid #eeeeee;`
-
` border-right:1px solid #eeeeee;`
-
` line-height:1.8em;`
-
}
-
.column:hover{
-
` background:#f9f9f9;`
-
}
Bare bone mark up for the table
-
<div class="table">
-
` <div class="table-head">`
-
` <div class="column" data-label=""></div>`
-
` </div>`
-
` <div class="row">`
-
` <div class="column" data-label=""></div>`
-
` </div>`
-
</div>
Bare bone markup consists of
-
.table which defines a table.
-
.table-head which defines a table header group not necessary unless you have some headings for your table.
-
.column which defines a table cell.
-
.row which defines a table row.
-
Here we have assigned a new attribute called data-label, which defines the label for the current column (will be discussed later in responsive tables)
Complete markup for the table. In this case we will create a data sheet for employer information.
-
<div class="table">
-
` <div class="table-head">`
-
` <div class="column" data-label="Employee No">Employee No</div>`
-
` <div class="column" data-label="User Name">User Name</div>`
-
` <div class="column" data-label="Real Name">Real Name</div>`
-
` <div class="column" data-label="Age">Age</div>`
-
` <div class="column" data-label="Profession">Profession</div>`
-
` <div class="column" data-label="Contact">Contact</div>`
-
` </div>`
-
` <div class="row">`
-
` <div class="column" data-label="Employee No">2311</div>`
-
` <div class="column" data-label="User Name">Lorem</div>`
-
` <div class="column" data-label="Real Name">Ipsum dolor</div>`
-
` <div class="column" data-label="Age">22</div>`
-
` <div class="column" data-label="Profession">Web designer</div>`
-
` <div class="column" data-label="Contact">1234567890</div>`
-
` </div>`
-
` <div class="row">`
-
` <div class="column" data-label="Employee No">4542</div>`
-
` <div class="column" data-label="User Name">Viverra</div>`
-
` <div class="column" data-label="Real Name">Nam non viverra</div>`
-
` <div class="column" data-label="Age">22</div>`
-
` <div class="column" data-label="Profession">Web developer</div>`
-
` <div class="column" data-label="Contact">2345678901</div>`
-
` </div>`
-
` <div class="row">`
-
` <div class="column" data-label="Employee No">5643</div>`
-
` <div class="column" data-label="User Name">Sed</div>`
-
` <div class="column" data-label="Real Name">Donec sed volutpat</div>`
-
` <div class="column" data-label="Age">25</div>`
-
` <div class="column" data-label="Profession">Editor</div>`
-
` <div class="column" data-label="Contact">3456789012</div>`
-
` </div>`
-
` <div class="row">`
-
` <div class="column" data-label="Employee No">5464</div>`
-
` <div class="column" data-label="User Name">Orci</div>`
-
` <div class="column" data-label="Real Name">Phasellus eu orci nunc</div>`
-
` <div class="column" data-label="Age">32</div>`
-
` <div class="column" data-label="Profession">User Interface</div>`
-
` <div class="column" data-label="Contact">4567890123</div>`
-
` </div>`
-
</div>
Responsive Table
We will use @media of CSS3 to convert our table into responsive. When screen is resized the mobile friendly table is turned on.
-
/* Responsive table */
-
@media all and (max-width: 640px){
-
` .table,`
-
` .row,`
-
` .column,`
-
` .column:before{`
-
` display:block; /* Converts a table, table row, table column and table column:before into a block element */`
-
` }`
-
` .table,`
-
` .row .column:last-child{`
-
` border-bottom:none;`
-
` }`
-
` .table-head{`
-
` position:absolute; /* Hides table head but not using display none */`
-
` top:-1000em;`
-
` left:-1000em;`
-
` }`
-
` .row{`
-
` border:1px solid #eeeeee;`
-
` border-top:2px solid #dddddd;`
-
` border-bottom:2px solid #dddddd;`
-
` margin:20px 0;`
-
` }`
-
` .row .column:nth-child(1){ /* first column of the row */`
-
` border-left:none;`
-
` }`
-
` .row .column:last-child{ /* last column of the row */`
-
` border-right:none;`
-
` }`
-
` .row:last-child .column,`
-
` .column{ /* Column in the last row and column */`
-
` border-bottom:1px solid #eeeeee;`
-
` }`
-
` .column:before{ /* prints the value of data-label attribute before the column data */`
-
` font-weight:bold;`
-
` padding-right:20px;`
-
` font-size:12px;`
-
` content:” “attr(data-label)” //”; /* call the attribute value of data-label and adds a string // */`
-
` }`
-
}
Everything setup hope you enjoyed our tutorial.Grab the source and feel free to check out the demo
Reference : Css-tricks and Table Standards
-
上一篇
Using the viewport meta tag to control layout on mobile browsers-在移动浏览器中使用viewport元标签控制布局 -
下一篇
Truncate String with Ellipsis 用省略号缩短文本长度