CSS adaptive layout realizes the overall centering of sub-element items and left alignment of internal items

CSS adaptive layout realizes the overall centering of sub-element items and left alignment of internal items

In daily work, we may encounter a layout like this: a parent element frame (adaptive width as the browser size) contains many buttons, and then these buttons (fixed width) need to be centered in the parent element, and the overall content of the buttons is arranged from left to right. Here is a solution, the example is as follows:

There is the following code:

<div class="wrap">
    <button class="btn">1</button>
    <button class="btn">2</button>
    <button class="btn">3</button>
    <button class="btn">4</button>
    <button class="btn">5</button>
    <button class="btn">6</button>
    <button class="btn">7</button>
</div>

The corresponding CSS is:

.wrap {
    border: 2px solid black;
    margin: 0 auto;
    padding: 10px;
    width: 40%;
}
.btn {
    outline: none;
    border: none;
    width: 180px;
    height: 30px;
    background-color: cornflowerblue;
    margin: 5px;
}

Display effect:

Display effect 1

Dynamically changing width:

Width Variation 1

As you can see, when we reduce the size of the browser window, the width of the parent element (black box) will be dynamically adjusted, and its child elements will also be dynamically arranged according to the width of the parent element. However, the button group is left-aligned in the parent container. If we want to keep the whole thing centered, we can do the following:

CSS code:

.wrap {
    border: 2px solid black;
    margin: 0 auto;
    padding: 10px;
    width: 40%;
    display: grid;
    grid-template-columns: repeat(auto-fill, 200px);
    justify-content: center;
}
.btn {
    outline: none;
    border: none;
    width: 180px;
    height: 30px;
    background-color: cornflowerblue;
    margin: 5px;
}

Set the parent element's display property to grid , set the grid column template to auto-fill, and set the width to 200px (the width here can be slightly larger than a single button element), and then set justify-content property value to center to center the content horizontally. The effect is as follows:

Display effect 2

Dynamically changing effects:

Width Variation 2

It can be seen that during the width change process, the sub-element as a whole has always remained centered.

To achieve this effect with CSS alone, I only thought of using grid. If you have other methods, please leave a message and learn together~

Tip: The compatibility of grid is not very good at present. If you need to support older browser versions, use it with caution!

compatibility

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  Detailed explanation of MySQL master-slave replication and read-write separation

>>:  How to reduce memory usage and CPU usage of web pages

Recommend

MySQL SQL statement to find duplicate data based on one or more fields

SQL finds all duplicate records in a table 1. The...

How to quickly delete all tables in MySQL without deleting the database

This article uses an example to describe how to q...

Four categories of CSS selectors: basic, combination, attribute, pseudo-class

What is a selector? The role of the selector is t...

Example of converting timestamp to Date in MySQL

Preface I encountered a situation at work: In the...

Mysql WorkBench installation and configuration graphic tutorial

This article shares with you the installation and...

How to safely shut down a MySQL instance

This article analyzes the process of shutting dow...

Web Design Tutorial (4): About Materials and Expressions

<br />Previous Web Design Tutorial: Web Desi...

How to find the my.ini configuration file in MySQL 5.6 under Windows

Make a note so you can come back and check it lat...

Application and implementation of data cache mechanism for small programs

Mini Program Data Cache Related Knowledge Data ca...

Solution to the problem of web page flash animation not displaying

<br />The solution steps are as follows: Sta...

WeChat applet implements simple chat room

This article shares the specific code of the WeCh...

What are the core modules of node.js

Table of contents Global Object Global objects an...

Install Docker for Windows on Windows 10 Home Edition

0. Background Hardware: Xiaomi Notebook Air 13/In...