这里介绍7种方法,仅供参考。

1、vm单位

<div class="square-shape">这是一个可以自适应的正方形,此法适用于移动端web页面。</div>
.square-shape {
  width: 50%;
  height: 50vw;
  border: 1px solid #f00;
}

2、padding-top实现

<div class="square-shape">这是一个可以自适应的正方形,设置高度避免被内容撑开多余的高度。奇怪的是,内容在框框外面。</div>
.square-shape {
  width: 50%;
  height: 0;
  padding-top: 50%;
  border: 1px solid #f00;
}

3、padding-bottom实现

<div class="square-shape">这是一个可以自适应的正方形,此法很好用,有内容也是正方形。</div>
.square-shape {
  width: 30%;
  height: 0;
  padding-bottom: 30%;
  border: 1px solid #f00;
}

4、伪元素的margin-top

<div class="square-shape">这里如果有内容,就不是正方形喽,限制height也没有用。</div>
.square-shape {
  width: 100%;
  overflow: hidden;
  border: 1px solid #f00;
}
.square-shape:after {
  content: '';
  display: block;
  margin-top: 100%;
}

5、伪元素的padding-top

<div class="square-shape">这里如果有内容,就不是正方形喽。</div>
.square-shape {
  width: 30%;
  max-width: 200px;
  border: 1px solid #f00;
}
.square-shape:after {
  content: '';
  display: block;
  padding-top: 100%;
}

6、子元素margin-top

<div class="square-shape">
  <div class="content">这是一个可以自适应的正方形</div>
</div>
.square-shape{
  overflow: hidden;
  width: 50%;
  background-color: black;
}
.content{
  margin-top: 100%;
}

7、伪元素的padding-bottom,内嵌absolute元素

<div class="square-shape">
    <div class="content">这是一个可以自适应的正方形</div>
</div>
.square-shape {
  width: 50%;
  border: 1px solid #f00;
}
.square-shape:after {
  content: '';
  display: block;
  padding-bottom: 100%;
}
.content {
  position: absolute;
  width: 100%;
  height: 100%;
}

css实现自适应正方形的更多相关文章

  1. css实现自适应正方形的多种方法实现

    方案一:CSS3 vw 单位 CSS3 中新增了一组相对于可视区域百分比的长度单位vw.vh.vmin.vmax.其中vw是相对于视口宽度百分比的单位,1vw = 1% viewport width, ...

  2. css实现自适应正方形的方法

    页面布局中,我们经常需要用百分比来实现宽度自适应,但是如果这时候高度要跟宽度呈固定比例变化,该怎么办呢? 很简单,我们可以利用元素的padding或margin的百分比值是参照父元素的宽度这一特性来实 ...

  3. CSS 实现自适应正方形

    在处理移动端页面时,我们有时会需要将banner图做成与屏幕等宽的正方形以获得最佳的体验效果,比如,商品详情页, 方法1.CSS3 vw单位 CSS3 中新增了一组相对于可视区域百分比的长度单位 vw ...

  4. CSS实现三角形、梯形、平行四边形、圆形、椭圆形、对话框、自适应正方形

    本文篇幅较长,希望能坚持看完,转载请注明出处,如果觉得好文请给个赞吧 CSS实现梯形 CSS实现三角形和梯形主要是依靠border是梯形的特性来做的,有点像相框的那种感觉. 首先我们先给一个正方形设置 ...

  5. div等比例缩放-------纯CSS实现自适应浏览器宽度的正方形

    摘自:https://blog.csdn.net/u010513603/article/details/78200207 1.方案一:CSS3 vw 单位 CSS3 中新增了一组相对于可视区域百分比的 ...

  6. 常见css垂直自适应布局(css解决方法)

    css3的盒模型, css3中添加弹性盒模型,最新弹性盒模型是flex,之前为box <!DOCTYPE html> <html > <head> <titl ...

  7. 常见css水平自适应布局

    左右布局,左边固定,右边自适应布局 BFC方法解决 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  8. css实现自适应屏幕高度;

    css实现自适应屏幕高度: <!DOCTYPE html><html lang="en"><head> <meta charset=&qu ...

  9. 移动端 css实现自适应正圆 ( 宽高随着手机屏幕宽度自适应 )

    序言:应朋友要求随手写了一下移动端 css实现自适应正圆 ( 宽高随着手机屏幕宽度自适应 ) ,以备后用 LESS代码: .adaptive-circle { margin: 50px auto 0; ...

随机推荐

  1. 最小化安装k8s

    最小化安装k8s Nick_4438 关注 2018.07.11 10:40* 字数 670 阅读 0评论 0喜欢 0 1.前言 之前写过一篇二进制手工安装k8s的文章,过程复杂,搞了多日才安装成功. ...

  2. Excel:函数中的万金油:INDEX+SMALL+IF+ROW

      很多人在Excel中用函数公式做查询的时候,都必然会遇到的一个大问题,那就是一对多的查找/查询公式应该怎么写?大多数人都是从VLOOKUP.INDEX+MATCH中入门的,纵然你把全部的多条件查找 ...

  3. 「Vue」父子组件之间的传值及调用方法

    a.父组件向子组件传值data(){},props数据区别data中的数据可读可写,是自己的数据props是个数组,中的数据是父组件传递过来的,只读不能写<login :dmsg='msg'&g ...

  4. es6笔记(4) Set数据结构

    概要 介绍: 集合是由一组无序且唯一的项组成的,这个数据结构使用了与有限集合相同的数学概念,应用在计算机的数据结构中. ES6提供了数据结构Set.它类似于数组,但是没有重复的值. 特点: key与v ...

  5. 记关于vue-cli3 本地代理模拟数据的实践

    网上说的基本都是使用express或http-server作为服务器或其它什么东西自己把玩php也有些年头,就用php好了 服务环境 apache,php先配置好隐藏php后缀扩展名: 在httpd. ...

  6. mybatis介绍——(一)

    官方API:http://www.mybatis.org/mybatis-3/index.html 中文: http://www.mybatis.org/mybatis-3/zh/index.html ...

  7. 【干货】使用EnCase来分析windows 7文件系统------认识元数据记录$MFT,数据恢复

    来源:Unit 6: Windows File Systems and Registry 6.1 Windows File Systems and Registry Windows NTFS File ...

  8. BGM时长

    1.can u feel it 00:08-00:30 22s 2.纤夫的爱 00:43-00:54 11s 3.渡情 00:55-01:52 57s 4.nobody 01:56-02:25 29s ...

  9. Linux DRM KMS 驱动简介【转】

    转自:https://blog.csdn.net/yangkuanqaz85988/article/details/48689521 Whoops,上次写完<Linux DRM Graphic ...

  10. 总结WCF开发中遇到的几个问题

    最近的项目,需要用到WCF,在以前的工作中,经常是将WCF托管在IIS中,主要有几下几个原因:      第一:部署非常方便,和部署一个站点没什么区别:      第二:不受防火墙的影响,因为一般服务 ...