Summary and sharing of CSS layout tips

Source: Internet
Author: User

Single Row layout

Center horizontally

    1. parent element Text-align:center; child element: Inline-block;

    • Advantages: good compatibility;

    • Insufficient: You need to set both child and parent elements

      <! DOCTYPE html>
    1. child element margin:0 Auto;

    • Pros: Good compatibility

    • Disadvantage: You need to specify a width

<! DOCTYPE html>
    1. Child element {display:table;margin:0 Auto;}

    • Pros: Only need to set itself

    • Insufficient: ie6,7 need to adjust structure

<! DOCTYPE html>
    1. Parent element: relative; child element: half of absolute;left:50%;margin-left:-width

    • Pros: Good compatibility

    • Cons: Need to know the width of child elements

<! DOCTYPE html>
    1. Parent element: relative; child element: Absolute;left:50%;transform:translate ( -50%,0);

    • Pros: Poor compatibility

    • Cons: You don't need to know the width of child elements

<! DOCTYPE html>
    1. Elastic box: Parent element: Display:flex;justify-content:center;

    • Advantages: Simple

    • Cons: Poor compatibility

      <! DOCTYPE html>

Center vertically

    1. Vertical-align:center;

<! DOCTYPE html>
<! DOCTYPE html>
    1. Parent element: position:relative; child element: Positon:absolute;top:50%;transform:translate ( 0,-50%);

<! DOCTYPE html>
    1. Parent element: position:relative; child element: Half of the height of the positon:absolute;top:50%;margin-top:-child element;

    2. Parent element: Display:flex;align-items:center;

<! DOCTYPE html>

Horizontal Vertical Center

    1. Parent element: Display:table-cell;vertical-align:middle;text-align:center;
      child elements; display:inline-block;

<! DOCTYPE html>
    1. Parent element: position:relative;
      Child element: Position:absolute

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.