简体   繁体   English

CSS:图像按宽度缩放

[英]CSS: Image scale with width

I have a div with an image background (in React with Next.js, but that shouldn't matter).我有一个带有图像背景的 div(在 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']}`}
>

Here is the CSS I'm currently using:这是我目前使用的 CSS:

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

I can't get the image to take the full height.我无法让图像占据全高。 It should always take the display's width, but take as much height as it can without stretching or repeating.它应该始终采用显示器的宽度,但在不拉伸或重复的情况下尽可能多地采用高度。 How can I do that?我怎样才能做到这一点?

在此处输入图片说明

Update: Yes, the container has more height than the element.更新:是的,容器的高度高于元素。

在此处输入图片说明

Update 2 Viira's solution almost works, but the image doesn't respect padding on the right side.更新 2 Viira 的解决方案几乎有效,但图像不考虑右侧的填充。

在此处输入图片说明

Maybe this can help.也许这会有所帮助。

Don't set it in background image try insert it in img tag不要将其设置在背景图像中尝试将其插入 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>

NOTE: The image is stretched to full width because its resolution is low.注意:图像被拉伸到全宽,因为它的分辨率很低。 you can set it's width to auto to display its original size.您可以将其宽度设置为自动以显示其原始大小。

Solution 2:解决方案2:

As the OP mentioned, padding is not respected by the image.正如 OP 所提到的,图像不尊重填充。 I came up with a solution.我想出了一个解决方案。 Since the .container div is in display: table;由于 .container div 在 display: table; padding isn't allowed there.那里不允许填充。 So, by giving the padding to its parent .background-image this issue will be lifted.因此,通过将填充提供给其父 .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>

Set padding for .background-image div so that the padding will apply.为 .background-image div 设置填充,以便应用填充。

So you want your image to determine the height of your div.所以你想让你的图像来确定你的 div 的高度。 That is not possible when using the image as background.当使用图像作为背景时,这是不可能的。

You need to load image into dom as an element which can then be used to determine the height of your div, if you want to show some content on top of the image in this case, you will have to overlay it on top of the image.您需要将图像作为元素加载到 dom 中,然后可用于确定 div 的高度,如果您想在这种情况下在图像顶部显示一些内容,则必须将其覆盖在图像顶部.

Here's a sample html structure you can use to achieve this这是您可以用来实现此目的的示例 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>

Here is how I ended up solving it:这是我最终解决它的方法:

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;
}

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

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