CSS horizontal centering and limiting the maximum width

CSS horizontal centering and limiting the maximum width

A CSS layout and style question: how to balance horizontal centering and limiting maximum width

In recent work, I encountered a problem like this:

1. Background

  • A horizontal three-column or two-column layout, with the content in one column changing;
  • When there is too little content, the whole thing needs to be displayed in the center;
  • When there is too much content, it needs to be omitted according to the maximum width;

2. Implementation ideas

The overall container needs to add properties:

.container {
    display: flex;
    justify-content: center;
}

2.1 Use flex:1 to achieve

In the previous code, the column that changes is set to flex: 1 or flex: auto, but the problem is that this will take up all the remaining space in the container and will not automatically adjust according to the content;

2.2 Set flex: none to achieve

The problem with this is that when there is too much content, it will not cover the excessive content.

2.3 Not setting properties

If the content in the middle is not fixed, do not set any flex attributes. If it is not enough, remember to overwrite it:

.clamp {
    flex: initial;
}

Note that initial is used here instead of none.

Also be careful not to squeeze the width of other columns. There was originally a picture in the left column, and the width was set, but because there was too much content in the middle column, the picture was compressed and deformed. Therefore, a min-width attribute is added to the image.

This is the end of this article about how to horizontally center with CSS and limit the maximum width. For more information about how to horizontally center with CSS and limit the maximum width, please search previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  JavaScript implements bidirectional linked list process analysis

>>:  A Brief Analysis of MySQL Connections and Collections

Recommend

How to simulate network packet loss and delay in Linux

netem and tc: netem is a network simulation modul...

Steps to set up HTTPS website based on Nginx

Table of contents Preface: Encryption algorithm: ...

Example of implementing dynamic verification code on a page using JavaScript

introduction: Nowadays, many dynamic verification...

Summary of Operator Operations That Are Very Error-Prone in JavaScript

Table of contents Arithmetic operators Abnormal s...

The difference between html block-level tags and inline tags

1. Block-level element: refers to the ability to e...

Detailed explanation of Vue's ref attribute

Summarize This article ends here. I hope it can b...

Teach you how to install mysql database on Mac

Download MySQL for Mac: https://downloads.mysql.c...

RGB color table collection

RGB color table color English name RGB 16 colors ...

A brief analysis of SQL examples for finding uncommitted transactions in MySQL

A long time ago, I summarized a blog post titled ...

Summary of 6 solutions for implementing singleton mode in JS

Preface Today, I was reviewing the creational pat...

How to use Celery and Docker to handle periodic tasks in Django

As you build and scale your Django applications, ...

How to use Vue3 asynchronous data loading component suspense

Table of contents Preface Creating Components Sum...

Introducing icons by implementing custom components based on Vue

Preface In project development, there are many wa...

MySQL latest version 8.0.17 decompression version installation tutorial

Personally, I think the decompressed version is e...