简体   繁体   English

HTML,CSS,响应式:如何使用包含不同大小图像的精灵?

[英]HTML, CSS, Responsive: How to use a sprite containing images of different sizes?

In my website, I require images of different sizes. 在我的网站上,我需要不同尺寸的图像。 For example: 例如:

  • Images A, B, C : 320px x 320px 图像A,B,C:320像素x 320像素
  • Image D, E : 400px x 200px 图像D,E:400像素x 200像素
  • Image F : 20px x 16px 图像F:20像素x 16像素

I want to make a single sprite for all the images. 我想为所有图像制作一个精灵。 I want to make it responsive so I use percentages to define its height and width. 我想使其具有响应性,因此我使用百分比来定义其高度和宽度。 All the methods I found online hard code heights and don't mention how to make it responsive. 我在网上找到了所有硬编码高度的方法,并且没有提及如何使其具有响应性。

The current method used: 当前使用的方法:

I combined equal sized images into one sprite 我将大小相等的图像合并为一个精灵

  • Images A, B, C - sprite1.png 图片A,B,C-sprite1.png
  • Images D, E - sprite2.png 图像D,E-sprite2.png
  • Image F - not using any sprite 图片F-不使用任何图片

I also created transparent images (holders) for the sizes above. 我还为上述尺寸创建了透明图像(支架)。

The reason I'm doing it this way because I figured out the below way of making it work and still be responsive: 之所以这样做是因为我想出了以下使它正常工作并保持响应的方法:

<div class="type-1">
    <img src="transparent_img_320_320.png" class="image_a" />
    <img src="transparent_img_320_320.png" class="image_b" />
    <img src="transparent_img_320_320.png" class="image_c" />
</div>

<div class="type-2">
    <img src="transparent_img_400_200.png" class="image_d" />
    <img src="transparent_img_400_200.png" class="image_e" />
</div>

<div class="type-3">
    <img src="transparent_img_20_16.png" class="image_f" />
</div>

And in CSS I put my images as background img as below (mentioning only one per one size but I'm repeating the same thing and changing position in my code): 在CSS中,我将图像作为背景img如下所示(每一种尺寸仅提及一个,但是我要重复相同的内容并更改代码中的位置):

.image_a {
    background: url(../images/sprite1.png);
    background-size: 300% 100%;
    background-position: 100% 0;
}

.image_d {
    background: url(../images/sprite2.png);
    background-size: 400% 200%;
    background-position: 300% 0;
}

Problems: 问题:

  • Need to create a unique transparent image for each file. 需要为每个文件创建唯一的透明图像。
  • Problematic to work with many sprites. 使用许多精灵时存在问题。
  • Have to put every image as background, as I haven't found a solution that doesn't use hardcoded height and width for . 必须将每个图像都用作背景,因为我还没有找到一种不使用硬编码的height和width的解决方案。
  • making transparent images stretch for images of different sizes isn't working. 为不同尺寸的图像拉伸透明图像是行不通的。 Is there a way to avoid the transparent images? 有没有办法避免透明图像?

Any other way to do this is appreciated. 任何其他方式可以做到这一点。

Requirements: multiple images of varying sizes, responsive height and width 要求:多张不同大小,响应高度和宽度的图像

additional-question: Do large images(such as background of the entire page) be a part of the sprite, considering I've 8 background images. 其他问题:考虑到我有8张背景图片,是否要将大图片(例如整个页面的背景)作为sprite的一部分。

I think you have few options to manage with your problem. 我认为您没有什么办法可以解决您的问题。

My first idea: 我的第一个想法:

Create one sprite file, with background-position and width , height with pixels. 创建一个精灵文件,其background-positionwidthheight以及像素。 You can use transform:scale() to fill your responsive content. 您可以使用transform:scale()填充您的响应内容。

But I never use sprite for big images for responsive print. 但是我从不使用大尺寸的图片进行响应式打印。 In my opinion it's not good habit because you load big .png file instead of one on demand (like your few backgrounds). 我认为这不是一个好习惯,因为您加载的是.png大文件,而不是按需加载(如您的几个背景)。

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

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