简体   繁体   English

使用CSS制作相对定位的div正方形

[英]Make a relatively positioned div square with CSS

I would like to know if there is a CSS only solution to make sure that a relatively positioned div would be exactly square under any circumstances; 我想知道是否存在仅CSS解决方案,以确保相对定位的div在任何情况下都完全是正方形; I have a script that would allow a user to upload an image which would apply it to the background image property. 我有一个脚本,该脚本将允许用户上传图像,并将其应用于背景图像属性。

The problem is that the system needs to support images of all sizes, so I can't just set a specific height and width. 问题在于系统需要支持所有尺寸的图像,因此我不能仅设置特定的高度和宽度。

Step 1 第1步

Simply give the div padding-bottom of 100% to create an intrinsic ratio, which means the div will always be square, no matter the width. 只需将div填充底部设置为100%即可创建固有比率,这意味着div不论宽度如何都将始终为正方形。 Then use position: relative/absolute to position a child element that strechtes the whole container. 然后使用position:relative / absolute放置拉伸整个容器的子元素。

This is a common CSS only technique to embed videos at certain aspect ratios, but it's not limited to that specific content type. 这是一种通用的CSS唯一技术,可以以特定的宽高比嵌入视频,但不限于特定的内容类型。

I created a fiddle to see it in action. 我创建了一个小提琴以查看其实际效果。 Please note: I used SuitCSS' flex embed component here, but you are in no way bound to using it. 请注意:我在这里使用了SuitCSS的flex embed组件,但是您绝不会使用它。

http://jsfiddle.net/mlnmln/tfm6q/1/ http://jsfiddle.net/mlnmln/tfm6q/1/

HTML: HTML:

<div class="FlexEmbed FlexEmbed-ratio">
  <div class="UserImage FlexEmbed-content"></div>
</div>

CSS: CSS:

/*
 * FlexEmbed component from suit-css. 
 * @see: https://github.com/suitcss/components-flex-embed/blob/master/lib/flex-embed.css
 * @see: http://suitcss.github.io/components-flex-embed/test/
 * @see: http://alistapart.com/article/creating-intrinsic-ratios-for-video
 */ 

.FlexEmbed {
  display: block;
  overflow: hidden;
  position: relative;
}

/**
 * The aspect-ratio hack is applied to an empty element because it allows
 * the component to respect `max-height`. Default aspect ratio is 1:1.
 */

.FlexEmbed-ratio {
  display: block;
  padding-bottom: 100%;
  width: 100%;
}

/**
 * Fit the content to the aspect ratio
 */

.FlexEmbed-content {
  bottom: 0;
  height: 100%;
  left: 0;
  position: absolute;
  top: 0;
  width: 100%;
}

Step 2 第2步

Find a method to resize image to fit/fill its container. 找到一种方法来调整图像大小以适合/填充其容器。 I would suggest the following solutions. 我建议以下解决方案。

  1. Backend: Crop the image through an image manipulation library like GDlib or ImageMagick. 后端:通过图像处理库(如GDlib或ImageMagick)裁剪图像。 That way users will only have to download the data they need. 这样,用户将只需要下载所需的数据。

  2. CSS: Use background-size: cover if your don't care about legacy browser support and don't have access to server side image manipulation. CSS:使用background-size:如果您不关心旧版浏览器支持并且无权访问服务器端图像操作,则进行覆盖。

    .UserImage { background-size: cover; .UserImage {background-size:cover;
    background-image: url( http://placekitten.com/200/400 ); 背景图片:url( http://placekitten.com/200/400 ); } }

3: JS: Position and crop the image container using DOM manipulation. 3:JS:使用DOM操作定位和裁剪图像容器。 This is basically the same Math as using a server-side image library, with the disadvantage that you place the load on the client (bandwidth and processing). 这与使用服务器端图像库的数学基本相同,但缺点是将负载(带宽和处理)放在客户端上。

Hoping to help. 希望能对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM