如何让背景封面单独div?
How to make a background cover a separate div?
我正在做一个带有侧边菜单的网站。屏幕的 30% 是菜单,其余是内容。
div的内容,我用COVER方式放了一张背景图。我用了第一个例子:
https://css-tricks.com/examples/FullPageBackgroundImage/css-1.php
但是,当图像占据整个背景时,此方法非常有效。正如在我的例子中,我想要完全相同的占据宽度的 70%,"eats" 图像角。
我该如何解决这个问题?
HTML:
<div id="esquerda" style="width: 30%; height: 500px">
....conteudo.....
</div>
<div id="direita" style="width: 70%; height: 500px">
<img src="fundo.jpg" class="bg">
</div>
CSS:
.bg {
min-height: 100%;
min-width: 1024px;
width: 100%;
height: auto;
position: fixed;
top: 0;
float: right;
}
@media screen and (max-width: 1024px){
.bg {
left: 50%;
margin-left: -512px;
}
}
在 CSS 中使用 background-image
属性。您可以创建一个单独的 class 以在仅具有该属性的 CSS 中使用。
.bgImage {
background-image: url("fundo.jpg");
}
然后将 class 应用到 <div>
标签。
<div id="direita" class="bgImage" style="width: 70%; height: 500px">
...
</div>
使用 background-image
和 3 个不同元素的基本方法(以防止主要与 Safari 相关的 xBrowser 问题)
在 #bg
图层元素
上将背景设置为 cover
*{ box-sizing: border-box;}
html, body{ height:100%; }
body{ position:static; margin:0; }
#bg{
background: url('http://www.gettyimages.ca/gi-resources/images/Homepage/Hero/UK/CMS_Creative_164657191_Kingfisher.jpg') 50%;
background-size: cover;
position: fixed;
top: 0; right: 0; bottom: 0;
width: 70%;
}
#menu{
position: fixed;
bottom: 0; top: 0; left: 0;
width: 30%;
background: rgba( 0, 0, 255, 0.4 );
}
#page{
position: relative;
border: 10px dashed #000;
margin-left: 30%;
width: 70%;
height: 2000px;
}
<div id="bg">BG</div>
<div id="menu">FIXED</div>
<div id="page">SCROLLABLE</div>
在该代码之上,您可以根据需要应用 CSS3 媒体查询 。
注意 "easiest" 不是 使用单独的#bg
元素,而是使用 background-attachment: fixed;
将 bg 图像直接设置为 #page
元素,但是,如前所述,图像可能不会出现在 Safari 上,因为它的大小设置为 cover
.
让我们用简单的方法来做 - 在 div 的样式上使用 CSS background-image: url("fundo.jpg");
。
我正在做一个带有侧边菜单的网站。屏幕的 30% 是菜单,其余是内容。
div的内容,我用COVER方式放了一张背景图。我用了第一个例子: https://css-tricks.com/examples/FullPageBackgroundImage/css-1.php
但是,当图像占据整个背景时,此方法非常有效。正如在我的例子中,我想要完全相同的占据宽度的 70%,"eats" 图像角。
我该如何解决这个问题?
HTML:
<div id="esquerda" style="width: 30%; height: 500px">
....conteudo.....
</div>
<div id="direita" style="width: 70%; height: 500px">
<img src="fundo.jpg" class="bg">
</div>
CSS:
.bg {
min-height: 100%;
min-width: 1024px;
width: 100%;
height: auto;
position: fixed;
top: 0;
float: right;
}
@media screen and (max-width: 1024px){
.bg {
left: 50%;
margin-left: -512px;
}
}
在 CSS 中使用 background-image
属性。您可以创建一个单独的 class 以在仅具有该属性的 CSS 中使用。
.bgImage {
background-image: url("fundo.jpg");
}
然后将 class 应用到 <div>
标签。
<div id="direita" class="bgImage" style="width: 70%; height: 500px">
...
</div>
使用 background-image
和 3 个不同元素的基本方法(以防止主要与 Safari 相关的 xBrowser 问题)
在 #bg
图层元素
cover
*{ box-sizing: border-box;}
html, body{ height:100%; }
body{ position:static; margin:0; }
#bg{
background: url('http://www.gettyimages.ca/gi-resources/images/Homepage/Hero/UK/CMS_Creative_164657191_Kingfisher.jpg') 50%;
background-size: cover;
position: fixed;
top: 0; right: 0; bottom: 0;
width: 70%;
}
#menu{
position: fixed;
bottom: 0; top: 0; left: 0;
width: 30%;
background: rgba( 0, 0, 255, 0.4 );
}
#page{
position: relative;
border: 10px dashed #000;
margin-left: 30%;
width: 70%;
height: 2000px;
}
<div id="bg">BG</div>
<div id="menu">FIXED</div>
<div id="page">SCROLLABLE</div>
在该代码之上,您可以根据需要应用 CSS3 媒体查询 。
注意 "easiest" 不是 使用单独的#bg
元素,而是使用 background-attachment: fixed;
将 bg 图像直接设置为 #page
元素,但是,如前所述,图像可能不会出现在 Safari 上,因为它的大小设置为 cover
.
让我们用简单的方法来做 - 在 div 的样式上使用 CSS background-image: url("fundo.jpg");
。