CSS: 图像宽度比例

CSS: Image scale with width

我有一个 div 和一个 image background(在 React 中有 Next.js,但这不重要)。

import styles from './my-component.module.css';
import Background from '../../../public/assets/images/background-image.png';

// later

<div
  style={{ backgroundImage: `url(${Background})` }}
  className={`${styles['background-image']}`}
>

这是我目前正在使用的CSS:

.background-image {
  border-radius: 8px;
  background-size: cover;
  background-repeat: no-repeat;
  height: auto;
}

我无法获取全高图像。它应该始终采用显示器的宽度,但在不拉伸或重复的情况下尽可能多地采用高度。我该怎么做?

更新:是的,容器的高度比元素高。

更新 2 Viira 的解决方案几乎可以工作,但图像不符合右侧的填充。

也许这会有所帮助。

不要在背景图片中设置它尝试将它插入 img 标签

*{box-sizing: border-box;}
body {
  margin: 0;
}
.background-image {
  
  border-radius: 8px;
  background-size: cover;
  background-repeat: no-repeat;
  height: auto;
}

.container {
  display: table;
  width: 100%;
}

.img {
  display: table-cell;
  width: 100%;
  position: absolute;
  z-index: -1;
}
 
.img img {
  width: 100%;
}

.text {
  display: table-cell;
  padding: 30px;
  width: 100%;
}
<div class="background-image">
  <div class="container">
    <div class="img">
      <img src="https://image.shutterstock.com/image-photo/beautiful-water-drop-on-dandelion-260nw-789676552.jpg">
      </div>
    <div class="text">
  <h1>Are you ready</h1>
  <p>Click the link</p>
  <button>Click Here</button>
    </div>
    </div>
</div>

注意:由于图像分辨率低,因此图像被拉伸至全宽。您可以将其宽度设置为自动以显示其原始大小。

解决方案 2:

正如 OP 所提到的,图像不考虑填充。我想出了一个解决方案。由于显示了 .container div:table;那里不允许填充。因此,通过为其父级 .background-image 提供填充,此问题将得到解决。

*{box-sizing: border-box;}
body {
  margin: 0;
}
.background-image {
  
  border-radius: 8px;
  padding: 15px 40px;
}

.container {
  display: table;
  width: 100%;
  max-width: 100%;
  position: relative;
}

.img {
  display: table-cell;
  width: 100%;
  position: absolute;
  z-index: -1;
}
 
.img img {
  width: 100%;
}

.text {
  display: table-cell;
  padding: 30px;
  width: 100%;
}
<div class="background-image">
  <div class="container">
    <div class="img">
      <img src="https://image.shutterstock.com/image-photo/beautiful-water-drop-on-dandelion-260nw-789676552.jpg">
      </div>
    <div class="text">
  <h1>Are you ready</h1>
  <p>Click the link</p>
  <button>Click Here</button>
    </div>
    </div>
</div>

为 .background-image 设置填充 div 以便应用填充。

因此您希望您的图像决定您 div 的高度。这在使用图像作为背景时是不可能的。

您需要将图像作为一个元素加载到 dom 中,然后可以使用该元素来确定 div 的高度,如果您想在此图像的顶部显示一些内容情况下,您必须将其叠加在图像之上。

这是一个示例 html 结构,您可以使用它来实现此目的

<div style="position:relative">
  <img src="https://images.unsplash.com/photo-1573496528816-a104a722b3db?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1950&q=80" style="width:100%; height: auto;" />
  <div style="position:absolute; top: 0; left: 0; width: 100%; height: 100%">
    // YOUR CONTENT
  </div>
</div>

以下是我最终解决问题的方法:

import PropTypes from 'prop-types';
import React from 'react';

import styles from './background-image-card.module.css';

function BackgroundImageCard({ alt, children, className, image }) {
  return (
    <div className={`${className} ${styles.container}`}>
      <div className={styles.overlay} />
      <img alt={alt} src={image} className={styles['background-image']} />
      <div className={styles.content}>{children}</div>
    </div>
  );
}

BackgroundImageCard.propTypes = {
  alt: PropTypes.string,
  children: PropTypes.node,
  className: PropTypes.string,
  image: PropTypes.string,
};

export default BackgroundImageCard;
.container {
    align-items: center;
    display: flex;
    justify-content: center;
    position: relative;
    width: 100%;
}

.overlay {
    background: var(--gray-800);
    border-radius: 8px;
    height: 100%;
    opacity: 0.8;
    position: absolute;
    width: 100%;
}

.background-image {
    border-radius: 8px;
    height: auto;
    width: 100%;
    z-index: -1;
}

.content {
    position: absolute;
}