What is a multi-column layout?

Column-count, column-width, column-gap, break-inside and Page-break-inside of the CSS are used to display multiple columns.


2. Application scenarios
1, text multi-column display

Column-count, column-gap, and column-rule can be used to display the following columns.

Create a new index.html file and copy the following code into the file. Save the file and open it in your browser to see the effect.

<! DOCTYPEhtml>
<html>

<head>
    <meta charset="utf-8">
    <title>Multicol starting point</title>
    <style>
        body {
            width: 90%;
            max-width: 900px;
            margin: 2em auto;
            font:.9em/1.2 Arial, Helvetica, sans-serif;
        }

        .container {
            column-count: 3;
            column-gap: 20px;
            column-rule: 4px dotted rgb(79.185.227);
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>Simple multicol example</h1>

        <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla luctus aliquam dolor, eu lacinia lorem
            placerat vulputate.
            Duis felis orci, pulvinar id metus ut, rutrum luctus orci. Cras porttitor imperdiet nunc, at ultricies
            tellus
            laoreet sit amet. Sed auctor cursus massa at porta. Integer ligula ipsum, tristique sit amet orci vel,
            viverra
            egestas ligula. Curabitur vehicula tellus neque, ac ornare ex malesuada et. In vitae convallis lacus.
            Aliquam
            erat volutpat. Suspendisse ac imperdiet turpis. Aenean finibus sollicitudin eros pharetra congue. Duis
            ornare
            egestas augue ut luctus. Proin blandit quam nec lacus varius commodo et a urna. Ut id ornare felis, eget
            fermentum
            sapien.</p>

        <p>Nam vulputate diam nec tempor bibendum. Donec luctus augue eget malesuada ultrices. Phasellus turpis est,
            posuere
            sit amet dapibus ut, facilisis sed est. Nam id risus quis ante semper consectetur eget aliquam lorem.
            Vivamus
            tristique elit dolor, sed pretium metus suscipit vel. Mauris ultricies lectus sed lobortis finibus. Vivamus
            eu
            urna eget velit cursus viverra quis vestibulum sem. Aliquam tincidunt eget purus in interdum. Cum sociis
            natoque
            penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
    </div>
</body>

</html>
Copy the code

Words are not broken sentences

Break -inside and page-break-inside can be used to not truncate the content.

Create a new index.html file and copy the following code into the file. Save the file and open it in your browser to see the effect.

<! DOCTYPEhtml>
<html>

<head>
    <meta charset="utf-8">
    <title>Simple multicol example, break-inside</title>
    <style>
        body {
            width: 90%;
            max-width: 900px;
            margin: 2em auto;
            font:.9em/1.2 Arial, Helvetica, sans-serif;
        }

        .container {
            column-width: 250px;
            column-gap: 20px;
        }

        .card {
            /* Uncomment to see the effect without truncating the content */
            /* break-inside: avoid; page-break-inside: avoid; * /
            background-color: rgb(207.232.220);
            border: 2px solid rgb(79.185.227);
            padding: 10px;
            margin: 0 0 1em 0;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="card">
            <h2>I am the heading</h2>
            <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla luctus aliquam dolor, eu lacinia lorem
                placerat
                vulputate. Duis felis orci, pulvinar id metus ut, rutrum luctus orci. Cras porttitor imperdiet nunc, at
                ultricies
                tellus laoreet sit amet. Sed auctor cursus massa at porta. Integer ligula ipsum, tristique sit amet orci
                vel, viverra egestas ligula.</p>
        </div>

        <div class="card">
            <h2>I am the heading</h2>
            <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla luctus aliquam dolor, eu lacinia lorem
                placerat
                vulputate. Duis felis orci, pulvinar id metus ut, rutrum luctus orci. Cras porttitor imperdiet nunc, at
                ultricies
                tellus laoreet sit amet. Sed auctor cursus massa at porta. Integer ligula ipsum, tristique sit amet orci
                vel, viverra egestas ligula.</p>
        </div>

        <div class="card">
            <h2>I am the heading</h2>
            <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla luctus aliquam dolor, eu lacinia lorem
                placerat
                vulputate. Duis felis orci, pulvinar id metus ut, rutrum luctus orci. Cras porttitor imperdiet nunc, at
                ultricies
                tellus laoreet sit amet. Sed auctor cursus massa at porta. Integer ligula ipsum, tristique sit amet orci
                vel, viverra egestas ligula.</p>
        </div>

        <div class="card">
            <h2>I am the heading</h2>
            <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla luctus aliquam dolor, eu lacinia lorem
                placerat
                vulputate. Duis felis orci, pulvinar id metus ut, rutrum luctus orci. Cras porttitor imperdiet nunc, at
                ultricies
                tellus laoreet sit amet. Sed auctor cursus massa at porta. Integer ligula ipsum, tristique sit amet orci
                vel, viverra egestas ligula.</p>
        </div>

        <div class="card">
            <h2>I am the heading</h2>
            <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla luctus aliquam dolor, eu lacinia lorem
                placerat
                vulputate. Duis felis orci, pulvinar id metus ut, rutrum luctus orci. Cras porttitor imperdiet nunc, at
                ultricies
                tellus laoreet sit amet. Sed auctor cursus massa at porta. Integer ligula ipsum, tristique sit amet orci
                vel, viverra egestas ligula.</p>
        </div>

        <div class="card">
            <h2>I am the heading</h2>
            <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla luctus aliquam dolor, eu lacinia lorem
                placerat
                vulputate. Duis felis orci, pulvinar id metus ut, rutrum luctus orci. Cras porttitor imperdiet nunc, at
                ultricies
                tellus laoreet sit amet. Sed auctor cursus massa at porta. Integer ligula ipsum, tristique sit amet orci
                vel, viverra egestas ligula.</p>
        </div>

        <div class="card">
            <h2>I am the heading</h2>
            <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla luctus aliquam dolor, eu lacinia lorem
                placerat
                vulputate. Duis felis orci, pulvinar id metus ut, rutrum luctus orci. Cras porttitor imperdiet nunc, at
                ultricies
                tellus laoreet sit amet. Sed auctor cursus massa at porta. Integer ligula ipsum, tristique sit amet orci
                vel, viverra egestas ligula.</p>
        </div>


    </div>

</body>

</html>
Copy the code

Iii. Reference documents
  • What is the multi-column layout of CSS?