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

Detailed explanation of DOM style setting in four react components

1. Inline styles To add inline styles to the virt...

Detailed explanation of how to view MySQL memory usage

Preface This article mainly introduces the releva...

XHTML Getting Started Tutorial: Using the Frame Tag

<br />The frame structure allows several web...

SQL query for users who have placed orders for at least seven consecutive days

Create a table create table order(id varchar(10),...

Install centos7 virtual machine on win10

1. Download VMware Workstation 64 version https:/...

Basic knowledge of website design: newbies please read this

Now many people are joining the ranks of website ...

What is WML?

WML (Wireless Markup Language). It is a markup la...

Implementation of debugging code through nginx reverse proxy

background Now the company's projects are dev...

Detailed explanation of the use of shared memory in nginx

In the nginx process model, tasks such as traffic...

Design a data collector with vue

Table of contents Scenario Core Issues Status mon...

Summary of relevant knowledge points of ajax in jQuery

Preface Students who learn JavaScript know that A...

Docker installs ClickHouse and initializes data testing

Clickhouse Introduction ClickHouse is a column-or...

js canvas realizes circular water animation

This article example shares the specific code of ...

The principle and basic use of Vue.use() in Vue

Table of contents Preface 1. Understanding with e...

Conditional comment style writing method and sample code

As front-end engineers, IE must be familiar to us...