html - 无法缩放背景 div 图像

标签 html css image background scaling

无论出于何种原因,无论做什么,我都无法手动缩放 div 的背景图像。 background-size 和 max-height,width 没有任何效果。该图像忽略了我的样式标签。有人可以解释为什么我不能格式化 div 的背景图片吗?我想将 img/Stage-Background.png 从 1600x1076 缩小到 750x650

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="SDL.css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Home</title>
</head>

<body>

<img src="img/SDL 4 Final Recompress.jpg" style="margin-left:auto; margin-right:auto; " />

<div style="background-image:url(img/Stage-Background.png); background-size:80px 60px;
background-repeat:no-repeat;">
<center>
<embed
src="http://blip.tv/play/AwGUv2w"
type="application/x-shockwave-flash" width="669"
height="500" allowscriptaccess="always"
allowfullscreen="true" style="margin-top:100px; margin-bottom:200px;">
</embed>
</center>
</div>

</body>
</html>

最佳答案

据我所知,我们无法缩放背景图片

如果您的图像尺寸大于 div 的尺寸,则

<div style="width:400px; height:400px;background:url(xyz.png);"></div>

xyz.png 尺寸大于 div 的宽度和高度。

您可以使用:

background-position:center;

否则尝试使用普通的 img 标签,然后使用绝对定位将您的元素放置在图像的顶部。

<div class="your_box_element" style="width:400px;height:400px;position:relative;">
    <img width="400px" height="400px" />
    <div class="your_text_on_top_of image" style="position:absolute;top:0px; left:0px;">
           Your text and other elements go here
    </div>
</div>

加载更大尺寸图像然后缩放的方法不会给您带来良好的性能,因此最好将图像适当缩放到 div 元素,以防您想将其用作背景。如果您将背景图像更改为所需的大小,那么您的示例会更好。

有关背景位置的更多详细信息,请参见此处 background-position设置正确的配合

如果您想了解更多关于背景图像位置以及它们在网络上的常用方式,请尝试阅读 Image Sprites

关于html - 无法缩放背景 div 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16293144/

相关文章:

css - 从云端加载 CSS

html - 为什么我的 slider 堆叠在一起? slider 新手

c++ - 将 IWICBtmap 写入文件 - 不正确的解析

JavaScript 只显示最后的结果

HTML 将 css 背景图像替换为视频

Javascript、php 和 html 编译器

c# - .net 4.5 框架中用于图像编辑的命名空间

html - 在多行中拆分句子(Bootstrap 4 + Mobile)

html - CSS 命令在 CSS 文件中不起作用,但在主 HTML 中起作用

html - 导航栏图标与超链接标签分开