html - 使用 width % 和 padding-bottom % 时方形 div 的固定大小

标签 html css square

我想创建一个具有固定大小的正方形的 Flexbox。 用 width % 和 padding-bottom % 解决。

但是如果我把图片放在那里,它会改变大小。

我该如何解决?

我尝试了 max-width、max-padding-bottom(哈哈,我知道)、padding-bottom: max(x%),...

我现在什么都不知道。

.flex-container {
	  display: flex;
	  flex-wrap: wrap;
	  justify-content: center;
	  width: 100%;
	}

	.flex-container > div {
    width: 32%;
    padding-bottom: 32%;
    position: relative;
    background-color: #000;
    margin: 0.5%;
	}

img.flex {
		height: 100%;
		width: 100%;
	}
<div class="flex-container">
    <div>
      <img class="flex"     src="https://upload.wikimedia.org/wikipedia/commons/5/54/KDE_4.png">
      
    </div>
	  <div></div>
	  <div></div>  
</div>

<p> some text <p>
<p> some text <p>
  
  <div class="flex-container">
    <div></div>
	  <div></div>
	  <div></div>  
</div>

最佳答案

根据您的代码,填充也是您希望显示图像的空间。

  • 可以设置图片的绝对位置。

.flex-container {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  width: 100%;
}

.flex-container>div {
  width: 32%;
  padding-bottom: 32%;
  position: relative;
  background-color: #000;
  margin: 0.5%;
}

img.flex {
  height: 100%;
  width: 100%;
  position: absolute;
  top: 0;
  left: 0;
}
<div class="flex-container">
  <div>
    <img class="flex" src="https://upload.wikimedia.org/wikipedia/commons/5/54/KDE_4.png">

  </div>
  <div></div>
  <div></div>
</div>

<p> some text
  <p>
    <p> some text
      <p>

        <div class="flex-container">
          <div></div>
          <div></div>
          <div></div>
        </div>

  • 您还可以使用伪元素将您的 div 拉伸(stretch)为正方形以避免占用整个空间。

    • 可以是父级

.flex-container {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  width: 100%;
}

.flex-container::before {
  content: "";
  padding-bottom: 33.33%;
  with: 0;
}

.flex-container>div {
  width: 32%;
  position: relative;
  background: linear-gradient(45deg, rgba(255, 255, 255, 0.1)50%, transparent 50%) #000;
  margin: 0.5%;
}

img.flex {
  width: 100%;
  max-height: 100%;/*might be better ? */
}
<div class="flex-container">
  <div>
    <img class="flex" src="https://upload.wikimedia.org/wikipedia/commons/5/54/KDE_4.png">
  </div>

  <div></div>
  <div></div>
</div>
<p> some text
  <p>
    <p> some text
      <p>
        <div class="flex-container">
          <div></div>
          <div>d</div>
          <div>d</div>
        </div>

  • 或 child

.flex-container {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  width: 100%;
}

.flex-container>div {
  width: 32%;
  position: relative;
  background-color: #000;
  margin: 0.5%;
}

.flex-container>div::before {
  content: '';
  float: left;
  width: 0;
  padding: 50% 0;
}

img.flex {
  height: 100%;
  width: 100%;
}
<div class="flex-container">
  <div>
    <img class="flex" src="https://upload.wikimedia.org/wikipedia/commons/5/54/KDE_4.png">

  </div>
  <div></div>
  <div></div>
</div>

<p> some text
  <p>
    <p> some text
      <p>

        <div class="flex-container">
          <div></div>
          <div></div>
          <div></div>
        </div>

如果可以使用垂直填充或边距来拉伸(stretch)使用父级宽度的 div,您还必须注意该方法使用的空间。

关于html - 使用 width % 和 padding-bottom % 时方形 div 的固定大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58823001/

相关文章:

ios - Square应用是否具有自定义URL方案?

javascript - 设置为 100% 高度的 div 在滚动时不会增长

javascript - 错误跟踪[第 : 42] Le contenu des éléments doit inclure un balisage ou des caractères au format correct 行

html - Chrome 不呈现 <form> 标签,FF 会

php - CSS 如何防止 html 文件的主体和包含的文件合并?

android - 延迟 MockWebServer 响应

javascript string regex替换引号之间的字符串

将文件移动到一个文件夹深处时 css 丢失

javascript - 在不移动网格的其他行的情况下替换行中的所有 div

square - Square API 充电时出现 Not Found 错误