Home >Web Front-end >CSS Tutorial >CSS3 uses vw and vh to implement adaptive code examples
The implementation of responsive layout relies on media queries (Media Queries). Select the width size of mainstream devices as breakpoints to write additional styles for adaptation. However, this will be more troublesome and can only be done on a few selected ones. Perfect fit for mainstream device sizes. This article mainly introduces you to the relevant information about pure css3 using vw and vh to achieve self-adaptation. I hope it can help you.
Even if adaptation is implemented through rem units, a script needs to be embedded to dynamically calculate the size of the root element. ·
In recent years, as the support for viewport units on mobile terminals has become more mature and widespread, we can try a new method to truly adapt to all device sizes.
Understanding Viewport units
First of all, we need to understand what a viewport is.
In the industry, a highly respected theory is the explanation about the viewport proposed by Peter-Paul Koch (known as the "PPK Master" in the world) - on the desktop, the viewport refers to the desktop, It refers to the visible area of the browser; on the mobile side, it is more complicated, and it involves three viewports: Layout Viewport (layout viewport), Visual Viewport (visual viewport), and Ideal Viewport.
The "viewport" in the viewport unit undoubtedly refers to the visible area of the browser on the desktop; but on the mobile side, it refers to the Layout among the three Viewports. Viewport.
The "viewport" in the viewport unit
According to the CSS3 specification, the viewport unit mainly includes the following four:
vw: 1vw is equal to 1% of the viewport width
vh: 1vh is equal to 1% of the viewport height
//iPhone 6尺寸作为设计稿基准 $vm_base: 375; @function vw($px) { @return ($px / 375) * 100vw; }2. Whether it is text or layout height, width, spacing, etc., vw is used as the CSS unit
.mod_nav { background-color: #fff; &_list { display: flex; padding: vm(15) vm(10) vm(10); // 内间距 &_item { flex: 1; text-align: center; font-size: vm(10); // 字体大小 &_logo { display: block; margin: 0 auto; width: vm(40); // 宽度 height: vm(40); // 高度 img { display: block; margin: 0 auto; max-width: 100%; } } &_name { margin-top: vm(2); } } } }3.1 Physics Pixel lines (that is, 1px on a normal screen and 0.5px on a high-definition screen) are implemented using the transform attribute scale.
//code from http://caibaojian.com/vw-vh.html .mod_grid { position: relative; &::after { // 实现1物理像素的下边框线 content: ''; position: absolute; z-index: 1; pointer-events: none; background-color: #ddd; height: 1px; left: 0; right: 0; top: 0; @media only screen and (-webkit-min-device-pixel-ratio: 2) { -webkit-transform: scaleY(0.5); -webkit-transform-origin: 50% 0%; } } ... }4. For pictures that need to maintain the aspect ratio, padding-top should be used instead
.mod_banner { position: relative; padding-top: percentage(100/700); // 使用padding-top height: 0; overflow: hidden; img { width: 100%; height: auto; position: absolute; left: 0; top: 0; } }From this, we can achieve a common layout page effect as follows: Method 2: Use vw and rem to make the layout more optimizedAlthough such a page looks well adapted, you will find that it uses the viewport The layout implemented by the unit automatically scales depending on the viewport size. No matter the viewport is too large or too small, it also loses the maximum and minimum width restrictions as the viewport becomes too large or too small. Of course, you don’t have to care about such a small unfriendly user experience, but we still try to fix such small flaws.
于是,联想到不如结合rem单位来实现布局?rem 弹性布局的核心在于动态改变根元素大小,那么我们可以通过:
给根元素大小设置随着视口变化而变化的 vw 单位,这样就可以实现动态改变其大小。
限制根元素字体大小的最大最小值,配合 body 加上最大宽度和最小宽度
这样我们就能够实现对布局宽度的最大最小限制。因此,根据以上条件,我们可以得出代码实现如下:
// rem 单位换算:定为 75px 只是方便运算,750px-75px、640-64px、1080px-108px,如此类推 $vm_fontsize: 75; // iPhone 6尺寸的根元素大小基准值 @function rem($px) { @return ($px / $vm_fontsize ) * 1rem; } // 根元素大小使用 vw 单位 $vm_design: 750; html { font-size: ($vm_fontsize / ($vm_design / 2)) * 100vw; // 同时,通过Media Queries 限制根元素最大最小值 @media screen and (max-width: 320px) { font-size: 64px; } @media screen and (min-width: 540px) { font-size: 108px; } } // body 也增加最大最小宽度限制,避免默认100%宽度的 block 元素跟随 body 而过大过小 body { max-width: 540px; min-width: 320px; }
小结
相对于做法一,个人比较推崇做法二,有以下两点原因:
第一,做法二相对来说用户视觉体验更好,增加了最大最小宽度的限制;
第二,更重要是,如果选择主流的rem弹性布局方式作为项目开发的适配页面方法,那么做法二更适合于后期项目从 rem 单位过渡到 vw 单位。只需要通过改变根元素大小的计算方式,你就可以不需要其他任何的处理,就无缝过渡到另一种CSS单位,更何况vw单位的使用必然会成为一种更好适配方式,目前它只是碍于兼容性的支持而得不到广泛的应用。
相关推荐:
The above is the detailed content of CSS3 uses vw and vh to implement adaptive code examples. For more information, please follow other related articles on the PHP Chinese website!