I would like to know if it is possible to create a background like this in CSS3.
The background should span a header div and the gradient should go from white to black independent of the screen width (always white on the left side and black on the right side).
Reason for not using the image is that it takes longer to load and that I can't get it to resize it's width when making the browser smaller than 1920px (the width of the image).
Have tried linear-gradient but I can't get it to work...
Regards,
Jens
If you also want the black bar at the top you should give dimensions to the background, stop the repeating and also position it where you want (treat it like a normal background image)
div {
background-color: black;
background-image: linear-gradient(to right, white, black);
background-repeat:no-repeat;
background-size:100% 20px; /*full width, 20px height*/
background-position:0 100%; /*gradient at bottom*/
/*just to give size to demo*/
min-height:50px;
}
<div></div>
Here's some CSS for you:
#grad {
background: gray; /* For browsers that do not support gradients */
background: -webkit-linear-gradient(left, white , black); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(right, white, black); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(right, white, black); /* For Firefox 3.6 to 15 */
background: linear-gradient(to right, white , black); /* Standard syntax */
}
Source: http://www.w3schools.com/css/css3_gradients.asp
I know the OP's question was answered. But I'll comment here anyway to deliver some more information to create a really more "complex" background.
First is you really can create multiple backgrounds stack on each other:
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Backgrounds_and_Borders/Using_multiple_backgrounds
Second is you can determine position, size, etc,... of a background-image. And here the concise syntax for it: https://www.w3schools.com/cssref/css3_pr_background.asp.
Why background-image? A basic (and important) theory of background in CSS is: A background of an element can have only 1 background-color, and multiple background-images sit on top of it (even if the background-color is declared after background-image, background-color will be still placed below the background-images), and you can resize, reposition those background-images. And an important thing is linear-gradient is count as a background-image, not background-color. The 2 links above do give all detailed information about it.
Here is a quick demo on a "more complex" background from the OP question using only 1 div HTML:
div {
background:
linear-gradient(to right, white, black) 0 100%/100% 20px no-repeat,
linear-gradient(to left, white, black) 0 0/100% 20px no-repeat,
black;
height: 100px;
}
<div></div>
I'm inspired writing this long comment because from a tutorial
https://levelup.gitconnected.com/how-to-implement-netflix-slider-with-react-and-hooks-bdb9b99d1ce4, there's a section from it there're verbose hacks in HTML and CSS to achieve what I'm able to do within just a single line of CSS background, and I think it's cool to share, isn't it?
/* simpler */
.box {
width: 100%;
height: 100px;
background: linear-gradient(to right,black 0%,black 30%,transparent 75%,transparent 100%), green;
}
/* more complex */
.content {
height: 100px;
position: relative;
}
.background {
display: flex;
height: 100%;
}
.left {
background: black;
width: 30%;
position: relative;
}
.left:before {
content: '';
position: absolute;
background-image: linear-gradient(to right,#000,transparent);
top: 0;
bottom: 0;
left: 100%;
width: 275px;
}
.right {
background: green;
width: 70%;
}
.content-container {
color: white;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
padding: 30px
}
<!-- simpler -->
<div class="box">
<div class="content-container">content here...</div>
</div>
<hr>
<!-- more complex -->
<div class="content">
<div class="background">
<div class="left">left</div>
<div class="right">right</div>
</div>
<div class="content-container">content here...</div>
</div>
Related
This question already has answers here:
Can I apply multiple background colors with CSS3?
(6 answers)
Closed 7 years ago.
How can I give a single div multiple background colors or images in CSS? For example, I want the top part to be red, then the middle to be blue and the bottom to be green.
A single linear-gradient as a background is enough
background: linear-gradient(to bottom,
red 0, red 33.33%,
blue 0, blue 66.66%,
green 0, green 100%
);
Example | Browser support
use gradients in css3.
YourDIVID {
background: red; /* For browsers that do not support gradients */
background: -webkit-linear-gradient(red, yellow, green); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(red, yellow, green); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(red, yellow, green); /* For Firefox 3.6 to 15 */
background: linear-gradient(red, yellow, green); /* Standard syntax */
}
TOP, BOTTOM, CENTER multiple color Tutorial For Sigle Line
#multiple {
width: 700px;
height: 500px;
background:linear-gradient(red, yellow, green, blue, purple, orange);
}
Use :before and :after pseudo element rules to have three different background layers (color and images) on a single element.
Note: This solution requires the contained elements to be wrapped to stay above the backgrounds. Otherwise links may not be clickable, text not selectable, etc..
Run the code snippet for a working example.
.layered-backgrounds {
background-color: #C63;
background-image: url(https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png);
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
position: relative;
height: 400px;
z-index: 1;
}
.layered-backgrounds:before {
position: absolute;
content: " ";
top: 0;
height: 120px;
width: 100%;
background: #39C;
z-index: 2;
opacity: 0.8;
}
.layered-backgrounds:after {
position: absolute;
content: " ";
bottom: 0;
height: 120px;
width: 100%;
background: #9C3;
z-index: 3;
opacity: 0.8;
}
.layered-backgrounds .content {
position: relative;
z-index: 4;
}
<div class="layered-backgrounds">
<div class="content">
Clickable and selectable contents.
</div>
</div>
I'm looking for easy way (if at all possible) to create background image from this image: http://postimg.org/image/x1kwb0uq3/
There are two horizontal lines and I need one to be at the top of the page all the time and other at the bottom and the thing is that I'm not sure what is the best practise to create such background. Should I slice this horizontal line from image or should I create it programatically using css rules. Because I'm stuck at how many different techniques there are to achieve the same thing and it really confuses me, because I want to write short, clean understandable code and code that is good performance wise.
I thought to do it programatically is a good choice, but still I think that's a lot of code for such simple thing.
Here's what it looks like:
HTML
<div id="divs-top">
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<div id="div4"></div>
<div id="div5"></div>
<div id="div6"></div>
<div id="div7"></div>
<div id="div8"></div>
<div id="div9"></div>
</div>
<div id="divs-bottom">
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<div id="div4"></div>
<div id="div5"></div>
<div id="div6"></div>
<div id="div7"></div>
<div id="div8"></div>
<div id="div9"></div>
CSS:
body {
margin: 0;
padding: 0;
}
#divs-top {
position: fixed;
top: 0;
width: 100%;
}
#divs-bottom {
position: fixed;
bottom: 0;
width: 100%;
}
#div1, #div2, #div3,
#div4, #div5, #div6,
#div7, #div8, #div9 {
width: 11.11%;
height: 5px;
float: left;
}
#div1, #div6 {
background-color: #e44b02;
}
#div2, #div7 {
background-color: #60cb34;
}
#div3, #div8 {
background-color: #003f28;
}
#div4, #div9 {
background-color: #ca000d;
}
#div5 {
background-color: #dbff26;
}
As you see I have to create selector for each div and horizontal line has 9 colors that's why I created 9 divs.
By using images it looks like an old technique. Other technique that I'm thinking is to make one div and apply some css styles so that div has border with horizontal gradients but I'm not sure how to do it properly.
What is the standard to do it properly? Any suggestions would be really appreciated as long as you provide a way that is clean and short in code if it's possible.
You could use linear-gradient with color stops to create bands like that. The syntax is simple (explained in inline code comments below):
background-image: linear-gradient(to right, /* gradient from left to right */
#f00, #f00 25%, /* start with red, end with red at 25% */
#00f 25%, #00f 50%, /* blue at 25% continue up to 50% */
#0f0 50%, #0f0 75%, /* green at 50% continue up to 75% */
#000 75%, #000 100% /* black at 75% continue up to 100% */
);
To keep it simple, in the example below there are two divs for the bands and a middle div for the content. You can then take it to next level by using ::before and ::after pseudo-elements on the content and eliminate separate divs for the bands.
Example Fiddle: http://jsfiddle.net/abhitalks/nve9v0mn/1/
Example Snippet:
div.line {
height: 6px;
background-image: linear-gradient(to right,
#f00, #f00 25%,
#00f 25%, #00f 50%,
#0f0 50%, #0f0 75%,
#000 75%, #000 100%
);
}
div.content {
min-height: 60vh;
background-color: #eee;
}
<div class="line"></div>
<div class="content"></div>
<div class="line"></div>
Edit:
If you want to support IE < 9, then the easiest would be to take a screenshot in a modern browser and use that image as a fallback. Remember though that background shorthand properties do not work well with IE<9 for all properties.
Your CSS would look something like this:
div.line {
height: 6px;
background: url('http://i.imgur.com/HTLnBfj.png') no-repeat;
background-size: 100%;
background-image: linear-gradient(to right,
...
);
}
Example Fiddle 2: https://jsfiddle.net/abhitalks/nve9v0mn/4/embedded/result/
You can use 1px single img and repeat in background of divs-top for top bar and same thing for bottom div.
You could use CSS gradients to get those colored borders. Upload an image containing only one colored border to http://www.colorzilla.com/gradient-editor/ (import image) and this service will output a CSS gradient for you.
When using background gradients you also only need one HTML element – the one that should have top and bottom borders (e.g. the body element). The following examples uses pseudo elements on the body tag to create those borders. The used gradient is not like yours, but you can click it yourself using the linked gradient editor.
body:before,
body:after {
content: '';
position: fixed;
left: 0;
width: 100%;
height: .5em;
}
body:before {
top: 0;
background: linear-gradient(to right, rgba(228,245,252,1) 0%,rgba(191,232,249,1) 50%,rgba(159,216,239,1) 51%,rgba(42,176,237,1) 100%);
}
body:after {
bottom: 0;
background: linear-gradient(to right, rgba(228,245,252,1) 0%,rgba(191,232,249,1) 50%,rgba(159,216,239,1) 51%,rgba(42,176,237,1) 100%);
}
I have a page on my site that has a gray background color that I am trying to add gradient to it in a different way than just a left to right. My page has an outer div that takes up 100% of the page's width. I then have an inner div that takes up 80% of the page's with, but auto aligned. I was wondering how, if I can, do a full cycle of my gradient within each side (left & right) of the outer div, the 10% part that shows before the inner div starts.
So say an A equals 10%, and everytime I put two colors together, that is the gradient taking place within that 10%. I want to do this...
#181818, #282828 AAAAAAAA #282828, #181818
How can I do this?
HTML
<div class="graypage">
<div class="homeimg">
gfdsgsg
</div>
</div>
CSS
.graypage, .whitepage { margin: 0 auto; }
/*------Page Wraps--------*/
.graypage {
width: 100%;
left: 0;
right: 0;
min-height: 100%;
background: -webkit-linear-gradient(left, #282828, #181818); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(right, #282828, #181818); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(right, #282828, #181818); /* For Firefox 3.6 to 15 */
background: linear-gradient(to right, #282828, #181818); /* Standard syntax */
}
.homeimg {
background-image: url("/images/bright_lights_small.jpg");
width: 80%;
background-size: cover;
background-position: center;
height: 100%;
position: absolute;
margin-right: 10%;
margin-left: 10%;
}
As I understand it, you'd like two bars - one on the left of the outerDiv and the other on the right. Each of these bars you'd like to be 10% of the page width. You'd also like each bar to cycle through the colours #181818, #282828, #aaaaaa, #282828, #181818.
I'd just use a linear-gradient with 12 colour-stops. Something like this:
#outerDiv
{
background: linear-gradient(to right,
#181818 0%,#282828 2%,#aaaaaa 4%,#aaaaaa 6%,#282828 8%,#181818 10%,
#181818 90%,#282828 92%,#aaaaaa 94%,#aaaaaa 96%,#282828 98%,#181818 100%); /* W3C */
width: 100%;
}
Credit: http://www.colorzilla.com/gradient-editor/
Here it's applied to the outer div and a solid colour is applied to the (80% as wide) inner div.
EDIT: Here's the (now updated) html and css used.
<style>
body
{
margin: 0;
padding: 0;
}
#outerDiv
{
background: linear-gradient(to right,
#181818 0%,#282828 10%,
#282828 90%,#181818 100%); /* W3C */
width: 100%;
}
#innerDiv
{
width: 80%;
margin: auto;
background-color: #dddddd;
}
</style>
</head>
<body>
<div id='outerDiv'>
<div id='innerDiv'>
<button id='goBtn'>Change the text</button>
<div class="menu-wrapper">
<ul>
<li>WORD1</li>
<li>WORD2</li>
<li>WORD3</li>
<li>WORD4</li>
</ul>
</div>
</div>
</div>
</body>
This code produces the following result:
Bacically, you have 2 posibilities here. You can set a repeating gradient
div {
width: 500px;
height: 100px;
background: repeating-linear-gradient(to right, yellow 0%, green 10%);
}
<div></div>
And you can play with the background-size
div {
width: 500px;
height: 100px;
background: linear-gradient(to right, yellow, green);
background-size: 10% 100%;
}
<div></div>
So I've been at it for a while trying to achieve this one shape with CSS with no good solutions. I need this to be an image because this div may resize and I want it to stay intact. I've also attempted to create an SVG which did not work out very well, I've seen some people work with gradient to make shapes but I'm not able to find any good guide to point me in the right direction. Any help is appreciated :)
Using gradients with angles is not fit for your case because (as already pointed out by King King in comments) as the width the increases, the angle of the gradient (or) the color stop percentages need to be modified to maintain the shape. That is very tricky and so this method can be employed only when the shape has fixed dimensions.
However gradients can still be used with the to [side] [side] syntax because gradients defined using this syntax can adapt to variations in container sizes. In this method no pseudo-elements are used.
$(document).ready(function() {
$('#increase').on('click', function() {
$('.gradient').css('width', '300px').css('height', '500px');
})
})
div {
display: inline-block;
vertical-align: top;
height: 300px;
width: 100px;
margin: 10px;
color: beige;
transition: all 1s;
}
.gradient {
padding: 10px;
background: linear-gradient(to top right, transparent 50%, tomato 50%) no-repeat, linear-gradient(to top right, transparent 0.1%, tomato 0.1%) no-repeat;
background-size: 100% 100px, 100% 100%;
background-position: 0% 100%, 0% -100px;
}
/* Just for demo */
body {
background: -webkit-radial-gradient(50% 50%, circle, aliceblue, steelblue);
background: radial-gradient(circle at 50% 50%, aliceblue, steelblue);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="gradient">Some content</div>
<br>
<br>
<button id="increase">Increase Width & Height</button>
Note that it is better to make sure that the text doesn't flow into the slanted section of the shape because wrapping the text around to fit within the shape is not straight-forward.
I have attempted to make that in css as per ur image. http://jsfiddle.net/3zkme/- See if this could help. Thanks.
HTML
<div style="margin:30px">
<div class="trapezoid">
</div>
</div>
CSS
.trapezoid{
top: 150px;
vertical-align: middle;
border-bottom: 120px solid red;
border-left: 200px solid transparent;
border-top-left-radius:0px;
height: 0;
width: 150px;
transform:rotate(270deg);
-ms-transform:rotate(270deg); /* IE 9 */
-webkit-transform:rotate(270deg); /* Opera, Chrome, and Safari */
}
/* ---------- */
.trapezoid {
position:relative;
}
.trapezoid:after {
content:' ';
left:-14px;
top:10px;
position:absolute;
background:red;
border-radius:0px 0 0 0;
width:164px;
height:40px;
display:block;
}
You do not use a gradient for this, you just need to use a pseudo-element like :after.
Sample code:
#bookmark {
width: 50px;
height: 100px;
position: relative;
background: red;
}
#bookmark:after {
content: "";
position: absolute;
left: 0;
bottom: 0;
width: 0;
height: 0;
border-bottom: 35px solid #FFF;
border-right: 50px solid transparent;
}
Live JSFiddle
If you want the shape to be filled in with a gradient, you can do that, too. Just add that to the CSS:
background: linear-gradient(to right, #ff0000 0%,#B00000 100%);
given a div that is 500px wide, is there a way to fill the background with 2 different colors using css? I know it can be done with a background image, but just wondering if it can be done with bg color.
eg :
You can't set multiple background colors, but you could set something like:
div.twocolorish {
background-color: green;
border-left: 20px solid red;
}
As long as you don't need text to go over the part in red then this would take care of you in one div.
I ended up with this solution using linear gradients:
.dualcol-test {
background: linear-gradient(to right, green 0%, green 80%, red 80%, red 100%);
}
<div class="dualcol-test"> This div has a green and red background <br><br><br> </div>
You can achieve 2 colors in 1 div by using pseudo-element :before
HTML:
<div class="twocolordiv"></div>
CSS:
.twocolordiv {
position: relative;
z-index: 9;
background: green;
width:500px;
height:100px;
}
.twocolordiv:before {
content: "";
position: absolute;
z-index: -1;
top: 0;
right: 20%;
bottom: 0;
left: 0;
background: red;
}
You can use linear-gradient background to do this
background: linear-gradient(90deg, green 50%,red 50%);
No, you can only set one background-color. However, you could split your container into two and set a different backgorund-color for each one.
This question got me thinking about how CSS3 would approach this problem.. and frankly the specification has me confused. That said, a couple of features that are creeping through the cracks: background-size and linear-gradient.
<style type="text/css">
#ji { width: 500px; height: 300px;
background:
-moz-linear-gradient(green, green) 0px 0px no-repeat,
-moz-linear-gradient(red, red) 200px 50px no-repeat,
-moz-linear-gradient(blue, blue) 0px 250px no-repeat,
-moz-linear-gradient(gray, gray) 300px 125px no-repeat;
-moz-background-size: 450px 50px, 50px 200px, 250px 250px, 50px 250px;
}
</style>
<div id="ji">
</div>
Give this a go :)
I'm sure there are better approaches to this problem, but it does demonstrate that we'll be afforded greater flexibility with CSS backgrounds (one day).
Edit: Forgot to mention that this will only work in Firefox, though there are Webkit equivalents for linear-gradient and background size
Using the :before css attribute allows you to 'fill' a div with the two colours.
.myDiv {
position: relative; /*Parent MUST be relative*/
z-index: 9;
background: green;
/*Set width/height of the div in 'parent'*/
width: 100px;
height: 100px;
}
.myDiv:before {
content: "";
position: absolute; /*set 'child' to be absolute*/
z-index: -1; /*Make this lower so text appears in front*/
/*You can choose to align it left, right, top or bottom here*/
top: 0;
right: 0;
bottom: 60%;
left: 0;
background: red;
}
<div class="myDiv">this is my div with multiple colours. It work's with text too!</div>
An easily edited sample can be seen LIVE DEMO
Using background-image / repeat-y is the easiest solution - however, maybe you want to change colours or widths or something with Javascript.
Here's a way to do this which allows text everywhere.
http://jsfiddle.net/WQ8CG/
HTML:
<div id="container"><div class="offset">text</div></div>
CSS:
#container {
background: #ccc;
border-right: 40px solid #aaa
}
.offset {
margin-right: -40px;
zoom: 1; /* to fix IE7 and IE6 */
position: relative /* to fix IE6 */
}
Better late then never. Thought this might help:
The htmls
<div id="content">
<div id="left"></div>
<div id="right"></div>
</div>
The csss
#content { background-color: #F1EBD9; }
#left { float: left; width: 14em; }
#right { margin-left: 14em; background-color: #FFF; }
You can view this # http://alexandergutierrez.info/stretch-background-color-in-a-two-col-layout
You could you inset box shadow, and change the shadow to whatever colour you required.
CSS
-moz-box-shadow: inset 50px 0px 0px 0px rgba(156, 244, 255, 1);
-webkit-box-shadow: inset 50px 0px 0px 0px rgba(156, 244, 255, 1);
box-shadow: inset 50px 0px 0px 0px rgba(156, 244, 255, 1);