Changing Underline color - html

I have this code here:
echo "<u><font color='red'><br>$username</font></u>";
Firstly, as you can see, it's underlined (< u >). Second, all text is all red. Well is there anyway to leave the text ($username) red but the underline black?

There's now a new css3 property for this: text-decoration-color
So you can now have text in one color and a text-decoration underline - in a different color... without needing an extra 'wrap' element
p {
text-decoration: underline;
-webkit-text-decoration-color: red; /* safari still uses vendor prefix */
text-decoration-color: red;
}
<p>black text with red underline in one element - no wrapper elements here!</p>
Codepen
NB:
1) Browser Support is limited at the moment to Firefox and Chrome (fully supported as of V57) and Safari
2) You could also use the text-decoration shorthand property which looks like this:
<text-decoration-line> || <text-decoration-style> || <text-decoration-color>
...so using the text-decoration shorthand - the example above would simply be:
p {
text-decoration: underline red;
}
p {
text-decoration: underline red;
}
<p>black text with red underline in one element - no wrapper elements here!</p>

Update from author:
This answer is outdated since text-decoration-color is now supported by most modern browsers.
:pseudo + em
In order to accurately replicate the size, stroke width, and positioning of the native text-decoration:underline without introducing extra HTML markup, you should use a pseudo-element with em units. This allows for accurate scaling of the element and native behavior without additional markup.
CSS
a {
text-decoration: none;
display: inline-table;
}
a:after {
content: "";
border-bottom: 0.1em solid #f00;
display: table-caption;
caption-side: bottom;
position: relative;
margin-top:-0.15em;
}
By using display:table-caption and caption-side on the pseudo-element and display inline-table, we can force the browser to vertically-align both line and link accurately, even when scaled.
In this instance, we use inline-table instead of inline-block to force the pseudo to display without the need to specify height or negative values.
Examples
JSFIDDLE: https://jsfiddle.net/pohuski/8yfpjuod/8/
CODEPEN: http://codepen.io/pohuski/pen/vEzxPj | (example with scaling)
Successfully Tested On:
Internet Explorer: 8, 9, 10, 11
Firefox: 41, 40, 39, 38, 37, 36
Chrome: 45, 44, 43, 42
Safari: 8, 7, 6.2
Mobile Safari: 9.0, 8.0
Android Browser: 4.4, 2.3
Dolphin Mobile: 8, 11.4

No. The best you can do is to use a border-bottom with a different color, but that isn't really underlining.

In practice, it is possible, if you use span element instead of font:
<style>
u { color: black; }
.red { color: red }
</style>
<u><span class='red'><br>$username</span></u>
See jsfiddle. Appears to work on Chrome, Safari, Firefox, IE, Opera (tested on Win 7 with newest versions).
The code in the question should work, too, but it does not work for some reason on WebKit browsers (Chrome, Safari).
By the CSS spec: “The color(s) required for the text decoration must be derived from the 'color' property value of the element on which 'text-decoration' is set. The color of decorations must remain the same even if descendant elements have different 'color' values.”

The easiest way I've tackled this is with CSS:
<style>
.redUnderline {
color: #ff0000;
border-bottom: 1px solid #000000;
}
</style>
<span class="redUnderline">$username</span>
Also, for an actual underline, if your item is a link, this works:
<style>
a.blackUnderline {
color: #000000;
text-decoration: underline;
}
.red {
color: #ff0000;
}
</style>
<span class="red">$username</span>

You can also use the box-shadow property to simulate an underline.
Here is a fiddle.
The idea is to use two layered box shadows to position the line in the same place as an underline.
a.underline {
text-decoration: none;
box-shadow: inset 0 -4px 0 0 rgba(255, 255, 255, 1), inset 0 -5px 0 0 rgba(255, 0, 0, 1);
}

Another way that the one described by danield is to have a child container width display inline, and the tipography color you want. The parent element width the text-decoration, and the color of underline you want. Like this:
div{text-decoration:underline;color:#ff0000;display:inline-block;width:50px}
div span{color:#000;display:inline}
<div>
<span>Hover me, i can have many lines</span>
</div>

A pseudo element works best.
a, a:hover {
position: relative;
text-decoration: none;
}
a:after {
content: '';
display: block;
position: absolute;
height: 0;
top:90%;
left: 0;
right: 0;
border-bottom: solid 1px red;
}
See jsfiddle.
You don't need any extra elements, you can position it as close or far as you want from the text (border-bottom is kinda far for my liking), there aren't any extra colors that show up if your link is over a different colored background (like with the box-shadow trick), and it works in all browsers (text-decoration-color only supports Firefox as of yet).
Possible downside: The link can't be position:static, but that's probably not a problem the vast majority of the time. Just set it to relative and all is good.

You can use this CSS to "simulate" an underline:
text-decoration: none;
border-bottom: 1px solid #000;

You can wrap your <span> with a <a> and use this little JQuery plugin to color the underline.
You can modify the color by passing a parameter to the plugin.
(function ($) {
$.fn.useful = function (params) {
var aCSS = {
'color' : '#d43',
'text-decoration' : 'underline'
};
$.extend(aCSS, params);
this.wrap('<a></a>');
var element = this.closest('a');
element.css(aCSS);
return element;
};
})(jQuery);
Then you call by writing this :
$("span.name").useful({color:'red'});
$(function () {
var spanCSS = {
'color' : '#000',
'text-decoration': 'none'
};
$.fn.useful = function (params) {
var aCSS = {
'color' : '#d43',
'text-decoration' : 'underline'
};
$.extend(aCSS, params);
this.wrap('<a></a>');
this.closest('a').css(aCSS);
};
// Use example:
$("span.name").css(spanCSS).useful({color:'red'});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="container">
<div class="user important">
<span class="name">Bob</span>
-
<span class="location">Bali</span>
</div>
<div class="user">
<span class="name">Dude</span>
-
<span class="location">Los Angeles</span>
</div>
<div class="user">
<span class="name">Gérard</span>
-
<span class="location">Paris</span>
</div>
</section>

here we can create underline with color in text
<u style="text-decoration-color: red;">The color of the lines should now be red!</u>
or
The color of the lines should now be red!
<h1 style=" text-decoration:underline; text-decoration-color: red;">The color of the lines should now be red!</u>

I think the easiest way to do this is in your css, use:
text-decoration-color: red;
This will change the color of the underline without changing the color of your text. Good luck!

Best way I came across doing is like this:
HTML5:
<p>Initial Colors <a id="new-color">Different Colors</a></p>
CSS3:
p {
color: #000000;
text-decoration-line: underline;
text-decoration-color: #a11015;
}
p #new-color{
color: #a11015;
text-decoration-line: underline;
text-decoration-color: #000000;
}

Problem with border-bottom is the extra distance between the text and the line. Problem with text-decoration-color is lack of browser support. Therefore my solution is the use of a background-image with a line. This supports any markup, color(s) and style of the line. top (12px in my example) is dependent on line-height of your text.
u {
text-decoration: none;
background: transparent url(blackline.png) repeat-x 0px 12px;
}

Related

Take control over text-decoration: underline without using border property

Ingredients: just an input tag of type text.
Question: How can I control the size, color, position of text-decoration: underline styling the input tag?
I saw that text-decoration-color is available but only in the moz web interpreter and I want something cross-browser like.
[EDIT] I accept JS, CSS, HTML, doesn't matter, but not any workaround: my question is specific, I want control over this specific attribute.
the easiest way is to remove the text decoration as :
text-decoration: none;
in your css file and use
border-bottom: 1px solid #FF0000;
instead of that, you can now change the color and size :)
what if you use
text-decoration: none;
border-bottom: 10px solid black;
so you have control over the border properties
You can simply use the CSS3 text-decoration-color property, it works with Chrome too.
Demo:
a {
text-decoration: underline;
-webkit-text-decoration-color: green;
text-decoration-color: green;
}
This is just a link
Note:
Notice the use of -webkit-text-decoration-color to make it compatible with Chrome.
You can check text-decoration-color Cross browser cpompatibility for further details about its browser support.
editing the text-decoration like, as you told, the color is possible. but font-size, position and more is hard. What you could do is instead of using the text-decoration is adding on your lets say <p> tag a border-bottom. Of this border you are able to change size and color. If you want to change the position or other things you should think of maybe adding a <div> with a <style> to edit all sort of things.
Use
text-decoration-color: #E18728;
text-decoration: underline dotted red;
Ref links:
https://css-tricks.com/almanac/properties/t/text-decoration-skip/
https://css-tricks.com/almanac/properties/t/text-decoration-style/
https://css-tricks.com/almanac/properties/t/text-decoration-line/
https://css-tricks.com/almanac/properties/t/text-decoration-color/
Here is another advanced way to get this done
HTML
<div class="underline-text"> This is Underlined text </div>
CSS
.underline-text{
position:relative;
padding-bottom:5px;
display:inline-block;
}
.underline-text:after{
position: absoulte;
bottom: 0px;
content: " ";
width:100%;
background:red;
height:2px;
left:0px;
}
Here is codepen.
https://codepen.io/sajiddesigner/pen/QvgeGO
You can do experiments there.
I Hope This example Help You :
a {
outline: none;
text-decoration: none;
border-bottom: 2px solid orange;
padding-bottom: 5px;
}
Hello World!
text-decoration is shorthand property for text-decoration-color, text-decoration-style and text-decoration-line.
syntax{
text-decoration : text-decoration-line text-decoration-style text-decoration-
color;
}
a{
text-decoration : underline red double;
}
So you can simple use text-decoration or individually define each property.
text-decoration-line - Is a style assigned to element and that can be underline, line-through, overline and such.
text-decoration-color - Is a color assigned to element.
text-decoration-style - Behaves much like
border-style, so you could use double, solid, dotted and such property values.
You can read more on this site.
And for compatibility with browser check on caniuse as some properties are partially supported.
a{
text-decoration-line:underline;
text-decoration-color:red;
text-decoration-style:double;
}
Text Decoration
Color and style of text-decoration
Text decoration is limited in CSS. The specs in CSS3 says you have these settings but actually only firefox supports them:
text-decoration-line
text-decoration-style
text-decoration-color
Size of text-decoration
You have a little bit control of the size of the line with font-style attribute. So as you can see it is relative to the font-size.
p {
text-decoration: underline;
}
#test1 {
font-size: 14px;
}
#test2 {
font-size: 40px;
}
<p id="test1">test 1</p>
<p id="test2">test 2</p>
Position of text-decoration
There is the attribute text-underline-position. But as the other attributes it is mostly not supported by the major Browsers. So sadly it is not possible to control the position of the text decoration.

Using * with :not in css

Im working on a html linter using css.
Reference: https://bitsofco.de/linting-html-using-css/
I like the idea of highlighting elements that have inline styles like so:
*[style] {
color: red !important;
border: 5px solid red !important;
}
However, I do have certain instances where I have to use inline styles, ie canvas elements.
How do I use the :not selector with the *?
Can I have multiple :nots, ie :not(canvas):not(form), etc
What you have works and excludes the canvas. And yes, you can chain multiple :not()s like that.
* {
border: 1px solid black;
}
*[style]:not(canvas):not(form) {
color: red !important;
border: 5px solid red !important;
}
<canvas style="foo">canvas</canvas>
<form style="foo">form</form>
<div style="foo">div</div>
the :not() rule matches anything not matching the subrule. The subrule is a valid css selector. writing [canvas] will match any element with a canvas attribute, so this isn't what you want.
The correct usage is:
*[style]:not(canvas):not(form)

Webkit / Blink browsers not respecting display styling on legend tag

Legend Tags in Webkit browsers seem not to accept any styling besides block and none for the CSS display property:
Here's the HTML
<legend>I should display as an inline block</legend>
<div>I should be on the same line</div>
And here's the CSS (put anything other than block or none as display style)
legend {
display: inline-block;
background: black;
color: white;
-webkit-margin-top-collapse: separate;
}
div {
display: inline-block;
background: blue;
color: white;
}
As you can see in this fiddle, the legend tag will always be styled as a block.
You will also see that despite I applied -webkit-margin-top-collapse: separate, which lets one apply margins to legend tags in webkit despite a quirk, the problem still persists.
I reckon this is a bug although it does not appear in the list of bugs when searching for legend, but does anyone know how to circumvent it?
I was able to get the legend and div to go side by side with the following CSS.
legend {
background: black;
color: white;
float:left;
}
div {
display: inline;
background: blue;
color: white;
}
http://jsfiddle.net/vhNbd/4/

CSS: Line-through with a color different from the text color? [duplicate]

This question already has answers here:
CSS strikethrough different color from text?
(15 answers)
Closed 8 years ago.
I’d like to have gray text with a red strike-through, but this style doesn’t work:
color: #a0a0a0;
text-decoration: line-through 3px red;
What am I doing wrong?
You can simulate the desired effect with two nested elements, e.g.:
span.inner {
color: green;
}
span.outer {
color: red;
text-decoration: line-through;
}
<span class="outer">
<span class="inner">foo bar</span>
</span>
jsfiddle
With no extra DOM (but may not work for some layouts for obvious reasons):
<style type="text/css">
.strikethrough {
position: relative;
}
.strikethrough:before {
border-bottom: 1px solid red;
position: absolute;
content: "";
width: 100%;
height: 50%;
}
</style>
<span class="strikethrough">Foo Bar</span>
A jsfiddle example here.
There is another way of looking at the meaning of the CSS2 specification; and that is the outer text-decoration will set the color of the "line-through" and text, but an inner color declaration (in a 'span' tag) can be used to reset the text color.
<p style="text-decoration:line-through;color:red">
<span style="color:#000">
The "line-through" and "color" are declared in 'p', and the inner 'span'
declares the correct color for the text.
</span>
</p>
It's not possible to make a line-through with a different color. It will be in the color you define with property color.
see http://www.w3.org/TR/CSS2/text.html#lining-striking-props
EDIT:
what came into my mind is to use a background-image with a 1px * 1px color dot in the color you like.
CSS:
.fakeLineThrough {
background-image: url(lineThroughDot.gif);
background-repeat: repeat-x;
background-position: center left;
}
HTML:
<span class="fakeLineThrough">the text</span>
The CSS2 specs says
The color(s) required for the text decoration must be derived from the 'color' property value of the element on which 'text-decoration' is set. The color of decorations must remain the same even if descendant elements have different 'color' values
I guess that means that you can't do it that way.
A workaround could be using some kind of border, and lifting it?

Remove outline from select box in FF

Is it possible to remove the dotted line surrounding a selected item in a select element?
I have tried to add the outline property in CSS but it did not work, at least not in FF.
<style>
select { outline:none; }
</style>
Update
Before you go ahead and remove the outline, please read this.
http://www.outlinenone.com/
Well, Duopixel’s answer is plain perfect. If we go a step further we can make it bulletproof.
select:-moz-focusring {
color: transparent;
text-shadow: 0 0 0 #000;
}
Only valid for Firefox and the ugly dotted outline around the selected option is gone.
I found a solution, but it is mother of all hacks, hopefully it will serve as a starting point for other more robust solutions. The downside (too big in my opinion) is that any browser that doesn't support text-shadow but supports rgba (IE 9) won't render the text unless you use a library such as Modernizr (not tested, just a theory).
Firefox uses the text color to determine the color of the dotted border. So say if you do...
select {
color: rgba(0,0,0,0);
}
Firefox will render the dotted border transparent. But of course your text will be transparent too! So we must somehow display the text. text-shadow comes to the rescue:
select {
color: rgba(0,0,0,0);
text-shadow: 0 0 0 #000;
}
We put a text shadow with no offset and no blur, so that replaces the text. Of course older browser don't understand anything of this, so we must provide a fallback for the color:
select {
color: #000;
color: rgba(0,0,0,0);
text-shadow: 0 0 0 #000;
}
This is when IE9 comes into play: it supports rgba but not text-shadow, so you will get an apparently empty select box. Get your version of Modernizr with text-shadow detection and do...
.no-textshadow select {
color: #000;
}
Enjoy.
Here is a collaboration of solutions to fix styling issues with Firefox select boxes. Use this CSS selector as part of your usual CSS reset.
Class removes outline as per question but also removes any background image (as I usually use a custom dropdown arrow and Firefoxes system dropdown arrow can't currently be removed). If using background image for anything other than dropdown image, simply remove line background-image: none !important;
#-moz-document url-prefix() {
select, select:-moz-focusring, select::-moz-focus-inner {
color: transparent !important;
text-shadow: 0 0 0 #000 !important;
background-image: none !important;
border:0;
}
}
This will target all firefox versions
#-moz-document url-prefix() {
select {
color: transparent !important;
text-shadow: 0 0 0 #000 !important;
}
}
You might want to remove the !important, if you plan to have the outline appear on other pages across your site that use the same stylesheet.
In general, form controls are impossible to style to that degree of accuracy. There's no browser I'm aware of that supports a sensible range of properties in all controls. That's the reason why there're a gazillion JavaScript libraries that "fake" form controls with images and other HTML elements and emulate their original functionality with code:
http://ryanfait.com/resources/custom-checkboxes-and-radio-buttons/
...
<select onchange="this.blur();">
If you use this the border stays until you select an item from the list.
Try one of these:
a:active {
outline: none;
-moz-outline: none;
}
a {
-moz-user-focus: none;
}
Reference
Here comes the solution
:focus {outline:none;}
::-moz-focus-inner {border:0;}
Remove outline/dotted border from Firefox All Selectable Tags.
Put this line of code in your style sheet:
*:focus{outline:none !important;}
Step 1) Add HTML:
Add the select options of your choice and add the attribute: contenteditable="true"
Step 2) Add CSS:
Use the [attribute] selector to select all elements that are contenteditable, and remove the border with the outline property:
[contenteditable] {
outline: 0px solid transparent;
}
select {
border: none;
}
<select contenteditable="true">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
select:focus {
box-shadow: none;
}
To remove the outline of the select box when selected/focused.
https://ssiddique.info/projects/jqueryplugins/demo/index.php?demo=CheckboxStylized check this out
Download the plugin from here
Add border-style: none to your select in CSS.
select {
border-style: none; }
input[type='range']::-moz-focus-outer {
border: 0;
outline: none !important;
}
working 100%
This will remove focus from the select element and the outline:
$("select").click(function(){
$(this).blur();
});
Though this isn't without its shortcomings on other browsers. You'll want to check the browser the user is using:
if (FIREFOX) {
//implement the code
}