HTML Align Center
Aligning content to the center of a webpage is a common task for web developers. Whether it’s text, images, or other HTML elements, centering can improve readability and aesthetics. This article will explore various methods to center content using HTML and CSS, providing comprehensive examples for each technique.
Using the <center>
Tag
The <center>
tag was once a straightforward way to center content. However, it is now deprecated in HTML5. Despite its deprecation, it’s useful to understand how it was used.
Example 1: Centering Text with <center>
<!DOCTYPE html>
<html>
<head>
<title>Center Example - how2html.com</title>
</head>
<body>
<center>This text is centered using the <center> tag.</center>
</body>
</html>
Output:
Centering with CSS
CSS provides more flexible and recommended methods for centering content. We’ll explore several CSS techniques for centering different types of content.
Example 2: Centering Text Using text-align
<!DOCTYPE html>
<html>
<head>
<style>
.center-text {
text-align: center;
}
</style>
<title>Text Align Center - how2html.com</title>
</head>
<body>
<div class="center-text">This text is centered using CSS.</div>
</body>
</html>
Output:
Example 3: Centering a Block Element Using margin
<!DOCTYPE html>
<html>
<head>
<style>
.center-block {
margin: auto;
width: 50%;
}
</style>
<title>Block Center - how2html.com</title>
</head>
<body>
<div class="center-block">This block is centered using margins.</div>
</body>
</html>
Output:
Example 4: Centering Using Flexbox
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
justify-content: center;
}
</style>
<title>Flexbox Center - how2html.com</title>
</head>
<body>
<div class="flex-container">
<div>This content is centered using Flexbox.</div>
</div>
</body>
</html>
Output:
Example 5: Centering Vertically and Horizontally with Flexbox
<!DOCTYPE html>
<html>
<head>
<style>
.flex-center {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
</style>
<title>Flexbox VH Center - how2html.com</title>
</head>
<body>
<div class="flex-center">
<div>This content is centered both vertically and horizontally using Flexbox.</div>
</div>
</body>
</html>
Output:
Example 6: Centering with Grid
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
place-items: center;
height: 100vh;
}
</style>
<title>Grid Center - how2html.com</title>
</head>
<body>
<div class="grid-container">
<div>This content is centered using CSS Grid.</div>
</div>
</body>
</html>
Output:
Example 7: Centering an Image
<!DOCTYPE html>
<html>
<head>
<style>
.center-image {
display: block;
margin-left: auto;
margin-right: auto;
}
</style>
<title>Image Center - how2html.com</title>
</head>
<body>
<img src="https://how2html.com/images/logo.png" class="center-image" alt="Centered Image">
</body>
</html>
Output:
Example 8: Centering a Table
<!DOCTYPE html>
<html>
<head>
<style>
.center-table {
margin-left: auto;
margin-right: auto;
}
</style>
<title>Table Center - how2html.com</title>
</head>
<body>
<table class="center-table">
<tr>
<td>Centered Table</td>
</tr>
</table>
</body>
</html>
Output:
Example 9: Centering Using Position Absolute
<!DOCTYPE html>
<html>
<head>
<style>
.absolute-center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
<title>Absolute Center - how2html.com</title>
</head>
<body>
<div class="absolute-center">This content is absolutely centered.</div>
</body>
</html>
Example 10: Centering with Inline-Block and Text-Align
<!DOCTYPE html>
<html>
<head>
<style>
.text-align-center {
text-align: center;
}
.inline-block-center {
display: inline-block;
}
</style>
<title>Inline-Block Center - how2html.com</title>
</head>
<body>
<div class="text-align-center">
<div class="inline-block-center">This content is centered using inline-block and text-align.</div>
</div>
</body>
</html>
Output:
Conclusion
Centering content in HTML and CSS is a fundamental skill for web developers. As demonstrated, there are multiple methods to achieve center alignment, each suitable for different scenarios. Whether you’re working with text, images, block elements, or complex layouts, understanding these techniques will enhance your web design and development capabilities. Remember, while the <center>
tag is deprecated, CSS offers powerful and flexible solutions for centering content in modern web development.