Home Web Front-end CSS Tutorial 4 ways to achieve equal distribution with CSS

4 ways to achieve equal distribution with CSS

Jun 28, 2017 am 11:00 AM
css Way

This article mainly introduces in detail the 4 ways to achieve equal distribution in CSS. Equal distribution refers to a layout method in which child elements evenly distribute the width of the parent element. This article will introduce the 4 ways to achieve equal distribution. Interested friends can refer to

Equal distribution refers to a layout method in which child elements evenly distribute the width of parent elements. This article will introduce 4 ways to achieve equal distribution

Idea 1: float

Disadvantages: There is coupling between structure and style, and there is a rounding error in the width percentage value under IE7-browser

【 1】float + padding + background-clip

Use padding to achieve the spacing between sub-elements, and use background-clip to make the padding part of the sub-element not display the background


CSS CodeCopy content to clipboard

  1. ##


XML/HTML Code

Copy content to clipboard

    ##<
  1. p class="parentWrap"> ##​ <

  2. p

    class="parent" style="background-color: lightgrey;"> ##                                                          p class

  3. =

    "child" style="background-color: lightblue;"> ;1p> ##                   <p

    class
  4. =
  5. "child"

    style="background-color: lightgreen;"> 2p>

  6.            <p class="child" style="background-color: lightsalmon;">3p>

  7.          <p class="child" style="background-color: pink;">4p>

  8. #    
  9. p>          
  10. p>

##【2】float + margin + calc

Use margin to achieve spacing between sub-elements, use calc () function calculates the width of the child element


CSS Code

Copy content to the clipboard

  • XML/HTML Code


    Copy content to clipboard

    <

      p
    1. class="parentWrap"> ##<

      p
    2. class

      ="parent" style="background-color: lightgrey;" > ##          <p

    3. class
    4. =

      " child" style="background-color: lightblue;">1p>

    5.            <p class="child" style="background-color: lightgreen;">2p>

    6.          <p class="child" style="background-color: lightsalmon;">3p>

    7.         <p class="child" style= "background-color: pink;">4p>

    8.      
    9. p>                                                        #p>
    10. ##【3】float + margin + (fix)

    11. Use margin to achieve spacing between sub-elements, and achieve compatibility by adding structure

      CSS Code

      Copy content to clipboard


    12. XML/HTML Code

    13. Copy content to clipboard
    ##<


    p

    class="parentWrap">

    1. ##<p class="parent" style=

      "background-color: lightgrey;"
    2. >

    3.         <p class="child" style="background-color: blue;">  

    4.             <p class="in" style="background-color: lightblue;">1p>  

    5.         p>  

    6.         <p class="child" style="background-color: green;">  

    7.             <p class="in" style="background-color: lightgreen;">2p>  

    8.         p>  

    9.         <p class="child" style="background-color: orange;">  

    10.             <p class="in" style="background-color: lightsalmon;">3p>  

    11.         p>  

    12.         <p class="child" style="background-color: red;">  

    13.             <p class="in" style="background-color: pink;">4p>  

    14.         p>                   

    15.     p>       

    16. p>  

    17.   

    思路二: inline-block

    Disadvantages: You need to set the vertical alignment vertical-align, and you need to deal with the gap problem of newline characters being parsed into spaces. IE7-The browser does not support setting the inline-block attribute for block-level elements. The compatible code is display:inline;zoom:1;

    [1]inline-block + padding + background- clip


    ##CSS CodeCopy content to clipboard

    ##XML/HTML Code


    Copy content to clipboard

    <

      p
    1. class="parentWrap"> <

    2. p
    3. class= "parent" style="background-color: lightgrey;">        <

    4. p
    5. class="child" style="background-color: lightblue ;">1p> ##<

      p
    6. class

      ="child" style="background-color: lightgreen; ">2p> #<p

    7. class
    8. =

      "child" style="background-color: lightsalmon;" >3p>

    9.         <p class="child" style="background-color: pink;">4p>                   

    10.     p>       

    11. p>  

    12.   

    13. 【2】inline-block + margin + calc


      CSS Code复制内容到剪贴板

      1.   

      2.   


      XML/HTML Code复制内容到剪贴板

      1. <p class="parentWrap">  

      2.     <p class="parent" style="background-color: lightgrey;">  

      3.         <p class="child" style="background-color: lightblue;">1p>  

      4.         <p class="child" style="background-color: lightgreen;">2p>  

      5.         <p class="child" style="background-color: lightsalmon;">3p>  

      6.         <p class="child" style="background-color: pink;">4p>                   

      7.     p>       

      8. p>  

      9.   

       【3】inline-block + margin + (fix)


      CSS Code复制内容到剪贴板

      1.   

      2.   


      XML/HTML Code复制内容到剪贴板

      1. <p class="parentWrap">  

      2.     <p class="parent" style="background-color: lightgrey;">  

      3.         <p class="child" style="background-color: blue;">  

      4.             <p class="in" style="background-color: lightblue;">1p>  

      5.         p>  

      6.         <p class="child" style="background-color: green;">  

      7.             <p class="in" style="background-color: lightgreen;">2p>  

      8.         p>  

      9.         <p class="child" style="background-color: orange;">  

      10.             <p class="in" style="background-color: lightsalmon;">3p>  

      11.         p>  

      12.         <p class="child" style="background-color: red;">  

      13.             <p class="in" style="background-color: pink;">4p>  

      14.         p>                   

      15.     p>       

      16. p>  

      17.   

      思路三: table

      缺点:元素被设置为table后,内容撑开宽度。若要兼容IE7-浏览器,需要改为

      结构。table-cell元素无法设置margin,设置padding及background-clip也不可行

      【1】table + margin负值


      CSS CodeCopy content to clipboard

      ##XML/HTML Code

      Copy content to clipboard

      ##<

        p
      1. class="parentWrap"> ##<

        p
      2. class

        ="parent" style="background-color: lightgrey;"> ;         <

        p
      3. class

        ="child" style="background-color: blue;"> ##                                                          #p class=

        "in"
      4. style

        ="background-color: lightblue;">1p> ##                                                    #p> ##                                        #"child"

        style
      5. =
      6. "background-color: green;"

        > #<

      7. p
      8. class="in" style="background-color: lightgreen;" >2p>

      9. ##          p> ##                                                            ##class="child" style="background-color: orange;">

      10.             <p class="in" style="background-color: lightsalmon;">3p>

      11.                                                                                                                 ##p class="child"

        style
      12. =
      13. "background-color: red;"

        >                                                                                style="background-color: pink;">4p >       

      14. p

        >                                                           #p> 【2】table + sibling selector

      15. ##CSS CodeCopy content to clipboard

      16. #
      17. XML/HTML CodeCopy content to clipboard

      18. ##<p

        class
      19. =
      20. "parent"

        style
      21. =
      22. "background-color: lightgrey;"

        >

      23. <p

        class
      24. =
      25. "child"

        style
      26. =
      27. "background-color: blue;"

        >

      28.         <p class="in" style="background-color: lightblue;">1p>  

      29.     p>  

      30.     <p class="child" style="background-color: green;">  

      31.         <p class="in" style="background-color: lightgreen;">2p>  

      32.     p>  

      33.     <p class="child" style="background-color: orange;">  

      34.         <p class="in" style="background-color: lightsalmon;">3p>  

      35.     p>  

      36.     <p class="child" style="background-color: red;">  

      37.         <p class="in" style="background-color: pink;">4p>  

      38.     p>                   

      39. p>       

      40.   

      思路四: flex


      CSS Code复制内容到剪贴板

      1.   

      2.   


      XML/HTML Code复制内容到剪贴板

      1. <p class="parent" style="background-color: lightgrey;">  

      2.     <p class="child" style="background-color: lightblue;">1p>  

      3.     <p class="child" style="background-color: lightgreen;">2p>  

      4.     <p class="child" style="background-color: lightsalmon;">3p>  

      5.     <p class="child" style="background-color: pink;">4p>                   

      6. p>     

      7.   

       以上就是本文的全部内容,希望对大家的学习有所帮助。

      本文链接:http://www.cnblogs.com/xiaohuochai/p/5456695.html

      The above is the detailed content of 4 ways to achieve equal distribution 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)

      Hot Topics

      Java Tutorial
      1659
      14
      PHP Tutorial
      1258
      29
      C# Tutorial
      1232
      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