Single line (shorthand) background style? - css

I'm following this guide: CSS background Property.
background: color image position/size repeat origin clip attachment initial|inherit;
I have this background CSS:
background-color: grey;
background-repeat: no-repeat;
background-size: contain;
background-position: center;
background-origin: content-box;
I'm trying to convert it to one line:
background: grey center / contain no-repeat content-box;
I need the background image and grey to be the background of the padding too.
Where am I going wrong?
div{
width: 200px;
height: 400px;
background: grey center / contain no-repeat content-box;
padding: 30px;
}
<div style="background-image: url(https://upload.wikimedia.org/wikipedia/en/5/50/NES_Super_Mario_Bros.png)"></div>
JSFiddle

I need the background image and grey to be the background of the padding too.
If you need the background-image and grey to be the background of the padding too then you should set background-clip as padding-box instead of content-box. content-box area does not include the padding or the border areas.
div {
width: 200px;
height: 400px;
background: grey center / contain no-repeat padding-box;
padding: 30px;
}
<div style="background-image: url(https://upload.wikimedia.org/wikipedia/en/5/50/NES_Super_Mario_Bros.png)"></div>
Note that I had mentioned background-clip and not background-origin in the above sentence. It is the background-clip that actually clips the background and not the background-origin. When only one box value is provided, both clip and origin are assumed to have the same value.
As per W3C Spec
If one value is present then it sets both ‘background-origin’ and ‘background-clip’ to that value. If two values are present, then the first sets ‘background-origin’ and the second ‘background-clip’.
Now the image goes to the edge, the image should be padded, and the color should not be, possible with separate rules but not one line.Link
If you mean to say that background-image is required (and must be restricted to content area only) whereas the color gets applied to padding area also as indicated in your question and comment here, the code provided in question will not work as-is.
When you use the shorthand background property to assign a value to the background-clip (like in question), you are giving it only a single value and so the same setting would apply to both the color and the image. This means both image and color will be clipped either to padding-box or content-box as applicable.
div {
width: 200px;
height: 400px;
background: grey center / contain no-repeat content-box;
padding: 30px;
}
<div style="background-image: url(https://upload.wikimedia.org/wikipedia/en/5/50/NES_Super_Mario_Bros.png)"></div>
You can still achieve what you need by using shorthand property but you have to use either of the following options:
Method 1: Assign both the image and color in the same line like in the below snippet. The shorthand property works here because we are assigning clip values specifically for the color and the image.
div {
width: 200px;
height: 400px;
background: url(https://upload.wikimedia.org/wikipedia/en/5/50/NES_Super_Mario_Bros.png) center / contain no-repeat content-box, grey center / contain no-repeat padding-box;
padding: 30px;
}
<div></div>
Method 2: Assign separate values for the background-origin and background-clip properties like in below snippet (pointed out in comment by Karsten Buckstegge also).
This works because of two reasons:
Background colors don't have a size, they fill up the entire area within the scope of the clip. So, color becomes available under padding area also.
Background image's size is set to contain and as per spec this means that the image must be scaled to fit the background positioning area which is nothing but the background-origin as mentioned here. Since background-origin is only content-box and background-repeat is set to no-repeat, the image will not occupy the padding area.
div {
width: 200px;
height: 400px;
background: grey center / contain no-repeat content-box padding-box;
padding: 30px;
}
<div style="background-image: url(https://upload.wikimedia.org/wikipedia/en/5/50/NES_Super_Mario_Bros.png)"></div>
Note that the image must be specified before the color because the one that is specified first becomes the top-most layer. If you add color before image then color will hide the image.

This is a problem with the shorthand.
If you just set one value in the end (in this case 'content-box'), it will set background-origin AND background-clip to that value. Therefor you have to set your background-clip after that to have it correct.
Final code then looks like this:
background: grey center / contain no-repeat content-box border-box;
See W3C background spec on that.

Related

How to add border radius with gradient and trasparent background for content inside container

I need border radius with gradient like below image. I have tried several ways but I am not getting output as required.
Possibly this is a duplicate in 'Possible to use border-radius together with a border-image which has a gradient?' and has few use full tips there. How ever I found a simple solution from git hub for your problem,but it won't solve the issue entirely, since we can not make the background transparent with this CSS trick but only can have solid color or a gradient as you wish. The problem here is explained in one of the answer given to the above mentioned question. according to that we can not achieve the transparent backgrounds with border radius and gradients, since gradient effect applies as border-image property in CSS. Go refer to that problem and see the accepted answer. By the way here's the promised tricky solution.
body{
padding: 10%;
box-sizing: border-box;
}
.border-container{
display: flex;
width: 100%;
height: 200px; /* to set the div visible, since without height it won't display if it does not have any elements inside */
border: double 4px transparent;
border-radius: 25px;
background-image: linear-gradient(#fff,#fff ), radial-gradient(circle at top left, #ef1eef,#163ae0);
background-origin: border-box;
background-clip: content-box, border-box;
}
<body>
<div class="border-container">
<!-- your elemnts -->
</div>
</body>

why isnt this background-origin working?

I'm messing around with the background origin property buy it doesnt seem to be working why? From my understanding background-origin dictates the paint area of the box-model, I would assume this should show the blue padding but it's not and I'm sure what it is that I'm doing wrong.
https://jsfiddle.net/36trf972/18/
div{
background-color: blue;
width: 100px;
height: 100px;
border: 5px dashed red;
padding: 10px;
background-clip: border-box;
background-origin: content-box;//not working-why?
background-size: cover;
background-position: center;
background-image: url("panda.jpg");
}
Use px instead of center.
I use background-repeat:no-repeat; for you to see result better.
div{
background-color: blue;
width: 100px;
height: 100px;
border: 5px dashed red;
padding: 10px;
background-clip: border-box;
background-origin: content-box;//not working-why?
background-size: cover;
background-position:50px 50px;
background-repeat:no-repeat;
background-image: url("data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxMTEhUSEhIVFhUVFxcYFhgWGBUYFxcYFRgWFxgXFhcYHSggGBolHRUVITEhJSkrLi4uFx8zODMsNygtLisBCgoKDg0OGhAQGi0lHSUrLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLf/AABEIAMABBwMBIgACEQEDEQH/xAAcAAABBQEBAQAAAAAAAAAAAAAEAQIDBQYHAAj/xABBEAABAwIDBQUGBAUDAgcAAAABAAIRAyEEEjEFQVFhcQYigZGhEzKxwdHwFEJS4QcVI2KSM3LxU6IWQ2OCk6Oy/8QAGgEAAgMBAQAAAAAAAAAAAAAAAgMAAQQFBv/EAC0RAAICAgEDAgUDBQEAAAAAAAABAhEDIRIEMUETUQUiMmGBQnGhI1KRwfAU/9oADAMBAAIRAxEAPwDDAp8qIFPlYkASBeKaXJuZMQIsJwao8ykYUSIPDUpapGJCEfghE1qeaaNwuFzU3OBu3dyQ7gqi07IwYsUbgi3hDPUbBZClhOcF6EDIRkJy8QlASmiDSpaRTCErWqJFE7mqMqZjpCYWpyIQELykIRmycF7SoLd0XKu6IGYan7Ggah952iy2KeSSTvWt7Uv7rQPdFlj62qVjy+orXYgPKVLCWETZBkJYT14hDZCNavshQ7pPNZQroX8PKtNjCa1M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CewrbOJc4ENuG3IHnIi4/e6ytY7xIn1Rm3ans6pbTcSwgOY6dWuEi/jHgql1QnVOinQ1ztCspkuDWiSdI3rQbP2Sxl6kOdw/K36oPZDgxpf+YyOg5KarVceQKXlk38qM85N6Rb1totbbU8AoW4uo+wsOX1VR+Jpt1Mnlc/RPpbVcTFMBgGrnXgceCy/+f2X5YHp2afCYcxJUeJ2tQZOaoCeDe8fRUNTHvMhn9QQA575a3nlOYBo8ioGsw0gPdkM60cz2jrn+RKFdGu83/gtYF5ZbHtPLgylTMkgAuPHkPqoK22ahzPfUIpy4U2sgGpBt3tQ3STzgX0dnw2cBjARF3tLJHH+mLx0RVXZTTldDXCBlNnAt3RyR8MWP9H+y2oQe0ZTFYt1Rxc8yfQchwUAK2D8LWg+yeGkbvZ048w2R6qnxm08ZTdlfVe0+AkcRAuFshPkvl/7+BsZRl2ZVCk79LvIohmDqHSlU8GO+ik/nWIOtep/kUv81rj/AM+r/wDI/wCqP5g9ArmefA2I5KWjUUJqEmTcnfvlOARAhLahBV5gqpEXtx1HRU1A7tFYYUxu6EfNF4Cg6ZqMFXgyR5aJEFSJAF929eWCcNnYhLR//9k=");
}
<div></div>
You need to remove the property:
background-size: cover;
Cover tells the browser to make sure the image always covers the entire container, even if it has to stretch the image or cut a little bit off one of the edges.
Also, add:
background-repeat:no-repeat;
To prevent any repetition.
When working with background-origin it's important to understand what it does (and what it doesn't do), to get the expected results.
background-origin specifies whether positioning of the background image should be relative to the padding box, border box, or content box. It does not crop or hide any part of the image. (That's what background-crop is for.)
So background-origin is actually behaving as expected your example:
background-origin: content-box // position relative to content box
background-position: center // position center of image at center of content
The image center is positioned at the center of the content box. From there it extends to all sides.
It happens to be that in your example, the center of the content box is the exact same spot as center of padding box or border box, because you have equal widths for border and padding on all sides.
Don't use the background-repeat:repeat along with the background-origin:content-box ..It just won't work.
For all other background-origin such as border-box and the padding-box the repeat option would work just perfectly fine..
If you are trying to fill the content with the background the only option for you is use cover in  background-size..
The best solution comes if you are willing to write an extra piece of code in html by just giving a container element like div for all the content that you need and just fill it with the background..

Assistance with Gradient Background

I'm attempting to create a background for a webpage that takes advantage of the gradient options in CSS3. What I want it to do is use a gradient that fills the full height of the screen, and then if the screen is scrolled beyond that, to just use the final color.
Unfortunately, all of my attempts end up with either the gradient repeating or staying fixed. Neither of these are acceptable for what I have in mind.
Could any of you help me? The closest I could get so far can be found below, but obviously it stays fixed. Everything else I've tried has pretty much had a repeating issue, even with no-repeat thrown into the mix.
html {
height: 100%
}
body {
background: gold no-repeat linear-gradient(silver, orange, gold);
background-attachment: fixed;
min-height: 100%;
margin: 0px;
}
You could make use of multiple backgrounds and stack them like in the below snippet where the first background is your linear-gradient and the second one is a solid color (which is same as the linear gradient's end color).
By not repeating the gradient (using the no-repeat), we can limit the gradient to be present only for the screen's height whereas the solid color background would by default run through the full size.
Here is what MDN says about multiple background stacking: link
These are layered atop one another with the first background you provide on top and the last background listed in the back. Only the last background can include a background color.
(emphasis is mine)
html {
height: 100%;
}
body {
background: linear-gradient(silver, orange, gold, red) no-repeat, gold;
margin: 0px;
}
/* Just for demo */
div {
min-height: 200vh;
}
<!-- Library included just to avoid prefixes so that users with older browser can view -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div>
Some content....
</div>
Note: I have added a red end color to the linear-gradient just to show how the solid color takes over from the point where the gradient ends.
Actually, it would look like this:
html {
height: 100%;
}
body {
background: linear-gradient(red, orange, gold) no-repeat, gold;
background-size: 100%;
margin: 0px;
}
div {
min-height: 200vh;
}
Here is a fiddle https://jsfiddle.net/v14m59pq/163/
Hope this help you man.
If you want that effect, you need two layers, back layer with the final color and the top layer with the gradient.
* {
margin: 0;
padding: 0;
}
html {
height: 100%;
background-color: gold;
}
body {
height: 100%;
background: gold no-repeat linear-gradient(silver, orange, gold);
}
I use the html with a gold color and the body with the gradient, simply means, the parent the main color and the children the gradient with the full viewport height.
Check link to see the result :)
http://codepen.io/TibicenasDesign/pen/VLywpL

CSS: Extend an image

I have an image that looks like this:
Is it possible to extend this image (perhaps inside a div) so that it would look something like this:
Thanks!
You can create a div of the same color using the CSS background-color property (I believe the hex should be ~#999). Then, position the image as a background-image within the div using the background-position: right property.
HTML
<div class="arrow">Home</div>​
CSS
#arrow {
background-color: #999;
background-image: url('http://i.stack.imgur.com/QDCz4.png');
background-position: right;
background-repeat: no-repeat;
/* sets div dimensions/text styles */
height: 24px;
color: white;
text-align: center;
line-height: 24px;
float: left;
padding-left: 20px;
padding-right: 30px; /* slightly longer to account for background image /*
}
JS Fiddle: http://jsfiddle.net/fbBsz/14/
Get a vertical slice of the gray part of very top left of the arrow with having width:1px. Take that one px slice image and repeat it on -x.
Here is something you can practice with
Since your image does not have a gradient, you have a better chance of matching the color(s) you want with just using background color.
you can set it as a background to a div
#elem {
display:block;
width:200px;
height:20x;
background: url(/filepath/to/image.gif) right top no-repeat #ccc;
}
Just make sure the background color is the same as the dark grey on the gif
No, this is not possible in CSS. You must set the width of the containing element, set the background image's url and set the x-position to right and set the repeat to no-repeat. Then set the background color to the same as the button's foreground color. If the button foreground is a pattern, you will have to use another image as the background.
No, not with that image at least :-)
Looks like you could make use the "sliding doors" technique – see http://www.alistapart.com/articles/slidingdoors/ for a good article about it

Positioning a fixed background image in CSS with top and right margins

I want to have a fixed position background image that scales and I need it to have a 220px top margin and a 170px right margin so that the image is not under the navigation bars. I have come close with the following code but the right margin varies due to the percent.
#container_hu
{
width: 100%;
text-align: left;
background: #fff;
border: 0px solid #000000;
height:100%;
padding-top:260px;
min-width:1100px;
background:url(../images/bg_image.jpg) no-repeat;
background-attachment:fixed;
background-position: 78% 220px;
background-size:25%;
}
Thanks for any help!
You can use CSS3's background-clip property to force clip the background to within the allowed content area. i.e. you can specify a padding to your DIV (to cover the nav bars etc..) and specify the background-clip property to content-box to achieve this. Example CSS below:
#container_hu {
/* this forces the bg to be rendered only within allowed content area (CSS3) */
background-clip: content-box;
background-image: url("your_image.jpg");
background-repeat: no-repeat;
/* this forces the bg to stretch with the container (CSS3) */
background-size: 100% 100%;
height: 300px;
width: 400px;
/* specify the top/left pixels to cover your nav-bar etc. */
padding-left: 50px;
padding-top: 50px;
}
So to start off its 170px right margin and then a percentage from what you describe.
I would consider 2 options:
Redo your entire layout in ems for all dimensions, margins, puddings etc. with a base unit on body. That way you may have more of a chance of things lining up. (in essence I'm saying start with ems, scale up in ems, as %age will not work)
You could also start with pixels and scale up in pixels but you'll need JavaScript to do the calculation and apply the new margin right onResize.

Resources