I've been attempting to build the following (figma):
As it stands i've tried padding, margins, psudeo elements, whitespaces and I'm pretty stuck on what to do. You can see its possible to have the underline styles on the icon, but when I create distance from 'ID', the underline gap appears. I need the icon to stay as far away as it is here but also keep the underline crossing.
HTML: Please note 'input-row' cannot be styled in this instance as it's used across other elements
<div class="input-row">
<a class="link" href="#">Acceptable Forms of ID <i class="fas fa-share-square"></i></a>
</div>
.fa-share-square{
font-size: 0.75rem;
cursor: pointer;
color: $secondary-five;
font-size: 1rem;
text-decoration: underline dotted $secondary-five;
text-underline-offset: .5rem;
text-decoration-thickness: 2px;
}
.link{
color: $secondary-five;
font-size: 1rem;
text-decoration: underline dotted $secondary-five;
text-underline-offset: .5rem;
text-decoration-thickness: 2px;
}
Thanks for your help.
You have a input-row container around these 2 elements,
Add the underline to the container, use margin and or padding to get it right under the text.
Revised Answer
It appears that I had misunderstood your original request. I now understand that you want the underlined (dots) to appear uniformly for the anchor including the space between the text and the Font Awesome icon.
This is more simple than the previous answer.
You will first need to remove the default text-under line from HTML anchors, this is done in the a.link CSS below.
Then you style the anchor as an inline-block/block level element (which it is by default), and style a border rather than text-underline, because text-underline won't (and symantically shouldn't) activate on a lack of text (whitespace). You can also customise the gap between the text and the underline using padding.
So:
HTML:
<div class="input-row">
<a class="link" href="#">Acceptable Forms of ID <i class="fas fa-share-square"></i></a>
</div>
And then you set your CSS styling thus:
CSS:
.fa-share-square{
font-size: 0.75rem;
cursor: pointer;
color: $secondary-five;
font-size: 1rem;
}
a.link {
color: #900;
font-size: 1rem;
text-decoration: none;
padding-bottom: 2px;
border-bottom: 2px dotted #00F;
}
Full example:
.fa-share-square{
font-size: 0.75rem;
cursor: pointer;
color: #900;
font-size: 1rem;
}
a.link {
color: #900;
font-size: 1rem;
text-decoration: none;
padding-bottom: 3px; /* Set the border distance from the text */
border-bottom: 3px dotted #00F; /* Set the border style */
}
.fa-share-square {
width: 3rem;
height: 1rem;
padding-left:1rem;
}
<p>(Extra CSS put in place to show the Font Awesome Icon part)</p>
<div class="input-row">
<a class="link" href="#">Acceptable Forms of ID
<i class="fas fa-share-square">ICON</i></a>
</div>
Manual Reference for CSS Border-bottom.
I had a stroke of genius in the shower. Line spacing.
At first I tried this on the Icon, but the spacing moved the underline to the right, so I needed to apply it to the letter beforehand.
Here you can see my changes and result.
<div class="input-row">
<a class="link" href="#">Acceptable Forms of I<span>D</span> <i class="fas fa-share-square"></i></a>
</div>
.link{
color: $secondary-five;
font-size: 1rem;
text-decoration: underline dotted $secondary-five;
text-underline-offset: .5rem;
text-decoration-thickness: 2px;
font-weight: bold;
}
span{
letter-spacing: 12px;
}
Some minor issues with the dots overlapping each other at certain spacing values, but this is a lot closer to a solution than anything else
You can simply remove text-decoration from your css and update as follows. Also set text-decoration:none for the .link class. Finally set border-bottom for the class input-row and width:fit-content. I am using color:red, you can use of your own choice.
HTML code:
<div class="input-row">
<a class="link" href="#">Acceptable Forms of ID <i class="fas fa-share-square"></i></a>
</div>
Updated CSS:
.fa-share-square {
font-size: 0.75rem;
cursor: pointer;
color: red;
font-size: 1rem;
/*text-decoration: underline dotted red;
text-underline-offset: .5rem;
text-decoration-thickness: 2px;*/
}
.link {
color: red;
font-size: 1rem;
text-decoration: none;
/*text-decoration: underline dotted red;
text-underline-offset: .5rem;
text-decoration-thickness: 2px;*/
}
.input-row {
border-bottom: 2px dotted red;
width: fit-content;
}
Related
I have a button div which contains two child div elements. One with a count class, and one with a caption class.
If I don't set a color for the .count class, then the button hover style (white) will apply to the count div. However, if I set a color in the .count class, then the button hover, no longer applies to the count class/div. I can define a hover style for the count div, however it will only apply when the cursor moves over the count, not the button as a whole. How can I set a specific color for the count class, but still have it's color change (to button hover color) when hover over the button?
Sample Code:
.button {
margin: 8px;
padding: 4px;
border: 1px solid black;
text-align: center;
cursor: pointer;
background-color: lightgrey;
}
.button .count {
font-size: 30px;
color: green;
}
.button:hover {
color: white;
background-color: gray;
}
.button .count:hover {
color: white;
}
<div class="container">
<div class="button">
<div class="count">
2
</div>
<div class="caption">
Videos
</div>
</div>
</div>
Sample demo:
https://codepen.io/raelb/pen/OJWrpKw
Just add a second selector targetting the child element in your .button:hover rule:
.button {
margin: 8px;
padding: 4px;
border: 1px solid black;
text-align: center;
cursor: pointer;
background-color: lightgrey;
}
.button .count {
font-size: 30px;
color: green;
}
.button:hover {
background-color: gray;
}
.button:hover,
.button:hover .count {
color: white;
}
<div class="container">
<div class="button">
<div class="count">
2
</div>
<div class="caption">
Videos
</div>
</div>
</div>
There's nothing forcing you to end your selector with :hover.
You are facing a css priority issue :)
The colour property defined inside the count class has a higher priority than the one you declare in the hover pseudo-class of your button.
A way to solve that would be to use:
.button:hover count { color: white; }
I have a clickable div I created that works like a button. I managed to create it's :hover state, but I can't get the :active state to work. I basically need the button to maintain its :hover style when clicked. I would like to use CSS only. I'm using Bootstrap 4.
.delete-ad-reason-box {
display: inline-block;
width: 100%;
border: 2px solid red;
border-radius: 0.25rem;
color: red;
font-size: 1em;
margin: 30px 0;
padding: 20px 15px;
line-height: 20px;
}
.delete-ad-reason-box i {
font-size: 1.5em;
margin-bottom: 10px;
color: red;
}
/* Here's the ":active" */
.delete-ad-reason-box:hover,
.delete-ad-reason-box:active {
border: 2px solid red;
background: red;
color: white;
}
.delete-ad-reason-box:hover>i,
.delete-ad-reason-box:active>i {
color: white;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<a href="#" class="col-lg-4 col-md-4 col-sm-10 col-xs-10 col-10">
<div class="mx-auto delete-ad-reason-box" id="#">
<i class="fas fa-frown d-block"></i> Click Me
</div>
</a>
You can't use : active for a div element. You can able to achieve it via javascript only
I suggest you use only 1 anchor tag element for this one. :active :focus pseudo element only works on a tag. So simplify your html structure to this one.
.click-me {display:block;color:red;border:1px solid red;width:100%;padding:20px 10px;text-decoration:none;font-weight:bold;}
.click-me:hover,.click-me:active,.click-me:focus {border:1px solid transparent;color:#fff;background:red;}
Click Me
This is a more simplified one and you don't need to add a div element inside the a tag.
This question is in reference to this documentation https://en.bem.info/methodology/css/#external-geometry-and-positioning
The parent class is "header" and the button is "button header__button". Typically, I would use (and see elsewhere in tutorials) "button button--header".
My site uses dropdown menus with the class "dropdown-menu" and I am also using "dropdown-menu--nav" but is "--nav" an appropriate modifier? By the logic in the documentation, I should use "dropdown-menu navbar__dropdown-menu". I'm lost because I can see both navbar and dropdown-menu being their own blocks, but when they interact with each other, I'm not so sure. If navbar is the block and dropdown-menu is the element, should I use "dropdown-link navbar__dropdown-link" instead of "dropdown-menu__link dropdown-menu__link--nav" for links?
I could see the "element block__element" approach working better because the block specific styling to that element would be near the block styling, instead of with the element styling as a modifier.
Example of how I use BEM for button example and dropdown menu: https://codepen.io/SROwl/pen/eYmVzBE
Explanation:
I would write dropdown-menu--nav as dropdown-menu__nav as it is a component of the dropdown menu. Also, I tend to rebase inner components seen in the nav example below.
The button example is used to show multiple classes being used to create the button look you want. However, if the .button class doesn't have any of the same styles as .header__button then this would not be necessary. You would simply use .header__button.
I would use a modifier if I wanted to change the button color, ex: .header__button--green.
I stray away from the BEM documentation a little where they want you to use something like: < div class="header__button header__button--green">. I use SCSS extends to include the properties of header__button to header__button--green so the markup ends up being: < div class="header__button--green"> rather than including both classes. This is a personal preference, some people do not like extends as they find it difficult to manage or don't like the way it compiles the css.
BEM
<!-- How BEM states it should be done -->
<div class="button">Shop Now</div>
<div class="button header__button">Shop Now</div>
<div class="button header__button header__button--green">Shop Now</div>
BEM EXTEND
<!-- How I prefer to do it -->
<div class="button2">Shop Now</div>
<div class="header__button2">Shop Now</div>
<div class="header__button2--green">Shop Now</div>
BEM NAV
<div class="dropdown-menu">
<div class="dropdown-menu__header">
Menu
</div>
<ul class="dropdown-menu__nav">
<li class="nav__link">Link 1</li>
<li class="nav__link--active">Link 2</li>
<li class="nav__link">Link 3</li>
</ul>
</div>
body {
font-family: sans-serif;
}
// BEM WAY
.button {
background: #000;
color: #fff;
border-radius: 5px;
font-size: 16px;
text-align: center;
max-width: 200px;
padding: 5px 15px;
text-transform: uppercase;
margin-bottom: 10px;
}
.header__button {
font-size: 32px;
color: #ccc;
&--green {
background-color: green;
}
}
// BEM-ish way
.button2 {
background: #000;
color: #fff;
border-radius: 5px;
font-size: 16px;
text-align: center;
max-width: 200px;
padding: 5px 15px;
text-transform: uppercase;
margin-bottom: 10px;
}
.header__button2 {
#extend .button2;
font-size: 32px;
color: #ccc;
&--green {
#extend .header__button2;
background-color: green;
}
}
// BEM-ish menu
.dropdown-menu {
background: gray;
width: 300px;
padding: 15px;
&__header {
background-color: #ccc;
padding: 5px;
}
&__nav {
background: darken(#ccc, 20%);
padding: 10px;
list-style-type: none;
.nav {
&__link {
text-transform: uppercase;
color: purple;
padding: 5px;
&--active {
#extend .nav__link;
color: pink;
background: gray;
}
}
}
}
}
When using text-decoration on a link, the child element (span) is not included, so then the underline doesn't extend:
a {
font-size: 36px;
text-decoration: underline dotted rgb(221, 221, 221);
color: #000;
}
a:hover {
text-decoration: none;
color: #000;
}
.badge-dark {
font-size: 9px;
margin-left: 2px;
position: relative;
text-align: center;
top: -5px;
}
<a href="#">
My title is here
<span class="badge badge-dark">Special</span>
</a>
See fiddle
Is it possible for the span to be included or is text-decoration ignoring spans by design?
https://developer.mozilla.org/en-US/docs/Web/CSS/text-decoration:
The text-decoration shorthand CSS property sets the appearance of decorative lines on text.
This means that the underline will be directly under the text in question and not under the element. If you zoom in enough, you will see the underline is actually under the word special
If you want to continue the line under special, perhaps you could use a pseduo element for your badge and add some non-breaking spaces for it to sit in:
a {
font-size: 36px;
text-decoration: underline dotted rgb(221, 221, 221);
color: #000;
}
a:hover {
text-decoration: none;
color: #000;
}
.badge {
display: inline-block;
position: relative;
text-decoration: underline dotted rgb(221, 221, 221);
}
.badge-dark:after {
content: 'Special';
display: inline-block;
color: #ffffff;
background: #555555;
padding: 3px 5px;
border-radius: 5px;
font-size: 9px;
position: absolute;
text-align: center;
right: 5px;
top: 50%;
transform: translateY(-50%);
margin-top: -5px;
}
<a href="#" class="badge badge-dark">
My title is here
</a>
You can apply text-decoration to your span, however it will appear just under the span text and not inline with the preceding text. To make it inline you will need to make your span the same height as its parent container. Alternatively you can likely use a pseudo element (:before or :after) to put the line where you want it.
This happens because of the CSS specification that basicly say, you can't have text-decoration in inline block elements. If you span to also be affected by text-decoration, you must change the display:inline-block. Further information can be found in this question .
Just to show you , how it would work, if the span was being effected by the text-decoration, here is an example:
a {
font-size: 36px;
text-decoration: underline dotted rgb(221, 221, 221);
color: #000;
}
a:hover {
text-decoration: none;
color: #000;
}
.badge-dark {
font-size: 20px;
margin-left: 2px;
position: relative;
text-align: center;
top: -5px;
}
<a href="#">
My title is here
<span class="badge badge-dark">Special</span>
</a>
Also, in the code you posted in SO, the text-decoration property is working properly, just not in the fiddle. If you want it to be equal through the whole link, try using border instead.
You can use border-bottom property instead of text-decoration.
also see that I have changed the a to an inline-block element.
.row {
background: #f8f9fa;
margin-top: 20px;
}
.col {
padding: 10px;
}
.title-link {
display: inline-block;
font-size: 36px;
border-bottom: 4px dotted rgb(221, 221, 221);
color: #000;
text-decoration: none;
}
.title-link:hover {
border-bottom: none;
color: #000;
text-decoration: none;
}
.badge-dark {
font-size: 9px;
margin-left: 2px;
position: relative;
text-align: center;
top: -5px;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col">
<a class="title-link" href="#">My title is here
<span class="badge badge-dark">Special</span></a>
</div>
</div>
</div>
The font size of the content in the badge is different from the other link text and you also have an alignment of top:-5px. These two break the line and even without using the bootstrap badge you would get the text-decoration broken. Yes it would extend to the span text but be broken and it would not be what you want. And bootstrap badge also has the style of text-decoration: none...
Another way of getting the dotted underline to extend the badge is removing the text-decoration and using border-bottom like this below:
a {
font-size: 36px;
border-bottom: 3px dotted rgb(221, 221, 221);
color: #000;
}
a:hover {
color: #000;
border-bottom: none;
}
I am working on a navbar in HTML. I don't understand why the elements do not line up nicely within the navbar.
body {
background-color: #ECEFF1; /* Blue Gray 50 */
font-family: 'Roboto', sans-serif;
margin: 0;
}
#header {
background-color: #ba68c8; /* Purple 300 */
color: #ffffff; /* White */
height: 24px;
padding: 8px;
}
.header-link {
font-size: 24px;
text-decoration: none;
}
<div id="header">
<a class="header-logo" href="#">
<img src="http://www.sigmacubes.com/img/logo_h.svg" height="24" />
</a>
<a class="header-link" href="#">
Text
</a>
</div>
Although the svg is aligned vertically, the text does not stay aligned vertically. Why is this happening?
What you can do is reduce the header-link font size to 20px and give it position absolute
.header-link {
font-size: 20px;
text-decoration: none;
position: absolute;
}
Or if you want to keep the size 24px you better give it margin-top: -5px too
.header-link {
font-size: 24px;
text-decoration: none;
position: absolute;
margin-top: -5px;
}
Is vertical-align:middle; an option for your header-link class?
Maybe add display: table-cell; in conjunction with vertical-align:middle; if that further matches the look that you wanted.