javascript - How to make all images fit in same size div - Stack Overflow

I want them to all fit inside the boxes with the border but I don't want them to appear stretched.

I want them to all fit inside the boxes with the border but I don't want them to appear stretched. how can I do this?

I have tried giving them a fixed height, width inside a div etc but they always appear to not look quite right

<CategoryContainer>
    <CategoryLink to={`${category.name.toLowerCase()}`}>
        <CategoryImage>
            <Image image={category.products[0].image} />
        </CategoryImage>
        <CategoryName>
            {`${category.name}`}
        </CategoryName>
    </CategoryLink>
</CategoryContainer>

and my styled ponents is:

export const Container = styled.div`
  display: flex;
  width: 1000px;
`;

export const CategoryContainer = styled.div`
  height: 300px;
  width: 33%;
  margin-right: 10px;
  position: relative;
  border: 1px solid black;
`;

export const CategoryLink = styled(Link)`
  text-decoration: none;
  color: black;
  z-index: 5;
  position: absolute;
`;

export const CategoryImage = styled.div`
`

export const CategoryName = styled.div`

`

I want them to all fit inside the boxes with the border but I don't want them to appear stretched. how can I do this?

I have tried giving them a fixed height, width inside a div etc but they always appear to not look quite right

<CategoryContainer>
    <CategoryLink to={`${category.name.toLowerCase()}`}>
        <CategoryImage>
            <Image image={category.products[0].image} />
        </CategoryImage>
        <CategoryName>
            {`${category.name}`}
        </CategoryName>
    </CategoryLink>
</CategoryContainer>

and my styled ponents is:

export const Container = styled.div`
  display: flex;
  width: 1000px;
`;

export const CategoryContainer = styled.div`
  height: 300px;
  width: 33%;
  margin-right: 10px;
  position: relative;
  border: 1px solid black;
`;

export const CategoryLink = styled(Link)`
  text-decoration: none;
  color: black;
  z-index: 5;
  position: absolute;
`;

export const CategoryImage = styled.div`
`

export const CategoryName = styled.div`

`
Share Improve this question edited Jan 3, 2018 at 19:07 Iman Bahrampour 6,8002 gold badges45 silver badges65 bronze badges asked Jan 3, 2018 at 17:11 The WalrusThe Walrus 1,2087 gold badges31 silver badges50 bronze badges 5
  • 2 Please include the rendered HTML & CSS ... – DaniP Commented Jan 3, 2018 at 17:13
  • max-height: 100%; max-width: 100%; should work in most browsers, I remember that potentially causing a patibility issue somewhere (I think it was safari) but it was a few years ago and may be ironed out by now. You need to not set an explicit height or width though, because that will cause it to skew. Just the two max properties and nothing else. – jmcgriz Commented Jan 3, 2018 at 17:25
  • @jmcgriz max-width: 100%; max-height: 100%; width: auto; will be sufficient I believe. – Deepak Kamat Commented Jan 3, 2018 at 18:13
  • @DeepakKamat but which ponent above do I apply these to? – The Walrus Commented Jan 4, 2018 at 10:16
  • The image, the img tags inside of the whichever container you are putting them. – Deepak Kamat Commented Jan 4, 2018 at 15:23
Add a ment  | 

2 Answers 2

Reset to default 3

A fairly simple idea is to keep the width relative to the height. Generally most responsive images' height is controlled by its width by setting height: auto and width: 100%, we can just reverse this since in your case the height is variable.

.the-images {
  /* This is where magic happens */
  max-width: 100%;
  height: 100%;
  width: auto;

  display: block;
  margin: 0 auto;
}

Or to maintain aspect ratio if the boxes are fluid in width, you can do this

.the-images {

  /* This is where magic happens */
  max-width: 100%;
  max-height: 100%;
  width: auto;

  display: block;
  margin: 0 auto;
}

A live example:

http://jsbin./vuduvev/edit?css,output

If setting the image as a background is an option: https://css-tricks./almanac/properties/b/background-size/

If not, use object-fit: https://css-tricks./almanac/properties/o/object-fit/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744837592a4596358.html

相关推荐

  • javascript - How to make all images fit in same size div - Stack Overflow

    I want them to all fit inside the boxes with the border but I don't want them to appear stretched.

    2天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信