简体   繁体   English

将图像宽度设置为页面宽度的一半

[英]Set image width to half page width

When trying to make the images on my page equal to 50 percent (half the page) it just sets my images equal to half their original size. 当试图使页面上的图像等于50%(页面的一半)时,它会将图像设置为原始尺寸的一半。 What i want is to maintain the image widths (100 percent), and make the flexbox boxes that contain each image, equal to 50 percent (half the page). 我想要的是保持图像宽度(100%),并使包含每个图像的flexbox框等于50%(半页)。 How do I achieve this while maintaining 100 percent of the images? 如何在保持100%的图像的同时实现这一目标?

  * { margin: 0; padding: 0; border: 0; } .gallery { background-color: blue; display: flex; flex-direction: row; flex-wrap: wrap; } .gallery img { width: 100%; height: 100px; } 
  <div class="gallery"> <div *ngFor="let image of images; let i = index;"> <img src={{image.src}} [style.width.%]="50" alt=""> </div> </div> 

在此处输入图片说明

Assuming none of your images are wider than half the viewport, you could set a width of 50vw on the images' parent (not the .gallery — the child <div> elements). 假设没有一个图像的宽度大于视口的一半,则可以在图像的父对象(而不是.gallery子元素<div> )上设置50vwwidth

.gallery > div {
  flex-basis: 50vw;
  flex-shrink: 0;
  flex-grow: 0;
}

You just have to set the flex-grow: 2 and the div width: 50% 您只需要设置flex-grow:2和div宽度:50%

Use the following CSS code: 使用以下CSS代码:

.gallery {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    width: 100%;
}
.gallery > div {
    flex-grow: 2;
    width: 50%;
    max-width: 50%;
}
.gallery img {
    width: 100%;
    height: auto; // use this to display responsive image
}

This should fix your issue. 这应该可以解决您的问题。

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

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