【发布时间】:2014-12-29 13:51:49
【问题描述】:
我有一个包含 div (.tablet) 的部分容器。 section 和 Div 都是响应式的
当视口达到 > 400px 时,我想在我的 div (.tablet) 中显示背景图片
但是,除非我指定 px 宽度和高度,否则 div 会折叠并消失。它忽略了背景图像。就好像背景图片不存在,div也没有内容一样!
是否可以使用不会折叠的灵活背景图像创建响应 DIV?我希望背景图像填充 DIV 并以 div 大小响应?
我尝试在 .tabletonly div 中使用以下规则
background-size: cover;
background-position: center;
background-attachment: fixed; /* Fixbackground * */
//--------------------------------------------- ----------------------
HTML
<!DOCTYPE html PUBLIC>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="styles/style1.css"/>
</head>
<body>
<div id="wrapper">
<div id="section">
<div id="tabletonly" class="span_1_of_2">
<div><div>
</div>
</div> // end wrapper
</body>
</html>
CSS 代码 ----------------------------------------------------
.span_1_of_2 {
width: 45%;
position: relative;
}
img {
max-width: 100%;
}
@media screen and (min-width: 400px) {
#tabletonly div{
background: url("images/shop.jpg" );
background-repeat: no-repeat;
}
}
【问题讨论】:
-
nb。背景图像就是这样,背景而不是内容。因此,它受制于应用它的元素的尺寸。您需要将尺寸应用于
.tablet,或者不使用背景图像,而是切换img元素的显示 -
如果你没有在 div 中放任何东西,只需使用 IMG 标签,然后将它们换掉。
标签: css html responsive-design media-queries