Css enables vertical center of text in the row and vertical center of css text

Source: Internet
Author: User

Css enables vertical center of text in the row and vertical center of css text

 

Previously, I used float, relative positioning, absolute positioning, display: table, and other css methods for positioning. I learned from the Internet that flex can achieve elastic layout, which is in line with future development trends.

1: vertically center the text in the box line. The solution is to set the Row Height of the text to the height of the box.

p {           border:#333333 solid 1px;           height:50px;           line-height:50px;           margin-bottom:30px;        }

 

 

2:The solution is to set the height of the box and set the same value for the padding-top and padding-bottom of the box.

 p {           border:#333333 solid 1px;           padding-top:30px;           padding-bottom:30px;           margin-bottom:30px;        }

3: vertically center the text in the box line. The solution is to convert the display attribute of the box into a table, and then add the span label to the text. The span attribute is displayed: table-cell.

p {           border:#333333 solid 1px;           height:60px;           display:table;           width:100%;           margin-bottom:30px;        }        p span {            display:table-cell;            vertical-align:middle;        }
<P> <span> People's Republic of China </span> </p>

4: To vertically center the text in the box line, the solution is to convert the attribute of the box display to flex.

        p {           border:#333333 solid 1px;           height:60px;           display:flex;           align-items:center;           margin-bottom:30px;        }
<P> <span> People's Republic of China </span> </p>

If the "People's Republic of China" level is also centered, the css should be adjusted:

P {border: #333333 solid 1px; height: 60px; display: flex; align-items: center;/* vertical direction */justify-content: center; /* horizontal direction */margin-bottom: 30px ;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.