html center setting

WBOY
Release: 2023-05-21 15:26:37
Original
13032 people have browsed it

HTML center setting

In web design, it is a very common requirement to center text, pictures and other elements. The following will introduce several HTML centering settings.

1. Text centering

You can use the text-align attribute to achieve the effect of horizontal centering of text, as shown in the following code:

<p style="text-align:center;">这是一段居中的文本。</p>
Copy after login

If you want the text to be vertically centered, you can Use the line-height attribute and height attribute, as shown in the following code:

<style>
.container {
  height: 300px;
  line-height: 300px;
  text-align: center;
}
</style>

<div class="container">这是一段居中的文本。</div>
Copy after login

2. Image centering

You can use the margin attribute in CSS to achieve the effect of horizontally centering the image, as shown in the following code :

<style>
.container {
  text-align: center;
}
img {
  margin: 0 auto;
}
</style>

<div class="container">
  <img src="image.jpg" alt="图片">
</div>
Copy after login

If you want the image to be vertically centered, you can use the position attribute and top attribute, as shown in the following code:

<style>
.container {
  position: relative;
  height: 400px;
}
img {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}
</style>

<div class="container">
  <img src="image.jpg" alt="图片">
</div>
Copy after login

3. The container is centered

If you want the entire container to be centered For centering, you can use the display attribute and margin attribute in CSS, as shown in the following code:

<style>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 400px;
}
</style>

<div class="container">
  <p>这是一个居中的容器。</p>
</div>
Copy after login

The above are several commonly used HTML centering settings, which can be used flexibly according to needs.

The above is the detailed content of html center setting. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!