简体   繁体   English

图像高度大小不会改变,只有宽度会改变

[英]Image Height size doesn't change and only width that change

I know it a bit strange to ask from the title, but I have a problem when changing the size of image inside the carousel. 我知道从标题中提出问题有点奇怪,但是在更改轮播中图像的大小时遇到​​问题。 no matter what I did the height of my image doesn't change instead it looks like give an effect like object-fit: cover what I want is to show the image to have size around width: 30vw; 不管我做了什么,图像的高度都不会改变,而是给人一种像object-fit: cover的效果object-fit: cover我想要的就是显示图像的width: 30vw; and height: 30vh; height: 30vh; but every time I change the width its work but when I change the height its not work. 但每次更改width起作用,但是每次更改height不会起作用。

I have some guess such as the container size, the col size, and the div that wrap my image will work if i change their size but in the end its not. 我有一些猜测,例如容器大小,col大小和包裹我的图像的div,如果我更改它们的大小,它们将起作用,但最终不会。

I already read some topic about this : 我已经阅读了一些有关此的主题:

css-object-fit-contain-is-keeping-original-image-width-in-layout CSS对象适合包含原始图像宽度布局

scaling-centering-and-cropping-image-with-object-fit-and-object-position 缩放中心和裁剪图像与对象适合和对象位置

but it still not work. 但它仍然无法正常工作。 Let's say I want to make my image size around width: 360px and height: 360px for example 假设我想将图片尺寸设置为width: 360pxheight: 360px

this is my code: 这是我的代码:

import React from "react";

// CSS
import './klien.css'

const Klien = () => {

    return (
        <div className="klien">
            <div className="klien-container">
                <p className="section-title">Klien Kita</p>

                <div className="carousel slide" data-ride="carousel">

                    <div className="carousel-item active">
                        <div className="col-12 col-sm-12 col-md-8 col-lg-8 card-center card-size">
                            <div className="mb-3">                    
                                <div className="text-center">
                                    <img className="img-size" src="http://placehold.it/360x360" alt="klien-logo" />
                                </div>              
                            </div>
                        </div>
                    </div>

                    <div className="carousel-item">
                        <div className="col-12 col-sm-12 col-md-8 col-lg-8 card-center card-size ">
                            <div className="mb-3">                    
                                <div className="text-center">
                                    <img className="card-img-top img-size" src="http://placehold.it/360x360" alt="klien-logo" />
                                </div>              
                            </div>
                        </div>
                    </div>

                    <div className="carousel-item">
                        <div className="col-12 col-sm-12 col-md-8 col-lg-8 card-center card-size">
                            <div className="mb-3">                    
                                <div className="text-center">
                                    <img className="card-img-top img-size" src="http://placehold.it/360x360" alt="klien-logo" />
                                </div>              
                            </div>
                        </div>
                    </div>

                    <div className="carousel-item">
                        <div className="col-12 col-sm-12 col-md-8 col-lg-8 card-center card-size">
                            <div className="mb-3">                    
                                <div className="text-center">
                                    <img className="card-img-top img-size" src="http://placehold.it/360x360" alt="klien-logo" />
                                </div>              
                            </div>
                        </div>
                    </div>

                </div> 
            </div>
        </div>
    );        
}

export default Klien;

and this is my css: 这是我的CSS:

.klien{
    background-color: #f6f7fd;
    min-height: 50vh;
}

.klien .klien-container{
    overflow: hidden;
    padding-top: 10vh;
}

.klien .section-title{
    color: #3b73c5;
    font-size: 3em;
    font-weight: 500;
    text-align: center;
}

.klien .carousel-size{
    /* height: 20vh; */
}

.klien .card-center{
    margin: 0% auto;
}

.klien .text-center{
    text-align: center;
}

.klien .card-size{
    max-height: 20vh;
}

.klien .img-container{
    height: 20vh;
    width: 20vh;
}

.klien .img-size{
    width: 30vw;
    height: 30vh;
    /* object-fit: contain; */
}

can someone help and give explanation about my problem? 有人可以提供帮助并提供有关我的问题的解释吗? in first I thought maybe it is because of object-fit: contain but it seems my guess about it is wrong 首先我以为可能是因为object-fit: contain但是我的猜测似乎是错误的

this is the screenshot: 这是屏幕截图:

更改图像大小时得到的内容

我期望做些什么

I would recommend to use a div with a background image, that way you have control of the responsiveness of your list. 我建议将div与背景图片一起使用,这样您就可以控制列表的响应速度。 It requires almost nothing to perform that change. 它几乎不需要任何更改。

You need to change your img tags to div 您需要将img标签更改为div

<div
  className="img-size"
  style={{ backgroundImage: `url(${"http://placehold.it/360x360"})`}}
/>

Your css 你的CSS

.klien .img-size{
    width: 30vw;
    height: 30vh;
    margin: 0 auto;
    background-position: center;
    background-size: cover;
    /* object-fit: contain; */
}

I made a codepen with this. 我用这个做了一个密码笔 Hope it helps, if you want another approach you can tell me. 希望对您有所帮助,如果您需要其他方法,可以告诉我。

Have you tried to change the image size using inline css? 您是否尝试过使用内嵌CSS更改图像大小? For example <img src="url" width="specified with in px or %"> 例如<img src="url" width="specified with in px or %">

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

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