I'm trying to display text (a number), which is displayed on a background image.
What I currently got is the following:
body {
background-image: url("https://i.picsum.photos/id/10/800/800.jpg");
background-repeat: no-repeat;
background-position: center;
width: 400px;
height: 400px;
}
#age {
/* 1 pixel black shadow to left, top, right and bottom */
text-shadow: 2px 0 0 #fff, -2px 0 0 #fff, 0 2px 0 #fff, 0 -2px 0 #fff, 1px 1px #fff, -1px -1px 0 #fff, 1px -1px 0 #fff, -1px 1px 0 #fff;
font-family: sans;
color: transparent;
font-size: 40vw;
}
<h1 id="age">27</h1>
Basically, it works more or less as I want. However, the number itself is always filled with white color. What I would like though, is to have that number displayed huge there, have it filled with no color (transparent) and only use it with a border around the number itself. Later on, I want to upgrade to a counter, that visibly counts to the defined number first. However, the first step for me would be to display the number without the white fill.
Do you know any possible way to do this and only display a white border and have the background shine through for the rest?
There is an experimental -webkit-text-stroke CSS property, that does what you need (see this answer):
#age-container {
background-image: url("https://images.pexels.com/photos/68568/primula-catchfly-blossom-bloom-pink-68568.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
width: 400px;
height: 400px;
display: flex;
}
#age {
margin: auto;
-webkit-text-stroke: 10px white;
font-family: sans-serif;
color: transparent;
font-size: 250px;
}
<div id="age-container">
<h1 id="age">27</h1>
</div>
Another option would be to use a font that already looks the way you need it to (like these).
The issue in 2020 with:
text-stroke
text-stroke-width
text-stroke-color
text-fill-color
is that - despite having been around for over half a decade - these are still experimental properties and have never yet been added to any offical spec (either W3C or WHAT-WG).
That's why, even though text-stroke now has wide support from browsers, all browsers (including Firefox) only support text-stroke with the -webkit prefix:
-webkit-text-stroke
See: https://caniuse.com/#feat=text-stroke
Using the -webkit- browser vendor prefix may not be an issue for you.
But if it is, there is another way to visually display a text-stroke-outlined transparent number within:
<h1 id="age">27</h1>
and that's to apply an SVG image background to the <h1> element.
Working Example:
body {
display: flex;
justify-content: center;
align-items: flex-start;
}
#age {
position: relative;
font-family: sans-serif;
color: rgba(0, 0, 0, 0);
background-image: url("https://images.pexels.com/photos/1749/fire-orange-emergency-burning.jpg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940");
}
#age[data-age="27"]::after {
content: '';
position: absolute;
top: 0;
left: 0;
background-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' viewBox='0 0 400 400'><text x='204' y='320' text-anchor='middle' stroke='rgb(255, 255, 255)' stroke-width='6' fill='rgba(0, 0, 0, 0)' style='font: 320px sans-serif; font-weight: 700;'>27</text></svg>");
}
#age,
#age[data-age="27"]::after {
width: 400px;
height: 400px;
background-repeat: no-repeat;
background-position: center;
}
<h1 id="age" data-age="27">27</h1>
Related
I have panel which I colored blue if this panel is being selected (clicked on it). Additionally, I add a small sign (.png image) to that panel, which indicates that the selected panel has been already selected before.
So if the user sees for example 10 panels and 4 of them have this small sign, he knows that he has already clicked on those panels before. This work fine so far. The problem is now that I can't display the small sign and make the panel blue at the same time.
I set the panel to blue with the css background: #6DB3F2; and the background image with background-image: url('images/checked.png'). But it seems that the background color is above the image so you cannot see the sign.
Is it therefore possible to set z-indexes for the background color and the background image?
You need to use the full property name for each:
background-color: #6DB3F2;
background-image: url('images/checked.png');
Or, you can use the background shorthand and specify it all in one line:
background: url('images/checked.png'), #6DB3F2;
For me this solution didn't work out:
background-color: #6DB3F2;
background-image: url('images/checked.png');
But instead it worked the other way:
<div class="block">
<span>
...
</span>
</div>
the css:
.block{
background-image: url('img.jpg') no-repeat;
position: relative;
}
.block::before{
background-color: rgba(0, 0, 0, 0.37);
content: '';
display: block;
height: 100%;
position: absolute;
width: 100%;
}
Based on MDN Web Docs you can set multiple background using shorthand background property or individual properties except for background-color. In your case, you can do a trick using linear-gradient like this:
background-image: url('images/checked.png'), linear-gradient(to right, #6DB3F2, #6DB3F2);
The first item (image) in the parameter will be put on top. The second item (color background) will be put underneath the first. You can also set other properties individually. For example, to set the image size and position.
background-size: 30px 30px;
background-position: bottom right;
background-repeat: no-repeat;
Benefit of this method is you can implement it for other cases easily, for example, you want to make the blue color overlaying the image with certain opacity.
background-image: linear-gradient(to right, rgba(109, 179, 242, .6), rgba(109, 179, 242, .6)), url('images/checked.png');
background-size: cover, contain;
background-position: center, right bottom;
background-repeat: no-repeat, no-repeat;
Individual property parameters are set respectively. Because the image is put underneath the color overlay, its property parameters are also placed after color overlay parameters.
And if you want Generate a Black Shadow in the background, you can use
the following:
background:linear-gradient( rgba(0, 0, 0, 0.5) 100%, rgba(0, 0, 0, 0.5)100%),url("logo/header-background.png");
You can also use short trick to use image and color both like this :-
body {
background:#000 url('images/checked.png');
}
really interesting problem, haven't seen it yet. this code works fine for me. tested it in chrome and IE9
<html>
<head>
<style>
body{
background-image: url('img.jpg');
background-color: #6DB3F2;
}
</style>
</head>
<body>
</body>
</html>
The next syntax can be used as well.
background: <background-color>
url('../assets/icons/my-icon.svg')
<background-position-x background-position-y>
<background-repeat>;
It allows you combining background-color, background-image, background-position and background-repeat properties.
Example
background: #696969 url('../assets/icons/my-icon.svg') center center no-repeat;
This actually works for me:
background-color: #6DB3F2;
background-image: url('images/checked.png');
You can also drop a solid shadow and set the background image:
background-image: url('images/checked.png');
box-shadow: inset 0 0 100% #6DB3F2;
If the first option is not working for some reason and you don't want to use the box shadow you can always use a pseudo element for the image without any extra HTML:
.btn{
position: relative;
background-color: #6DB3F2;
}
.btn:before{
content: "";
display: block;
width: 100%;
height: 100%;
position:absolute;
top:0;
left:0;
background-image: url('images/checked.png');
}
Here is how I styled my colored buttons with an icon in the background
I used "background-color" property for the color and "background" property for the image.
<style>
.btn {
display: inline-block;
line-height: 1em;
padding: .1em .3em .15em 2em
border-radius: .2em;
border: 1px solid #d8d8d8;
background-color: #cccccc;
}
.thumb-up {
background: url('/icons/thumb-up.png') no-repeat 3px center;
}
.thumb-down {
background: url('/icons/thumb-down.png') no-repeat 3px center;
}
</style>
<span class="btn thumb-up">Thumb up</span>
<span class="btn thumb-down">Thumb down</span>
Assuming you want an icon on the right (or left) then this should work best:
.show-hide-button::after {
content:"";
background-repeat: no-repeat;
background-size: contain;
display: inline-block;
background-size: 1em;
width: 1em;
height: 1em;
background-position: 0 2px;
margin-left: .5em;
}
.show-hide-button.shown::after {
background-image: url(img/eye.svg);
}
You could also do background-size: contain;, but that should be mostly the same. the background-position will depened on your image.
Then you can easily do an alternative state on hover:
.show-hide-button.shown:hover::after {
background-image: url(img/eye-no.svg);
}
You can try with box shadow: inset
.second_info_block {
background: url('imageURL');
box-shadow: inset 0 0 0 1000px rgba(0,0,0,.4);
}
<li style="background-color: #ffffff;"><img border="0" style="border-radius:5px;background: url(images/picture.jpg') 50% 50% no-repeat;width:150px;height:80px;" src="images/clearpixel.gif"/></li>
Other Sample Box Center Image and Background Color
1.First clearpixel fix image area
2.style center image area box
3.li background or div color style
body
{
background-image:url('image/img2.jpg');
margin: 0px;
padding: 0px;
}
I have a full-page background image that I'd like to overlay scanlines over. I'm wanting to replicate the more traditional diagonal scanline effects that I grew up seeing in digital art of the noughties, such as implemented here in Bootstrap's pattern mask 5:
I've seen a few tutorials for diagonal scanlines, but haven't been able to find anything like this. How would I accomplish it in CSS?
here is an approximation using multiple background:
html {
height:100%;
background:
radial-gradient(#000 0.5px,transparent 0.5px) 0 0 /3px 3px,
radial-gradient(#000 0.5px,transparent 0.5px) 1px 1px /3px 3px,
radial-gradient(#000 0.5px,transparent 0.5px) 2px 2px /3px 3px,
url(https://i.picsum.photos/id/102/800/800.jpg) center/cover;
}
You can compare with the below that use the image pattern
html {
height:100%;
background:
url(https://i.ibb.co/C0MjrsJ/05.png),
url(https://i.picsum.photos/id/102/800/800.jpg) center/cover;
}
Try this code:
.view {
position: relative;
overflow: hidden;
cursor: default;
}
img{
position: relative;
display: block;
}
.view .mask{
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
width: 100%;
height: 100%;
overflow: hidden;
background-attachment: fixed;
background: url(https://i.ibb.co/C0MjrsJ/05.png);
background-attachment: fixed;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
height: 100%;
}
<div class="view" bis_skin_checked="1">
<img src="https://mdbootstrap.com/img/Photos/Others/nature-sm.jpg" class="img-fluid" alt="Image of ballons flying over canyons with mask pattern one.">
<div class="mask pattern-5 flex-center" bis_skin_checked="1">
<p class="white-text">.pattern-5</p>
</div>
</div>
All of the below methods to accomplish this were provided by Temani, I've just gathered them in one place to make them easier to choose from based on your needs.
Solution 1
This solution replicates the effect exactly for Firefox, but can only approximate the effect for Chrome and Edge because they don't support sub-pixel values:
html {
height:100%;
/* fallback for Firefox */
background:
radial-gradient(#000 0.5px,transparent 0.5px) 0 0 /3px 3px,
radial-gradient(#000 0.5px,transparent 0.5px) 1px 1px /3px 3px,
radial-gradient(#000 0.5px,transparent 0.5px) 2px 2px /3px 3px,
url(https://i.picsum.photos/id/102/800/800.jpg) center/cover;
/*Chrome and the latest version of Edge*/
background:
conic-gradient(from -90deg at 1px 1px,#000 0 90deg,transparent 0) 0 0 /3px 3px,
conic-gradient(from -90deg at 1px 1px,#000 0 90deg,transparent 0) 1px 1px/3px 3px,
conic-gradient(from -90deg at 1px 1px,#000 0 90deg,transparent 0) 2px 2px/3px 3px,
url(https://i.picsum.photos/id/102/800/800.jpg) center/cover;
}
It works on all modern versions of Firefox and Chrome, but only the very latest version of Edge.
Note that the very latest version of Edge (the Chromium-based one, and what Microsoft calls the "New Microsoft Edge") is currently only available as a standalone installer that hasn't been actively pushed by Microsoft. Therefore, it's highly unlikely for your site's users to be using this version of Edge at this point, even if they're on Windows 10 and up to date.
Solution 2
This is the exact same method used by Bootstrap in pattern-mask-5, and although it works on all modern browsers, it requires the use of an image in addition to adding a container <div> to the markup:
HTML:
<div class="bg-container">
</div>
CSS:
.bg-container {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: url(local/img.png); /* download the image here: https://i.ibb.co/C0MjrsJ/05.png and link to it */
}
Solution 3
The solution posted by Temani here is by far the cleanest, most cross-browser solution to this issue.
html {
height:100%;
background:
url('data:image/svg+xml;utf8,<svg viewBox="0 0 3 3" xmlns="http://www.w3.org/2000/svg"><rect width="1" height="1" /></svg>') 0 0 /3px 3px,
url('data:image/svg+xml;utf8,<svg viewBox="0 0 3 3" xmlns="http://www.w3.org/2000/svg"><rect width="1" height="1" /></svg>') 1px 1px/3px 3px,
url('data:image/svg+xml;utf8,<svg viewBox="0 0 3 3" xmlns="http://www.w3.org/2000/svg"><rect width="1" height="1" /></svg>') 2px 2px/3px 3px,
url(https://i.picsum.photos/id/102/800/800.jpg) center/cover;
}
It makes use of SVGs and therefore doesn't require any external images, and works on all modern browsers.
Here's a very simple css solution for horizontal scanlines using a pseudo element:
.scanlines::after {
content:'';
position: absolute;
width:100%;
height: 100%;
background-image: linear-gradient(rgba(39, 43, 46, 0.6) 1px, transparent 1px);
background-size: 2px 2px;
background-attachment: fixed;
}
This creates an overlay that matches the size of the root image element. Then we create the scanline - actually two lines (one dark semi-opaque, one transparent) - and repeat it. You can adjust the color and height of the line here:
background-image: linear-gradient(rgba(39, 43, 46, 0.6) 1px
and the space between the lines here:
background-size: 2px 2px;
The root element looks like this in my case:
/* The background/header image */
.scanlines {
background-image: url('https://www.telegraph.co.uk/content/dam/Travel/2018/January/sydney-best-GETTY.jpg?imwidth=1400');
background-size: cover;
background-repeat: no-repeat;
background-position: center;
width:100vw;
height:100vh;
position: relative;
}
Codepen here:
I did play around with rotating the pseudo element 45 deg, but the results weren't satisfactory so I decided to post the answer for horizontal lines only.
https://developer.mozilla.org/en-US/docs/Web/CSS/linear-gradient
I'd like to simulate a drop shadow effect using border-image and linear-gradient (for scroll performance reasons, I am not using the native box-shadow effect).
As can be seen in the example below and in the fiddle, my attempted approach involves using border-image for the gradient, border-image-outset to move the shadow outside the content box, and border-width to show only the bottom edge.
Admittedly, I don't understand border-image so well, particularly when it comes to using it with linear-gradients. Through trial and error, I achieved what seemed to be a satisfactory result. But as it turns out, when the width of the div is short enough, the "shadow" disappears entirely.
What can I do to achieve a drop shadow like in the top box, but one that works regardless of the box size? Your help with this is really appreciated!
.box
{
/* the "shadow" */
border-image: linear-gradient(to bottom, rgba(0,0,0,0.5) 0%, rgba(0,0,0,0.5) 10%, rgba(0,0,0,0) 100%) 100 repeat;
border-image-outset: 0px 0px 6px 0px;
border-width: 0px 0px 6px 0px;
border-style: solid;
/* other stuff */
font-family: sans-serif;
font-size: 20px;
color: #FEFEFE;
background: #007277;
margin: 10px 0px;
float: left;
clear: left;
padding: 50px;
}
<div class="box">
Here's longer text, where the "shadow" appears how I want it to.
</div>
<div class="box">
Short
</div>
For the short border to work you need to change the
100 repeat;
to
0 0 100 0 repeat;
.box
{
/* the "shadow" */
border-image: linear-gradient(to bottom, rgba(0,0,0,0.5) 0%, rgba(0,0,0,0.5) 10%, rgba(0,0,0,0) 100%) 0 0 100 0 repeat;
border-image-outset: 0px 0px 6px 0px;
border-width: 0px 0px 6px 0px;
border-style: solid;
/* other stuff */
font-family: sans-serif;
font-size: 20px;
color: #FEFEFE;
background: #007277;
margin: 10px 0px;
float: left;
clear: left;
padding: 50px;
}
<div class="box">
Here's longer text, where the "shadow" appears how I want it to.
</div>
<div class="box">
Short
</div>
This link may help you a little on your border imaging https://css-tricks.com/understanding-border-image/
I known you can add an outline border with CSS3.
outline: 10px solid red;
Now I was wondering how I can add also a radius to that outline border.
I have tried this one, but doesn't work:
.radius {
padding: 20px 60px;
text-transform: capitalize;
-moz-outline: 10;
outline: 10px solid red;
-webkit-border-radius: 40px;
-moz-border-radius: 40px;
border-radius: 40px;
}
Try using CSS-Tricks' Infinite Borders technique and applying border-radius.
This method will require borders and box-shadow and not outline.
img {
border-radius: 4px;
/* #1 */
border: 5px solid hsl(0, 0%, 40%);
/* #2 */
padding: 5px;
background: hsl(0, 0%, 20%);
/* #3
outline: 5px solid hsl(0, 0%, 60%); */
/* #4 AND INFINITY!!! (CSS3 only) */
box-shadow:
0 0 0 10px red,
0 0 0 15px orange,
0 0 0 20px yellow,
0 0 0 25px green,
0 0 0 30px blue;
/* If you could do pseudo elements
you could get a few more... */
/* Also, HSL is awesome but don't use it if
you need super old browser support */
}
body { padding: 50px; text-align: center; }
<img src="https://www.randomlists.com/img/animals/chipmunk.jpg">
Firefox has a property -moz-outline-radius, however the request to implement a similar feature in WebKit was closed as WONTFIX. The plan for the future is to make the outlines follow the borders.
I realize this doesn't help much, but the answer to your question is: currently, no (not in a cross browser way). In the meantime you should use an alternative approach like the one suggested by thekalaban.
#MichaelYaeger Similar answer to user1685185 but with an updated JSFiddle, use border-radius and box-shadow. This JS Fiddle is shown using a "border" around a circular button (bootstrap), but the same applies an image, etc.
I am trying to achieve a gradient + text shadow effect in Chrome/Safari using CSS text-shadow and a combination of text-shadow and background-image: -webkit-gradient, see example blw. I can only make one of the effects apply(if I add the shadow the gradient disappears. What am I doing wrong?
h1 {
font-size: 100px;
background-image: -webkit-gradient(linear, left top, left bottom, from(white), to(black));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
text-shadow: 0 1px 1px #fff;
}
The gradient "disappears" because the text-shadow is on a level above the background.
The text (which is transparent)
The shadow
The background.
We can work around this by copying the text and put it below the original layer, then apply the shadow there, for example:
h1 {
position: relative;
font-size: 100px;
text-align: center;
}
h1 div {
background-image: linear-gradient(white, black);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
position: absolute;
width: 100%;
}
h1:after {
text-shadow: 10px 10px 11px #fff;
color: transparent;
}
#hello:after {
content: 'Hello World';
}
<h1 id="hello"><div>Hello World</div></h1>
With no extra HTML markup or pseudo elements you can achieve this effect using the filter property and drop-shadow function. This method also works with a background image vs gradient.
h1 {
font:54px 'Open Sans', 'Helvetica', Arial, sans-serif;
background-image: linear-gradient(#787878, #484848);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
-webkit-filter: drop-shadow(2px 2px #333);
filter: drop-shadow(2px 2px #333);
}
Fiddle: https://jsfiddle.net/eywda89g/
This answer is similar to the answer by #KennyTM above, except his answer has the shadow hard-coded into the CSS, which is not suitable for dynamic text such as in a CMS. Also, his method requires a separate ID for each instance, which would be very tedious if you plan to use this effect a lot. The example below uses a class instead, and allows dynamic text.
Try this:
h1 {
position: relative;
font-size: 100px;
text-align: center;
}
h1 div {
background-image: linear-gradient(teal, black);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
position: absolute;
width: 100%;
}
h1:after {
text-shadow: 2px 2px 2px #000000;
color: transparent;
}
.gradient-shadow:after {
content: attr(title); /* Pulls the text from the 'title' attribute to make the shadow */
}
And then in your HTML:
<h1 class="gradient-shadow" title="Hello World"><div>Hello World</div></h1>
Just make sure that the text in the <div> matches the text in the title attribute.
Here is a Codepen example of this method:
https://codepen.io/mprewitt/pen/gexypd
These answers helped me a lot in getting to my final result. Thank you.
So I figured I would share it. Seeing the colour of my text is light, I needed a darker "border" at the top to make it pop.
Also while 'ems' are harder to work with (as opposed to px), I found that the transition of colours for the text-shadow looks a lot smoother as I wanted to make it a gradient as well :)
Works on Edge, Chrome, Vivaldi and FireFox, a little blurry though.
<h1 class="text3d">Privacy Policy</h1>
.text-3d{
background-image:linear-gradient(to bottom,#f7eb3b 30%,#f5d839 40%,#eead34 50%, #eb9531 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
filter:
drop-shadow(-0.015em -0.015em 0 #ffff00)
drop-shadow(-0.015em -0.015em 0 #bf290c)
drop-shadow(0 -0.005em 0 #bf290c)
drop-shadow(0.010em 0.025em 0 #bf290c)
drop-shadow(0.015em 0.030em 0 #b6240b)
drop-shadow(0.020em 0.035em 0 #a91d0b)
drop-shadow(0.025em 0.040em 0 #8d0d09)
drop-shadow(0.030em 0.045em 0 #830708)
drop-shadow(0.035em 0.050em 0 #680a07)
drop-shadow(0.01em 0.08em 0.01em rgba(0,0,0,0.10))
}