Is it possible to have this striped and dashed background with CSS only?
Background is created by Chrome when inspecting flex elements, and I find it really cool.
You can use repeating-linear-gradient like this:
body {
padding: 0;
margin: 0;
height: 100vh;
width: 100w;
background: repeating-linear-gradient(
45deg,
tomato 0px,
tomato 2px,
transparent 2px,
transparent 9px
);
}
div {
height: 100vh;
width: 100vw;
background: repeating-linear-gradient(
-45deg,
white 0px,
white 4px,
transparent 4px,
transparent 12px
);
}
<div></div>
Tweak with the code
These few lines do produce a somewhat similar pattern, but it won't look good on a low DPI screen. So, in my opinion, it's better to use an SVG pattern instead of pure CSS.
You can use repeating-linear-gradient but i couldn't make it dashed:
//Example
body, html {
background: repeating-linear-gradient(
-55deg,
#606dbc,
#606dbc 5px,
#465298 5px,
#465298 10px
);
}
Here's the SVG solution to my problem. CSS doesn't look to good.
<svg width="100%" height="100%" xmlns="http://www.w3.org/2000/svg">
<!-- Let's define the pattern -->
<!-- The width and height should be double the size of a single checker -->
<pattern id="pattern-checkers" x="0" y="0" width="100%" height="15" patternUnits="userSpaceOnUse" patternTransform="rotate(45)">
<!-- Two instances of the same checker, only positioned apart on the `x` and `y` axis -->
<!-- We will define the `fill` in the CSS for flexible use -->
<line x1="0" y1="7" x2="100%" y2="7" stroke="#F3817F"
stroke-dasharray="8 4" />
</pattern>
<!-- Define the shape that will contain our pattern as the fill -->
<rect x="0%" y="0" width="100%" height="100%" fill="url(#pattern-checkers)"></rect>
</svg>
I suggest to use patterns for creating wonderful backgrounds: http://projects.verou.me/css3patterns/
Related
I have a div with a width and height of approximately 300px each. The background of this div should be filled repeatedly with a smaller image of 8px width and 8px height. This smaller image is embedded into a larger sprite image. How can I only use this 8x8 pixel tile to pave the background?
What I've tried so far:
.world {
width: 300px;
height: 300px;
background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAIAAABvFaqvAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4wcYFjsX3EPV0QAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAxklEQVQ4y+2UsQ3CMBBF/0VImYDKFWNQZB28AwOwQ7KOC8agomKCVJ8ituU7GwkhVxFXWT+nF8nvy0JSRFAMye0wqRghxrheTmV+Wx4A9G7BmlpfAi0lswZ0mj2DsiNjLbBx09lRw1oN+m0OAKB7hIQWvFSM43ZY5V7mI88AhB+KZCiZZSiZ9e/RN6BaP1k6MtY2Ry1rPXv01D1yET3qR2ZF+qXX+zMBDJaSuGP1VMXEV/te9q6/G8hV+h2VI2NtrvZn9uzRGzl9Uzsn2uDrAAAAAElFTkSuQmCC') 0 -16px repeat;
}
<div class="world"></div>
In this example code, the shritesheet has a width and height of 24px each. It contains 9 differently coloured points, each of which has a diameter of 7px.
I want that only one single point is used repeatedly as background for the div. However, in this code, the whole picture is repeated instead beginning at the third row of points.
Note, this exemplary spritesheet is only an example. The actual spritesheet is a little more complicated. Replacing the spritesheet
with CSS code is not a solution.
One idea is to consider SVG as background. The trick is to make the image inside the SVG and rely on viewbox to cut the image and show only the needed part then make the whole SVG a pattern for your background.
Simply edit the viewBox to select the pattern you want (x y 8 8 where x,y are equal to [0,8,16])
.box {
width: 100px;
height: 100px;
display:inline-block;
}
.one {
background: url('data:image/svg+xml;utf8,<svg viewBox="16 0 8 8" width="8" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><image xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAIAAABvFaqvAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4wcYFjsX3EPV0QAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAxklEQVQ4y+2UsQ3CMBBF/0VImYDKFWNQZB28AwOwQ7KOC8agomKCVJ8ituU7GwkhVxFXWT+nF8nvy0JSRFAMye0wqRghxrheTmV+Wx4A9G7BmlpfAi0lswZ0mj2DsiNjLbBx09lRw1oN+m0OAKB7hIQWvFSM43ZY5V7mI88AhB+KZCiZZSiZ9e/RN6BaP1k6MtY2Ry1rPXv01D1yET3qR2ZF+qXX+zMBDJaSuGP1VMXEV/te9q6/G8hV+h2VI2NtrvZn9uzRGzl9Uzsn2uDrAAAAAElFTkSuQmCC" x="0" y="0" height="24" width="24" /></svg>') repeat;
}
.two {
background: url('data:image/svg+xml;utf8,<svg viewBox="16 8 8 8" width="8" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><image xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAIAAABvFaqvAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4wcYFjsX3EPV0QAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAxklEQVQ4y+2UsQ3CMBBF/0VImYDKFWNQZB28AwOwQ7KOC8agomKCVJ8ituU7GwkhVxFXWT+nF8nvy0JSRFAMye0wqRghxrheTmV+Wx4A9G7BmlpfAi0lswZ0mj2DsiNjLbBx09lRw1oN+m0OAKB7hIQWvFSM43ZY5V7mI88AhB+KZCiZZSiZ9e/RN6BaP1k6MtY2Ry1rPXv01D1yET3qR2ZF+qXX+zMBDJaSuGP1VMXEV/te9q6/G8hV+h2VI2NtrvZn9uzRGzl9Uzsn2uDrAAAAAElFTkSuQmCC" x="0" y="0" height="24" width="24" /></svg>') repeat;
}
.three {
background: url('data:image/svg+xml;utf8,<svg viewBox="8 0 8 8" width="8" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><image xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAIAAABvFaqvAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4wcYFjsX3EPV0QAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAxklEQVQ4y+2UsQ3CMBBF/0VImYDKFWNQZB28AwOwQ7KOC8agomKCVJ8ituU7GwkhVxFXWT+nF8nvy0JSRFAMye0wqRghxrheTmV+Wx4A9G7BmlpfAi0lswZ0mj2DsiNjLbBx09lRw1oN+m0OAKB7hIQWvFSM43ZY5V7mI88AhB+KZCiZZSiZ9e/RN6BaP1k6MtY2Ry1rPXv01D1yET3qR2ZF+qXX+zMBDJaSuGP1VMXEV/te9q6/G8hV+h2VI2NtrvZn9uzRGzl9Uzsn2uDrAAAAAElFTkSuQmCC" x="0" y="0" height="24" width="24" /></svg>') repeat;
}
<div class="box one"></div>
<div class="box two"></div>
<div class="box three"></div>
I have an SVG:
<svg xmlns="https://www.w3.org/2000/svg" width="100%" height="100%">
<defs>
<pattern id="stripes" patternUnits="userSpaceOnUse" width="7" height="6" patternTransform="rotate(45)">
<line x1="1" y="0" x2="1" y2="7" stroke="#fffa72" stroke-width="1.5" />
</pattern>
</defs>
<rect width="100%" height="100%" fill="#fffeea" />
<rect width="100%" height="100%" fill="url(#stripes)" />
</svg>
It tested it with an external tool and it works nicely:
Now I'd like to use it in background-image. I encoded the SVG with https://www.url-encode-decode.com/
I then take the encoded SVG and put it into my SCSS:
background-image: url('data:image/svg+xml;charset=utf8,%3Csvg+xmlns%3D%22https%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22+width%3D%22100%25%22+height%3D%22100%25%22%3E%0D%0A++%3Cdefs%3E%0D%0A++++%3Cpattern+id%3D%22stripes%22+patternUnits%3D%22userSpaceOnUse%22+width%3D%227%22+height%3D%226%22+patternTransform%3D%22rotate%2845%29%22%3E%0D%0A++++++%3Cline+x1%3D%221%22+y%3D%220%22+x2%3D%221%22+y2%3D%227%22+stroke%3D%22%23fffa72%22+stroke-width%3D%221.5%22+%2F%3E%0D%0A++++%3C%2Fpattern%3E%0D%0A++%3C%2Fdefs%3E%0D%0A++%3Crect+width%3D%22100%25%22+height%3D%22100%25%22+fill%3D%22%23fffeea%22+%2F%3E%0D%0A++%3Crect+width%3D%22100%25%22+height%3D%22100%25%22+fill%3D%22url%28%23stripes%29%22+%2F%3E%0D%0A%3C%2Fsvg%3E');
this doesn't work. the background remain empty. I know my HTML works fine because when I plug a different background image with a SVG I found online, it works nicely:
background-image: url('data:image/svg+xml;charset=utf8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Cdefs%3E%3Cpattern%20id%3D%22a%22%20patternUnits%3D%22userSpaceOnUse%22%20width%3D%225%22%20height%3D%225%22%20patternTransform%3D%22rotate(45)%22%3E%3Cpath%20stroke%3D%22%23fffa72%22%20d%3D%22M1%200v5%22%2F%3E%3C%2Fpattern%3E%3C%2Fdefs%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22url(%23a)%22%2F%3E%3C%2Fsvg%3E');
What am I doing wrong?
Your xmlns value is incorrect. It's http://www.w3.org/2000/svg, not https://.
body {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100%25' height='100%25'%3E%3Cdefs%3E%3Cpattern id='stripes' patternUnits='userSpaceOnUse' width='7' height='6' patternTransform='rotate(45)'%3E%3Cline x1='1' y='0' x2='1' y2='7' stroke='%23fffa72' stroke-width='1.5' /%3E%3C/pattern%3E%3C/defs%3E%3Crect width='100%25' height='100%25' fill='%23fffeea' /%3E%3Crect width='100%25' height='100%25' fill='url(%23stripes)' /%3E%3C/svg%3E");
}
html, body {height: auto;}
<div></div>
Documentation: https://www.w3.org/2000/svg - yep, with https:// :D
In embedded <svg> elements most browsers default the attribute to http://www.w3.org/2000/svg when it doesn't resolve (you can basically delete it and it still works). But they don't do it for background-image base64 encoded SVGs.
In short, it won't work as image if it's invalid.
Maybe this is not the answer to your exact question,
but you might make your background using a linear-gradient with a way simpler code:
body {
background: linear-gradient(135deg, #fff 2px, #fffa72, #fff 5px, #fff 9px, #fffa72, #fff 12px) 0 0 / 10px 10px;
}
Save your svg to a file.svg and try using https://www.developertoolkits.com/base64/encoder
It takes images and turns them into data urls that you can then drop inline or in css like you are trying to do.
The website you are using is a url encoder.
I am currently working on a scrollbar which looks just like this. If this text is to long for you just skip to the example!
There will be arrows at the left and right side to scroll either left or right. The color of the scrollbar is not grey as it seems but rgba(0,0,0,0.6). So its a transparent black, which will be used in front of images.
Now I want the last few letters in the line to fade out like in this example. To accomplish that I am using a div overlay with:
background-image: linear-gradient(to right, rgba(255,255,255,0), black)
But if I would do that with a transparent scrollmenu, it would mess up the background-color of the scrollmenu (if you don´t know what I mean look here).
So I found a solution by combining two divs with linear-gradients on the very right, which, if you lay them over each other, create exactly the background color of the scrollmenu. One of these lays behind the font, one of them overlays the font. That way I can achieve some transparency on the font. Here is an example for you guys:
#rightPart, #leftPart{
width:200px; height:50px;
position:absolute; top:0;left:0;
color: white;
}
#rightPart{
z-index:-1;
background:linear-gradient( to right, rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0));
}
#leftPart{
background:linear-gradient( to left, rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0));
}
<div id="rightPart"> Slight Transparency effect on this </div>
<div id="leftPart"></div>
The problem is that the transparency effect is limited to the background transparency of 0.5. Therefore the transparency effect can´t get as strong as I want it to be.
Now I am asking for a solution, with which I could achieve a stronger transparency effect. I would appreciate your suggestions.
Please remember that I can´t just make a specific word in the end transparent, since there is always a different word in the end of the scrollbar! Consequently I would need to make the font itself in a specific area transparent. And I personally don´t know how to do that (especially if it is supposed to work on all of the newest browser versions - including IE9+).
<svg> filters are just what your looking for.
IE9 here to ruin your day CAN I USE IT?
Now to adjust the filter simply change the offset of the stop elements inside the linearGradiant.
offset to determine where the effect takes place
stop-color and stop-opacity to determine what the effect should be
example:
<br>
<svg height="40" width="200" viewbox="0 0 100 20" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="endfade" x1="0%" y1="0%" x2="100%" y2="0">
<stop offset="50%" stop-opacity="1" />
<stop offset="90%" stop-opacity="0" />
</linearGradient>
</defs>
<text id="spesial" fill="url(#endfade)" x="0" y="15">Your text here</text>
</svg>
<br>Color?
<br>
<svg height="40" width="200" viewbox="0 0 100 20" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="pinkblue" x1="0%" y1="0%" x2="100%" y2="0">
<stop offset="25%" stop-opacity="1" stop-color="pink" />
<stop offset="50%" stop-opacity="1" stop-color="aqua" />
<stop offset="90%" stop-opacity="0" stop-color="aqua" />
</linearGradient>
</defs>
<text id="spesial" fill="url(#pinkblue)" x="0" y="15">Your text here</text>
</svg>
Well, I can not offer a solution with full browser support.
But since there isn't any other answer, may be it can be useful
First, let's create a base div that will hold the semitransparent color.
Above it, let's set a div with the text. overlayed by a gradient from transparent to gray. this makes the right side of the div fully gray.
The trick is to set for this div a mix-blend-mode of hard-light. This makes gray behave as transparent, keeping black as black
#base {
left: 0px;
width: 200px;
height: 50px;
position: relative;
background: rgba(0,0,0,0.5);
}
#text {
position: absolute;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
mix-blend-mode: hard-light;
}
#text:after {
content: "";
position: absolute;
left: 0px;
top: 0px;
height: 100%;
width: 100%;
background: linear-gradient(90deg, transparent 30%, gray 80%);
}
#base:nth-child(2) {
background: rgba(0,0,0,0.25);
}
#base:nth-child(3) {
background: rgba(0,0,0,0.125);
}
<div id="base">
<div id="text"> Slight Transpareny effect on this </div>
</div>
<div id="base">
<div id="text"> Slight Transpareny effect on this </div>
</div>
<div id="base">
<div id="text"> Slight Transpareny effect on this </div>
</div>
Setting a background position works on Chrome, Safari and Firefox, but not on IE 8-11. What's wrong here?
DEMO on Dabblet
DEMO on Webdevout
.logo {
display: block;
width: 200px;
border: 2px solid red;
background: url("layout/logo.png") center right no-repeat; /* fallback image */
background-image: url('data:image/svg+xml;base64,...'), none; /* two bg to only use svg on supported browsers. IE 11 uses this image */
background-position: center right;
background-repeat: no-repeat;
background-size: auto 100%;
}
UPDATE:
IE seems to ignore background positioning on SVG images. Here's a WORKAROUND DEMO
I guess the best way of doing this with a guaranty of cross-browser working is by making the background image using a pattern and then set the attributes like x,y,width or height to position it as you want directly or later in jquery via the attr method for example: $("#img1").attr("width","500");
<defs>
<pattern id="img1" patternUnits="userSpaceOnUse" width="100" height="100">
<image xlink:href="layout/logo.png" x="0" y="0" width="100" height="100" />
</pattern>
</defs>
and set the pattern to fill the SVG path like this
<path fill="url(#img1)" id="my_svg" d="M5,50 l0,100 l100,0 l0,-100 l-100,0
M215,100
a50,50 0 1 1 -100,0 50,50 0 1 1 100,0
M265,50
l50,100 l-100,0 l50,-100
z"/>
or you can set the background in this very way but in the CSS like this
#my_svg{
fill:url(#img1);
}
There is also an alternative way which i do not recommend but may be the answer in some cases and that is the transform="translate(x,y)" for the image in pattern or the patternTransform="translate(x,y)" for the pattern which is quit like the css version if it..for example:
<pattern id="img1" patternUnits="userSpaceOnUse" width="100" height="100" patternTransform="translate(0,0)">
<image xlink:href="layout/logo.png" x="0" y="0" width="100" height="100" transform="translate(0,0)" />
</pattern>
anyway there is always good practice to know many different ways of doing something in our job to be able to survive ;-)
How can I make this box in CSS?
I've seen a few tutorials that teach how to create boxes with arrows, however, in my case, none of those tutorials are suitable.
I created your element with the surrounding 1px border. I'm using one <div> element and taking advantage of the :before and :after pseudo-elements (browser-support). The main rectangle has a regular 1px border, but the triangle elements are essentially 2 triangles, one darker than the other.
The lighter triangle sits on top of the darker triangle, which has the effect of hiding it, and is shifted slightly to the left to show the darker triangle underneath. The resulting illusion is a 1px dark border on the triangle itself.
Here's a question that asks a similar question:
How can I create a "tooltip tail" using pure CSS?
One of the answers actually has a great explanation of how one can achieve this triangle effect:
https://stackoverflow.com/a/5623150/196921
Also, this is an excellent reference for all the fancy things you can do with borders (thanks to PSCoder):
http://css-tricks.com/examples/ShapesOfCSS/
... and here's a sweet css generator (thanks to David Taiaroa):
http://cssarrowplease.com/
Anyway, here's the corresponding code:
#arrow {
width: 128px;
height: 100px;
background-color: #ccc;
border: 1px solid #999;
position: relative;
}
#arrow:after {
content: '';
position: absolute;
top: 0px;
left: 128px;
width: 0;
height: 0;
border: 50px solid transparent;
border-left: 12px solid #ccc;
}
#arrow:before {
content: '';
position: absolute;
top: 0px;
left: 129px;
width: 0;
height: 0;
border: 50px solid transparent;
border-left: 12px solid #999;
}
<div id="arrow"></div>
Here is the solution I created
There are 2 simple ways to do this. The first, less efficient way is to have 2 elements. I take advantage of the :after pseudo element. I used position:absolute on the :after for 2 reasons.
You can place the element where you need to
It prevents the end of the triangle from being cut off
The key to creating the triangle is using the border property. You have 2 borders with the color of transparent set. These 2 borders are opposite of the direction you want to go. So if you want to make a right triangle, then use top and bottom. What gives the arrow it's shape is the last border. It also goes in the opposite direction. So for a right triangle, you would use border-left with a color. To get this to be the proper height, you must do half of the height of the box you want to place it on
SVG is the recommended way to create such shapes. It offers simplicity and scale-ability.
We can use SVG's polygon or path element to create a shape like above and stroke / fill it with some solid color, gradient or a pattern.
Only one attribute points is used to define shapes in polygon element. This attribute consists of a list of points. Each point must have 2 numbers, an x coordinate and a y coordinate. A straight line is drawn automatically from the last point to the starting point to close the shape.
Below is the necessary code to create this shape:
<polygon points="10,12 265,10 285,93 265,184 10,184"
stroke="#333"
stroke-width="2"
fill="#eee" />
Below is a brief description of the above code:
points attribute defines the structure of the shape.
stroke attribute defines the color for the outline / border.
stroke-width defines the thickness of the outline / border.
fill attribute defines the color for the interior shape to be filled.
Output Image:
Working Example:
body {
background: #b6cdc7 url("https://www.hdwallpapers.net/previews/hot-air-balloon-over-the-mountain-987.jpg") no-repeat;
background-position: center bottom;
background-size: cover;
margin: 0;
}
.box {
justify-content: center;
align-items: center;
height: 100vh;
display: flex;
}
<div class="box">
<svg width="300" height="200" viewBox="0 0 300 200">
<polygon points="10,12 265,10 285,93 265,184 10,184" stroke="#333" stroke-width="2" fill="#eee" />
</svg>
</div>
This shape can be filled with gradient or pattern as well.
Working Example:
body {
background: #b6cdc7 url("https://www.hdwallpapers.net/previews/hot-air-balloon-over-the-mountain-987.jpg") no-repeat;
background-position: center bottom;
background-size: cover;
margin: 0;
}
.box {
justify-content: center;
align-items: center;
height: 100vh;
display: flex;
}
<div class="box">
<svg width="300" height="200" viewBox="0 0 300 200">
<defs>
<linearGradient id="grad">
<stop offset="0" stop-color="#11a798" />
<stop offset="1" stop-color="#23645d" />
</linearGradient>
</defs>
<polygon id="shape" points="10,12 265,10 285,93 265,184 10,184" stroke="#333" stroke-width="2" fill="url(#grad)" />
</svg>
</div>
We can apply shadow on this shape using SVG's filters.
Working Example:
body {
background: #b6cdc7 url("https://www.hdwallpapers.net/previews/hot-air-balloon-over-the-mountain-987.jpg") no-repeat;
background-position: center bottom;
background-size: cover;
margin: 0;
}
.box {
justify-content: center;
align-items: center;
height: 100vh;
display: flex;
}
<div class="box">
<svg width="300" height="200" viewBox="0 0 300 200">
<defs>
<linearGradient id="grad">
<stop offset="0" stop-color="#11a798" />
<stop offset="1" stop-color="#23645d" />
</linearGradient>
<filter id="shadow">
<feGaussianBlur in="SourceAlpha" stdDeviation="4" />
<feMerge>
<feMergeNode />
<feMergeNode in="SourceGraphic" />
</feMerge>
</filter>
</defs>
<polygon id="shape" points="10,12 265,10 285,93 265,184 10,184" stroke="#333" stroke-width="2" fill="url(#grad)" filter="url(#shadow)" />
</svg>
</div>