Reputation: 1
I can't get background-position to work. I've tried putting the code everywhere I can think of!!! Any ideas??? Thanks
http://codepen.io/anon/pen/ogPrxV
My html is
<div class="container">
<div class="inner"></div>
</div>
My css is
.container {
width: 80%;
background-color: red;
background-position: center;
text-align: center;
}
.inner {
border: 1px solid;
height: 160px;
width: 320px;
background-position: 50% 50%;
background: green url("welcome-small.png") no-repeat;
}
Upvotes: 0
Views: 46
Reputation: 1
Sorry- I didn't see your answers - the site didn't email me when I got a response so I didn't realise (I got an answer elsewhere)!!!
Basically - what I needed was margin: auto (because I have a div nested within a div). It is easy when you know how!!! ;-)
Thanks for your help guys. Dayan was right on the money!!!
Upvotes: 0
Reputation: 1
.inner {
border: 1px solid;
height: 160px;
width: 320px;
background: green url("welcome-small.png") no-repeat;
background-position: 50% 50%;
}
Upvotes: 0
Reputation: 114991
The background
property is shorthand for all background properties. It overides any previously stated individual properties already set.
Perhaps the simplest solution here is just to move the background position setting to after the shorthand
.inner {
border: 1px solid;
height: 160px;
width: 320px;
background: green url("welcome-small.png") no-repeat;
background-position: 50% 50%;
}
Upvotes: 1
Reputation: 15715
your background
property rewrites the background-position, see this Fiddle
This is because background will also set the background-position to 0 0
by default.
.inner {
border: 1px solid;
height: 160px;
width: 320px;
background: green url("http://images.visitcanberra.com.au/images/canberra_hero_image.jpg") no-repeat;
background-position: 20% 50%;
}
just take the background-position
property below background
property
.container {
width: 80%;
background-color: red;
background-position: center;
text-align: center;
}
.inner {
border: 1px solid;
height: 160px;
width: 320px;
margin: auto;
background: green url("http://images.visitcanberra.com.au/images/canberra_hero_image.jpg") no-repeat;
background-position: 20% 50%;
}
<div class="container">
<div class="inner"></div>
</div>
Upvotes: 1
Reputation: 8031
Make use of margin
to center your container
and remove the extra background
property you have.
.container {
width: 80%;
background-color: red;
text-align: center;
margin: 0 auto;
}
Here is your updated codepen that shows it centered. http://codepen.io/anon/pen/QwVXGv
Upvotes: 1
Reputation: 10430
You're using shorthand for the background image so you should include the position within the background: style.
.inner {
border: 1px solid;
height: 160px;
width: 320px;
background: url("welcome-small.png") no-repeat 50% 50% green;
}
Upvotes: 1