美文网首页
宽高等比布局

宽高等比布局

作者: yuho_小豪哥 | 来源:发表于2019-11-04 10:28 被阅读0次

实际开发中有时候会需要用到一种布局方式,即 DOM 元素的宽高始终为一特定比例。而这种布局的实现主要依赖于一个基础的 CSS 知识点:当 margin/padding 取形式为百分比的值时,无论是 left/right,还是 top/bottom,都是以父元素的width为参照物的,比如 padding: 100% 就等于父元素的宽度。

W3C 规范

Note that in a horizontal flow, percentages on ‘margin-top’ and ‘margin-bottom’ are relative to the width of the containing block, not the height (and in vertical flow, ‘margin-left’ and ‘margin-right’ are relative to the height, not the width).
Note that percentages on ‘padding-top’ and ‘padding-bottom’ are relative to the width of the containing block, not the height (at least in a horizontal flow; in a vertical flow they are relative to the height).

大意便是,水平流中,margin、padding 的 top/bottom 百分比都是以父元素宽度为参照的;而在垂直流中,margin 的 left/right 以及 padding 的 top/bottom 的百分比则是以父元素高度为参照。

实例分析

实现如下布局

  • A 元素左右距离10px
  • A 元素在窗口水平垂直居中
  • A 元素中的文字A水平垂直居中
  • A 的高度始终为A宽度的50%
html.png

代码实现

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>宽高等比布局</title>
    <style>
      .parent{
        overflow: hidden;
        position: absolute;
        left: 10px;
        right: 10px;
        top: 50%;
        transform: translateY(-50%);
        background-color: #c52121;
      }

      .child{
        width: 100%;
        padding-top: 50%;
        background-color: #988f8f;
      }
      span{
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        font-size: 20px;
        color: #D8D8D8;
      }

      img{
        float: left;
        margin-right: 10px;
        border: 2px solid #fdf6e3;
      }
    </style>
</head>
<body>
  <section class="parent">
      <div class="child">
        <span>
          <img src="https://tuimeizi.cn/sexy?w=300&h=300&o=2" alt="" width="200px">测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例测试案例
        </span>
      </div>
  </section>
</body>
</html>

参考文章

相关文章

  • 宽高等比布局

    实际开发中有时候会需要用到一种布局方式,即 DOM 元素的宽高始终为一特定比例。而这种布局的实现主要依赖于一个基础...

  • canvas/onmousedown/onmouseover/o

    canvas 宽高等比缩放,不要用css控制初始宽高 onmouseenter|onmouseleave与onmo...

  • 图片等比设置宽高

    图片设置等比,除去父级100%,子元素padding-bottom外也可以直接css属性。css属性:aspect...

  • 利用Canvas压缩图片

    实现方法说明: 1 调用wx.getImageInfo取得图片原始宽高,计算输出图片宽高(限定短边等比缩放) 2 ...

  • react-native Image resizeMode 属性

    1.cover:(默认) 等比例填充image设定的宽高,图片可能会有裁剪: 2.stretch: 非等比例填充i...

  • Masonry 的进阶 用法

    等比例 :如果给出一个 button,不要写死button 的宽高,否则不同设备显示会有偏差,所以,我们可以用等比...

  • canvas

    js 不建议用css控制canvas宽高,会等比缩放。852 CanvasRenderingContext2D.f...

  • 两种方式实现CSS双飞翼布局

    双飞翼布局,就是两端固定宽高,中间自适应的三栏布局 先来张图,左边和右边的灰色块是固定宽高的,中间绿色的区域是宽高...

  • 第二章:第一节 移动端布局

    一、居中布局 居中布局通常分为两种,一种是固定宽高,另一种是非固定宽高。 非固定宽高通常都是靠里面的内容来撑起盒子...

  • react native布局笔记

    react native 布局笔记 宽高 在react native中,高宽都是无单位的(unitless),它代...

网友评论

      本文标题:宽高等比布局

      本文链接:https://www.haomeiwen.com/subject/ncvrbctx.html