【问题标题】:background cover still leaves images too big背景封面仍然留下图像太大
【发布时间】:2016-02-17 01:38:12
【问题描述】:

我正在尝试为我的作品集展示一堆徽标。我有 9 个不同大小的不同徽标,它们只是不适合他们的 div 框。我虽然 background-size:cover 可以修复它,但有些东西刚刚关闭。有人能指出我正确的方向,让这些图像正确按比例缩小,以免徽标被切割吗?

html:

<div class="page-wrapper__content">
  <h2 class="page-wrapper__heading">Projects and Skills</h2>
  <p class="page-wrapper__description">hey</p>
  <div class="imagebox">
    <div class="logorow">
      <div class="logo html"></div>
      <div class="logo css"></div>
      <div class="logo javascript"></div>
    </div>
    <div class="logorow">
      <div class="logo jquery"></div>
      <div class="logo bootstrap"></div>
      <div class="logo sass"></div>
    </div>
    <div class="logorow">
      <div class="logo python"></div>
      <div class="logo django"></div>
      <div class="logo github"></div>
    </div>
  </div>
</div>

css:

  .page-wrapper__content {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    flex-wrap: wrap;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    padding: 0 30%;
    color: #fff; //white
    transform: skewX(18deg);
    transition: transform 1s, opacity 1s;
    background-size: cover;
}


  .imagebox {
    border: 3px solid red;
    width: 100%;
    height: 33%;
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;
    justify-content: space-around;
    .logorow {
      display: flex;
      flex-wrap: nowrap;
      border: 1px solid yellow;
      height: 33%;
      width: 100%;
    }
      .logo {
        background-size: cover;
        border: 1px solid purple;
        height: 100%;
        width: 100%;
      }
    .html {
      background-image: url('../images/html5.png');
    }

【问题讨论】:

标签: html css image flexbox


【解决方案1】:

只需使用background-size:contain; 而不是cover
contain,根据MDN docs

尽可能大地缩放图像并保持图像纵横比(图像不会被压扁)的关键字。图像在容器内加了信箱。当图像和容器具有不同的尺寸时,空白区域(左/右的顶部/底部)用背景色填充。除非被其他属性(例如 background-position)覆盖,否则图像会自动居中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多