How make text-decoration: underline with 2px padding? - html

I like an obedient frotend developer must create underline with 2px padding instead of 1px by default. Is exist simple solution for this?
PS Yeahh guys, I know about div with black backgrond color and 1px * Npx and position: relative, but It's so slowly...

You could wrap the text in a span and give it a border-bottom with padding-bottom:2px;.
Like so
span{
display:inline-block;
border-bottom:1px solid black;
padding-bottom:2px;
}
Example: http://jsfiddle.net/uSMGU/

For cross-browsing it is better to use text-underline-offset over the text-underline-position, because text-underline-position isn't supported by iOS Safari
So use this answer: https://stackoverflow.com/a/63607426/1894907
#line{
text-decoration-line: underline;
text-underline-offset: 2px;
}

A great way to do this without adding and extra spans and having more control is using the :after selector.
Useful especially for navigation menus:
.active a:after {
content: '';
height: 1px;
background: black;
display:block;
}
If you want more or less space between the text and the underline, add a margin-top.
If you want a thicker underline, add more height:
.active a:after {
content: '';
height: 2px;
background: black;
display:block;
margin-top: 2px;
}

Simply use:
text-decoration: underline;
text-underline-position: under;

#line{
text-decoration-line: underline;
text-underline-offset: 1px;
}
<div id="line">
Text with line
</div>
just use
{
text-decoration-line: underline;
text-underline-offset: 2px;
}

how about using border-bottom:1px; padding:what-you-likepx

.my-underlined-text {
text-decoration: underline;
text-underline-offset: 8px;
}

This is my solution...
HTML
<p>hola dasf hola dasdsaddasds dsadasdd<span></span></p>
CSS
p {
color: red;
text-decoration: none;
position: absolute;
}
a:hover {
color: blue;
}
p span {
display:block;
border-bottom:3px solid black;
width: 50%;
padding-bottom: 4px;
}

I used #jake's solution, but it gave me trouble with the horizontal alignment.
My nav links are flex row, center aligned and his solution caused the element to shift upwards in order to stay horizontally center-aligned.
I fixed it by doing this:
a.nav_link-active {
color: $e1-red;
margin-top: 3.7rem;
}
a.nav_link-active:visited {
color: $e1-red;
}
a.nav_link-active:after {
content: '';
margin-top: 3.3rem; // margin and height should
height: 0.4rem; // add up to active link margin
background: $e1-red;
display: block;
}
This will maintain the horizontal alignment for you.

You can do this with a bit of a hack using ::after elements, and positioning them manually. This does mean that you have to maintain the content attribute of the after element but you could make this easier by using a data attribute in the html tag and referring to it. See the example below -
span:after {
font-size: 1rem;
position: absolute;
content: 'Hello there..';
width: 100%;
height: 100%;
white-space: pre;
text-decoration-skip-ink: none;
text-decoration-line: underline;
left: 0px;
top: 10px;
color: white;
z-index: -1;
text-decoration: underline;
text-decoration-style: wavy;
text-decoration-color: black;
}
span {
position: relative;
}
<span>Hello there</span>

Try this:
.yourElement{
border-bottom: 1px solid #000;
line-height: 2;
}

Related

How to don't apply hover effects to content added in :before? css

I'm adding '>' in :before property to links, but when apply the :hover effect also take the styles text-decoration: underline; so, I need that don't apply the effect in the content added in :before, I tried resolve this whit this options
a:before {
content: '> ';
position: relative;
display: inline-block;
margin-right: 5px;
pointer-events: none;
}
a:before:hover {
content: '>';
pointer-events: none;
}
a:before:hover {
text-decoration:none;
}
but none works.
this is how links behave with hover
here is an example of my code, the structure, <a> inside <p> is because the content is from a text editor
.super {
color: rgb(40, 40, 40);
background-color: rgb(239, 239, 239);
margin-bottom: 35px;
padding: 10px;
}
p {
position: relative;
word-break: break-word;
font-size: 17px;
padding: 0px;
}
a {
left: 0px;
display: flex;
position: relative;
left: 0;
display: flex;
text-decoration: none;
}
a::before {
content: '> ';
position: relative;
display: inline-block;
margin-right: 5px;
}
a:hover{
text-decoration:underline;
}
<div class='super'>
<p>
<a href='https://google.com' target='_blanck'>Example to link</a>
</p>
</div>
There are 2 things in your code that stop if from working:
The main reason is the use of display:flex
you also have the incorrect order for :hover:before but that doesn't matter because it won't work with display:flex anyway.
With the default display for a elements, the text-underline doesn't affect the content added using the :before pseudo-element. However using display:flex changes how it is displayed and causes the text-underline to be added to the content added in a:before as well as the link text:
Working example:
UPDATE: To meet your new new requirement of indentation on a long link and not being able to change the HTML.
.super {
color: rgb(40, 40, 40);
background-color: rgb(239, 239, 239);
margin-bottom: 35px;
padding: 10px;
}
p {
position: relative;
word-break: break-word;
font-size: 17px;
padding: 0px;
}
a {
position: relative;
text-decoration: none;
display: block;
padding-left: 15px;
}
a:before {
content: '>';
position:absolute;
left:0;
top: 0;
text-decoration:none;
}
a:hover{
text-decoration:underline;
}
a:hover:before{
text-decoration:none;
}
<div class='super'>
<p>
<a href='https://google.com' target='_blanck'><span>This is a very long link This is a very long link This is a very long link This is a very long link This is a very long link This is a very long link This is a very long link This is a very long link This is a very long link</span></a>
</p>
</div>
Also, FYI, the correct way to use :hover and :before together is :hover:before, but in this case it doesn't make a difference when you are using display:flexas you can see below:
Example using the correct CSS a:hover:before with display:flex (and as you can see it doesn't work):
.super {
color: rgb(40, 40, 40);
background-color: rgb(239, 239, 239);
margin-bottom: 35px;
padding: 10px;
}
p {
position: relative;
word-break: break-word;
font-size: 17px;
padding: 0px;
}
a {
left: 0px;
display:flex;
position: relative;
text-decoration: none;
}
a::before {
content: '> ';
position: relative;
display: inline-block;
margin-right: 5px;
text-decoration:none!important;
}
a:hover{
text-decoration:underline;
}
a:hover::before {
text-decoration:none!important;
}
<div class='super'>
<p>
<a href='https://google.com' target='_blanck'>Example to link</a>
</p>
</div>
I think you misplaced location hover
a:hover:before

My button isnt working

HTML code:
<div id="but_2" class="button">
Portfolio
</div>
<div id="but_1" class="button">Home</div>
CSS code:
.button {
background-color: #ff0000;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
#but_1 {
position: absolute;
right: 50px;
top:20px;
}
My css button isn't working, it brings me to where I need to be, but it is blue and is underlined(the text in the button) I have text-decor none, but it still isnt going away.
Target the a element to remove the underline and change the color. You can target it with .button a {}
.button {
background-color: #ff0000;
border: none;
padding: 15px 32px;
text-align: center;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
#but_1 {
position: absolute;
right: 50px;
top: 20px;
}
.button a {
text-decoration: none;
color: white;
}
<div id="but_2" class="button">Portfolio
</div>
<div id="but_1" class="button">Home</div>
You need to be more specific and target the a href attribute.
For example:
.button a {
text-decoration: none;
color: #000;
}
https://jsfiddle.net/xojev64e/
a {text-decoration: none; color:#FF0000;} shows unvisited link
a:visited {color:#FF0000;} shows visited link
a:hover {color:#FF0000;} shows mouse over link
a:active {color:#FF0000;} show selected link
use all property to define you color
I think you have not clear your browsing history of your browser. First clear you history, session, or cookie and then perform following steps.
Add following to your css file
.button a {
text-decoration: none;
color: white;
}
And add this for more attraction on mouseover
.button a:hover {
text-decoration: none;
color: black;
}
Hope this will help you.. :)

How can I change the colour of my text without using the colour attribute? (HTML & CSS)

On my website, I have used a customisable template for my navigation bar. The thing I want to do is to change the underlining colour of the text and not change the actual colour of the text (and as you know the underlining feature and the text have to be in the same selector. Now you might be thinking, just make a vertical rule and colour it! The thing is, I do not know the amount of space between an underline and a piece of text. Is there any way to colour the text a different colour from the underline? Thanks!
Screenshots:
Code Input: 1
Result: 2
One solution would be to "fake" the underline with a bottom border. It might not work depending on the structure of your HTML, but something like this:
text-decoration: none;
border-bottom: 1px solid #FF0000;
You cannot isolate the underline color and control it separate from text color; it inherits the same color from the text.
However, you can use border, instead.
nav a {
color: white
text-decoration: none;
border-bottom: 1px solid salmon;
background-color: salmon;
}
nav a.active {
color: #333;
border-bottom: 1px solid #333;
}
Use inline-block as display value for each link and apply a bottom border:
ul li a{
display:inline-block;
border-bottom:2px solid #eeeeee;
float:left;
padding: 10px;
background-color:red;
color:#ffffff;
text-decoration:none;
}
change padding, background color and font color as per your style.
This is another solution. Put the mouse over the element!
ul{
margin: 0;
padding: 0;
}
ul li a{
height: 50px;
position: relative;
padding: 0px 15px;
display: table-cell;
vertical-align: middle;
background: salmon;
color: black;
font-weight: bold;
text-decoration: none;
}
ul li a:hover:after{
display: block;
content: '\0020';
position: absolute;
bottom: 0px;
left: 0px;
width: 100%;
height: 3.5px;
background: #000000;
z-index: 9;
}
<ul>
<li><a href='#'>Menu Item</a></li>
</ul>

How can I put a (working) bottom margin on a hovered <a>?

I was wondering - when making my website - how i can put a bottom margin that lifts an a-tag up.
CSS:
.navbar a {
background-color: #494949;
border: 1px solid #ffffff;
font-family: sans-serif;
font-weight: bold;
padding: 5px 10px;
text-decoration: none;
}
.navbar a:hover {
text-decoration: none;
font-family: sans-serif;
font-weight: bold;
color: #494949;
padding: 10px 10px;
border: 1px #494949 solid;
background-color: #67E727;
}
And here's also my html:
<div class="navbar">
Home
Info
Doneren
Gallerij
Contact
</div>
I really hope you can help me out :)
greetings form the Netherlands!
You do not have to repeat all properties in hover state, only those you want to change. a tag is a inline element, see how box model works. To push it top on hover you can't use margin, you can use something like this:
.navbar a:hover {
position: relative;
top: -10px;
}
Is something like this what you're looking for? It moves the whole nav bar down a bit so the highlighting doesn't get cut off, and it moves the text up on hover. Here is the changed parts:
.navbar{
margin-top:20px;
}
.navbar a:hover {
position: relative;
top: -5px;
}

Custom select not working on IE9

I have this select that is behaving strange on IE9.
First of all links that should open wiki page not working only on IE9 browser and second problem is on hover, why when cursor pass over help and log off the icon is overridden by hover background color?
<ul id="main">
<li class="username" tabindex="1" > <a>USER</a>
<ul class="curent_buser">
<li class="help">Help</li>
<li class="logoff">LogOff</li>
</ul>
</li>
</ul>
CSS:
ul#main {
color: gray;
width: 120px;
border-left: 1px solid #f2f2f2;
border-right: 1px solid #f2f2f2;
border-top: 1px solid #f2f2f2;
list-style: none;
font-size: 12px;
letter-spacing: -1px;
font-weight: bold;
text-decoration: none;
height:30px;
background:green;
}
ul#main:hover {
opacity: 0.7;
text-decoration: none;
}
#main > li{
background: url('http://cdn1.iconfinder.com/data/icons/crystalproject/24x24/actions/1downarrow1.png') 100% 0 no-repeat;
outline:0;
padding:10px;
}
ul#main li ul {
display: none;
width: 116px;
background: transparent;
border-top: 1px solid #eaeaea;
padding: 2px;
list-style: none;
margin: 7px 0 0 -3px;
}
ul.curent_buser li a {
color: gray;;
cursor: pointer;
}
ul.curent_buser{
background:lime !important;
}
ul#main li ul li a {
display: block;
padding: 5px 0;
position: relative;
z-index: 5;
}
#main li:focus ul, #main li.username:active ul {
display: block;
}
.help{
background: url("http://cdn1.iconfinder.com/data/icons/musthave/16/Help.png") no-repeat 100% center ;
height: 25px;
margin-bottom: 2px;
border-bottom: 1px solid white;
}
.help:hover{
background: #f4f4f4;
}
.logoff{
background: url("http://cdn1.iconfinder.com/data/icons/cc_mono_icon_set/blacks/16x16/on-off.png") no-repeat 100% center ;
height: 25px;
}
.logoff:hover{
background: #f4f4f4 ;
height: 25px;
}
.help a,.logoff a{
color:gray;
font-family: Museo700Regular,sans-serif;
letter-spacing: 0;
font-size: small;
}
​
Fiddle: http://jsfiddle.net/RwtHn/1455/
I can at least help you with the Icon issue. The issue is that you are overidding the background with a color. You can have a color or a background image. Not both. You will need to either have a different image in the background that is essentially the same but with different colors, do without the image when you hover or do without the color when you hover.
I'm sorry I can't be more helpful with the IE problem. I sincerely hate IE for things like this.
EDIT: This is something that you can do as mentioned in the comment below
.logoff:hover{
background: #f4f4f4 url("http://cdn1.iconfinder.com/data/icons/cc_mono_icon_set/blacks/16x16/on-off.png");
height: 25px;
}
Thanks ANeves for this information. I learnt something here too.
OK, for the overridden icon issue credits goes for "ANeves",
but you may use below CSS for preventing extra code lines:
#main > li > ul > li:hover
{
background-color: #f4f4f4;
}
for the IE9 clicking issue, just add below CSS:
#main ul:hover
{
display: block;
}
and that's it
thanks to http://www.cssplay.co.uk/menus/cssplay-click-click.html
On hover you are overriding the background property. Since this property has both the colour and the image, you are overriding the image as well.
Set only the colour, then:
.help:hover{
background-color: #f4f4f4;
}
.logoff:hover{
background-color: #f4f4f4 ;
}
http://jsfiddle.net/RwtHn/1456/