I would like to add a white background to my letters.
I have an image as example, but I would like to build this image with CSS.
To see the difference, edit html with editor and add black as body background.
I have tried this with text-shadow, but it didn't work great.
My question is how can I build this with css?
Kind regards,
Itouch
I believe that text-shadow is the only way to go.
something like :
text-shadow: -1px 0 #fff, 0 1px #fff, 1px 0 #fff, 0 -1px #fff
Demo : http://codepen.io/mbrillaud/pen/zGxdMQ
I have a solution which is not really answering your question but still it might help (i hope).
You can make use of SVG and stroke-linejoin.
<svg height="100" width="250">
<text
x="0"
y="50"
fill="red"
stroke="#ffffff"
stroke-width="3px"
font-size="50px"
letter-spacing="-4"
stroke-linejoin="round"
stroke-linecap="round"
font-weight="600">I love SVG!</text>
</svg>
JSBIN: http://jsbin.com/zemeya/2/edit?html,css,output
Edit: I changed the font to be more funky adding this line between head tags.
<link href='http://fonts.googleapis.com/css?family=Indie+Flower' rel='stylesheet' type='text/css'>
It's a combination of text-shadow and letter-spacing.
First of all you need to get a solid "border" around your text.
For that use this:
text-shadow:-1px 0 #fff, 0 1px #fff, 1px 0 #fff, 0 -1px #fff,
-2px 0 #fff, 0 2px #fff, 2px 0 #fff, 0 -2px #fff
But that's not all. As there is another shadow which is kinda blurry you have to add another text-shadow:
text-shadow:-1px 0 #fff, 0 1px #fff, 1px 0 #fff, 0 -1px #fff,
-2px 0 #fff, 0 2px #fff, 2px 0 #fff, 0 -2px #fff,
2px 2px 3px #000;
Now you may remove some letter-spacing. You can do this by use a nagative value for it:
letter-spacing: -6px;
#import url(http://fonts.googleapis.com/css?family=Chewy);
span {
font-family: 'Chewy', cursive;
letter-spacing: -6px;
font-size:50px;
color:#555;
text-shadow:-1px 0 #fff,
0 1px #fff,
1px 0 #fff,
0 -1px #fff,
-2px 0 #fff,
0 2px #fff,
2px 0 #fff,
0 -2px #fff,
2px 2px 3px #000;
}
<span>home</span>
Demo
You can play with text-shadow property, but than you would have to stack values to achieve thicker stroke this way:
body {
background-color: #ddd;
}
p {
font-size: 52px;
color: #666666;
text-shadow: 0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,0 0 3px #fff,
4px 4px 6px #000, 4px 4px 6px #000;
}
<p>home</p>
Also on codepen.
You can do a colored outline on text by using the text-shadow in CSS. The trick is you have to do multiple shadows:
.white_background {
text-shadow: 0px 0px 1px #fff,
0px 0px 1px #fff,
0px 0px 1px #fff,
0px 0px 1px #fff;
}
CSS supports multiple listings of text-shadow. Each listing stacks with the others. If you stack up enough, they give the appearance of a solid outline.
Related
I am attempting to style some text, with white text and a black stroke. I want it to have a thick stroke while still having the text be visible.
For this, I used the following CSS:
element {
-webkit-text-stroke: 6px black;
color: white;
paint-order: stroke;
}
However, paint-order does not function properly on Chrome browsers. Here is a demonstration, Firefox on the left and Chrome on the right. You can see on the right that Chrome is not honoring the paint-order attribute, despite supposedly being compatible since version 35.
The CodeSandbox I made to demonstrate.
Is there some additional attribute needed for this to work on all browsers? My Chrome version is 80.0.3987.132 and Firefox is 74.0.
Chrome only supports paint-order on SVG text. There is no support for HTML text, although it could be supported in the future since it's supported in Firefox and Safari. In the meantime you can use text-shadow as a fallback for Chrome and older browsers.
Related ticket: https://bugs.chromium.org/p/chromium/issues/detail?id=815111
edit to anyone falling here , text-shadow is still the fallback to use for HTML/CSS and IE11, but FF,Chrome and edge will apply it to your SVG.
-webkit-text-stroke, is non-standard and off standards track, where IE11 also fails .
original answer
Does your browser supports paint-order ?
check it here : https://caniuse.com/#feat=mdn-css_properties_paint-order
you can use many text-shadow to get the blur effect finally looking alike a stroke effect as a possible alternative:
demo of the idea that's worth what it is:
h1, h2 {
color:white;
text-shadow: 0 0 3px black, 0 0 3px black, 0 0 3px black, 0 0 3px black, 0 0 3px black,0 0 3px black, 0 0 3px black, 0 0 3px black, 0 0 3px black, 0 0 3px black,0 0 3px black, 0 0 3px black, 0 0 3px black, 0 0 3px black, 0 0 3px black,0 0 3px black, 0 0 3px black, 0 0 3px black, 0 0 3px black, 0 0 3px black;
letter-spacing:4px;
}
/* dummy demo */
h2 {
text-shadow:
0 0 2px blue,0 0 2px blue,0 0 2px blue,0 0 2px blue,0 0 2px blue,
0 0 4px white,0 0 4px white,0 0 4px white,0 0 4px white,0 0 4px white, 0 0 4px white,0 0 4px white,0 0 4px white,0 0 4px white,0 0 4px white,0 0 4px white,0 0 4px white,0 0 4px white,0 0 4px white,0 0 4px white, 0 0 4px white,0 0 4px white,0 0 4px white,0 0 4px white,0 0 4px white,0 0 4px white,0 0 4px white,0 0 4px white,0 0 4px white,0 0 4px white, 0 0 4px white,0 0 4px white,0 0 4px white,0 0 4px white,0 0 4px white,
0 0 6px crimson,0 0 6px crimson,0 0 6px crimson,0 0 6px crimson,0 0 6px crimson,0 0 6px crimson,0 0 6px crimson,0 0 6px crimson,0 0 6px crimson,0 0 6px crimson,0 0 6px crimson,0 0 6px crimson,0 0 6px crimson,0 0 6px crimson,0 0 6px crimson,0 0 6px crimson,0 0 6px crimson,0 0 6px crimson,0 0 6px crimson,0 0 6px crimson;
}
<h1>Hello The World</h1>
<h2>Hello The World</h2>
If you want to implement this average text-shadow stroking only where paint-order is not implemented, #supports could be usefull https://developer.mozilla.org/en-US/docs/Web/CSS/#supports / https://caniuse.com/#feat=css-featurequeries
I tried to make a "text border" with CSS text-shadow. Finally there are 121 shadows:
text-shadow:
-5px -5px 0 var(--grau_dunkel),-4px -5px 0 var(--grau_dunkel),-3px -5px 0 var(--grau_dunkel),-2px -5px 0 var(--grau_dunkel),-1px -5px 0 var(--grau_dunkel),0 -5px 0 var(--grau_dunkel),1px -5px 0 var(--grau_dunkel),2px -5px 0 var(--grau_dunkel),3px -5px 0 var(--grau_dunkel),4px -5px 0 var(--grau_dunkel),5px -5px 0 var(--grau_dunkel),
-5px -4px 0 var(--grau_dunkel),-4px -4px 0 var(--grau_dunkel),-3px -4px 0 var(--grau_dunkel),-2px -4px 0 var(--grau_dunkel),-1px -4px 0 var(--grau_dunkel),0 -4px 0 var(--grau_dunkel),1px -4px 0 var(--grau_dunkel),2px -4px 0 var(--grau_dunkel),3px -4px 0 var(--grau_dunkel),4px -4px 0 var(--grau_dunkel),5px -4px 0 var(--grau_dunkel),
-5px -3px 0 var(--grau_dunkel),-4px -3px 0 var(--grau_dunkel),-3px -3px 0 var(--grau_dunkel),-2px -3px 0 var(--grau_dunkel),-1px -3px 0 var(--grau_dunkel),0 -3px 0 var(--grau_dunkel),1px -3px 0 var(--grau_dunkel),2px -3px 0 var(--grau_dunkel),3px -3px 0 var(--grau_dunkel),4px -3px 0 var(--grau_dunkel),5px -3px 0 var(--grau_dunkel),
-5px -2px 0 var(--grau_dunkel),-4px -2px 0 var(--grau_dunkel),-3px -2px 0 var(--grau_dunkel),-2px -2px 0 var(--grau_dunkel),-1px -2px 0 var(--grau_dunkel),0 -2px 0 var(--grau_dunkel),1px -2px 0 var(--grau_dunkel),2px -2px 0 var(--grau_dunkel),3px -2px 0 var(--grau_dunkel),4px -2px 0 var(--grau_dunkel),5px -2px 0 var(--grau_dunkel),
-5px -1px 0 var(--grau_dunkel),-4px -1px 0 var(--grau_dunkel),-3px -1px 0 var(--grau_dunkel),-2px -1px 0 var(--grau_dunkel),-1px -1px 0 var(--grau_dunkel),0 -1px 0 var(--grau_dunkel),1px -1px 0 var(--grau_dunkel),2px -1px 0 var(--grau_dunkel),3px -1px 0 var(--grau_dunkel),4px -1px 0 var(--grau_dunkel),5px -1px 0 var(--grau_dunkel),
-5px 0 0 var(--grau_dunkel),-4px 0 0 var(--grau_dunkel),-3px 0 0 var(--grau_dunkel),-2px 0 0 var(--grau_dunkel),-1px 0 0 var(--grau_dunkel),1px 0 0 var(--grau_dunkel),2px 0 0 var(--grau_dunkel),3px 0 0 var(--grau_dunkel),4px 0 0 var(--grau_dunkel),5px 0 0 var(--grau_dunkel),
-5px 1px 0 var(--grau_dunkel),-4px 1px 0 var(--grau_dunkel),-3px 1px 0 var(--grau_dunkel),-2px 1px 0 var(--grau_dunkel),-1px 1px 0 var(--grau_dunkel),0 1px 0 var(--grau_dunkel),1px 1px 0 var(--grau_dunkel),2px 1px 0 var(--grau_dunkel),3px 1px 0 var(--grau_dunkel),4px 1px 0 var(--grau_dunkel),5px 1px 0 var(--grau_dunkel),
-5px 2px 0 var(--grau_dunkel),-4px 2px 0 var(--grau_dunkel),-3px 2px 0 var(--grau_dunkel),-2px 2px 0 var(--grau_dunkel),-1px 2px 0 var(--grau_dunkel),0 2px 0 var(--grau_dunkel),1px 2px 0 var(--grau_dunkel),2px 2px 0 var(--grau_dunkel),3px 2px 0 var(--grau_dunkel),4px 2px 0 var(--grau_dunkel),5px 2px 0 var(--grau_dunkel),
-5px 3px 0 var(--grau_dunkel),-4px 3px 0 var(--grau_dunkel),-3px 3px 0 var(--grau_dunkel),-2px 3px 0 var(--grau_dunkel),-1px 3px 0 var(--grau_dunkel),0 3px 0 var(--grau_dunkel),1px 3px 0 var(--grau_dunkel),2px 3px 0 var(--grau_dunkel),3px 3px 0 var(--grau_dunkel),4px 3px 0 var(--grau_dunkel),5px 3px 0 var(--grau_dunkel),
-5px 4px 0 var(--grau_dunkel),-4px 4px 0 var(--grau_dunkel),-3px 4px 0 var(--grau_dunkel),-2px 4px 0 var(--grau_dunkel),-1px 4px 0 var(--grau_dunkel),0 4px 0 var(--grau_dunkel),1px 4px 0 var(--grau_dunkel),2px 4px 0 var(--grau_dunkel),3px 4px 0 var(--grau_dunkel),4px 4px 0 var(--grau_dunkel),5px 4px 0 var(--grau_dunkel),
-5px 5px 0 var(--grau_dunkel),-4px 5px 0 var(--grau_dunkel),-3px 5px 0 var(--grau_dunkel),-2px 5px 0 var(--grau_dunkel),-1px 5px 0 var(--grau_dunkel),0 5px 0 var(--grau_dunkel),1px 5px 0 var(--grau_dunkel),2px 5px 0 var(--grau_dunkel),3px 5px 0 var(--grau_dunkel),4px 5px 0 var(--grau_dunkel),5px 5px 0 var(--grau_dunkel);
Is this generally bad, are there too many shadows or does it work anyways?
This is not forbidden by the specification, some people would tell you that this code is unreadable, so you should consider generating such construction automatically using tools like LESS or SASS if you want code looking good.
What about performance issues, I'd avoid that if animations are involved (even those achieved by combining scrolling with things like vh dimensions or poisition:fixed) but it should be perfectly fine if the text does not need to be re-rendered while the page is opened.
It may need checking in practice thou if it impacts the site in your case or not as it is hard to tell in a general case. Test especially how it behaves in Firefox as Chrome seems to optimize such things better, so FF may be the bottleneck.
I have the following css for headers:
h1 {
color: white;
text-shadow: -1px 0 #000, 0 1px #000, 1px 0 #000, 0 -1px #000;
}
This is the css I use for print:
#media print {
h1 {
-webkit-print-color-adjust:exact;
}
}
When I try to print it with Chrome, I only get a blank box.
Here is the solution:
h1 {
text-shadow: 1px 0 #000, 0 1px #000, -1px 0 #000, 0 -1px #000;
}
#media print {
-webkit-filter: drop-shadow(1px 0 0 #000) drop-shadow(0 1px 0 #000) drop-shadow(-1px 0 0 #000) drop-shadow(0 -1px 0 #000);
text-shadow: 1px 0 #000, 0 1px #000, -1px 0 #000, 0 -1px #000;
}
I am having the following css code.
box-shadow: 5px 0 0 0px #ccc, 0px 5px 0 0px #ccc, 5px 5px 0 0px #ccc, 0px 10px 0 0px #ccc, 5px 10px 0 0px #ccc, 0px 15px 0 0px #ccc, 5px 15px 0 0px #ccc;
-webkit-box-shadow: 5px 0 0 0px #ccc, 0px 5px 0 0px #ccc, 5px 5px 0 0px #ccc, 0px 10px 0 0px #ccc, 5px 10px 0 0px #ccc, 0px 15px 0 0px #ccc, 5px 15px 0 0px #ccc;
-moz-box-shadow: 5px 0 0 0px #ccc, 0px 5px 0 0px #ccc, 5px 5px 0 0px #ccc, 0px 10px 0 0px #ccc, 5px 10px 0 0px #ccc, 0px 15px 0 0px #ccc, 5px 15px 0 0px #ccc;
Is it possible to create mixin for this ?
you can Create mixins via the #mixin rule. The #mixin keyword must be followed by the mixin name and a declaration block.
The following lines define a mixin, and give it three property-value pairs:
#mixin .shadow{
box-shadow: 5px 0 0 0px #ccc, 0px 5px 0 0px #ccc, 5px 5px 0 0px #ccc, 0px 10px 0 0px #ccc, 5px 10px 0 0px #ccc, 0px 15px 0 0px #ccc, 5px 15px 0 0px #ccc;
-webkit-box-shadow: 5px 0 0 0px #ccc, 0px 5px 0 0px #ccc, 5px 5px 0 0px #ccc, 0px 10px 0 0px #ccc, 5px 10px 0 0px #ccc, 0px 15px 0 0px #ccc, 5px 15px 0 0px #ccc;
-moz-box-shadow: 5px 0 0 0px #ccc, 0px 5px 0 0px #ccc, 5px 5px 0 0px #ccc, 0px 10px 0 0px #ccc, 5px 10px 0 0px #ccc, 0px 15px 0 0px #ccc, 5px 15px 0 0px #ccc;
}
at the rounded corner, it has crisp edge, is this normal?
-moz-box-shadow: inset 0 0 0px 2px #FFF;
-webkit-box-shadow: inset 0 0 0px 2px #FFF;
box-shadow: inset 0 0 0px 2px #FFF;
I tried border method, doesn't seem to be improved..