Reputation: 1109
I would like to use 1 background image (1366x768) to use in several divs. But if i use :
.my_divs{
background-image: url("image.jpg");
}
I only have the top left of the image on each divs. I would like to use the portion of the image based on the div position.
I can use transparency on the div and set the background to body
.
It will work, but in my case i'm already using background-image
for body
with another image.
body{
background-image: url("another_image.jpg");
}
Is there a way to maybe "see through" body
and show the html
background-image ? Or another css trick ?
EDIT : add simple example
<style>
.mydivs{
padding:30px;
margin:30px;
background-image: url("2.jpg");
}
body{
background-image: url("1.jpg");
}
#container{
}
</style>
<body>
<div id='container'>
<div class='mydivs'>1</div>
<div class='mydivs'>2</div>
<div class='mydivs'>3</div>
</div>
</body>
Upvotes: 9
Views: 11980
Reputation: 73
This is not necessarily a good solution -- but how about setting the borders of inner divs instead of the margins: the borders will be not transparent and cover the background image, the inner divs will be transparent and show the background image. You can set up the borders in vw and vh to make them responsive.
.inner-divs {
width: 20%;
height: 100%;
background-color: transparent;
border: rgb(255, 255, 255) solid 5vw;
}
Upvotes: 1
Reputation: 46785
I think the background-attachment
property may do the trick for you.
See: http://www.w3.org/TR/CSS21/colors.html#background-properties
If you use background-attachment: fixed
, the background image is fixed with
respect to the viewport. So, if you apply it to several elements, it is as if the background image is being viewed through "lenses" over the page.
If you page has a vertical scroll bar, then the background image will remain fixed as the content moves (this may not suit your design).
.bgdeco {
width: 700px;
height: 100px;
margin: 30px;
border: 1px solid yellow;
background-image: url(http://placekitten.com/2000/700);
background-attachment: fixed;
background-position: top center;
}
<div class="bgdeco"></div>
<div class="bgdeco"></div>
<div class="bgdeco"></div>
Upvotes: 14
Reputation: 11570
If you know how many divs you'll be spanning you can use background-position
with a bit of simple math. Take a look at this fiddle.
Upvotes: 2
Reputation: 828
Yes you can set a transparent background to any HTML tag including body! You can achieve your goal via multiple ways..
background: none;
to
body and all leading tagsbackground-image
to parent div and
set all chind div's background: none;
check out below example
html, body {
padding: 0px;
margin: 0px;
}
body {
background: red;
}
#parent {
background: url('http://thumb7.shutterstock.com/display_pic_with_logo/996335/251280085/stock-vector-abstract-polygonal-space-low-poly-dark-background-with-connecting-dots-and-lines-connection-251280085.jpg');
background-size: cover;
margin: 10px;
padding: 10px;
display: block;
float: left;
}
.child {
display: block;
float: left;
width: 100px;
height: 100px;
font-size: 40px;
color: #fff;
border: 2px solid #ccc;
margin: 5px;
}
<div id="parent">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
<div class="child">4</div>
<div class="child">5</div>
<div class="child">6</div>
<div class="child">7</div>
</div>
Upvotes: 0
Reputation: 819
HTML
<div id='container'>
<div id='divHead' class='mydivs'> </div>
<div id='divTorso' class='mydivs'> </div>
<div id='divLegs' class='mydivs'> </div>
</div>
CSS
#container {
background-image:url('myPic.jpg');
}
.mydivs {
background-color:transparent;
}
Upvotes: 1
Reputation: 41065
You can use background-position: xpos ypos;
to adjust the position of the image.
Upvotes: -1