CSS3 Flex implements horizontal and vertical centering of elements

Source: Internet
Author: User
Tags html tags

There are many tutorials on the internet for Flex, and there are also some calls for Flexbox, some called flex, in fact, these two names are not wrong, just flexbox old point, and Flex is just out of the things, in order to facilitate the explanation, catch up with new technology, I'm going to call this layout a flex layout.

Element Center, I believe that as a front-end engineer You will always use, whether in the horizontal direction, or vertically centered, can be in your career wandering. But a lot of the time to achieve vertical centering, or more troublesome. But you don't have to worry about it, so let's share it. It is easy to achieve the center effect of elements in horizontal and vertical direction through flex layout. Center Horizontally

Horizontal centering is the simplest we'll look directly at the code.

1. Horizontal centering of individual elements

CSS3 Flexbox easily implements elements of the horizontal center and vertically centered CSS code View Source

1 . box{
2 Display:flex;
3 Justify-content:center;
4 Background: #0099cc
5 }
6 h1{
7 Color: #FFF
8 }
HTML codeView Source
1 <div class= "box" >
2
3 </div>

In this code, we just need to add two attributes to the parent element of the H1 tag, and the Justify-content function is to center the class for box's div box. The box is centered, the elements inside the box are centered naturally, and the advantage is that you don't need to set any style on the element that needs to be centered (H1), if: Width,margin.

2. Multiple H1 elements are centered horizontally

CSS3 Flexbox easily implements the horizontal center of elements and vertically centers HTML code View Source

1 <div class= "box" >
2
3
4
5 </div>
CSS CodeView Source
01 . box{
02 Display:flex;
03 Justify-content:center;
04 width:100%;
05 Background: #0099cc
06 }
07 h1{
08 Font-size:1rem;
09 Padding:1rem;
10 BORDER:1PX dashed#fff;
11 Color: #FFF;
12 Font-weight:normal;
13 }

The code is quite neat, in the flex layout, the object is the child element and the parent element, so here we might as well as the body as a normal label use, although rarely used, but in order to explain the body tag is also very grounded gas, So in this case, the body tag is used as the parent element of box.

Now let's analyze the code, there are two things in Flex, one is the Flex container (the subproject parent element), and the other is a subproject (the Flex container child element). If you do not add a style to the. box, a H1 tag takes up one line, which means that the page will display three lines of text "Flex elastic layout justify-content attribute element is horizontally centered". If we add display:flex to. box, then the three H1 labels are properly lined up in a row, which is equivalent to floating, except that he will not be wrapped in a line that is more than the width of the. box. element is centered vertically

The vertical center of the element is sometimes troublesome in the beginning development, but it is simple to use the flex layout. Let's take a look at the example.

1. Single H1 label vertically centered

CSS3 Flexbox easily implements the horizontal center of elements and vertically centers HTML code View Source

1 <div class= "box" >
2
3 </div>
CSS CodeView Source
01 . box{
02 Display:flex;
03 width:980px;
04 Height:30rem;
05 Align-items:center;
06 Background: #0099cc
07 }
08 h1{
09 Font-size:1rem;
10 Padding:1rem;
11 BORDER:1PX dashed#fff;
12 Color: #FFF
13 }

In order for everyone to see clearly, define the high, blue background of the. box; Add a border to the H1 element. In this way, the H1 element is obediently centered, do not have to set the height of H1, no absolute positioning, yes, it is so simple and rude.

2. Multiple H1 labels are centered vertically

CSS3 Flexbox easily implements the horizontal center of elements and vertically centers HTML code View Source

1 <div class= "box" >
2
3
4
5 </div>
CSS CodeView Source
01 . box{
02 Display:flex;
03 width:980px;
04 Height:30rem;
05 Align-items:center;
06 Background: #0099cc
07 }
08 h1{
09 Font-size:1rem;
10 Padding:1rem;
11 BORDER:1PX dashed#fff;
12 Color: #FFF
13 }

The above example has more than two H1 tags on the HTML code, and the style has not changed. With flex vertically centered, the element, picture, text center problem instantly vanished.

Note: The DIV,H1 tag is just an example, and the Flex attribute also applies to other tag HTML tags.

If you want the parent element to be centered horizontally, you just need to give the body a property to be OK. The code looks like this: view source

1 body{
2 Display:flex;
3 Justify-content:center;
4 }

In order to see the effect, here we set the width of the. box to 960px and the background color to #0099cc.

3. Multiline H1 label vertically centered HTML code View Source

1 <div class= "box" >

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.