zoukankan      html  css  js  c++  java
  • 纯css设置元素高度与宽度相等

    设置图片高度等于宽度

    .img-box{
      100%;
      height:0;
      position: relative;
      padding-bottom: 100%
    }
    .img-box img{
      100%;
      height:100%;
      position: absolute;
    }
    

    如果仅仅想要外层div同宽高,而图片保持原图大小不被撑大, 可以将图片属性换成下面这个

    .img-box img{
      max-100%;
      max-height:100%;
      margin: auto;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
    }
    

    如果子元素根据父元素设置宽度,那么将其高度设置为0,并将padding-bottom设置为百分比,则该子元素的高度将根据它的宽度计算

    .img-box{
          400px;
          height:800px;
    }
    .img-box img{
          100%;
          height:0;
          padding-bottom:100%;
          background-color: #0f8bcb;
    }  
    

    如果子元素是图片,需要使用下面的方法

    .img-box-parent{
          400px;
          height:800px;
    }
    .img-box{
          100%;
          height:0;
          padding-bottom:100%;
          position: relative;
    }
    .img-box img{
          100%;
          height:100%;
          position: absolute;
          top:0;
          left:0;
    }
    
  • 相关阅读:
    插件集合
    postgis_LayerTransform
    react-高阶组件
    react-自定义事件
    Immutable 详解及 React 中实践
    babel-preset-env: a preset that configures Babel for you
    彻底解决Webpack打包慢的问题
    打包图片
    drag
    brush
  • 原文地址:https://www.cnblogs.com/black-Q/p/15457429.html
Copyright © 2011-2022 走看看