CSS(层叠样式表)中的文本对齐方式用于控制文本在元素内的水平对齐。常见的文本对齐方式包括左对齐、右对齐、居中对齐和两端对齐。
text-align: left;
)text-align: right;
)text-align: center;
)text-align: justify;
)<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Text Alignment Example</title>
<style>
.left-align {
text-align: left;
}
.right-align {
text-align: right;
}
.center-align {
text-align: center;
}
.justify-align {
text-align: justify;
}
</style>
</head>
<body>
<div class="left-align">
This text is left-aligned.
</div>
<div class="right-align">
This text is right-aligned.
</div>
<div class="center-align">
This text is center-aligned.
</div>
<div class="justify-align">
This text is justified. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</body>
</html>
text-align
属性设置一致。display: block;
或 display: inline-block;
确保元素有明确的宽度。通过以上方法,可以有效地控制文本的对齐方式,提升页面的美观性和可读性。
领取专属 10元无门槛券
手把手带您无忧上云