HTML - relationship between <a> vs <i> - html

My goal is to come up with a dropdown menu:
$(document).on('click', '.dropdown', function() {
var is_visible = $(this).find('.dropdown-content').is(":visible");
$('.dropdown-content').toggle(false);
if (is_visible == false){
$(this).find('.dropdown-content').toggle(true);
}
});
.dropdown {
position: relative;
display: inline-block;
cursor: pointer;
}
div.dropdown-content {
display: none;
white-space: nowrap;
position: absolute;
padding: 12px 16px;
z-index: 1;
margin-top: 15px;
background: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class='dropdown'>
<i class="fa fa-ellipsis-h fa-lg" aria-hidden="true"></i>
<div class="dropdown-content">
<div><i class="fa fa-share-alt" aria-hidden="true"></i> Share</div>
<div><i class="fa fa-trash-o fa-fw"></i> Delete</div>
</div>
</div>
The question is, what is the semantic role of <a> tag in the dropdown-content item definition (provided I don't use its href attribute and use JavaScript to handle the click event)?
In other words: if I want to have a clickable icon, in which cases should I surround it with an <a> tag?

if I want to have a clickable icon, in which cases should I surround it with <a> tag?
The only case where you should wrap anything with an <a> tag is if it's a link, or a placeholder for where a link could be.
Otherwise, if you want to make something clickable, the appropriate element is <button type="button">.
The one exception to that is if you need to wrap anything that's not phrasing content, in which case I recommend using <div role="button" tabindex="0> with a keydown handler to trigger click events on Enter, and Space.
Additional notes about accessibility for icons, and icon fonts in general: If a network request fails and the icon font fails to load, your sighted users will find themselves in the same situation as your non-sighted users, where it's unclear as to what the button should do.
Generally speaking it's better to pair icons with textual labels, but if that's not possible, consider using an <img> element with [alt] text instead of the icon.
If that's not possible, at least add an [aria-label] attribute.

Related

Need help on styling the icon in service portal

I am working on Servicenow service portal where i created a custom icon-link widget that displays the image as glyph icon.
I almost got everything but my image is stylying as-is comparing to glyph icon.
/* CIRCLE ICON ---------- */
a.circle_icon {
display: block;
padding: 20px 0px 20px 70px;
position: relative;
}
a.circle_icon .fa {
position:absolute;
left: 0px;
top: 10px;
}
/* Image Circle ------- */
a.image_icon {
display: block;
padding: 20px 0px 20px 70px;
position: relative;
}
a.image_icon .fa {
position:absolute;
left: 0px;
top: 10px;
}
<!--// Circle Icon -->
<a ng-if="::(options.link_template == 'Circle Icon')" ng-href="{{::data.href}}" class="circle_icon {{::options.class_name}} text-{{::options.color}}" target="{{::data.target}}">
<span class="fa fa-stack fa-2x">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-{{::options.glyph}} fa-stack-1x fa-inverse"></i>
</span>
<h2>{{::options.title}}</h2>
<span class="text-muted">{{::options.short_description}}</span>
</a>
<!--// Image Icon -->
<a ng-if="::(options.link_template == 'Image Icon')" ng-href="{{::data.href}}" class="image_icon {{::options.class_name}} text-{{::options.color}}" target="{{::data.target}}">
<span class="fa fa-stack fa-2x">
<img src="{{data.target}}"/>
</span>
<h2>{{::options.title}}</h2>
<span class="text-muted">{{::options.short_description}}</span>
</a>
With this code my output looks like this.
Click here
Circle Icon is Out of the box and Image icon is my custom code. I want the aeroplance image looks same as above Get help and Community icon.
I have a few followup questions for you.
1.What is the reason for creating a new widget? (Instead of using Icon link OOB widget)
I'm assuming you wanted to have a custom image in place of the
bootstrap icon.
if yes there is an alternate solution without cloning also you can use your custom image. (Using page in designer add a class, Write style[custom image css] and that's it, without cloning its possible)
2.With the OOB Icon link widget, you can do that with instance options, if you cloned the OOB Icon link widget you can go to instance options add modify,
How to do that?
Right-click on the widget, go to instance options.
scroll to the bottom last field Template has the option as 'Circle icon'
Save and done (Please refer to screenshots attached below)

How to differenct colors for fa fa and text using css

I'd like to have separate colors for fa-search and text with this html using css?
<div class="logos1"><i class="fa fa-search"><span class="padding">logos</span></i></div>
Not sure if I understand your request - the title of your post indicates that you want the same colors for the icon and text - but the question requests different colors.
Fa icons are colored via the color css property so you can set a given color for them. Note that the FA icons are added as ::before pseudo elements - so can be styled directly or via inherited colors.
Also - although you can have content within the i that houses the icon - it is more common to treat that as a separate element and close the <i> before adding the other content in the span.
EDIT: in response to the comment - I have altered the code so that the a has the logos class - so that you can directly target this and not other a elements.
a.logos1 {
text-decoration: none;
color: blue;
}
a .fa {
color: red;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"/>
<a href="http://4309.co.uk/logos/" class="logos1">
<i class="fa fa-search"></i>
<span class="padding">logos</span>
</a>

How to change font-size for text within an <a> tag in HTML

I'm setting up a website and inserting social media links in the footer. I am using Font Awesome's social media icons (see code to understand). To use the icons you must set the tag with a class of whichever icon you would like. I am trying to have the icon bigger than the text associated with it.
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<a style="font-size:25px;" href="https://www.facebook.com/username/" class="fa fa-facebook"> #username</a>
<a style="font-size:25px;" href="https://www.instagram.com/username/" class="fa fa-instagram"> #username</a>
This code results in the icon and the text associated being both 25px. I would like to change the font-size for the text only to 20px.
You are using the font awesome wrong. They always use the < i > tag for that so you won't have to separate it with css so this is how the html should look:
a {
font-size:20px;
}
a i {
font-size:25px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<a href="https://www.facebook.com/username/">
<i class="fa fa-facebook"></i> #username
</a>
<a href="https://www.instagram.com/username/">
<i class="fa fa-instagram"></i> #username
</a>
Also you don't have to apply the inline-block style to the icon, that is already done in the plugin CSS.
Use the span tag
<a>#<span style="font-size: 20px">username</span></a>.
In this way the font size for username is targeted by the span.
Target the pseudo-element
a.fa::before {
font-size:25px;
display: inline-block;
}
a {
font-size:20px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
#username
#username
The icon appears in the generated content in ::before. Target that pseudo-element with a stylesheet.
.fa::before {
font-size: 25px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
#username
#username
Depending on your HTML version:
HTML 5 states that the element "may be wrapped around entire paragraphs, lists, tables, and so forth, even entire sections, so long as there is no interactive content within (e.g. buttons or other links)".
HTML 4.01 specifies that elements may only contain inline elements. fas is a block element, so it may not appear inside an <a>.
You could use a <div> with display: inline inside of your tag, if you use HTML5
Or a span, or some inline property if your HMLT should be compatible < 5
a{
font-size: 200px;
}
.fas{
font-size: 20px;
display: inline;
}
<div class="fas">hi</div>Hello

Removing underline from <img> or <i> if wrapped by anchor link using :has()

<a href="link">
<i class="icon is-ac-coloured fab fa-facebook-square fa-2x"></i>
</a>
a:has(> img), a:has(> i) {
text-decoration: none;
}
Why can I still see the underline on the image?
I'd like it to be removed if an anchor link wraps an image or an italic tag.
:has() is a CSS4 selector its a working draft: https://drafts.csswg.org/selectors-4/#relational, looking at https://caniuse.com/#search=%3Ahas there isnt great browser support. So that might be the reason you are seeing nothing its simply not being parsed.
Going to have to write this using JavaScript or alternatively attach a class to your anchors that are warping images and icons.
.remove-text-decoration {
text-decoration: none;
}

Bootstrap Button Inside List View Not Working

I am trying to get a button inside a list view in bootstrap to get to two different links and it always follows the list view link, not the button link if I press the button. How do I achieve this?
<a href="foo.html" class="list-group-item">
<p>This links somewhere different than the button</p>
<button href="bar.html" class="btn btn-success">Example</button>
</a>
This doesn't appear to be easily accomplished with Bootstrap as-is, but a few tweaks can get us what we want. The main problem is that nested anchors aren't really valid HTML. However, we can achieve the same result by absoultely positioning a link above another link.
Have a look at this JS Bin:
http://jsbin.com/febivi/2/
In summary:
Add a new class to the list-group that will define our new container:
<ul class="list-group action-list-group">
<li class="list-group-item">
<a class="list-group-link" href="http://stackoverflow.com">Cras justo odio</a>
<a class="btn btn-sm btn-default" href="http://google.com">Go</a>
</li>
<li class="list-group-item">
<a class="list-group-link" href="http://stackoverflow.com">Cras justo odio</a>
<a class="btn btn-sm btn-default" href="http://google.com">Go</a>
</li>
</ul>
The primary link is ".list-group-link" and the secondary link is the ".btn" action.
Next we add some CSS to style list-group-item's inside action-list-group:
.action-list-group {
position: relative;
}
/* remove the list group padding since our nested anchor tag will now have it */
.action-list-group .list-group-item {
padding: 0;
}
.action-list-group .list-group-item > a.list-group-link {
display: block;
/* inherit from .list-group-item */
padding: 10px 15px;
color: #555;
}
/* re-add the link styling */
.action-list-group .list-group-item > a.list-group-link:hover {
background: #f5f5f5;
text-decoration: none;
}
.action-list-group .btn {
position: absolute;
right: 5px;
margin-top: 5px;
top: 0;
}
Where it says "inherit", if you were using the Sass version of Bootstrap you could use sass's #include or #extend to include the same styling as bootstrap's .list-group-item > a.
There is javascript solution that you can use as well. The one benefit being that you can then have your button element in the flow of the list-group.
HTML:
<a href="foo.html" class="list-group-item">
<p>This links somewhere different than the button</p>
<button data-href="bar.html" class="btn btn-success">Example #1</button>
</a>
JQUERY:
$('.list-group-item').on('click', function (event) {
event.preventDefault();
$target = $(event.target);
if ($target.is('button')) {
window.location=$target.data('href');
} else {
window.location=$target.closest('a').prop('href');
}
});
Basically, the click handler works to prevent the normal behavior of the list-group-item anchor tag using event.preventDefault(). Then using event.target, you can get the element that dispatched the event. If the target was a button, you retrieve its data property and set the window.location to it. Otherwise, the jQuery closest() method is used to find the nearest anchor tag and use its href value to set the window.location. In jQuery, the closest method starts with the current element (which could be the anchor itself or any of its ancestors that are not button elements) and travels up the DOM from there.
Also, note that since the button element does not have an href attribute, this example uses a data attribute to store the link reference.