how to draw excel like table in html, give example code
- User
how to draw excel like table in html, give example code
- Assistant
To draw an Excel-like table in HTML, you can use the
<table>element along with the appropriate tags for headers, rows, and cells. Here's an example code to create a basic table:<!DOCTYPE html> <html> <head> <style> table { border-collapse: collapse; width: 100%; } th, td { text-align: left; padding: 8px; border-bottom: 1px solid #ddd; } th { background-color: #f2f2f2; } </style> </head> <body> <table> <tr> <th>Product</th> <th>Price</th> <th>Quantity</th> </tr> <tr> <td>Product 1</td> <td>$10</td> <td>5</td> </tr> <tr> <td>Product 2</td> <td>$20</td> <td>3</td> </tr> <tr> <td>Product 3</td> <td>$15</td> <td>2</td> </tr> </table> </body> </html>This code defines a table with three columns: Product, Price, and Quantity. The
<tr>tags represent table rows, while the<th>tags represent table headers (column headers). The<td>tags within the rows represent table cells.The CSS styles are added within the
<style>tags to give the table a clean and professional appearance. The property is used to merge the borders of adjacent cells, and other styling properties like , , and are used to improve the aesthetics. - User
now i want to change width of middle cell of second row and lower third cell of second row
- Assistant
To change the width of the middle cell in the second row and the lower third cell in the second row, you can use the
colspanattribute and CSS styles. Here's an updated example code:<!DOCTYPE html> <html> <head> <style> table { border-collapse: collapse; width: 100%; } th, td { text-align: left; padding: 8px; border-bottom: 1px solid #ddd; } th { background-color: #f2f2f2; } td.middle-cell { width: 40%; /* Adjust the width as desired */ } td.lower-cell { width: 60%; /* Adjust the width as desired */ } </style> </head> <body> <table> <tr> <th>Product</th> <th>Price</th> <th>Quantity</th> </tr> <tr> <td>Product 1</td> <td class="middle-cell">$10</td> <td>5</td> </tr> <tr> <td>Product 2</td> <td colspan="2" class="lower-cell">$20</td> <!-- colspan attribute makes the cell span multiple columns --> </tr> <tr> <td>Product 3</td> <td>$15</td> <td>2</td> </tr> </table> </body> </html>In this updated code, two CSS classes have been added: and . The class sets the width of the middle cell in the second row to 40%, while the class sets the width of the lower third cell in the second row to 60%.