css3 background gradient full width with scaled image - css

Is there a way to set the size of the image independent from the general size of the background with css?
With following code I set the size of the of the background, so the gradient and the image have the width of 30px.
background(url("../images/icons/double_arrow_37px.svg"), linear-gradient(to top bottom, rgb(171, 129, 85), rgb(148, 112, 74)));
background-size: 30px 37px;
What I need is to set the width of the image to 30px and the gradient to a width of 100% of the button.
I already know the workaround to create a extra image with the correct dimensions, but maybe there is a smarter way with css?
Full Example:
body {
background-color: #000;
}
.button-custom {
color: #fff;
font-family: $font-centennial;
background-image: url("http://metk.de/kunden/stackoverflow/double_arrow_37px.svg");
background-size: 30px 37px;
background-position: center left;
background-repeat: no-repeat;
margin-top: 70px;
padding: 15px 45px;
border-radius: 0;
border: 0;
text-transform: uppercase;
overflow: hidden;
}
.button-custom.bronze {
background-color: #ab8155;
}
.button-custom.bronze:hover {
background: url("http://metk.de/kunden/stackoverflow/double_arrow_37px.svg"), -moz-linear-gradient(bottom, #ab8155, #94704a);
background: url("http://metk.de/kunden/stackoverflow/double_arrow_37px.svg"), -webkit-linear-gradient(bottom, #ab8155, #94704a);
background: url("http://metk.de/kunden/stackoverflow/double_arrow_37px.svg"), linear-gradient(to top bottom, #ab8155, #94704a);
background-position: center left;
background-size: 30px 37px;
background-position: center left;
background-repeat: no-repeat;
color: #fff;
}
Contact

In CSS3, you can use multiple images background. linear-background is interpreted as an image not a color. Known that, you can write something like that :
body {
height: 600px; /* not relevant for your problem */
width: 600px;
}
div {
height: 500px; /* not relevant for your problem */
width: 500px; /* not relevant for your problem */
border: 3px dashed green; /* not relevant for your problem */
background: url("http://i436.photobucket.com/albums/qq90/KatDJZ/Forums/18556-Robot_Unicorn_Attack.jpg"), -moz-linear-gradient(top, red 0%, blue 100%);
background: url("http://i436.photobucket.com/albums/qq90/KatDJZ/Forums/18556-Robot_Unicorn_Attack.jpg"), -webkit-gradient(linear, left top, left bottom, color-stop(0%, red), color-stop(100%, blue));
background: url("http://i436.photobucket.com/albums/qq90/KatDJZ/Forums/18556-Robot_Unicorn_Attack.jpg"), -webkit-linear-gradient(top, red 0%, blue 100%);
background: url("http://i436.photobucket.com/albums/qq90/KatDJZ/Forums/18556-Robot_Unicorn_Attack.jpg"), -o-linear-gradient(top, red 0%, blue 100%);
background: url("http://i436.photobucket.com/albums/qq90/KatDJZ/Forums/18556-Robot_Unicorn_Attack.jpg"), -ms-linear-gradient(top, red 0%, blue 100%);
background: url("http://i436.photobucket.com/albums/qq90/KatDJZ/Forums/18556-Robot_Unicorn_Attack.jpg"), linear-gradient(to bottom, red 0%, blue 100%);
background-position: 50% 50%, 50% 50%;
background-repeat: no-repeat, no-repeat;
background-size: 150px, 300px;
}
<div>Yo!</div>

Related

Using a variable inside -moz-linear-gradient breaks the style

I try to use custom variables inside my CSS.
Under FireFox: everything is okay.
Under Google Chrome or Microsoft Edge: it works pretty well on linear-gradient but not on -moz-linear-gradient
Here is my code: https://codepen.io/Bronzato1/pen/VwWBJjP?editors=1100
To show you the problem, I created the first class which works as expected and the second class with the usage of a variable inside -moz-linear-gradient breaks the style !
HTML
<div class="first red"></div>
<div class="second red"></div>
CSS
.red {
--custom-color: #FF0000;
}
.first::before{
content:'';
position: absolute;
width: 19rem;
height: 14rem;
background: linear-gradient(128deg, transparent 51%, black 53%, var(--custom-color) 70%, transparent 72%) no-repeat bottom left;
/*FF*/
background: -moz-linear-gradient(128deg, transparent 51%, black 53%, black 70%, transparent 72%) no-repeat bottom left;
background-color: #000000;
}
.second::before{
content:'';
position: absolute;
top: 250px;
width: 19rem;
height: 14rem;
background: linear-gradient(128deg, transparent 51%, black 53%, var(--custom-color) 70%, transparent 72%) no-repeat bottom left;
/*FF*/
background: -moz-linear-gradient(128deg, transparent 51%, black 53%, var(--custom-color) 70%, transparent 72%) no-repeat bottom left;
background-color: #000000;
}
You need to put your prefixed styles above the standard style, e.g.
background: -moz-linear-gradient(...) no-repeat bottom left;
background: linear-gradient(...) no-repeat bottom left;
Otherwise the browser will attempt to use the last valid style, which I believe causes problems since background is a combined style so you end up overwriting your linear-gradient with a -moz-linear-gradient that Chrome doesn't understand.
Working example:
.red {
--custom-color: #FF0000;
}
.first::before {
content: '';
position: absolute;
width: 19rem;
height: 14rem;
background: -moz-linear-gradient(128deg, transparent 51%, black 53%, red 70%, transparent 72%) no-repeat bottom left;
background: linear-gradient(128deg, transparent 51%, black 53%, var(--custom-color) 70%, transparent 72%) no-repeat bottom left;
background-color: #000000;
}
.second::before {
content: '';
position: absolute;
top: 250px;
width: 19rem;
height: 14rem;
background: -moz-linear-gradient(128deg, transparent 51%, black 53%, var(--custom-color) 70%, transparent 72%) no-repeat bottom left;
background: linear-gradient(128deg, transparent 51%, black 53%, var(--custom-color) 70%, transparent 72%) no-repeat bottom left;
background-color: #000000;
}
<div class="first red"></div>
<div class="second red"></div>
Try this:
.red {
--custom-color: #FF0000;
}
.first::before, .second::before{
content:'';
position: absolute;
width: 19rem;
height: 14rem;
background: linear-gradient(128deg, transparent 51%, black 53%, var(--custom-color) 70%, transparent 72%) no-repeat bottom left;
/*FF*/
background: -moz-linear-gradient(128deg, transparent 51%, black 53%, red 70%, transparent 72%) no-repeat bottom left;
background-color: #000000;
}
.second::before{
top: 250px;
}

How to move background gradient left position

I am trying to move the left position of the gradient. But it is not working.
Even after adding background-position property, it does not works.
header{
height:100px;
border:1px solid blue;
background: linear-gradient(to top, #e20e0e 50px, #0000 50px);
background-position-x: 50px;
}
<header></header>
Turn off background-repeat.
header{
height:100px;
border:1px solid blue;
background: linear-gradient(to top, #e20e0e 50px, #0000 50px);
background-position-x: 50px;
background-repeat: no-repeat;
}
<header></header>
You should add background-repeat: no-repeat; property.
Check out the similar question which is answered already.
CSS Background Gradient with offset
header{
height:100px;
border:1px solid blue;
background: linear-gradient(to top, #e20e0e 50px, #0000 50px);
background-position: 50px;
background-repeat: no-repeat;
}
<header></header>

How to add a border to a jagged border container

Hello I am having trouble trying to come up with a way of adding a border of 1px to a container with a jagged border such as:
https://codepen.io/swizenfeld/pen/ZyBybW
body {
background: #f4f4f4;
}
.edge {
width: 100%;
height: 400px;
background: #fff;
margin-top: 30px;
}
.edge:before {
content: " ";
display: block;
position: relative;
width: 100%;
top:-30px;
height:30px;
background: linear-gradient(135deg, transparent 75%, white 76%) 0 50%,
linear-gradient(-135deg, transparent 75%, white 76%) 0 50%;
background-repeat: repeat-x;
background-size: 30px 30px, 30px 30px;
}
<div class="edge"></div>
Any ideas?
You need to add more linear-gradient() to show jagged border
body {
background: #f4f4f4;
}
.edge {
width: 100%;
height: 400px;
background: #fff;
margin-top: 30px;
}
.edge:before {
content: " ";
display: block;
position: relative;
width: 100%;
top:-30px;
height:30px;
background: linear-gradient(135deg, transparent 75%, white 76%) 0 50%, linear-gradient(-135deg, transparent 75%, white 76%) 0 50%, linear-gradient(45deg, red 30%, transparent 0%), linear-gradient(-45deg, red 30%, transparent 0%);
background-repeat: repeat-x;
background-size: 30px 30px, 30px 30px;
}
<div class="edge"></div>
For border-left, -bottom, -right, try to play with below snippet and see the comment also given for css properties.
body {
background: #ccc;
}
.edge {
width: 100%;
height: 400px;
background: white;
margin-top: 30px;
border-left:2px solid red;
border-bottom:2px solid red;
border-right:2px solid red;
position:relative; /*make it relative*/
}
.edge:after {
content: " ";
display: block;
position:absolute; /*make it absolute*/
width: 100%;
top:-6px; /* play with top and height too*/
height:23px;
/*background: linear-gradient(135deg, transparent 75%, white 76%) 0 50%, linear-gradient(-135deg, transparent 75%, white 76%) 0 50%, linear-gradient(45deg, red 30%, transparent 0%), linear-gradient(-45deg, red 30%, transparent 0%);*/
background: linear-gradient(45deg,white 14px, red 16px, transparent 17px), linear-gradient(-45deg, white 14px, red 16px, #ccc 17px);
background-repeat: repeat-x;
background-size: 30px 30px, 30px 30px;
}
<div class="edge"></div>

Is it possible to make this particular button effect in pure CSS?

I'm designing a site for a school project, and I'm trying to design a particular style for the buttons and navigation, but I'm not sure how to go about this.
I considered doing a border effect, but I stopped short as I realized that it doesn't just involve changing individual side's colors but cutting two sides in half and coloring those pieces differently. A gradient on a div behind it might work, but not only would that get complicated, but it would look blurry while I'm going for sharpness like an edge on a 3D shape. Is this doable, or would I have to use images?
EDIT: Wow, looks like there's a lot of methods out there. Code Golf, anyone?
A solution without css gradient if you want to support IE8 too: http://jsfiddle.net/2am780pq/
HTML:
<a class="button">Cool</a>
CSS:
.button {
display: inline-block;
position: relative;
background-color: #4755e7;
padding: 10px 20px;
color: #fff;
}
.button:before {
content: '';
position: absolute;
top: 50%;
bottom: -5px;
left: -5px;
right: -5px;
margin: auto;
background-color: #4451dc;
z-index: -1;
}
.button:after {
content: '';
position: absolute;
top: -5px;
bottom: 50%;
left: -5px;
right: -5px;
margin: auto;
background-color: #5d67e9;
z-index: -1;
}
without gradient nor pseudo-elemts, box-shadow could do the job too:
http://codepen.io/anon/pen/NPaZBd
a{
display: inline-block;
color: #FFF;
padding:5px 1em;
line-height:2em;
background:#4755E7;
margin:1em;
box-shadow:-0.8em -0.8em 0 -0.5em #5d67e9,
0.8em -0.8em 0 -0.5em #5d67e9,
-0.8em 0.8em 0 -0.5em #4451dc,
0.8em 0.8em 0 -0.5em #4451dc;
}
/* add an inside blurry border too ? */
a:nth-child(even) {
box-shadow:-0.8em -0.8em 0 -0.5em #5d67e9,
0.8em -0.8em 0 -0.5em #5d67e9,
-0.8em 0.8em 0 -0.5em #4451dc,
0.8em 0.8em 0 -0.5em #4451dc,
inset 0 0 1px
}
link
link link
link bigger link
link even bigger works still
Yes, with gradient backgrounds and nested elements. This is NOT cross-browser compatible in browsers that do not support CSS3.
Live example: JSFiddle
The HTML:
<span>Click Me</span>
The CSS:
.button {
display: inline-block;
padding: 4px;
background: rgba(115,127,255,1);
background: -moz-linear-gradient(top, rgba(115,127,255,1) 0%, rgba(68,81,220,1) 50%, rgba(68,81,220,1) 51%, rgba(68,81,220,1) 100%);
background: -webkit-gradient(left top, left bottom, color-stop(0%, rgba(115,127,255,1)), color-stop(50%, rgba(68,81,220,1)), color-stop(51%, rgba(68,81,220,1)), color-stop(100%, rgba(68,81,220,1)));
background: -webkit-linear-gradient(top, rgba(115,127,255,1) 0%, rgba(68,81,220,1) 50%, rgba(68,81,220,1) 51%, rgba(68,81,220,1) 100%);
background: -o-linear-gradient(top, rgba(115,127,255,1) 0%, rgba(68,81,220,1) 50%, rgba(68,81,220,1) 51%, rgba(68,81,220,1) 100%);
background: -ms-linear-gradient(top, rgba(115,127,255,1) 0%, rgba(68,81,220,1) 50%, rgba(68,81,220,1) 51%, rgba(68,81,220,1) 100%);
background: linear-gradient(to bottom, rgba(115,127,255,1) 0%, rgba(68,81,220,1) 50%, rgba(68,81,220,1) 51%, rgba(68,81,220,1) 100%);
}
.button span {
display: inline-block;
background: #4755E7;
color: #fff;
padding: 0.5em 0.75em;
}
Here one element solution, simplier markup :D
<b>Im sexy and i know it!</b>
http://jsfiddle.net/ebdq20vm/1/
b {
padding: 20px;
display: inline-block;
color: #FFF;
background: #5d67e9;
background: -moz-linear-gradient(top, #5d67e9 50%, #4451dc 51%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(50%, #5d67e9), color-stop(51%, #4451dc));
background: -webkit-linear-gradient(top, #5d67e9 50%, #4451dc 51%);
background: -o-linear-gradient(top, #5d67e9 50%, #4451dc 51%);
background: -ms-linear-gradient(top, #5d67e9 50%, #4451dc 51%);
background: linear-gradient(to bottom, #5d67e9 50%, #4451dc 51%);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#5d67e9', endColorstr='#4451dc', GradientType=0);
position: relative;
z-index: 5;
}
b:before {
content:'';
position: absolute;
top: 4px;
left: 4px;
right: 4px;
bottom: 4px;
background-color: #4755E7;
display: block;
z-index: -1;
}

full page background in css not appearing

For some reason my full page background in css is not working. I did this once before and it worked great, but now I get nothing but a white background. This is a section of my relevant css code:
* {
margin: 0;
padding: 0;
}
/* render html5 elements as block */
header, footer, section, aside, nav, article {
display: block;
}
html {
line-height: 1;
color: #555;
font-family: 'Trebuchet MS', Verdana, Arial, Helvetica, sans-serif;
font-size: 14px;
background-image: url(file:///HD/Users/Barbra/Sites/HSMAI/images/vbcc_background.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
- moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
/* layout */
#wrapper {
width: 960px;
margin: 0 auto;
background-image: linear-gradient(bottom, #86D1C7 5%, #A8BCFA 53%);
background-image: -o-linear-gradient(bottom, #86D1C7 5%, #A8BCFA 53%);
background-image: -moz-linear-gradient(bottom, #86D1C7 5%, #A8BCFA 53%);
background-image: -webkit-linear-gradient(bottom, #86D1C7 5%, #A8BCFA 53%);
background-image: -ms-linear-gradient(bottom, #86D1C7 5%, #A8BCFA 53%);
background-image: -webkit-gradient(
linear,
left bottom,
left top,
color-stop(0.05, #86D1C7),
color-stop(0.53, #A8BCFA)
);
border: 2px solid #333;
/* curved border radius */
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
padding: 10px;
margin-top: 10px;
margin-bottom: 10px;
}
I am certain all files are in the proper place and all links are specified correctly.
The value for background-image is only the URL to the image.
If you put any other value, the declaration is invalid.
Change:
background-image: url(file:///HD/Users/Barbra/Sites/HSMAI/images/vbcc_background.jpg) no-repeat center center fixed;
to
background-image: url(file:///HD/Users/Barbra/Sites/HSMAI/images/vbcc_background.jpg)
or
background: url(file:///HD/Users/Barbra/Sites/HSMAI/images/vbcc_background.jpg) no-repeat center center fixed;
Give the background to the body element and it should work.
background-image: url(file:///HD/Users/Barbra/Sites/HSMAI/images/vbcc_background.jpg) no-repeat center center fixed;
should be
background: url('///HD/Users/Barbra/Sites/HSMAI/images/vbcc_background.jpg') no-repeat center fixed;

Resources