【问题标题】:css width same as heightcss 宽度与高度相同
【发布时间】:2014-03-15 00:34:13
【问题描述】:

我想在我的 css 文件中执行下一个技巧,以便(高度 = 宽度)不设置像素。我想这样做,无论浏览器的分辨率是多少,在这两个维度上都有相同的值。

#test{
    height: 100%;
    width: (same as height);
}

我更喜欢使用 css 而不是 javascript。

提前谢谢你。

【问题讨论】:

  • 此问题可能重复:stackoverflow.com/questions/5445491/…
  • @dpwivagg 我不这么认为。 OP 想要 width: same as height 而不是 height: same as width
  • 不能用 css 引用其他 css 语句。您可以使用更少的 css 来执行此操作。 lesscss.org
  • @Hashem Qolami,我试过用 jquery 来做,但问题是它不适用于智能手机的所有分辨率。

标签: html css


【解决方案1】:

目前唯一的 CSS 方法(AFAIK)是使用视口相关值(vh / vw)

目前支持不是很好:http://caniuse.com/viewport-units 但这里有一个快速演示

JSFiddle

CSS

.box {
    background-color: #00f;
    width: 50vw;
    height:50vw;
}

框是响应式的,但始终保持方形。

% 值将不起作用,因为 height:100% 不等于 width:100%,因为它们指的是不同的事物作为父级的相关维度。

【讨论】:

  • 我认为这是我真正想要的..我会尝试的。谢谢
  • 知道如何在其父元素中定位这样的.box 元素,使其水平和垂直居中吗?
  • 在 SO 上有很多答案。随便搜索一下。
  • 抱歉,我的代码中有一个错误,这要归功于我最喜欢的 margin: auto; position: absolute; top, bottom, right, left: 0; 解决方案无法正常工作,因此问题 - 我将其归咎于 vw
  • @Paulie_D - 这个问题没有“很多答案”。这个解决方案是不正确的,因为它没有回答标题,而是回答了一个非常具体的场景。我来这里寻求标题的答案却一无所获:/
【解决方案2】:

ma​​x-width / max-height 对我有用:

在 div (.box) 中设置一个确定大小的图像。

.parent {
       position: absolute;
}

.box {
    width: 100%;
    height: 100%;
}

.box img {
   max-width: 100%;
   max-height: 100%;
}

【讨论】:

    【解决方案3】:

    另一种选择是使用 img 元素行为来制作 纵横比

    这里我使用 svg 图像并将其与 data url 内联以使其更简单

    要描述所需的纵横比,您可以使用 viewBox svg 属性viewBox='0 0 width-ratio height-ratio'

    例子:

    html,
    body {
        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
            "Ubuntu", "Cantarell", "Fira Sans",
            "Droid Sans", "Helvetica Neue", sans-serif;
        margin: 0;
        padding: 0;
    }
    body {
        margin: 1rem;
    }
    
    .row {
      padding: 8px 0px;
    }
    
    .block {
      display: inline-block;
      vertical-align: top;
      position: relative;
      margin-left: 8px;
      margin-right: 8px;
    }
    
    .block-content {
      position: absolute; 
      top: 0; 
      left: 0; 
      right: 0; 
      bottom: 0; 
      display: flex; 
      justify-content: center; 
      align-items: center;
    }
    
    .ratio--width-to-height {
      height: 100%; 
      width: auto;
    }
    
    .ratio--height-to-width {
      height: auto; 
      width: 100%;
    }
    <h1>
      Block aspect ratio with svg image
    </h1>
    
    <h2>
      width to height
    </h2>
    
    <div class="row">   
      <div class="block" style="background: lime; height: 120px;">
        <img class="ratio--width-to-height" 
        src="data:image/svg+xml;utf8,&lt;svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1 1'&gt;&lt;/svg&gt;">
        <div class="block-content">1 : 1</div>
      </div>
    
      <div class="block" style="background: cyan; height: 120px;">
        <img class="ratio--width-to-height" 
        src="data:image/svg+xml;utf8,&lt;svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 2 1'&gt;&lt;/svg&gt;">
        <div class="block-content">2 : 1</div>
      </div>
      
      <div class="block" style="background: orange; height: 120px;">
        <img class="ratio--width-to-height" 
        src="data:image/svg+xml;utf8,&lt;svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1 1.25'&gt;&lt;/svg&gt;">
        <div class="block-content">1 : 1.25</div>
      </div>
    </div>
    
    <h2>
      height to width
    </h2>
    
    <div class="row">   
      <div class="block" style="background: lime; width: 120px;">
        <img class="ratio--height-to-width" 
        src="data:image/svg+xml;utf8,&lt;svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1 1'&gt;&lt;/svg&gt;">
        <div class="block-content">1 : 1</div>
      </div>
    
      <div class="block" style="background: cyan; width: 120px;">
        <img class="ratio--height-to-width" 
        src="data:image/svg+xml;utf8,&lt;svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 2 1'&gt;&lt;/svg&gt;">
        <div class="block-content">2 : 1</div>
      </div>
      
      <div class="block" style="background: orange; width: 120px;">
        <img class="ratio--height-to-width" 
        src="data:image/svg+xml;utf8,&lt;svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1 1.25'&gt;&lt;/svg&gt;">
        <div class="block-content">1 : 1.25</div>
      </div>
    </div>

    codepen 上的相同示例https://codepen.io/forceuser/pen/MMWBBx

    【讨论】:

    • 请说明“html”的用法,我在 W3School 或任何其他文档中都找不到它
    • 对于因问题标题而来到这里的任何人,这就是您正在寻找的答案。您使用比例为 1:1 的子图像标签,并将父图像的宽度设为自动。对于因无知而发布“可能重复”的人来说,它不是重复的,因为百分比填充技巧适用于父级的宽度而不是高度。所以它只有在你试图使高度与宽度匹配时才有效,而不是相反
    • 纵横比属性在技术上是最好的方法,但它仅在某些浏览器的最新版本中受支持。这是目前最好的。
    【解决方案4】:

    这是我的方式

      aspect-ratio: 1 / 1;
      height: 100%;
    

    例子:

    .my-container {
      width: 100%;
      height: 150px;
      background: black;
      padding: 20px
    }
    
    .my-element {
      background: #fff;
      aspect-ratio: 1 / 1;
      height: 100%;
    }
    <div class="my-container">
      <div class="my-element">Height = Width</div>
    </div>

    【讨论】:

    • 太棒了!但是我们可以使用它吗? caniuse.com/mdn-css_properties_aspect-ratio - 目前支持率约为 70%,基本上 Safari 还没有实现它。如果您使用它,您的网站将在 iPhone 和 Safari 上中断。 ?‍♂️
    • 一亿个赞...Safari 现在支持这个...谢谢...这应该是最佳答案
    猜你喜欢
    • 1970-01-01
    • 2018-07-04
    • 2015-01-18
    • 2018-12-29
    • 1970-01-01
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    相关资源
    最近更新 更多