Table of Contents
About using css to implement text and imagesVertical and horizontal centering" >About using css to implement text and imagesVertical and horizontal centering
Home Web Front-end CSS Tutorial About using css to center text and images vertically and horizontally

About using css to center text and images vertically and horizontally

Jun 06, 2017 pm 01:25 PM

About using css to implement text and imagesVertical and horizontal centering

##I have always believed that a good memory is not as good as a bad writing. Recently, I have encountered a lot of vertical centering. I will sort it out for future reference.

1. Center the text vertically and horizontally

1. Center the text horizontally:

There is nothing to say about horizontally centering text. It can be easily achieved by using text-align: center;.


2. Vertical centering:

1) Simple single line text 

 Use line-height==height to make single line text Centered vertically.

1 <p>
2     垂直水平居中
3 </p>
Copy after login
1 p{
2     width: 200px;
3     height: 200px
4     text-align: center;
5     line-height: 200px;
6     background:#1AFF00;7 }
Copy after login
Simply put, just use the p tag, like this

<p>垂直水平居中</p>
Copy after login

##

1 p{
2     width: 200px;
3     height: 200px;
4     text-align: center;
5     line-height: 200px;
6     background:#00ABFF;7 
}
Copy after login

The effect is as follows:

​​ ##Attributes of elements, block-level parent elements

display

: table;Inline elements

display: table-cell;
vertical-align

: mid

dl

e; (inline)

1 <p
2     <span>国际《儿童权利公约》界定的儿童系指18岁以下的任何人</span>
3 </p>
Copy after login
 1 p{ 
 2     width: 200px; 
 3     height: 200px; 
 4     display: table; 
 5     background:#1AFF00; 
 6 } 
 7 span{ 
 8     display: table-cell; 
 9     vertical-align: middle;10 }
Copy after login

(block level)

1 <p>
2     <p>国际《儿童权利公约》界定的儿童系指18岁以下的任何人</p>
3 </p>
Copy after login
Positioning+transform: translateY(-50% ); ##
 1 p{ 
 2     position: relative; 
 3     width: 200px; 
 4     height: 200px; 
 5     background: #00ABFF; 
 6 } 
 7 p{ 
 8     position: absolute; 
 9     top: 50%;
 10     left: 0;
 11     width: 200px;
 12     height: 64px;
 13     transform: translateY(-50%);14 }
Copy after login
Positioning+marginnegative value
 1 p{ 
 2     position: relative; 
 3     width: 200px; 
 4     height: 200px; 
 5     background:#1AFF00; 
 6 } 
 7 p{ 
 8     position: absolute; 
 9     top: 50%;
 10     left: 0;
 11     width: 200px;
 12     height: 64px;
 13     margin-top: -32px;
 14 }
Copy after login
Positioning+margin: auto;

 1 p{ 
 2     position: relative; 
 3     width: 200px; 
 4     height: 200px; 
 5     background:#00ABFF; 
 6 } 
 7 p{ 
 8     position: absolute; 
 9     top: 0;
 10     left: 0;
 11     right: 0;
 12     bottom: 0;
 13     margin: auto;
 14     width: 200px;
 15     height: 64px;
 16 }
Copy after login

Two Both have fixed width and fixed height, and the parent element uses the

padding

value, which is the height of the parent element minus half the height of the child element

 1 p{ 
 2     width: 200px; 
 3     height: 64px; 
 4     padding: 68px 0; 
 5     background:#1AFF00; 
 6 } 
 7 p{ 
 8     width: 200px; 
 9     height: 64px;
 10 }
Copy after login

Elements use overflow: hidden; (css hack) child elements use margin value. This value is the height of the parent element minus half of the height of the child element.

p{
    width: 200px;
    height: 200px;
    overflow: hidden;
    background:#00ABFF;
}
p{
    width: 200px;
    height: 64px;
    margin:68px auto;
}
Copy after login

The effect is as follows:

 

##2. Center the image vertically and horizontally

1. Horizontal centering


 

1) img is inline-block in the initial setting of css, inline Block element, if you want to center it horizontally, use text-align:center;

 

2) Set display:block; for the img element to convert it to a block-level element. To center horizontally, use margin:0 auto;

2. Center vertically

  1.jpg


 

Use this picture as a demonstration

1 <p>
2     <img alt="" src="1.jpg" />
3 </p>
Copy after login

Line-height==height vertical-align: middle;

p{
    width: 198px;
    height: 198px;
    text-align: center;
    line-height: 198px;
    border: 1px solid #8900FF;
}
img{
    vertical-align: middle;
}
Copy after login

display: table-cell;vertical-align: middle;

p{
    display: table-cell;
    vertical-align: middle;
    width: 198px;
    height: 198px;
    border: 1px solid #8900FF;
}
img{
    display: block;
    margin: 0 auto;
}
Copy after login
display: table-cell;vertical-align: middle; text-align: center;
p{
    display: table-cell;
    vertical-align: middle;
    text-align: center;
    width: 198px;
    height: 198px;
    border: 1px solid #8900FF;
}
Copy after login
Positioning+display: block;transform: translate(-50%,-50%);
p{
    position: relative;
    width: 198px;
    height: 198px;
    border: 1px solid #8900FF;
}
img{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    display: block;
}
Copy after login
Positioning+margin negative value
p{
    position: relative;
    width: 198px;
    height: 198px;
    border: 1px solid #8900FF;
}
img{
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -75px 0 0 -75px;
}
Copy after login

  定位+margin: auto;

p{
    position: relative;
    width: 198px;
    height: 198px;
    border: 1px solid #8900FF;
}
img{
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    display: block;
}
Copy after login

   overflow: hidden;margin值

p{
    width: 198px;
    height: 198px;
    overflow: hidden;
    border: 1px solid #8900FF;
}
img{ 8     margin: 25px;
}
Copy after login

  padding值

 p{
 2     padding: 25px;
 3     width: 148px;
 4     height: 148px;
 5     border: 1px solid #8900FF;
 6 }
Copy after login

  用table的属性+vertical-align: middle;实现

1 <p>2     <span><img alt="" src="1.jpg" /></span>3 </p>
Copy after login
p{
    display: table;
    width: 198px;
    height: 198px;
    text-align: center;
    border: 1px solid #8900FF;
}
span{
    display:table-cell;
    vertical-align: middle;
}
Copy after login

  用background实现

1 <p></p>
Copy after login
1 p{
2     width: 198px;
3     height: 198px;
4     border: 1px dashed #8900FF;
5     background: url(1.jpg) no-repeat center center;
6 }
Copy after login

  效果如下:

  

 原文来自:http://www.cnblogs.com/Ni-F/p/6937931.html 感谢作者分享!

The above is the detailed content of About using css to center text and images vertically and horizontally. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

See all articles