Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 如何使用背景大小设置最小宽度_Html_Css - Fatal编程技术网

Html 如何使用背景大小设置最小宽度

Html 如何使用背景大小设置最小宽度,html,css,Html,Css,我有一个1000像素X 600像素的图像。我想使用它作为div的响应背景,但不管浏览器窗口有多小,我想将最小宽度设置为1000px 这是令人沮丧的,因为似乎没有一种简单的方法来定义最小宽度 如果我使用background size:cover——出于某种原因,图像的默认值远远大于1000px 请提供帮助如果媒体查询是一个选项,您可以为小于1000px宽的浏览器视口设置媒体查询 假设已设置HTML视口: <meta name="viewport" content="width=device-

我有一个1000像素X 600像素的图像。我想使用它作为div的响应背景,但不管浏览器窗口有多小,我想将最小宽度设置为1000px

这是令人沮丧的,因为似乎没有一种简单的方法来定义最小宽度

如果我使用background size:cover——出于某种原因,图像的默认值远远大于1000px


请提供帮助

如果媒体查询是一个选项,您可以为小于1000px宽的浏览器视口设置媒体查询

假设已设置HTML视口:

<meta name="viewport" content="width=device-width">

我也有同样的问题。下面的代码适用于我。我只需要在同一个元素中添加一个最小宽度。这里我只使用了html元素,我认为使用body元素更为传统

     /* CSS Style Sheet */

 html    { background-image:url(example.JPG);
                    background-size:cover;
                    min-width:1000px;}

我希望这会有所帮助,

现在有一个选项,可以选择
背景大小
只缩放图像,直到图片达到实际高度/宽度。这样,您就不必设置最小宽度或任何东西

在CSS中:

.bg {
    width: auto; /* Scale the div to the parent width */
    height: 900px; /* The div needs some height to be visible */
    background-image: url(bg.png); /* This is your background image */
    background-size: 100%; /* Always size the image to the width of the div */
    background-position: top; /* Position the image to the top center of the div */
}

/* For any viewports less than 1000px wide */
@media (max-width: 1000px) {

    .bg {
        background-size: 1000px 600px; /* Force the image to its minimum width */
    }

}
body {
  background-size: cover;
}

参考资料:

您可以在
背景尺寸中使用
计算

.bg {
    background-size: calc(max(100%, 1000px));
}

calc
也适用于
background position
,对于一侧或两侧

必须尝试在以图像为背景的父元素上定义最小宽度?然后你的其他内容可以放进去。我做了,但它不起作用。“背景大小:封面”似乎使用标签的高度来确定图像的宽度。这太愚蠢了。为什么不使用设定的div高度?你是说最大宽度而不是最小宽度,对吗?!或者,你的最后一句话毫无意义:图像的默认值远远大于1000px。你有一些html要发布吗?你的背景尺寸在正文上吗?请注意,不要忘记使用前缀。我更希望用户
背景大小:initial
在媒体中查询一个:D
背景尺寸:封面在图像的实际尺寸下不会停止收缩
.bg {
    background-size: calc(max(100%, 1000px));
}