So I thought it'd be a great idea to add tooltips to my Neocities site, but I seem to have run into an issue I can't find the answer to...
Okay for some ungodly reason my tooltip class isn't working. I assigned my div the class, and the span inside it the tooltiptext class, but it would still just use what I had assigned the body. I only noticed this when the text was still white, when it should've been black, among other things.
Here's the html section:
<h1>please god ignore the background, I haven't found a good one yet</h1>
<img id="A wooden door framed with clip-art of flowers." style="position: relative;" src="images/flowerydoor.png" />
<div class="tooltip">
<p>this is literally copy+pasted from w3schools what the actual fuck-
<span class="tooltiptext">wait a minute this should have black text why isn't the class working</span></p>
</div>
I'm including the header and image parts because I'm desperate and worry the answer lies within one of the miniscule details
here's the stylesheet:
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: white;
color: black;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
}
.tooltip:hover .tooltiptext {
visibility: visible;
}
body {
background-color: #fbb291;
color: white;
font-family: Verdana;
}
Once again all copy+pasted from w3schools just to make sure it wasn't me
Like I said, the text of the tooltip-assigned div still has white text, and nothing from the tooltip class...
Either the body is overriding my class, or there's something going on with the class itself that's stopping it from working.
I don't know if this helps, but I have assigned a class to my body, which works perfectly fine. I'm wondering if there's something going on with it? I mean, it shouldn't, because I have another page using said class, along with divs using other classes that work perfectly fine!
.door {
margin: 0 auto;
background-image: url("https://64.media.tumblr.com/1adbeafb3ca992a7681ede48ddedcbbd/d5886a952040c00b-9b/s250x400/a917bb1772111a1460eac4922c0502e0ba860bd1.jpg");
/*position: relative;*/
width: 600px;
height: 900px;
text-align: center;
}
I apologize if I'm not making much sense, I'm not super familiar with certain html and css terms.
In this snippet based on your code, the tooltip text is black:
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: white;
color: black;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
}
.tooltip:hover .tooltiptext {
visibility: visible;
}
body {
background-color: #fbb291;
color: white;
font-family: Verdana;
}
<h1>please god ignore the background, I haven't found a good one yet</h1>
<img id="A wooden door framed with clip-art of flowers." style="position: relative;" src="images/flowerydoor.png" />
<div class="tooltip">
<p>this is literally copy+pasted from w3schools what the actual fuck-
<span class="tooltiptext">wait a minute this should have black text why isn't the class working</span></p>
</div>
If you're using other libraries with their own CSS or are deploying this on a third-party website, there could be a namespace collision. You can check what styles are applying to an HTML element using the Chrome DevTools or similar tools in other browsers. Here is a guide for doing this in Chrome: https://developer.chrome.com/docs/devtools/css/overrides/
Please note: I have already tried the solution listed here which does not serve my purposes as it still draws a box around the image
Update: I have taken a further look into this and found that one solution is to draw an SVG of the image in question and make this the button so that there is not such a large box surrounding the image. I would prefer not to do this as I have many assets but would this be the only solution?
I am trying to place a button on my website but make it so that the clickable area is exactly the image and nothing else. I have been trying to find solutions for this but perhaps I am not searching the correct terms.
At present, I am using Bootstrap to create a button for a carousel. This works fine but the issue I am having is the square border around the button itself which will cause a problem as I put more buttons on the page that are closely placed together.
I would like the clickable area to be restricted strictly to the png itself, which includes the white border seen on the image linked above.
My code at present looks as follows.
HTML:
<div class='background'></div>
<div class="map">
<img id='mainMap' src='assets/maps/map.png' alt='map'>
<!-- Modal's toggle button has data that's used to determine what content to use -->
<!-- Bridge icons -->
<button type="button" class="btn btn-primary-outline-btn bridge-btn" id='bridge1' data-toggle="modal" data-target="#exampleModal"><img class="bridge_icon" src="assets/icons/bridge1.png" alt="image"></button>
</div>
CSS:
.camera_icon, .video_icon{
width: 30px;
height: 30px;
}
.bridge_icon {
width: 150px;
height: auto;
margin: 0px;
padding: 0px;
}
#bridge1 {
top: 3%;
left: 10%;
position: inherit;
}
/* Buttons */
.btn, .btn-outline-primary, .video-btn {
background-color: transparent;
border-color: #ccc;
padding: 0px;
box-shadow: none;
}
.btn:focus,.btn:active {
outline: none !important;
box-shadow: none;
}
.bridge-btn {
border-color: hotpink;
}
.btn-outline-primary:hover, .video-btn:hover{
background-color: #e4dbef;
border-color: #ccc;
}
.btn-outline-primary:focus, .video-btn:focus{
background-color: #e4dbef;
outline: none;
border-color: #ccc;
}
.btn:focus, .btn:active:focus, .btn.active:focus { outline: none; ! important; outline-style: none; }
Any help you could provide would be very useful.
Thank you.
If I understand correctly the button has a border you don't want? If that is the case try changing btn-primary-outline-btn to btn-transparent-outline-btn
I have created this page here Page
and I want to make it editable in such a way that I want pencil icons in front of each heading and in front of each paragraph and image respectively and once I click on the edit icon the text comes in the textbox field. How can i do this? What is the best possible solution. I just need a little help with bringing the text in textbox field on pencil icon click. Should i be using any built in property of css if there is any? or should I use any package?
If I'm understanding your intent correctly, you just want to do something like this with a quick illusion of editable labels?
.special-edit {
position: relative;
}
.special-edit input {
font-family: 'FontAwesome';
font-size: 2rem;
padding: .5rem 0 .5rem 2.5rem;
width: 100%;
border: none;
border-bottom: transparent 3px solid;
outline: 0;
transition: .35s ease;
}
.special-edit input:hover, .special-edit input:focus {
background-color: #f1f1f1;
border-bottom: #ddd 3px dotted;
}
.special-edit:after {
font-family: 'FontAwesome';
content: '\f044';
position: absolute;
top: 1rem;
left: .5rem;
color: gray;
font-size: 1.5rem;
}
body {
margin: 3rem 5rem;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="special-edit">
<input type="text" placeholder="Hey this needs edited" required/>
</div>
...and a CODEPEN to tinker. Cheers!
Many WYSIWYG editors use a HTML property called "editable" to turn divs into editable objects
https://www.w3schools.com/TAGS/att_global_contenteditable.asp
That said, what you want to achieve is a homoungus task, you´d be better using some js framework to create text editors, and tinyMCE is the industry standard.
Situation and problem description
I'm currently working on mobile optimization of a web page according to hints given from Google Developer's PageSpeed Insights Tool and I get a lot of warnings about tap targets being too close to each other. The problem is: PageSpeed sees multiple tap targets when there's just one intended to be.
Example
PageSpeed Output (simplyfied):
The tap target <span class="glyphicon"> is close to 1 other tap targets.
The tap target <span class="badge"> is close to 1 other tap targets.
corresponding CSS/HTML (simplified):
.glyphicon::before {
content: "x"; /* substitute for same size shopping cart symbol of custom font*/
}
.badge {
background-color: #999;
border-radius: 10px;
color: #fff;
display: inline-block;
font-size: 12px;
font-weight: 700;
line-height: 1;
min-width: 10px;
padding: 3px 7px;
text-align: center;
vertical-align: baseline;
}
a {
text-decoration: none;
border-color: #000;
border-radius: 3px;
border-style: solid;
border-width: 1px;
margin: 1px 2px;
padding: 5px 8px;
}
<a href="//some.where">
<span class="glyphicon"></span> <span class="badge">21</span>
</a>
Question
You can see easily that the intention is to have one tap target which is the link, consisting of two (or in similar cases more) HTML elements.
What can I do to make Google's PageSpeed to recognize just the parenting link for a tap target and ignore it's children?
You're wrapping the anchor element around two inline elements. If you wrap it around a block level element, like a div, you'll have one block level link instead of multiple inline links. You'll also have the advantage of being able to size the tap target in terms of height and width to optimize the tap target for Google.
The devil is in the details, and we don't seem to have any hint on why PageSpeed interprets the individual <span> elements within the <a> as tap targets, as it was already mentioned there could be event handlers bound to the elements triggering this warning.
With that disclaimer, I will assume PageSpeed does in fact warn you about structures like
<a href=#>
<span class=glyph>..</span> <span>..</span>
</a>
So I'll focus on removing elements from it, as to prevent the symptoms (I truly don't believe PageSpeed nags about inline markup within an anchor).
.glyph::before {
content: "x";
margin: 0 1ex 0 0; /* hated to see again */
}
.badge {
background-color: #999;
border-radius: 10px;
color: #fff;
display: inline-block;
font-size: 12px;
font-weight: 700;
line-height: 1;
min-width: 10px;
padding: 3px 7px;
text-align: center;
vertical-align: baseline;
}
a {
text-decoration: none;
/* Tip: Use shorthand to define, full property to override */
/*
border-color: #000;
border-style: solid;
border-width: 1px;
*/
border: 1px solid #000;
border-radius: 3px;
margin: 1px 2px;
padding: 5px 8px;
/* prevent lines from wrapping, removes the need for */
white-space: nowrap;
}
<a href="//some.where" class="glyph">
<span class="badge">21</span>
</a>
What this does is move around some logic, reducing the amount of elements needed to style it. (With some added tips in the comments)
This should remove the .. is close to 1 other tap targets warnings, as these elements are simply removed.
I currently set the title attribute of some HTML if I want to provide more information:
<p>An <span class="more_info" title="also called an underscore">underline</span> character is used here</p>
Then in CSS:
.more_info {
border-bottom: 1px dotted;
}
Works very nice, visual indicator to move the mouse over and then a little popup with more information. But on mobile browsers, I don't get that tooltip. title attributes don't seem to have an effect. What's the proper way to give more information on a piece of text in a mobile browser? Same as above but use Javascript to listen for a click and then display a tooltip-looking dialog? Is there any native mechanism?
You can fake the title tooltip behavior with Javascript. When you click/tab on an element with a title attribute, a child element with the title text will be appended. Click again and it gets removed.
Javascript (done with jQuery):
$("span[title]").click(function () {
var $title = $(this).find(".title");
if (!$title.length) {
$(this).append('<span class="title">' + $(this).attr("title") + '</span>');
} else {
$title.remove();
}
});
CSS:
.more_info {
border-bottom: 1px dotted;
position: relative;
}
.more_info .title {
position: absolute;
top: 20px;
background: silver;
padding: 4px;
left: 0;
white-space: nowrap;
}
Demo: http://jsfiddle.net/xaAN3/
Here is a CSS only solution. (Similar to #Jamie Pate 's answer, but without the JavaScript.)
We can use the pseudo class :hover, but I'm not sure all mobile browsers apply these styles when the element is tapped. I'm using pseudo class :focus because I'm guessing it's safer. However, when using pseudo class :focus we need to add tabindex="0" to elements that don't have a focus state intrinsically.
I'm using 2 #media queries to ensure all mobile devices are targeted. The (pointer: coarse) query will target any device that the primary input method is something "coarse", like a finger. And the (hover: none) query will target any device that the primary pointing system can't hover.
This snippet is all that's needed:
#media (pointer: coarse), (hover: none) {
[title] {
position: relative;
display: inline-flex;
justify-content: center;
}
[title]:focus::after {
content: attr(title);
position: absolute;
top: 90%;
color: #000;
background-color: #fff;
border: 1px solid;
width: fit-content;
padding: 3px;
}
}
/*Semantic Styling*/
body {
display: grid;
place-items: center;
text-align: center;
height: 100vh;
}
a {
height: 40px;
width: 200px;
background: #fa4766;
color: #fff;
text-decoration: none;
padding: 10px;
box-sizing: border-box;
border-radius: 10px;
}
/*Functional Styling*/
#media (pointer: coarse), (hover: none) {
[title] {
position: relative;
display: flex;
justify-content: center;
}
[title]:focus::after {
content: attr(title);
position: absolute;
top: 90%;
color: #000;
background-color: #fff;
border: 1px solid;
width: fit-content;
padding: 3px;
}
}
<a title="this is the Title text" tabindex="0">Tag with Title</a>
Obviously, you'll need to open this on a mobile device to test it.
Here is a Pen with the same code.
Given that a lot of people nowadays (2015) use mobile browsers, and title still hasn't found a form of exposure in mobile browsers, maybe it's time to deprecate reliance upon title for meaningful information.
It should never be used for critical information, but it is now becoming dubious for useful information, because if that information is useful and cannot be shown to half the users, then another way of showing it to almost all users needs to be found.
For static pages, perhaps some visible text near to the relevant control, even as fine print. For server-generated pages, browser sniffing could provide that only for mobile browsers. On the client side, javascript could be used to trap the focus event, via bubbling, to show the extra text next to the currently focussed element. That would minimise the screen space taken up, but would not necessarily be of much use, since, in a lot of instances, bringing focus to a control can only be done in a way that immediately activates its action, bypassing the ability to find out about it before using it!
Over all though, it appears that the difficulties of showing the title attribute on mobile devices, may lead to its demise, mostly due to needing an alternative that is more universal. That is a pity, because mobiles could use a way to show such extra info on-demand, without taking up the limited screen space.
It seems strange that the w3c and mobile browser makers did not do anything about this issue a long time ago. At least they could have displayed the title text on top of the menu that appears when a long press on a control is made.
Personally, I wish it was placed at the top of a right-click/long-touch menu, as it won't timeout, and would be available on all browsers.
The other alternative is to construct footnotes, so an [n] type superscript is put next to the element/text needing more info, linking to explanatory text in a list at the bottom of the page. Each of those can have a similar [n] type link back to the original text/element. That way, it keeps the display uncluttered, but provides easy bidirectional swapping in a simple way. Sometimes, old print media ways, with a little hyperlink help, are best.
The title attribute has been hijacked by some browsers to provide help text for the pattern attribute, in that its text pops up if the pattern doesn't match the text in the input element. Typically, it is to provide examples of the right format.
Slightly more elaborated version of flavaflo's answer:
Uses pre-defined div as pop-up that can hold HTML, rather than reading from a title attribute
Opens/closes on rollover if mouse is used
Opens on click (touch screen) and closes on click on the open pop-up or anywhere else on the document.
HTML:
<span class="more_info">Main Text<div class="popup">Pop-up text can use <b>HTML</b><div></span>
CSS:
.more_info {
border-bottom: 1px dotted #000;
position: relative;
cursor: pointer;
}
.more_info .popup {
position: absolute;
top: 15px; /*must overlap parent element otherwise pop-up doesn't stay open when rolloing over '*/
background: #fff;
border: 1px solid #ccc;
padding: 8px;
left: 0;
max-width: 240px;
min-width: 180px;
z-index: 100;
display: none;
}
JavaScript / jQuery:
$(document).ready(function () {
//init pop-ups
$(".popup").attr("data-close", false);
//click on pop-up opener
//pop-up is expected to be a child of opener
$(".more_info").click(function () {
var $title = $(this).find(".popup");
//open if not marked for closing
if ($title.attr("data-close") === "false") {
$title.show();
}
//reset popup
$title.attr("data-close", false);
});
//mark pop-up for closing if clicked on
//close is initiated by document.mouseup,
//marker will stop opener from re-opening it
$(".popup").click(function () {
$(this).attr("data-close",true);
});
//hide all pop-ups
$(document).mouseup(function () {
$(".popup").hide();
});
//show on rollover if mouse is used
$(".more_info").mouseenter(function () {
var $title = $(this).find(".popup");
$title.show();
});
//hide on roll-out
$(".more_info").mouseleave(function () {
var $title = $(this).find(".popup");
$title.hide();
});
});
Demo here https://jsfiddle.net/bgxC/yvs1awzk/
As #cimmanon mentioned: span[title]:hover:after { content: attr(title) } gives you a rudimentary tooltip on touch screen devices. Unfortunately this has problems where the default ui behavior on touch screen devices is to select the text when any non-link/uicontrol is pressed.
To solve the selection problem you can add span[title] > * { user-select: none} span[title]:hover > * { user-select: auto }
A full solution may use some other techniques:
Add position: absolute background, border, box-shadow etc to make it look like a tooltip.
Add the class touched to body (via js) when the user uses any touch event.
Then you can do body.touched [title]:hover ... without affecting desktop users
document.body.addEventListener('touchstart', function() {
document.body.classList.add('touched');
});
[title] {
border-bottom: 1px dashed rgba(0, 0, 0, 0.2);
border-radius:2px;
position: relative;
}
body.touched [title] > * {
user-select: none;
}
body.touched [title]:hover > * {
user-select: auto
}
body.touched [title]:hover:after {
position: absolute;
top: 100%;
right: -10%;
content: attr(title);
border: 1px solid rgba(0, 0, 0, 0.2);
background-color: white;
box-shadow: 1px 1px 3px;
padding: 0.3em;
z-index: 1;
}
<div>Some text where a portion has a <span title="here's your tooltip">tooltip</span></div>
Depending on how much information you want to give the user, a modal dialogue box might be an elegant solution.
Specifically, you could try the qTip jQuery plugin, which has a modal mode fired on $.click():
The title attribute is not supported in any mobile browsers **in a way that it would show the tooltip the same as to desktop mouse users** *(the attribute itself is ofcourse supported in the markup)*.
It's only basically for desktop users with a mouse, keyboard only users can't use it either, or screenreaders.
You can achieve almost similar with javascript as you said.
I was searching for an easy CSS only solution, and this is really the most easy one I found:
<link rel="stylesheet" href="https://unpkg.com/balloon-css/balloon.min.css">
<span aria-label="Whats up!" data-balloon-pos="up">Hover me!</span>
Working example: https://jsfiddle.net/5pcjbnwg/
If you want to customize the tooltip, you find more info here:
https://kazzkiq.github.io/balloon.css/
To avoid using JavaScript, I used this CSS-only tooltip:
http://www.menucool.com/tooltip/css-tooltip
It works great in Mobile and Desktop, and you can customize the styles.
Thanks to #flavaflo for their answer. This works in most cases but if there is more than one title to lookup in the same paragraph, and one opens over the link to another, the unopened link shows through the first. This can be solved by dynamically changing the z-index of the title that has "popped up":
$("span[title]").click(function () {
var $title = $(this).find(".title");
if (!$title.length) {
$(this).append('<span class="title">' + $(this).attr("title") + '</span>');
$(this).css('z-index', 2);
} else {
$title.remove();
$(this).css('z-index', 0);
}
});
Also, you can make both the hover over display and the click display multiline by adding
(linefeed) to the title='' attribute, and then convert that to <br /> for the html click display:
$(this).append('<span class="title">' + $(this).attr("title").replace(/\\n/g, '<br />') + '</span>');
Extremely late to the party but for future visitors, here is a tweak of #Flavaflo's answer to fade the "tooltip" in and out
JQuery:
$(".more_info").click(function () {
var $title = $(this).find(".title");
if (!$title.length) {
$(this).append('<span class="title">' + $(this).attr("title") + '</span>');
} else {
$($title).fadeOut(250, function() {
$title.remove();
});
}
});
CSS:
.more_info {
border-bottom: 1px dotted;
position: relative;
}
.more_info .title {
position: absolute;
top: 20px;
background: green;
padding: 4px;
left: 0;
color: white;
white-space: nowrap;
border-radius:3px;
animation: fadeIn linear 0.15s;
}
#keyframes fadeIn {
0% {opacity:0;}
100% {opacity:1;}
}
Fiddle here: http://jsfiddle.net/L3paxb5g/
I know this is an old question, but i have found a CSS solution that works on mobile too, it doesn't use title at all and it's easy to implement, explained here:
https://www.w3schools.com/css/css_tooltip.asp
Explanation:
On mobile, with the touchscreen,the first input acts as css hover, so it works like a toggle tooltip when you press on it.
Code example:
.tooltip {
position: relative;
display: inline-block;
border-bottom: 2px dotted #666;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 15em;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
position: absolute;
z-index: 1;
bottom: 125%;
left: 50%;
margin-left: -8em;
opacity: 0;
transition: opacity 0.3s;
padding: 0.5em;
}
.tooltip .tooltiptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #555 transparent transparent transparent;
}
.tooltip:hover .tooltiptext {
visibility: visible;
opacity: 1;
}
<div class="tooltip">Hover over me
<span class="tooltiptext">Tooltip text</span>
</div>