Add stroke around text - on the outside - with css? - html

I have the following HTML and CSS:
body { background-color: gray; }
h1 {
color: white;
font-size: 2.5em;
}
<h1>WHAT CARRER SHOULD YOU HAVE ?</h1>
Which renders like this:
I want to add a stroke around it, that means a black border around these text.
I Googled and found -webkit-text-stroke, and came up with:
body { background-color: gray; }
h1 {
color: white;
font-size: 2.5em;
-webkit-text-stroke: 2px black;
}
<h1>WHAT CARRER SHOULD YOU HAVE ?</h1>
However, the effect is not what I want:
As you can see, it seems that the stroke is added inside the text, which make the text looks too thin for me.
How can I make the stroke outside the text?
Fiddle: http://jsfiddle.net/jpjbk1z7/
PS: only webkit support is needed

For a smooth outside stroke emulated by text shadow, use the following 8-axis shadow:
text-shadow:
-1px -1px 0 #000,
0 -1px 0 #000,
1px -1px 0 #000,
1px 0 0 #000,
1px 1px 0 #000,
0 1px 0 #000,
-1px 1px 0 #000,
-1px 0 0 #000;
For customisation, you can use this SASS mixin instead (although changing the size does have side effects on rendering):
#mixin stroke($color: #000, $size: 1px) {
text-shadow:
-#{$size} -#{$size} 0 $color,
0 -#{$size} 0 $color,
#{$size} -#{$size} 0 $color,
#{$size} 0 0 $color,
#{$size} #{$size} 0 $color,
0 #{$size} 0 $color,
-#{$size} #{$size} 0 $color,
-#{$size} 0 0 $color;
}
This gives a very smooth stroke, without missing parts, like on the 4 axis solution.

Firefox and Safari now support a new CSS property called paint-order which can be used to simulate an outside stroke:
h1 {
color: #00ff01;
font-size: 3em;
-webkit-text-stroke: 5px black;
}
.fix-stroke {
paint-order: stroke fill;
}
<h1>the default often is ugly</h1>
<h1 class="fix-stroke">paint-order: stroke fill πŸ˜€</h1>
Screenshot:

One option is to use text-shadow to simulate a stroke. Example:
text-shadow:
-1px -1px 0 #000,
1px -1px 0 #000,
-1px 1px 0 #000,
1px 1px 0 #000;

The -webkit-text-stroke doesn't support placing the stroke on the outside of the text
as this CSS-Tricks article explains:
The stroke drawn by text-stroke is aligned to the center of the text
shape (as is the default in Adobe Illustrator), and there is currently
no option to set the alignment to the inside or outside of the shape.
Unfortunately this makes it much less usable, as no matter what now
the stroke interferes with the shape of the letter destroying the
original type designers intent. A setting would be ideal, but if we
had to pick one, outside stroke would have been much more useful.
What about SVG?
Well it seems that it also places the stroke on the inside -
FIDDLE
However,
you might be able to simulate this effect (depending on what you need) by:
Change your font to a sans serif like verdana and
Increase the font-size of the text you are adding a stroke to.
body {
background: grey;
font-family: verdana;
}
.stroke,
.no-stroke {
color: white;
font-size: 2.5em;
}
.stroke {
-webkit-text-stroke: 2px black;
font-size: 2.7em;
}
<h1 class="stroke">WHAT CAREER SHOULD YOU HAVE?</h1>
<h1 class="no-stroke">WHAT CAREER SHOULD YOU HAVE?</h1>

Further elaborating on the other text-shadow solutions, for pixel-perfect thick round outlines the number of shadows should increase with the radius of the stroke. For example, a 10px thick outline requires 2β‹…10β‹…Ο€ β‰ˆ 63 shadows distributed in all directions. To generate this in javascript, one could something like:
const color = "#FFF" /* white outline */
const r = 10 /* width of outline in pixels */
const n = Math.ceil(2*Math.PI*r) /* number of shadows */
var str = ''
for(var i = 0;i<n;i++) /* append shadows in n evenly distributed directions */
{
const theta = 2*Math.PI*i/n
str += (r*Math.cos(theta))+"px "+(r*Math.sin(theta))+"px 0 "+color+(i==n-1?"":",")
}
document.querySelector("#myoutlinedtext").style.textShadow = str
If the thickness is static, then just run this once to generate the string and paste it into your CSS.

One way I found was to stack two elements on each other having the element that stays behind get the double stroke width. The element behind leaks the stroke to the outside of the visible element making it a true stroke outside.
Also, another option is to use :after to create this second element. The downside is that you can't programmatically change the stroke
This might not work properly on big stroke values.
body { background-color: gray; }
h1 {
color: white;
font-size: 2.5em;
font-family: verdana;
}
.stroke { -webkit-text-stroke: 2px black; }
.stack .stroke { -webkit-text-stroke: 4px black; }
h1.stroke-fs { font-size: 2.7em; }
.stack { position: relative; }
.stack > h1:not(:first-child) {
left: 0;
margin: 0;
position: absolute;
top: 0;
}
.stroke-after:after {
-webkit-text-stroke: 4px black;
content: attr(value);
position: absolute;
top: 0;
left: 0;
z-index: -1;
}
Stack
<div class="stack">
<h1 class="stroke">WHAT CARRER SHOULD YOU HAVE ?</h1>
<h1>WHAT CARRER SHOULD YOU HAVE ?</h1>
</div>
<hr />
After
<div style="position: relative">
<h1 class="stroke-after" value="WHAT CARRER SHOULD YOU HAVE ?">WHAT CARRER SHOULD YOU HAVE ?</h1>
</div>
<hr />
Native
<h1 class="stroke">WHAT CARRER SHOULD YOU HAVE ?</h1>
<hr />
Font size
<h1 class="stroke stroke-fs">WHAT CARRER SHOULD YOU HAVE ?</h1>

I have also tried text-shadow and -webkit-text-stroke, but unsuccessful in all ways.
I could only get the result by making two divs(background and foreground) i.e,
background div with -webkit-text-stroke and foreground div without -webkit-text-stroke.
<div style="position:absolute;width:1280px;height:720px;left:0px;top:0px" >
<div style="color:#CDCDCD;
font-family:sans-serif;
font-size:57px;
-webkit-text-stroke-color:black;
-webkit-text-stroke-width:0.04em;" >
<p>
Text with outine stroke outwards.
</p>
</div>
</div>
<div style="position:absolute;width:1280px;height:720px;left:0px;top:0px" >
<div style="color:#CDCDCD;
font-family:sans-serif;
font-size:57px;" >
<p>
Text with outine stroke outwards.
</p>
</div>
</div>
Bt the only problem with the html generated through is twice the size(size in memory) as the div is repeated twice.
Does anyone has some better solution?

I know this is an old question but have a look at this:
https://jsfiddle.net/1dbta9cq/
you can simply place another text on top of the stroked text with absolute positioning, it's not so tidy but it solves the problem
<div class="container">
<h1 class="stroke">Stroke</h1>
<h1 class="no-stroke">Stroke</h1>
</div>
.container{
width:300px;
}
.stroke{
position:absolute;
-webkit-text-stroke: 10px black;
}
.no-stroke{
color:white;
position:absolute
}

Here’s a SCSS mixin for simulating text outlines with multiple shadows:
#use "sass:math";
#use 'sass:list';
#mixin text-outline($offset, $color, $num-steps: 16) {
$shadows: ();
#for $i from 0 to $num-steps {
$angle: $i * 360deg / $num-steps;
$x: calc(#{math.cos($angle)} * #{$offset});
$y: calc(#{math.sin($angle)} * #{$offset});
$shadows: list.append($shadows, #{$x} #{$y} 0 #{$color}, $separator: comma);
}
text-shadow: $shadows;
}
For larger offsets, a greater number of shadows (β€œsteps”) is required for good results. Note that in general, however, more shadows will be more expensive for the browser to render.
Example:
span {
color: #FF004D;
#include utils.type-outline(
$offset: calc(0.05rem + 0.05em),
$color: #FFFFFF,
$num-steps: 32
);
}
Result:

I'm obviously late to the party but I find the easiest is probably the best way forward to a solution.
For me, I did the outside stroke effect by just making the weight of the font larger.
body { background-color: gray; }
h1 {
color: white;
font-size: 2.5em;
font-weight: bold;
-webkit-text-stroke: 2px black;
}
Is it the most correct ?
Depending on the situation then the answer is yes Or no.
Does it work in your use case ? ...for me , yes perfefctly.

Text shadow might be used to achieve this result
http://css3generator.com

Here's a SASS mixin I created to make it easy to create a text outline using the prefixed properties (-webkit, -moz) when supported, but falling back to just a color with text shadow. Note that the fallback doesn't work well with opaque fill colors, but other than that I think it's a pretty good solution, until we can get a standard method that has better browser support.
Fiddle
Mixin
#mixin text-stroke($fill-color, $stroke-color, $stroke-width) {
color: $fill-color;
text-shadow: -$stroke-width -$stroke-width 0 $stroke-color,
$stroke-width -$stroke-width 0 $stroke-color,
-$stroke-width $stroke-width 0 $stroke-color,
$stroke-width $stroke-width 0 $stroke-color;
#supports ((-webkit-text-stroke-color: $stroke-color) and (-webkit-text-fill-color: white))
or ((-moz-text-stroke-color: $stroke-color) and (-moz-text-fill-color: white)) {
color: unset;
text-shadow: unset;
-moz-text-fill-color: $fill-color;
-webkit-text-fill-color: $fill-color;
-moz-text-stroke-color: $stroke-color;
-webkit-text-stroke-color: $stroke-color;
-moz-text-stroke-width: $stroke-width;
-webkit-text-stroke-width: $stroke-width;
}
}
Example Usage
(Makes text semi-transparent black with a white outline)
.heading-outline {
#include text-stroke(rgba(#000,.5), #fff, 1px);
}

Related

How to add border color to an html character using css?

I am currently tasked to create checks inside of a pandas DataFrame table of different colors when certain data is showing.
I was able to find the following:
<p>I will display <span style="color:green">βœ”</span></p>
<p>I will display <span style="color:yellow">βœ”</span></p>
While this works per se ... It would be nice to have them bigger if possible, or at least a border around the check itself (not a box around it).
Clearly this is not the way to go since it's an ASCII character, so curious if anyone had found a similar need. Google only returns checkbox information, not a check.
Just use text-shadow to add a border around your tick-mark icon. Also, you can further improve readability by increasing the size of the icons.
Check the Code Snippet below for a practical example of using text-shadow and increasing the icon size:
p:nth-child(1) span {color:green; text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black; font-size: 50px;}
p:nth-child(2) span {color:yellow; text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;}
<p>I will display <span>βœ”</span></p>
<p>I will display <span>βœ”</span></p>
You can create a box by border and make the font-size change as done in the snippet.
Another option is you can Search google with keywords checkbox fafa icon or check icon fafa
<p>I will display <span style="border: 1px solid black;font-size: 50px;color:green">βœ”</span></p>
<p>I will display <span style="color:yellow">βœ”</span></p>
An approach with better reusability would be to create the checkmark via a CSS class on a pseudo element ::before or ::after:
.checkmark-before::before,
.checkmark-after::after {
content: "βœ”";
color: orange;
display: inline-block;
vertical-align: middle;
text-align: center;
}
.checkmark-before.x2::before,
.checkmark-after.x2::after {
font-size: 2em;
}
.checkmark-before.bordered::before,
.checkmark-after.bordered::after {
text-stroke: 1px #a00;
-webkit-text-stroke: 1px #a00;
width: 1.2em;
}
.checkmark-before.yellow::before,
.checkmark-after.yellow::after {
color: yellow;
}
<button class="checkmark-after" type="button">I will display </button>
<p class="checkmark-before x2"> Done!</p>
Google
<p class="checkmark-before x2 checkmark-after yellow bordered">Combined!</p>

How to apply CSS styles to text only

I am trying to apply a style to a like of HTML text. What I want is basically:
What I get is basically:
As you can see, the first line is indented, but not any other line. So far, I have the text inside of a <span>, which is nested inside of a <div>.
.slide-text .text {
background-color: rgba(0, 0, 0, .6);
color: #FFF;
padding: 8px 17px;
}
.slide-text .slide-title {
font-family: "Titillium Web", Arial, Helvetica, sans-serif;
font-weight: 700;
}
.slide-text .slide-content {
font-family: "Open Sans", Arial, Helvetica, sans-serif;
font-weight: 500;
}
My HTML code is:
<div class="slide-text">
<div class="slide-title"><span class="text">[TITLE]</span>
</div>
<div class="slide-content"><span class="text">[TEXT]</span>
</div>
</div>
They work great, as long as neither the title or the content are more than one line. As soon as they go over two or more lines, the span loses its inner padding. Changing the inner span to display: inline-block; gives it a block display as soon as it goes into two lines. Is there a way to get the effect I am looking for?
The CSS guru Chris Coyier has an article on CSS-Tricks listing several methods to solve this. One method is the one with box-shadow. It is already mentioned as an answer, but it needs some more love to work in modern Firefox :).
.multi-line-padded {
background: black;
color: white;
/* For the top and bottom padding */
padding: 0.5em 0;
/* Text height (1.0) + compensate for padding (0.5 * 2) */
line-height: 2;
/* For the left and right padding */
/* Vendor prefixes FIRST */
-webkit-box-shadow: 1em 0 0 black, -1em 0 0 black;
-moz-box-shadow: 1em 0 0 black, -1em 0 0 black;
box-shadow: 1em 0 0 black, -1em 0 0 black;
/* Firefox defaults to `box-decoration-break: split`, we need `clone` */
box-decoration-break: clone;
}
<p><span class="multi-line-padded">You think water moves fast? You should see ice. It moves like it has a mind. Like it knows it killed the world once and got a taste for murder.</span></p>
<p style="text-align: right;"><span class="multi-line-padded">Samuel L. Ipsum</span></p>
Try following the example in the best answer to this similar question. It essentially suggests using a box-shadow to create padding.
span {
background:#ff0;color:#000;
box-shadow:0.2em 0 0 #ff0,-0.2em 0 0 #ff0;
-moz-box-shadow:0.2em 0 0 #ff0,-0.2em 0 0 #ff0;
-webkit-box-shadow:0.2em 0 0 #ff0,-0.2em 0 0 #ff0;
}
will indent all of your text equally and add yellow padding (change the #ff0 to any HTML color value you need). However, you will have to reformat the existing code, since it will indent further than the shadow.
You can try to use
style='width: fit-content;'
For more info: https://developer.mozilla.org/en-US/docs/Web/CSS/fit-content

Why won't text shadow show on this button?

I have a button being used in an overlay. I can't see why the text shadow won't display on the "find out more" text within the button.
HTML:
Find out more<img src="img/glyphicons-224-chevron-right.png" class="chevronr" alt="some_text">
CSS:
.apply {
display: inline-block;
background: linear-gradient(#98e901 ,#5ba119);
vertical-align: middle;
box-shadow: 0 4px 0 rgba(69,69,69,.11);
text-align: center;
text-shadow: 0, 4, 4, 0;
border-radius: 4px;
padding-top: 8px;
padding-right: 42px;
padding-bottom: 8px;
padding-left: 42px;
color: #fff;
font-family: Raleway, arial, helvetica, sans-serif;
font-weight: 800;
font-size: 28px;
line-height: 50px;
margin-bottom: 30px;
letter-spacing: -1px;
}
I can't see anything in my styling that would conflict but when testing the browser isn't rendering the text shadow?
Your text-shadow property is invalid. With 4 properties, it should be of the format offset-x offset-y blur-radius color or color offset-x offset-y blur-radius. Note the use of space separators rather than the comma separators - commas are used to separate multiple shadows.
To rewrite the example provided in your question, making assumptions about the intent, it would be:
text-shadow: 0 4px 4px #000;
More information on text-shadow
Try like this: Demo 1
text-shadow: 2px 2px 2px #000;
or like this: Demo 2
text-shadow: 0px 4px rgba(0, 4, 4, 0.4);
You have used
text-shadow: 0, 4, 4, 0;
Which, unfortunately, has a few things wrong with it.
The Text-shadow property takes the values of
Value: none | [<color> || <length> <length> <length>? ,]* [<color> || <length> <length> <length>?] | inherit
This property accepts a comma-separated list of shadow effects to be
applied to the text of the element. The shadow effects are applied in
the order specified and may thus overlay each other, but they will
never overlay the text itself. ~ w3.org
This means that it takes only three values, and not four.
You are also missing units for your declaration (I have used px for this demo).
text-shadow: 3px 3px 5px red;
is a valid declaration.
where it will
place a shadow to the right and below the element's text. The shadow will have a 5px blur radius and will be red.
Text-shadow is used usuall with px formatted numbers so
text-shadow: 0, 4, 4, 0;
must be
text-shadow: 0px 4px 4px ;

How to draw a text outline in css with outwards stroke? [duplicate]

I have the following HTML and CSS:
body { background-color: gray; }
h1 {
color: white;
font-size: 2.5em;
}
<h1>WHAT CARRER SHOULD YOU HAVE ?</h1>
Which renders like this:
I want to add a stroke around it, that means a black border around these text.
I Googled and found -webkit-text-stroke, and came up with:
body { background-color: gray; }
h1 {
color: white;
font-size: 2.5em;
-webkit-text-stroke: 2px black;
}
<h1>WHAT CARRER SHOULD YOU HAVE ?</h1>
However, the effect is not what I want:
As you can see, it seems that the stroke is added inside the text, which make the text looks too thin for me.
How can I make the stroke outside the text?
Fiddle: http://jsfiddle.net/jpjbk1z7/
PS: only webkit support is needed
For a smooth outside stroke emulated by text shadow, use the following 8-axis shadow:
text-shadow:
-1px -1px 0 #000,
0 -1px 0 #000,
1px -1px 0 #000,
1px 0 0 #000,
1px 1px 0 #000,
0 1px 0 #000,
-1px 1px 0 #000,
-1px 0 0 #000;
For customisation, you can use this SASS mixin instead (although changing the size does have side effects on rendering):
#mixin stroke($color: #000, $size: 1px) {
text-shadow:
-#{$size} -#{$size} 0 $color,
0 -#{$size} 0 $color,
#{$size} -#{$size} 0 $color,
#{$size} 0 0 $color,
#{$size} #{$size} 0 $color,
0 #{$size} 0 $color,
-#{$size} #{$size} 0 $color,
-#{$size} 0 0 $color;
}
This gives a very smooth stroke, without missing parts, like on the 4 axis solution.
Firefox and Safari now support a new CSS property called paint-order which can be used to simulate an outside stroke:
h1 {
color: #00ff01;
font-size: 3em;
-webkit-text-stroke: 5px black;
}
.fix-stroke {
paint-order: stroke fill;
}
<h1>the default often is ugly</h1>
<h1 class="fix-stroke">paint-order: stroke fill πŸ˜€</h1>
Screenshot:
One option is to use text-shadow to simulate a stroke. Example:
text-shadow:
-1px -1px 0 #000,
1px -1px 0 #000,
-1px 1px 0 #000,
1px 1px 0 #000;
The -webkit-text-stroke doesn't support placing the stroke on the outside of the text
as this CSS-Tricks article explains:
The stroke drawn by text-stroke is aligned to the center of the text
shape (as is the default in Adobe Illustrator), and there is currently
no option to set the alignment to the inside or outside of the shape.
Unfortunately this makes it much less usable, as no matter what now
the stroke interferes with the shape of the letter destroying the
original type designers intent. A setting would be ideal, but if we
had to pick one, outside stroke would have been much more useful.
What about SVG?
Well it seems that it also places the stroke on the inside -
FIDDLE
However,
you might be able to simulate this effect (depending on what you need) by:
Change your font to a sans serif like verdana and
Increase the font-size of the text you are adding a stroke to.
body {
background: grey;
font-family: verdana;
}
.stroke,
.no-stroke {
color: white;
font-size: 2.5em;
}
.stroke {
-webkit-text-stroke: 2px black;
font-size: 2.7em;
}
<h1 class="stroke">WHAT CAREER SHOULD YOU HAVE?</h1>
<h1 class="no-stroke">WHAT CAREER SHOULD YOU HAVE?</h1>
Further elaborating on the other text-shadow solutions, for pixel-perfect thick round outlines the number of shadows should increase with the radius of the stroke. For example, a 10px thick outline requires 2β‹…10β‹…Ο€ β‰ˆ 63 shadows distributed in all directions. To generate this in javascript, one could something like:
const color = "#FFF" /* white outline */
const r = 10 /* width of outline in pixels */
const n = Math.ceil(2*Math.PI*r) /* number of shadows */
var str = ''
for(var i = 0;i<n;i++) /* append shadows in n evenly distributed directions */
{
const theta = 2*Math.PI*i/n
str += (r*Math.cos(theta))+"px "+(r*Math.sin(theta))+"px 0 "+color+(i==n-1?"":",")
}
document.querySelector("#myoutlinedtext").style.textShadow = str
If the thickness is static, then just run this once to generate the string and paste it into your CSS.
One way I found was to stack two elements on each other having the element that stays behind get the double stroke width. The element behind leaks the stroke to the outside of the visible element making it a true stroke outside.
Also, another option is to use :after to create this second element. The downside is that you can't programmatically change the stroke
This might not work properly on big stroke values.
body { background-color: gray; }
h1 {
color: white;
font-size: 2.5em;
font-family: verdana;
}
.stroke { -webkit-text-stroke: 2px black; }
.stack .stroke { -webkit-text-stroke: 4px black; }
h1.stroke-fs { font-size: 2.7em; }
.stack { position: relative; }
.stack > h1:not(:first-child) {
left: 0;
margin: 0;
position: absolute;
top: 0;
}
.stroke-after:after {
-webkit-text-stroke: 4px black;
content: attr(value);
position: absolute;
top: 0;
left: 0;
z-index: -1;
}
Stack
<div class="stack">
<h1 class="stroke">WHAT CARRER SHOULD YOU HAVE ?</h1>
<h1>WHAT CARRER SHOULD YOU HAVE ?</h1>
</div>
<hr />
After
<div style="position: relative">
<h1 class="stroke-after" value="WHAT CARRER SHOULD YOU HAVE ?">WHAT CARRER SHOULD YOU HAVE ?</h1>
</div>
<hr />
Native
<h1 class="stroke">WHAT CARRER SHOULD YOU HAVE ?</h1>
<hr />
Font size
<h1 class="stroke stroke-fs">WHAT CARRER SHOULD YOU HAVE ?</h1>
I have also tried text-shadow and -webkit-text-stroke, but unsuccessful in all ways.
I could only get the result by making two divs(background and foreground) i.e,
background div with -webkit-text-stroke and foreground div without -webkit-text-stroke.
<div style="position:absolute;width:1280px;height:720px;left:0px;top:0px" >
<div style="color:#CDCDCD;
font-family:sans-serif;
font-size:57px;
-webkit-text-stroke-color:black;
-webkit-text-stroke-width:0.04em;" >
<p>
Text with outine stroke outwards.
</p>
</div>
</div>
<div style="position:absolute;width:1280px;height:720px;left:0px;top:0px" >
<div style="color:#CDCDCD;
font-family:sans-serif;
font-size:57px;" >
<p>
Text with outine stroke outwards.
</p>
</div>
</div>
Bt the only problem with the html generated through is twice the size(size in memory) as the div is repeated twice.
Does anyone has some better solution?
I know this is an old question but have a look at this:
https://jsfiddle.net/1dbta9cq/
you can simply place another text on top of the stroked text with absolute positioning, it's not so tidy but it solves the problem
<div class="container">
<h1 class="stroke">Stroke</h1>
<h1 class="no-stroke">Stroke</h1>
</div>
.container{
width:300px;
}
.stroke{
position:absolute;
-webkit-text-stroke: 10px black;
}
.no-stroke{
color:white;
position:absolute
}
Here’s a SCSS mixin for simulating text outlines with multiple shadows:
#use "sass:math";
#use 'sass:list';
#mixin text-outline($offset, $color, $num-steps: 16) {
$shadows: ();
#for $i from 0 to $num-steps {
$angle: $i * 360deg / $num-steps;
$x: calc(#{math.cos($angle)} * #{$offset});
$y: calc(#{math.sin($angle)} * #{$offset});
$shadows: list.append($shadows, #{$x} #{$y} 0 #{$color}, $separator: comma);
}
text-shadow: $shadows;
}
For larger offsets, a greater number of shadows (β€œsteps”) is required for good results. Note that in general, however, more shadows will be more expensive for the browser to render.
Example:
span {
color: #FF004D;
#include utils.type-outline(
$offset: calc(0.05rem + 0.05em),
$color: #FFFFFF,
$num-steps: 32
);
}
Result:
I'm obviously late to the party but I find the easiest is probably the best way forward to a solution.
For me, I did the outside stroke effect by just making the weight of the font larger.
body { background-color: gray; }
h1 {
color: white;
font-size: 2.5em;
font-weight: bold;
-webkit-text-stroke: 2px black;
}
Is it the most correct ?
Depending on the situation then the answer is yes Or no.
Does it work in your use case ? ...for me , yes perfefctly.
Text shadow might be used to achieve this result
http://css3generator.com
Here's a SASS mixin I created to make it easy to create a text outline using the prefixed properties (-webkit, -moz) when supported, but falling back to just a color with text shadow. Note that the fallback doesn't work well with opaque fill colors, but other than that I think it's a pretty good solution, until we can get a standard method that has better browser support.
Fiddle
Mixin
#mixin text-stroke($fill-color, $stroke-color, $stroke-width) {
color: $fill-color;
text-shadow: -$stroke-width -$stroke-width 0 $stroke-color,
$stroke-width -$stroke-width 0 $stroke-color,
-$stroke-width $stroke-width 0 $stroke-color,
$stroke-width $stroke-width 0 $stroke-color;
#supports ((-webkit-text-stroke-color: $stroke-color) and (-webkit-text-fill-color: white))
or ((-moz-text-stroke-color: $stroke-color) and (-moz-text-fill-color: white)) {
color: unset;
text-shadow: unset;
-moz-text-fill-color: $fill-color;
-webkit-text-fill-color: $fill-color;
-moz-text-stroke-color: $stroke-color;
-webkit-text-stroke-color: $stroke-color;
-moz-text-stroke-width: $stroke-width;
-webkit-text-stroke-width: $stroke-width;
}
}
Example Usage
(Makes text semi-transparent black with a white outline)
.heading-outline {
#include text-stroke(rgba(#000,.5), #fff, 1px);
}

Edit line thickness of CSS 'underline' attribute

Since you can underline any text in CSS like so:
h4 {
text-decoration: underline;
}
How can you also then edit the 'line' that is drawn, the color you get on the line is easily specified as color: red but how does one edit the height of the line, i.e. the thickness?
Here is one way of achieving this :
HTML :
<h4>This is a heading</h4>
<h4><u>This is another heading</u></h4>
​CSS :
u {
text-decoration: none;
border-bottom: 10px solid black;
}​
Here is an example: http://jsfiddle.net/AQ9rL/
Recently I had to deal with FF which underlines were too thick and too far from the text in FF, and found a better way to deal with it using a pair of box-shadows:
.custom-underline{
box-shadow: inset 0 0px 0 white, inset 0 -1px 0 black
}
First shadow is put on top of the second one and that's how you can control the second one by varying the 'px' value of both.
Plus: various colors, thickness and underline position
Minus: can not use on non-solid backgrounds
Here I made couple of examples:
http://jsfiddle.net/xsL6rktx/
There is text-decoration-thickness, currently part of CSS Text Decoration Module Level 4. It's at "Editor's Draft" stage - so it's a work in progress and subject to change. As of October 2022, it has about 93% coverage so it's pretty safe to use.
The text-decoration-thickness CSS property sets the thickness, or
width, of the decoration line that is used on text in an element, such
as a line-through, underline, or overline.
a {
text-decoration-thickness: 2px;
}
Codepen: https://codepen.io/mrotaru/pen/yLyLOgr (Firefox only)
There's also text-decoration-color, which is part of CSS Text Decoration Module Level 3. This is more mature (Candidate Recommendation) and is supported in most major browsers (exceptions are Edge and IE). Of course it can't be used to alter the thickness of the line, but can be used to achieve a more "muted" underline (also shown in the codepen).
Very easy ... outside "span" element with small font and underline, and inside "font" element with bigger font size.
<span style="font-size:1em;text-decoration:underline;">
<span style="font-size:1.5em;">
Text with big font size and thin underline
</span>
</span>
Another way to do this is using ":after" (pseudo-element) on the element you want to underline.
h2 {
position:relative;
display:inline-block;
font-weight:700;
font-family:arial,sans-serif;
text-transform:uppercase;
font-size:3em;
}
h2:after {
content:"";
position:absolute;
left:0;
bottom:0;
right:0;
margin:auto;
background:#000;
height:1px;
}
I will do something simple like :
.thickness-underline {
display: inline-block;
text-decoration: none;
border-bottom: 1px solid black;
margin-bottom: -1px;
}
You can use line-height or padding-bottom to set possition between them
You can use display: inline in some case
Demo : http://jsfiddle.net/5580pqe8/
The background-image can also be used to create an underline. This method handles line breaks.
It has to be shifted down via background-position and repeated horizontally. The line width can be adjusted to some degree using background-size (the background is limited to the content box of the element).
.underline
{
--color: green;
font-size: 40px;
background-image: linear-gradient(var(--color) 0%, var(--color) 100%);
background-repeat: repeat-x;
background-position: 0 1.05em;
background-size: 2px 5px;
}
<span class="underline">
Underlined<br/>
Text
</span>
a {
text-decoration: none;
position: relative;
}
a.underline {
text-decoration: underline;
}
a.shadow {
box-shadow: inset 0 -4px 0 white, inset 0 -4.5px 0 blue;
}
<h1>Default: some text alpha gamma<br>the quick brown fox</h1>
<p>Working:</p>
<h1>Using Shadow: some text alpha gamma<br>the quick brown fox<br>even works with<br>multiple lines</h1>
<br>
Final Solution:
http://codepen.io/vikrant-icd/pen/gwNqoM
a.shadow {
box-shadow: inset 0 -4px 0 white, inset 0 -4.5px 0 blue;
}
Thanks to the magic of new css options this is now possible natively:
a {
text-decoration: underline;
text-decoration-thickness: 5px;
text-decoration-skip-ink: auto;
text-underline-offset: 3px;
}
As of yet support is relatively poor. But it'll land in other browsers than ff eventually.
My Solution :
https://codepen.io/SOLESHOE/pen/QqJXYj
{
display: inline-block;
border-bottom: 1px solid;
padding-bottom: 0;
line-height: 70%;
}
You can adjust underline position with line-height value, underline thickness and style with border-bottom.
Beware to disable default underline behavior if you want to underline an href.
Now, as can be seen in the picture below, the property is fully supported in most browsers (according to Mozilla).
So, you can use the following attributes:
.thin {
text-decoration-line: underline;
text-decoration-style: solid;
text-decoration-color: red;
text-decoration-thickness: 1px;
}
.thick {
text-decoration-line: underline;
text-decoration-style: solid;
text-decoration-color: red;
text-decoration-thickness: 5px;
}
.shorthand {
text-decoration: underline solid red 5px;
}
(example code from Mozilla).