Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




HTML - Table Border


So, in the previous example, we were able to represent the below table.


But the table didn't have borders around it.


Let us see in the below example, how can we draw borders around the table.


How to create a table in HTML with border?


To create a table in HTML with border, we need to set the border attribute of the table element with a numeric value.


Confused?


Let us simplify with the below example.


Example :



<html>	
<body>
	<table border = "1">
  		<tr>
    		<th>Name</th>
    		<th>Class</th>
    		<th>Roll</th>
  		</tr>
  		<tr>
    		<td>John</td>
    		<td>5</td>
    		<td>1</td>
  		</tr>
  		<tr>
    		<td>Paul</td>
    		<td>7</td>
    		<td>12</td>
  		</tr>
  		<tr>
    		<td>Mohan</td>
    		<td>3</td>
    		<td>21</td>
  		</tr>
	</table>
</body>
</html>


Output :




So, in the above example, we have set the border property of the table element to 1.


<table border = "1">

And we get the desired border around the table.


Now, to get more control over the border, we need to make use of the CSS property.


How to create a table in HTML with border using CSS properties?


To get more control over the border, we need to add the CSS property called border for table, th and td elements.


This will ensure that the border is wrapped around table, th and td elements.


Let us see it in action with the below example.


Example :



<html>
<style>
	table, th, td {
  		border:1px solid black;
	}
</style>	
<body>
	<table>
  		<tr>
    		<th>Name</th>
    		<th>Class</th>
    		<th>Roll</th>
  		</tr>
  		<tr>
    		<td>John</td>
    		<td>5</td>
    		<td>1</td>
  		</tr>
  		<tr>
    		<td>Paul</td>
    		<td>7</td>
    		<td>12</td>
  		</tr>
  		<tr>
    		<td>Mohan</td>
    		<td>3</td>
    		<td>21</td>
  		</tr>
	</table>
</body>
</html>


Output :




So, if you see the above output, the border is drawn around the entire table. To get the border, we have set the border property for table, th and td elements.


<style>
	table, th, td {
		border:1px solid black;
	}
</style>

Now, if you look at the values for the border property, they are 1px, solid and black.

  1. 1px



    Says the size of the border.

  2. solid



    Says the border not have any dots and be represented as straight line(We will learn more about this later in this tutorial).

  3. black



    Says the border would be black in color.

Although we got the border we have desired, however the border has double lines. Let us see how can we get rid of it using the border-collapse property.


border-collapse in HTML


The border-collapse property in HTML is used to get rid of the double border.


Example :



<html>
<style>
	table, th, td {
  		border:1px solid black;
  		border-collapse:collapse;
	}
</style>	
<body>
	<table>
  		<tr>
    		<th>Name</th>
    		<th>Class</th>
    		<th>Roll</th>
  		</tr>
  		<tr>
    		<td>John</td>
    		<td>5</td>
    		<td>1</td>
  		</tr>
  		<tr>
    		<td>Paul</td>
    		<td>7</td>
    		<td>12</td>
  		</tr>
  		<tr>
    		<td>Mohan</td>
    		<td>3</td>
    		<td>21</td>
  		</tr>
	</table>
</body>
</html>


Output :




So, if you look at the above code, we just got rid of the double border by setting the border-collapse property to collapse.


table, th, td {
	border:1px solid black;
	border-collapse:collapse;
}

In the previous example we have seen that there is a solid border or the border is drawn with straight line.


border:1px solid black;

Let us see next, how we can draw a dotted border.


Example :



<html>
<style>
	table, th, td {
  		border:1px dotted black;
	}
</style>	
<body>
	<table>
  		<tr>
    		<th>Name</th>
    		<th>Class</th>
    		<th>Roll</th>
  		</tr>
  		<tr>
    		<td>John</td>
    		<td>5</td>
    		<td>1</td>
  		</tr>
  		<tr>
    		<td>Paul</td>
    		<td>7</td>
    		<td>12</td>
  		</tr>
  		<tr>
    		<td>Mohan</td>
    		<td>3</td>
    		<td>21</td>
  		</tr>
	</table>
</body>
</html>


Output :




So, if you see the above output, a dotted border is drawn around the table. That is because we have set the value of the border property to dotted.


table, th, td {
	border:1px dotted black;
}
Spring_Boot


Alternatively, we can draw a dotted border using the border-style property.


border-style in HTML


Example :



<html>
<style>
	table, th, td {
  		border-style:dotted;
	}
</style>	
<body>
	<table>
  		<tr>
    		<th>Name</th>
    		<th>Class</th>
    		<th>Roll</th>
  		</tr>
  		<tr>
    		<td>John</td>
    		<td>5</td>
    		<td>1</td>
  		</tr>
  		<tr>
    		<td>Paul</td>
    		<td>7</td>
    		<td>12</td>
  		</tr>
  		<tr>
    		<td>Mohan</td>
    		<td>3</td>
    		<td>21</td>
  		</tr>
	</table>
</body>
</html>


Output :




So, in the above example, we have changed the border-style property to dotted and a dotted border is drawn around the table.


The border-style property also has other values that sets different styles for the border.

  1. border-style : dotted;

  2. border-style : solid;

  3. border-style : dashed;

  4. border-style : groove;

  5. border-style : double;

  6. border-style : ridge;

  7. border-style : inset;

  8. border-style : outset;

You can try the above on your own and see how it works.


Similarly, you can also change the color of the border using the border-color property.


border-color in HTML


We can set the color of the border using the border-color property.


Example :



<html>
<style>
	table, th, td {
		border-style:dotted;
  		border-color:red;
	}
</style>	
<body>
	<table>
  		<tr>
    		<th>Name</th>
    		<th>Class</th>
    		<th>Roll</th>
  		</tr>
  		<tr>
    		<td>John</td>
    		<td>5</td>
    		<td>1</td>
  		</tr>
  		<tr>
    		<td>Paul</td>
    		<td>7</td>
    		<td>12</td>
  		</tr>
  		<tr>
    		<td>Mohan</td>
    		<td>3</td>
    		<td>21</td>
  		</tr>
	</table>
</body>
</html>


Output :




In the above example, we have set the border-color property to red. And the color of the border is red.