Skip to main content

Organizing Data In Table: A Quick Guide

 

Organizing Data In Table: A Quick Guide




We can use tables to structure data in columns and rows. The table is the HTML way to lay out the data. The CSS way to create the layout on the web page is CSS floatflexbox, and CSS grid.

We cover an example to understand how to create a table on the web page. You can view the HTML table example at the below codepen link:

https://codepen.io/taimoorsattar/pen/NWpdwbp

For example, we can create a table in HTML for customer’s grocery item bill as below:

<table border="3" cellpadding="10" cellspacing="0">
   <caption>Grocery Items Bill</caption>
   <thead>
      <colgroup>
         <col width="60%">
         <col width="20%">
         <col width="20%" span="1" style="background-color:#f1f1f1;">
      </colgroup>
      <tr>
         <th align="left" class="col-item-name">Item Name</th>
         <th align="center" class="col-quantity">Quantity</th>
         <th align="center" class="col-price">Price</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td>Potatoes</td>
         <td align="center">51</td>
         <td align="center">$1.00</td>
      </tr>
      <tr>
         <td>Nuts</td>
         <td align="center">20</td>
         <td align="center">$5</td>
      </tr>
      <tr>
         <td>Onions</td>
         <td align="center">4</td>
         <td align="center">$3.00</td>
      </tr>
      <tr>
         <td>Very long awkwardly named yet still delicious item here</td>
         <td align="center">4</td>
         <td align="center">$3.00</td>
      </tr>
      <tr>
         <td>Carrots</td>
         <td align="center">12</td>
         <td align="center">$2.99</td>
      </tr>
   </tbody>
   <tfoot>
      <td class="price_txt" scope="col" colspan="2">Total Price</td>
      <td align="center" >$33.79</td>
   </tfoot>
</table>

The above code creates an HTML table on the page (without CSS) as below:

To structure the HTML table, we have to use proper tags and attributes in the code. Some of the HTML tags that we can use in the table are described below.



Also, in the code, we use attributes to assign properties for the HTML table. Some of the attributes are described below.


Style the HTML

To style the Grocery Items Bill table, we can use the below CSS.

caption {
	font-size: 1.5rem;
	margin-bottom: 1.2rem;
}

table {
	table-layout: auto;
	border-spacing: 0; /*  Same as cellspacing="0" */
	border-collapse: collapse;
	width: 450px;
	margin: 40px auto;
}

table th,
table td {
	border: 1px solid black;
	vertical-align: top;
}

/* No need for this */
.col-item-name {
	width: 60%;
}

/* No need for this */
.col-quantity,
.col-price {
	width: 20%;
}

table th {
	background-color: #869960;
	color: #fff;
	text-shadow: 1px 1px 0 rgba(0, 0, 0, 0.4);
}

table tbody tr:nth-child(even) td {
	background-color: #dcdcdc;
}

.price_txt {
	text-align: right;
	font-weight: bold;
}

Rather than, using CSS to adjust the spacing of the table (e.g. CSS box model), we already specify it using HTML attributes.

Note that due to compatibility issue, if some of the attributes is not supported, we can using CSS to style elements. In the above example, we use both CSS and HTML attributes to assign properties/styles to elements.

Also, in the above CSS, we use the nth-child pseudo-selector to target/style even table rows.








Comments

Popular posts from this blog

Detecting The User's Color Scheme Preference With CSS

Detecting The User's Color Scheme Preference With CSS If you’re a developer, chances are that you use dark mode on your machine and code editor. If not, what are you waiting for? Join the dark side! Jokes apart, it is common nowadays to allow users to select a different theme when visiting a website. Now you can do this with CSS only, not the theme selection itself, for that you still need JS but with CSS you can now detect the user’s machine color scheme (light or dark) and display the correct colors on your website immediately. To do this we need to use the CSS variables. According to the website  Can I use , the “CSS variables” feature is available on 95% of the currently used browsers around the world. We also need to use the  prefers-color-scheme  media query, which according to  Can I use  is supported by about 90% of the currently used browsers. In this article, I will show you how to use the CSS variables and the  prefers-color-scheme  to setup the default colors of the web

Top 10 Python Frameworks Ranked on Github

  Top 10 Python Frameworks Ranked on Github A framework is a library that makes building web applications easier. Frameworks provide a structure for developers so they can focus more on the business logic of their applications. Python has one of the largest ecosystems with a great number of different libraries, frameworks, and tools for developers. Let's take a look at the best Python  frameworks for web development . Here is a list of the ten highest-ranked Python frameworks  on GitHub. Django    Django is an open-source framework that makes things very fast and scalable. It enables programmers to develop apps and websites of different complexity within a short time. Django's advantages are reusability of components, less code, low coupling, and a principle of not repeating. Moreover, the framework is easy to use. You don't need special tools to develop a Django project. You can write it in a typical text editor, such as notepad. In this way, Django is easy to learn for be

1.Change windows password without knowing the existing password

We all know that to change password in windows we need to enter the existing password and then we can proceed to enter the new password.However there is a trick to change password without knowing the existing password. This trick comes handy when the system is logged in and we have forgotten the password or to pull a prank on your friend(do it at your own risk) Step 1: Right click on computer and select manage Step 2: click on Local users and Groups and click on users. On the right pane you will see list of users. step 3: Right click on any user whose password you want to change and click on set password: step 4: click on proceed(Don’t worry about the message) step 5: Enter the new password step 6: Click on ok and your password will change. You can even use the same method to change HOMEGROUP password. (NOTE: This trick works on WIN 7, 8 , 8.1 ,10)