Changing :hover to touch/click for mobile devices - css
I've had a look around but can't quite find what i'm looking for.
I currently have a css animation on my page which is triggered by :hover. I would like this to change to 'click' or 'touch' when the page is resized past width 700px using media queries.
Here is what i have at the moment: http://jsfiddle.net/danieljoseph/3p6Kz/
As you can see, the :hover will not work on mobile devices but i still want to ensure it works the same way just by click, not hover.
I would rather use css if possible but happy with JQuery also.
I have a feeling this is very easy to do but i am just missing something very obvious! Any help would be appreciated.
Here is the css animation:
.info-slide {
position:absolute;
bottom:0;
float:left;
width:100%;
background:url(../images/blue-back.png);
height:60px;
cursor:pointer;
overflow:hidden;
text-align:center;
transition: height .4s ease-in-out;
-webkit-transition: height .4s ease-in-out;
-moz-transition: height .4s ease-in-out;
}
.info-slide:hover {
height:300px;
}
If you use :active selector in combination with :hover you can achieve this according to w3schools as long as the :active selector is called after the :hover selector.
.info-slide:hover, .info-slide:active{
height:300px;
}
You'd have to test the FIDDLE in a mobile environment. I can't at the moment.
correction - I just tested in a mobile, it works fine
You can add onclick="" to hovered element. Hover will work after that.
Edit: But you really shouldn't add anything style related to your markup, just posted it as an alternative.
document.addEventListener("touchstart", function() {}, true);
This snippet will enable hover effects for touchscreens
I got the same trouble, in mobile device with Microsoft's Edge browser. I can solve the problem with: aria-haspopup="true". It need to add to the div and the :hover, :active, :focus for the other mobile browsers.
Example html:
<div class="left_bar" aria-haspopup="true">
CSS:
.left_bar:hover, .left_bar:focus, .left_bar:active{
left: 0%;
}
On most devices, the other answers work. For me, to ensure it worked on every device (in react) I had to wrap it in an anchor tag <a> and add the following:
:hover, :focus, :active (in that order), as well as role="button" and tabIndex="0".
I am a CSS noob but I have noticed that hover will work for touch screens so long as it's a "hoverable" element: image, link, button. You can do it all with CSS using the following trick.
Change your div background to an actual image tag within the div or create a dummy link around the entire div, it will then register as a hover when you touch the image.
Doing this will mean that you need the rest of your page to also be "hoverable" so when you touch outside of the image it recognizes that info-slide:hover has ended. My trick is to make all of my other content dummy links.
It's not very elegant but it works.
A CSS only solution for those who are having trouble with mobile touchscreen button styling.
This will fix your hover-stick / active button problems.
body, html {
width: 600px;
}
p {
font-size: 20px;
}
button {
border: none;
width: 200px;
height: 60px;
border-radius: 30px;
background: #00aeff;
font-size: 20px;
}
button:active {
background: black;
color: white;
}
.delayed {
transition: all 0.2s;
transition-delay: 300ms;
}
.delayed:active {
transition: none;
}
<h1>Sticky styles for better touch screen buttons!</h1>
<button>Normal button</button>
<button class="delayed"><a href="https://www.google.com"/>Delayed style</a></button>
<p>The CSS :active psuedo style is displayed between the time when a user touches down (when finger contacts screen) on a element to the time when the touch up (when finger leaves the screen) occures. With a typical touch-screen tap interaction, the time of which the :active psuedo style is displayed can be very small resulting in the :active state not showing or being missed by the user entirely. This can cause issues with users not undertanding if their button presses have actually reigstered or not.</p>
<p>Having the the :active styling stick around for a few hundred more milliseconds after touch up would would improve user understanding when they have interacted with a button.</p>
Well I agree with above answers but still there can be an another way to do this and it is by using media queries.
Suppose this is what you want to do :
body.nontouch nav a:hover {
background: yellow;
}
then you can do this by media query as :
#media(hover: hover) and (pointer: fine) {
nav a:hover {
background: yellow;
}
}
And for more details you can visit this page.
I think this simple method can achieve this goal.
With CSS you can turn off pointer event to 'none' then use jQuery to switch classes.
.item{
pointer-events:none;
}
.item.clicked{
pointer-events:inherit;
}
.item:hover,.item:active{
/* Your Style On Hover Converted to Tap*/
background:#000;
}
Use jQuery to switch classed:
jQuery('.item').click(function(e){
e.preventDefault();
$(this).addClass('clicked')l
});
Related
:hover pseudo state not triggering in IE
I have this markup <button class="toggle" aria-label="Toggle"> <div class="globe-img"></div> </button> and this SASS: .globe-img { background-image: url('../images/globe.png'); height: 50px; width: 50px; &:hover { background-image: url('../images/globe-hv.png'); } } It works in all the latest browsers but IE. The hover pseudo state does not trigger in IE. I have found a number of questions on Stackoverflow about this, but they are all older and have not provided a solution (yet), so I figured it might be worth asking again. Note that both states have a background image defined. I have added z-index and tried an IMG tag instead of the DIV. I tried display:block and added background colors. I appreciate any new pointers. If nothing else, I will just use Javascript to add a regular CSS class upon hover.
I could be wrong since I don't have IE to test... I'm guessing the button as a wrapper is the issue. I assume the button's hover state clobbers the div's hover state. Does it work if you remove the <button>? Alternately does it work if you move the hover to the button? .globe-img { background-image: url('../images/globe.png'); height: 50px; width: 50px; } button:hover .globe-img { background-image: url('../images/globe-hv.png'); }
Does tapping an <a> tag with an iPad trigger CSS :focus status?
I have a dropdown which uses CSS a#hoverlink:hover+div to make a div to appear, then you can click a link in the div. On Android, when you click the hoverlink, it triggers :hover so this works fine. I heard this isn't working in an iPad. How can I make it work? I'm thinking using :focus as an alternative to :hover would do the job. Will clicking trigger :focus in iPad safari? I would just test it, but I don't have an iPad to try it with: a#hoverlink+div { transition: all 2s; } a#hoverlink:not(:focus)+div { visibility: hidden; opacity: 0; } the link<div> yes! focus worked, now wondering if click works<br/>click here!</div>
You can try using :target. Below is a purely CSS example. You should be able to tweek it with Javascript to your needs. Here is a reference for using :target https://css-tricks.com/on-target/ a#hoverlink+div { transition: all 2s; } a#hoverlink:not(:target)+div { visibility: none; opacity: 0; } Show The Div <div>You should see this once the link is clicked</div>
Per the comments, it sounds like the :active idea doesn't work. While IMI's :target technique would work, in my case it would conflict with JavaScript-driven use of the hash string for other purposes. So my answer is just to use JavaScript onclick to add a class which will cause the dropdown to appear.
websites for mobile apps
I'm developing a website for mobile phones (mostly Blackberry). I can't figure out how one develops like this. Some phones don;t support CSS. If I want a button with an up and a down state, how do I do it? I'd usually make an anchor, then put the image in the background, then I could control the background position with pseudo classes link and active. <a id="btnSearch"></a> #btnSearch{ height: 16px; overflow: hidden; background-image: url(img/btnSearch.png); } #btnSearch:link, #btnSearch:visited, #btnSearch:hover{ background-position:0 0; } #btnSearch:active{ background-position:0 -16px; } but I can't do this because some mobile devices will show nothing.
Well, if they don't support CSS, then you obviously can't get button effects such as those you describe. The best you can do is provide alternate text within the anchor: <a id="btnSearch"><span>some text</span></a> and hide that text in browsers that do support CSS: #btnSearch > span { display: none; }
Rollover without image change
I have a small thumbnail image that I wish to change when the mouse rolls over it. I was wondering if there was a way to do this without swapping images on the rollover. For example, through CSS could I have the opacity change on rollover? If you have any other ideas about how to manipulate the image with CSS on rollover for making a change I am open. Thanx!
You could put both images in one bigger image, use it as a background image and change only the position on roll-over.
With CSS3, there is an opacity option. This way you wouldn't be forced to reload an image when they hover above something. #div { background-image: url('blah.png'); } #div:hover { opacity: 0.5; } I'm not exactly sure if that's the right way to use it so you should use google for more examples. However, you should be careful because not all browsers might be supporting CSS3 yet.
Try using the :hover style on a tag. It may not be supported very well in early IE editions. But you can do something like: img { border: 1px solid black; } img:hover { border: 1px solid white; }
How to change background-color on text links on hover but not image links
I have a CSS rule like this: a:hover { background-color: #fff; } But this results in a bad-looking gap at the bottom on image links, and what's even worse, if I have transparent images, the link's background color can be seen through the image. I have stumbled upon this problem many times before, but I always solved it using the quick-and-dirty approach of assigning a class to image links: a.imagelink:hover { background-color: transparent; } Today I was looking for a more elegant solution to this problem when I stumbled upon this. Basically what it suggests is using display: block, and this really solves the problem for non-transparent images. However, it results in another problem: now the link is as wide as the paragraph, although the image is not. Is there a nice way to solve this problem, or do I have to use the dirty approach again? Thanks,
I tried to find some selector that would get only <a> elements that don't have <img> descendants, but couldn't find any... About images with that bottom gap, you could do the following: a img{vertical-align:text-bottom;} This should get rid of the background showing up behind the image, but may throw off the layout (by not much, though), so be careful. For the transparent images, you should use a class. I really hope that's solved in CSS3, by implementing a parent selector.
I'm confused at what you are terming "image links"... is that an 'img' tag inside of an anchor? Or are you setting the image in CSS? If you're setting the image in CSS, then there is no problem here (since you're already able to target it)... so I must assume you mean: <a ...><img src="..." /></a> To which, I would suggest that you specify a background color on the image... So, assuming the container it's in should be white... a:hover { background: SomeColor } a:hover img { background-color: #fff; }
I usually do something like this to remove the gap under images: img { display: block; float: left; } Of course this is not always the ideal solution but it's fine in most situations.
This way works way better. a[href$=jpg], a[href$=jpeg], a[href$=jpe], a[href$=png], a[href$=gif] { text-decoration: none; border: 0 none; background-color: transparent; } No cumbersome classes that have to be applied to each image. Detailed description here: http://perishablepress.com/press/2008/10/14/css-remove-link-underlines-borders-linked-images/
Untested idea: a:hover {background-color: #fff;} img:hover { background-color: transparent;}
The following should work (untested): First you a:hover { background-color: #fff; } Then you a:imagelink:hover { background-color: inherit; } The second rule will override the first for <a class="imagelink" etc.> and preserve the background color of the parent. I tried to do this without the class="", but I can't find a CSS selector that is the opposite of foo > bar, which styles a bar when it is the child of a foo. You would want to style the foo when it has a child of class bar. You can do that and even fancier things with jQuery, but that may not be desirable as a general technique.
you could use display: inline-block but that's not completely crossbrowser. IE6 and lower will have a problem with it. I assume you have whitespaces between <a> and <img>? try removing that like this: <a><img /></a>
I had this problem today, and used another solution than display: block thanks to the link by asker. This means I am able to retain the link ONLY on the image and not expand it to its container. Images are inline, so they have space below them for lower part of letters like "y, j, g". This positions the images at baseline, but you can alter it if you have no <a>TEXT HERE</a> like with a logo. However you still need to mask the text line space and its easy if you use a plain color as background (eg in body or div#wrapper). body { background-color: #112233; } a:hover { background-color: red; } a img { border-style: none; /* not need for this solution, but removes borders around images which have a link */ vertical-align: bottom; /* here */ } a:hover img { background-color: #112233; /* MUST match the container background, or you arent masking the hover effect */ }
I had the same problem. In my case I am using the image as background. I did the following and it resolved my problem: background-image: url(file:"use the same background image or color");