gpt4 book ai didi

css - 在我的元素中制作 CSS Sprite 有问题吗?

转载 作者:太空宇宙 更新时间:2023-11-04 00:34:59 26 4
gpt4 key购买 nike

我在制作 CSS Sprite 时遇到了问题。这是我第一次使用 sprite,所以我不是很擅长。

这是 HTML 代码:

<div class="dfeature-box">
<div class="icon"><img class="multi" src="images/sprite.jpg"></div>
<div class="title">Title</div>
<div class="details">description.</div>
</div>

这是 CSS 代码:

text-align: center;
margin-bottom: 30px;
display: inline-block;
width: 100%;
max-width: 272px;
}

.dfeature-box .icon {
display: flex;
display: -webkit-flex;
justify-content: center;
align-items: center;
width: 106px;
height: 106px;
background-color: #ffffff;
border-radius: 16px;
text-align: center;
margin-bottom: 35px;
margin-left: auto;
margin-right: auto;
}

.dfeature-box .icon .multi{
background-position:126px 9px;
height:47px;
width: 64px;

这是我想要使用的图像(一张图像中有 3 个图标)- 在我的代码中,我使用了最后一张图像(两个人)

enter image description here

这是结果(如您所见,所有 3 张图片都粘在一起):

enter image description here

我也有一个重要的问题:如何在 CSS 中使用图像而不是在 HTML (class=icon) 中使用图像?如您所见,“img”在“class=icon”中使用。

==>> 第一次更新:

请根据您的建议检查这些新代码:这是结果: enter image description here

CSS:

.sprite-bg{
background: url("images/sprite.jpg");
}

.bg-1{
width:62px;
height:62px;
background-position: 0px 0px;
}

HTML:

<div class="dfeature-box">
<div class="icon"><div class="sprite-bg bg-1"></div></div>
<div class="title">Title</div>
<div class="details">Description</div>
</div>

看看这段 HTML 代码:你确定红色标记的部分吗?我应该把什么放在那里? enter image description here

此外,请查看我在第一篇文章中的代码。我想在我的代码中使用 sprite。

==>> 第二次更新:

.dfeature-box {
text-align: center;
margin-bottom: 30px;
display: inline-block;
width: 100%;
max-width: 272px;
}

.dfeature-box .icon {
display: flex;
display: -webkit-flex;
justify-content: center;
align-items: center;
width: 106px;
height: 106px;

background-color: #ffffff;
border-radius: 16px;
text-align: center;
margin-bottom: 35px;
margin-left: auto;
margin-right: auto;
-webkit-transform-origin: center center;
-moz-transform-origin: center center;
-ms-transform-origin: center center;
transform-origin: center center;
-webkit-transform: scale(1);
-moz-transform: scale(1);
-ms-transform: scale(1);
transform: scale(1);
-webkit-box-shadow: 0 8px 12px rgba(31, 27, 90, 0.08);
box-shadow: 0 8px 12px rgba(31, 27, 90, 0.08);
-webkit-transition: all 0.3s ease;
transition: all 0.3s ease;
}

.sprite-bg{

background: url("https://i.imgur.com/VO1dBBA.jpg");

}
.bg-1{
width:62px;
height:62px;
background-position: 0px 61px;
}
.bg-2{
width: 64px;
height: 62px;
background-position: 62px 61px;
}
.bg-3{
width: 64px;
height: 62px;
background-position: 127px;
}

.dfeature-box .icon i {
font-size: 50px;
background: -webkit-linear-gradient(to bottom, #45b35e, #6ad56a);
background: linear-gradient(to bottom, #45b35e, #6ad56a);
color: transparent;
-webkit-text-fill-color: transparent;
-webkit-background-clip: text;
background-clip: text;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hustbee</title>

<link rel="stylesheet" type="text/css" href="css/style.css">

</head>
<body>
<div class="dfeature-box">
<div class="icon"><div class="sprite-bg bg-1"></div></div>
<div class="title">TITLE</div>
<div class="details">long description.</div>
</div>


<div class="dfeature-box">
<div class="icon"><div class="sprite-bg bg-2"></div></div>
<div class="title">TITLE</div>
<div class="details">long description.</div>
</div>

<div class="dfeature-box">
<div class="icon"><div class="sprite-bg bg-3"></div></div>
<div class="title">TITLE</div>
<div class="details">long description.</div>
</div>

</body>
</html>

最佳答案

检查下面的例子:

您需要检查几件事情:

  1. Sprite 图片总是在 backgound:url(path to image)
  2. 上调用
  3. 为图标创建 3 个类。
  4. 调用图像的不同位置。

.sprite-bg{

background: url(https://cdn.tutsplus.com/webdesign/uploads/legacy/tuts/373_sprites/angry_birds.png);

}
.bg-1{
width:110px;
height:110px;
background-position: 0px 0px;
}
.bg-2{
width: 58px;
height: 60px;
background-position: 194px 89px;
//border: 2px solid red;
float:right;
}
<div class="sprite-bg bg-1"></div>
<div class="sprite-bg bg-2"></div>

关于css - 在我的元素中制作 CSS Sprite 有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59678654/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com