Home Web Front-end CSS Tutorial 7 ways to center containers horizontally and vertically with css

7 ways to center containers horizontally and vertically with css

May 21, 2017 am 10:31 AM
css vertical center level

This article mainly introduces in detail the 7 ways of using CSS to center the container horizontally and vertically. It has certain reference value. Interested friends can refer to

This kind of CSS layout is usually used. There are quite a lot of them, and it is also a question that often comes up in interviews. You will find a lot of them after searching online, but I still want to summarize them myself.
There are many methods of this kind. This article only summarizes a few of them to deepen the impression.
The renderings are all like this:

Method 1: position plus margin


# #XML/HTML CodeCopy content to clipboard

  1. ##<

    p class ="wrap">

  2. ##<
  3. p class="center">p>

    p>

##CSS Code

Copy content to clipboard

## /**css**/

.wrap {
    width
  1. :

    200px; height: 200px; background : yellow; position: relative; ##} .wrap .center {

    width
  2. :
  3. 100px

    ; height: 100px; background: green; margin: auto; position: absolute; left: 0; rightright: 0; top: 0; bottombottom: 0; }

  4. Compatibility: supported by all major browsers, but not supported by IE6
  5. Method 2:
  6. diaplay:table-cell

XML/HTML Code

Copy content to clipboard

  1. <

    p
  2. class

    ="wrap"> <

    p
  3. class

    = "center">p>

    p
  4. >

  5. CSS Code

Copy content to clipboard

/*&/ .wrap{

width
    :
  1. 200px

    ; height: 200px; background: yellow ; display: table-cell; vertical-align: middle; text-align: center;

  2. } .center{ display: inline-block; vertical-align: middle; width: 100px; height: 100px; background : green;

  3. }

Compatibility: Due The reason for display:table-cell is that IE67 is not compatible

Method three: position plus transform


XML/HTML CodeCopy content to clipboard

  1. #

  2. ##<

    p class="wrap">

  3. ##<
  4. p class="center"> p> ##

  5. p

    >

##CSS Code


Copy content to clipboard

##/* css */ .wrap {

position
    :
  1. relative

    ; background: yellow; width: 200px; height: 200px;} .center { position: absolute; background: green; top:50%; left:50%; -webkit-transform:translate(-50%,-50%); transform:translate(-50%,-50%); width: 100px; height: 100px; }

  2. Compatibility: Transform is not supported under IE9 and below, and the mobile version performs better.
  3. Method 4:
  4. flex;align-items: center;justify-content: center

##XML/HTML Code

Copy content to clipboard


#

    ##<
  1. p class

    =
  2. "wrap"
  3. > ##​ <p class=

    "center"
  4. >

    p> ## p>

  5. ##CSS Code

  6. Copy content to clipboard
    1. /* css */ .wrap { background: yellow; width: 200px; height: 200px; display: flex; align-items: center; justify -content: center;

    2. } .center { background: green; width: 100px; height: 100px;

    3. }

    Mobile preferred

    Method five: display :flex;margin:auto


    XML/HTML CodeCopy content to clipboard

    1. ##

    2. ##<

      p class="wrap">

  7. ##<
  8. p class="center">p> ; ##

  9. p

    >

CSS Code

Copy content to clipboard

/* css */
    .wrap {
  1. background

    : yellow; width: 200px; height: 200px ; display: flex; } .

    center
  2. {
  3. background

    : green; width: 100px; height: 100px; margin: auto; }

  4. Preferred method for mobile terminal

Six:

Pure position

XML/HTML Code

Copy content to clipboard

##

  1. ##<

    p
  2. class

    ="wrap"> ##<p

  3. class
  4. =

    "center">p > ##p

    >

  5. CSS Code

  6. Copy content to clipboard

  1. /* css */ .wrap { background: yellow; width: 200px; height: 200px; position: relative;

  2. } /**method one**/ .center { background: green; position: absolute; width: 100px; height: 100px; left: 50px; top: 50px;

  3. } /** Method Two**/ .center { background: green; position: absolute; width: 100px; height: 100px; left: 50%; top: 50%; margin-left:-50px; margin-top:-50px;

  4. }

Compatibility: Applicable to all browsers

Calculation formula for method one in method six As follows:
The left value calculation formula of the child element (conter): left=(width of the parent element - width of the child element) / 2=(200-100) / 2=50px;
Child element (conter) The top value calculation formula: top=(height of parent element - height of child element) / 2=(200-100) / 2=50px;
Method 2 calculation formula:
The left value is fixed at 50%;
The margin-left of the child element= -(width of the child element/2)=-100/2= -50px;
The top value is the same, fixed at 50%
The margin-top= of the child element -(Height of child element/2)=-100/2= -50px;

Method 7:Compatible with lower version browsers, no fixed width and height


XML/HTML CodeCopy content to clipboard

  1. <p class="table">

  2. <p class= "tableCell">                                                         #=

    "content"
  3. >

    No fixed width and height, adaptivep> ##     p>  

  4. p>

  5. ##CSS Code

  6. Copy content to clipboard
##/*css*/

.table {
height

:

200px;/*The height value cannot be less*/ width

:
    200px
  1. ;

    / *The width value cannot be less*/ display: table; position: relative; float:left; background: yellow;

  2. } .tableCell { display: table-cell; vertical-align: middle; text-align: center;

    :
  3. 10px
  4. ;

    *top: 50%;

    *
  5. left
  6. : 50%;

    } .
  7. content
  8. {

    *
  9. position
  10. :

    relative;

  11. *

    top: -50%;

    *
  12. left
  13. : -50%;

    background: green

    ;
  14. }

  15. To summarize the above seven methods for now, there are too many methods. In fact, as long as you are used to one or two of them, it will be enough.

  16. Summary
  17. If it is a mobile terminal, then it is more convenient to use methods four and five. It also supports non-fixed width and height, which is fast, accurate and ruthless
  18. That is, use flex; align-items: center; justify-content: center;

##XML/HTML Code

Copy content to clipboard



#

##<

p
  1. class

    ="wrap"

    >
  2. ##​ <p class="center">

  3. p> ## p>

  4. ##CSS Code Copy content to clipboard

  5. ##/* css */

    .wrap {
  6. background:

    yellow
    ;

    width

    : 200px; height:

    200px
      ;
    1. display

      : flex; align- items: center; justify-content: center; ##} .center { background: green; width: 100px; height: 100px

      ;
    2. } Or display:flex;margin:auto;

      XML/HTML Code
    3. Copy content to clipboard

    ##<

    p

    class="wrap"

    >

    1. <
    2. p

      class ="center">p

      >
    3. p>

    4. CSS CodeCopy content to clipboard

      1. ##/* css */ .wrap { background: yellow; width: 200px; height: 200px; display : flex;

      2. ##} .
      3. center

        { background: green; width: 100px; height: 100px; margin: auto;

      4. }
      5. If it is a PC version, compatibility must be considered. Method six is ​​good, that is, pure position.


      XML/HTML Code

      Copy content to clipboard

      1. ##<
      2. p class="wrap"> <

      3. p class ="center">p>

        p>


      CSS Code

      Copy content to clipboard

        /* css */
      1. .wrap {

        background: yellow; width: 200px; height: 200px ; position: relative; ##}

        /**method one**/
      2. .
      3. center

        { background: green; position: absolute; width: 100px ; height: 100px; left: 50px; top: 50px; ​

      4. }  

        /**Method Two**/
      5. .
      6. center

        { background: green; position: absolute; width: 100px; height: 100px; left: 50%; top: 50%; margin-left:- 50px; margin-top:-50px; }

      7. If the height of the middle element on the PC side is not fixed, then just use method seven, and the code will not be copied.

      8. If we really want to sum up the vertical css elements, there should be more than ten or twenty types. However, it is not necessary to master them all, as long as you have a general understanding and there are no side effects when using them.

    The above is the detailed content of 7 ways to center containers horizontally and vertically with css. 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)

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.

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.

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.

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 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 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

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-

See all articles