How to use text-align property inside a table in CSS ?
Last Updated :
10 Mar, 2023
Text align is a CSS (Cascading Style Sheets) property that is used to specify the horizontal alignment of text within an HTML element. It is commonly used to align text within a block-level element such as a paragraph, heading, or table cell.
The "text-align" property accepts several values.
- left: It aligns the text to the left edge of the element. This will align the text to the left margin of the container that the element is inside, so any extra space will be on the right side
- center: It centers the text horizontally within the element.
- right: It aligns the text to the right edge of the element. This will align the text to the right margin of the container that the element is inside, so any extra space will be on the left side.
- justify: It stretches the lines of text to fill the entire width of the element, creating a clean edge on both sides.
- end: This will align the text to the end of the container that the element is inside, so for left-to-right writing systems, it will appear on the right side of the container, and for right-to-left writing systems, it will appear on the left side of the container.
- start: You can align the text to the start within that element by applying the "text-align: start" property to it in your CSS.
- initial: It is a CSS property that sets the value of the "text-align" property to its initial or default value. The initial value of "text-align" is typically "left", which aligns text or other content to the left side of the container.
- inherit: The "inherit" value is useful when you want an element to inherit a specific property from its parent, rather than setting a new value directly. It can make your CSS more efficient and easier to maintain since you don't have to update the same property on multiple elements if you want to change its value.
- unset: This is a CSS property that resets the value of the "text-align" property to its default value, which is typically "inherit" or "initial", depending on the specific property.
- calc(): The "text-align" property does not accept the "calc()" function as a value.
- var(): The "text-align" property does not accept the "var()" function as a value either, but you can use CSS variables with "text-align" indirectly by defining the value of the variable and then using it as the value of the "text-align" property.
Note: You can apply the "text-align" property to individual elements or to a group of elements using a CSS class.
Insert a table using HTML: Let us first create a simple table in HTML.
Step 1: Use the <table> tag to create a table along with several other elements to define the structure and content of the table.
Step 2: The <tr> element defines a row within the table, and the <th> and <td> elements define header and data cells, respectively.
Example 1: The following code demonstrates the different text alignments using the classes. We define four custom classes as ".center", ".left", ".right", and ".justify". The ".center" class centers text within an element, the ".left" class left aligns text within an element, the ".right" class right aligns text within an element, and the ".justify" class sets stretch the lines of text to fill the entire width of the element, creating a clean edge on both sides.
We then apply these custom classes to our table elements. For example, we apply the .center class to the first header cell. We apply the ".left" class to the second header cell, which left-aligns the text "Name" within that cell. We apply the ".right" class to the third header cell, which right-aligns the text "Price" within that cell.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<style>
/* Define custom classes */
.center {
text-align: center;
}
.left {
text-align: left;
}
.right {
text-align: right;
}
.justify {
text-align: justify;
}
/* Apply custom classes to table elements */
table {
border-collapse: collapse;
width: 50%;
}
th,
td {
padding: 18px;
text-align: left;
border: 1px solid #ddd;
}
th {
background-color: #4CAF50;
color: white;
}
td.center {
text-align: center;
}
td.right {
text-align: right;
}
td.justify {
font-weight: justify;
}
</style>
</head>
<body>
<h1 style="color:green;">GeeksforGeeks</h1>
<h3>Text-align class for inside a table</h3>
<table>
<thead>
<tr>
<th class="center">ID(center-aligned)</th>
<th class="left">Name(left-aligned)</th>
<th class="right">Price(right-aligned)</th>
<th class="justify">Quantity</th>
</tr>
</thead>
<tbody>
<tr>
<td class="center">1</td>
<td class="left">Product A</td>
<td class="right ">100</td>
<td class="justify">34</td>
</tr>
<tr>
<td class="center">2</td>
<td class="left">Product B</td>
<td class="right">50</td>
<td class="justify">56</td>
</tr>
<tr>
<td class="center">3</td>
<td class="left">Product C</td>
<td class="right ">200</td>
<td class="justify">12</td>
</tr>
</tbody>
</table>
</body>
</html>
Output:
Output
Text-align class for inside a table: To align text inside a table cell, you can use the "text-align" property in CSS. You can add this property to the CSS style for the cell, or you can define a class that sets the "text-align" property and apply that class to the appropriate cells. You can see various text-align properties in the following picture.
Text- align inside a table
Example 2: This example uses the "text-align" class to center the content of the "ID" column and right-align the "Salary" column.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Text-align Class Example</title>
<style>
table {
width: 80%;
margin: 0 auto;
border-collapse: collapse;
border: 2px solid green;
}
th,
td {
padding: 10px;
text-align: left;
border: 1px solid green;
background-color: rgba(255, 255, 255, 0.797);
color: green;
}
.center {
text-align: center;
}
.right {
text-align: right;
}
</style>
</head>
<body>
<h1 style="color:green">GeeksforGeeks</h1>
<h3>Text-align class for inside a table</h3>
<table>
<thead>
<tr>
<th class="center">ID</th>
<th class="center">First Name</th>
<th class="center">Last Name</th>
<th class="right">Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td class="center">1</td>
<td>Isha</td>
<td>Patel</td>
<td class="right">$1,50,000</td>
</tr>
<tr>
<td class="center">2</td>
<td>Ravina</td>
<td>Rani</td>
<td class="right">$60,000</td>
</tr>
<tr>
<td class="center">3</td>
<td>Keshav</td>
<td>Anthony</td>
<td class="right">$45,000</td>
</tr>
</tbody>
</table>
</body>
</html>
Output:
Text align example
These were different methods you can add text-align inside a table. You can choose which one works perfectly according to your design.
Similar Reads
How to Center an Image using text-align Property in CSS ?
Aligning an image horizontally within a container can sometimes be challenging. In this article, we will explore how to center an image using the text-align property in CSS. Understanding the text-align PropertyThe text-align property in CSS is used to specify the horizontal alignment of text within
2 min read
How to use text-overflow in a table cell ?
In this article, we will see how to use text-overflow in a table cell. A text-overflow property in CSS is used to specify that some text has overflown and hidden from view. Approach: The white-space property must be set to "nowrap" and the overflow property must be set to "hidden". The overflowing c
2 min read
How to Align Right in a Table Cell using CSS ?
Aligning right in a table cell means positioning the content of the cell to the right side. In CSS, this can be achieved by applying the text-align property to the table cell or using the td selector.Align right in a table cell Using text-align propertyTo align the contents of a table cell to right,
2 min read
How to prevent text in a table cell from wrapping using CSS?
Preventing text from wrapping in a table cell ensures the content stays on one line, maintaining the layout's integrity. Using the CSS property white-space: nowrap;, you can control text behavior, avoiding unwanted line breaks within table cells.Syntax:white-space: normal|nowrap|pre|pre-wrap|pre-lin
2 min read
How to create the tabular layout using CSS Property ?
Tabular layouts are a common requirement for web developers when designing websites. Traditionally, tables were used to create these layouts, but with the advent of CSS, developers now can use more modern approaches like flexbox and grid. This article will discuss how to create tabular layouts using
4 min read
How to vertically align text inside a flexbox using CSS?
Vertically aligning text inside a flexbox involves positioning the text centrally within the vertical space of a flex container. This can be achieved using CSS properties such as align-items and justify-content to control the vertical alignment and centering of the text. Here we have some common app
2 min read
How to Align Text and Icon on the Same Line with Tailwind CSS?
TailwindCSS is a utility-first CSS framework that offers a wide range of classes to help style elements quickly and efficiently. It provides various classes to align text and icons on the same line, ensuring that both elements are visually harmonious and well-positioned. These are the following appr
2 min read
How to set the table layout algorithm using CSS ?
You can set the table layout algorithm using table-layout property which is used to display the layout of the table. In this article, you learn both auto and fixed layout algorithms to form table. Syntax: table-layout: auto|fixed; Approach 1: Using auto layout algorithm. Note: In the auto-layout alg
2 min read
How to align the last line of a paragraph element to the right using CSS?
In this article, we will set the alignment of last paragraph element to the right by using CSS property. To set the alignment of last paragraph to right, we use CSS text-align-last property. The text-align-last is used to set the last line of the paragraph just before the line break. The line break
2 min read
How to put the caption below the table using CSS ?
In this article we are going to learn How to put the caption below the table using CSS, To put the caption below the table we can use the CSS caption-side property. This property is used to specify the position where the table caption is placed. It is used in HTML Tables. This property can be used w
1 min read