Home > Web Front-end > CSS Tutorial > What is the idea of ​​​​implementing elements to adapt to the screen size in css?

What is the idea of ​​​​implementing elements to adapt to the screen size in css?

王林
Release: 2020-06-03 17:00:06
forward
3281 people have browsed it

What is the idea of ​​​​implementing elements to adapt to the screen size in css?

Before implementing elements to adapt to the screen size, let’s first introduce a CSS knowledge point. If the values ​​of the margin and padding attributes of

elements (whether they are top and bottom margins or left and right margins) are set to percentages, they are calculated based on the width.

In other words, when the aspect ratio is known, although CSS cannot determine the value of height, it can determine the value of attributes such as padding-top.

Implementation ideas:

1. Calculate the aspect ratio (height/width), and set it to the value of padding-top, and set height to 0 (the height of the element supported by padding-top ).

2. At this time, the actual content of the element is squeezed below, so use absolute positioning to change its position.

(Video tutorial recommendation: css video tutorial)

Implementation code:

html code:

<div class="ac_coupon-wrap">
    <div class="ac_coupon-content">
        <!-- 内容 -->
    </div>
</div>
Copy after login

css code:

.ac_coupon-wrap {
    height: 0;
    padding-top: 15.16%;
    position: relative;
    .ac_coupon-content {
        position: absolute;
        top: 0;
        width: 100%;
        height: 100%;
        background-size: cover;
    }
}
Copy after login

Recommended tutorial: css quick start

The above is the detailed content of What is the idea of ​​​​implementing elements to adapt to the screen size in css?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template