CSS easily implements fixed-ratio block-level containers

CSS easily implements fixed-ratio block-level containers

When designing H5 layout, you will usually encounter banners.

For example, if you want to display it as 2:1, of course the picture returned by the backend should be 2:1, but things are often not so satisfactory, so what should we do?

It always feels inappropriate to write the width and height in a fixed way

Default width: 100%; let the height be adaptive and the image will slowly expand (the product manager will say that this is a bad user experience, but I am a user and I think it is good)

The method is here

        .banner-wrapper {
            position: relative;
            width: 100%;
            padding-top: 50%;
        }

        .banner {
            position: absolute;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
        }
        
        <div class="banner-wrapper">
            <img class="banner" src="./img/portfolio/cabin.png" alt="">
        </div>

Let me explain

padding-top: 50%; is the key. Using percentages to write padding is relative to its width (don’t ask me why this is the case).

So width: 100%; padding-top: 50%; will perfectly present a 2:1 box

Finally, put the entire img on

Don’t you think it’s very simple to use after finishing the work?

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.

<<:  Installation and deployment of MySQL Router

>>:  Use personalized search engines to find the personalized information you need

Recommend

MySQL query optimization using custom variables

Table of contents Optimizing sorting queries Avoi...

Mini Program Custom TabBar Component Encapsulation

This article example shares the specific code for...

jQuery implements simple button color change

In HTML and CSS, we want to set the color of a bu...

MySQL data types full analysis

Data Type: The basic rules that define what data ...

MySQL randomly extracts a certain number of records

In the past, I used to directly order by rand() t...

The difference between div and table in speed, loading, web application, etc.

1: Differences in speed and loading methods The di...

How to convert Chinese into UTF-8 in HTML

In HTML, the Chinese phrase “學好好學” can be express...

The Complete List of MIME Types

What is MIME TYPE? 1. First, we need to understan...

Detailed explanation of Docker compose orchestration tool

Docker Compose Docker Compose is a tool for defin...

Implementation of Docker deployment of SQL Server 2019 Always On cluster

Table of contents Docker deployment Always on clu...

How to use CocosCreator for sound processing in game development

Table of contents 1. Basics of audio playback in ...

Things to note when writing self-closing XHTML tags

The img tag in XHTML is so-called self-closing, w...

The concept of MySQL tablespace fragmentation and solutions to related problems

Table of contents background What is tablespace f...