How to set multiple conditions on css3 - css

I'm trying to write script on css that display a div on click and hide another div at same time.
CSS:
.box{
display:none
}
.box:target{
display:block;
}

You can't, because CSS doesn't listen for client-side clicks. The closest you'll get is :active, which registers when the mouse button is held down.
You can, however, do this with the Checkbox Hack: http://tympanus.net/codrops/2012/12/17/css-click-events/
1 Wrap an element in a checkbox
2 Use CSS like this: input[type=checkbox]:checked ~ #IDOfElementWrappedInCheckbox
And, also on the link,
The :target Way:
'There is another way, well known to “fake” a click event with CSS, using the :target pseudo-class. This pseudo-class is quite similar to the :hover one in the way that it matches only a specific scenario.
The special event for the :target pseudo-class depends on what we call a “fragment identifier”. To put it simple, this pseudo-class refers to a hashtag you can see sometimes at the end of the URL. So it matches when the hashtag and the ID of an element are the same.
The HTML
Click me!
<p id="id" class="to-be-changed">I'm going to be red! It's gonna be legen... Wait for it...</p>
The CSS
.to-be-changed {
color: black;
}
.to-be-changed:target {
color: red;
}
Basically, when clicking on the link (href="#id"), the URL changes and you go to the anchor #id in the page. In this very moment, the element having this id can be targeted with the :target pseudo-class.'
'and the :focus way (also on the link):
THE :FOCUS WAY
Let’s continue with another way using a pseudo-class; the :focus one this time. It’s pretty much the same idea, except, it doesn’t expect a URL change. It relies on the user’s focus on a particular element.
When you’re on a web page, you can press the tab key to navigate through various elements on the page. It’s particularly useful when filling forms, to go from one field to another without having to use the mouse. It’s also used by blind or visually impaired people to navigate through a site.
What’s important to note is that some elements can be focused, like links, inputs and such, and some other can’t, like paragraphs, divisions, and plenty others. Actually they can, but you’ll need to add the tabindex attribute with a numeric value.
How it works
The HTML
<span tabindex="0">Click me!</span>
<p class="to-be-changed">I'm going to be red! It's gonna be legen... Wait for it...</p>
The CSS
span:focus ~ .to-be-changed {
color: red;
}
So, when you click on the span or reach it with the tab key, it becomes focused and matches the :focus pseudo-class. The adjacent sibling selector does the rest. Pretty easy, right? If you don’t want to mess with the tabindex for any reason, you can simply use a link with a # href. It will work like a charm as well.'
The last thing on the link,
'
The Transition Hack
This is probably the most wicked way to handle a click event in CSS. Seriously guys, this is madness. This technique comes from Joel Besada and has always been one of my favorite CSS tricks.
The idea is to store a CSS style in a CSS transition. Yeah, you read it right, a CSS transition. Actually, the idea is pretty simple. It relies on applying a pseudo-infinite delay to a change in order to prevent it to get back to the default value. It may sound complicated but it’s fairly easy, trust me. Please have a look at the code.
How it works
The HTML
<span>Click me!</span>
<p class="to-be-changed">I'm going to be red! It's gonna be legen... Wait for it...</p>
The CSS
.to-be-changed {
transition: all 0s 9999999s;
}
span:active ~ .to-be-changed {
transition: all 0s;
color: red;
}
The idea behind the first declaration is to delay any change to approximately 116 days to make sure the changes will stay once they’ve been set. It is not infinite, but kind of, right?
But we don’t want to apply the changes 116 days after clicking, we want it to be set immediately! So the idea is to override the delay during the click (:active pseudo-class) to apply the changes. Then when the click will be released, the old transition property will kick back in, setting back the delay to 9999999s, preventing the changes to going back to the default state.'

i turn my comment into a short answer.
For youger browser you might do it in CSS.
.
shy , :focus ~ .show {
display:none;
}
:focus ~ .shy {
display:block;
}
:focus { /* toggle hide/show) just loosing focus on click */
pointer-events:none;
}
<p tabindex="0">click to hide/show (toggle) next content</p>
<div class="show"> shown if no click</div>
<div class="shy">shown if clicked</div>
http://codepen.io/gc-nomade/pen/oxybl

Related

Differentiate between :focus via tab-key and :focus via click in CSS

Although I am quite certain that the answer to my question will be "Can't be done" I'd like to be sure and ask you guys here.
I have a rather typical scenario in which I want to enable tabbing through my website (i.e. using tab key on keyboard). The item the user has just tabbed upon should be marked visually via CSS. So far, so good. This, obviously, demands the focus-pseudo class:
a {
color: #000;
&:hover {
color: lighten(#000, 10%); // discreet change
}
&:focus {
background-color: green; // extreme change
}
}
But I want to apply this style solely when the user tabs through the page. When the user hovers or clicks an element the style should be something different.
Example: A user hovers or clicks an anchor. Then the visual aid can be discreet because the user already knows which element he has interacted upon. But when he tabs through the page he can not be so sure and thus the styling should be more drastic.
The problem I am having is: An element gets the focus-styles applied on on both tabbing the page and clicking on it.
Is there a CSS-only way to apply styles solely when an element got focused via tabbing?
Again, I am pretty sure that this is not possible, but just to be sure I have asked the question.
There's a new CSS selector :focus-visible that is intended to solve this scenario by targeting only the elements that were focused via keyboard input.
This is only supported natively in Firefox today, however there is a polyfill that makes this possible in all browsers through a .focus-visible class name.
The :focus pseudo-class does not discriminate based on how the element entered focus in the first place. So indeed, this is not possible with just CSS. At the very least you'd need to annotate the element on focus via an event handler.
The :hover and :active pseudo-classes won't be of any help here since the former only applies when the mouse pointer is on the element and the latter only applies when the mouse button is down, i.e. neither state persists the way :focus does, since an element remains in focus even after the mouse pointer has left the element, making it indistinguishable from an element that received focus via tabbing.
Just so to list this as an alternative answer (which I also chose, tbh):
One can also work with javascript to disable the mousedown-event. This event is not really that useful, the click-event still works and it prevents the clicked element from getting the focus-state. This, in turn, makes using the :focus property in CSS useful again as it now only triggers when the user navigates that element via tab key.
I didn't have any luck with pure CSS so I wrote a simple fiddle:
var allowTabFocus = false;
$(window).on('keydown', function(e) {
console.log(e);
$('*').removeClass('tab-focus');
if(e.keyCode === 9) {
allowTabFocus = true;
}
});
$('*').on('focus', function() {
if(allowTabFocus) {
$(this).addClass('tab-focus');
}
});
$(window).on('mousedown', function() {
$('*').removeClass('tab-focus');
allowTabFocus = false;
})

Can't change color property, although the selector is working

I've the following problem, I'm trying to change the color of the text of a "< li>" element, in joomla menu. I give the menu a link to css selector called blueMenu, this is my CSS regarding the class:
.blueColor {
color: blue;
}
However this doesn't change the color of the text, on the other hand if I change "color" with "background-color" the background of the text becoms blue. Any idea what may causing the problem?
You dont give much information, but it might be that the li has a child element inside that its overwriting the li styling, make sure you using the style on the last child.
You can also force it with !important;
.blueColor {
color: blue!important;
}
This really much depends on your template.
As already said, reasons can be inline-styles, or may more "distinct" declarations.
If you just specify the class like you did in .blueColor this will be treated with a lower priority as e.g. li.blueColor or to get even more clear both with be treated with a lower priority as e.h. #someId.andClass .subElementClass li.blueColor a.thisIsWhatIsReallyBlue
This is more about CSS specifications than a Joomla-Problem though.
You might check the style that is really applied by just launching your Development-Tools of your webbrowser (for Chrome simply press F12 or right-click on the element and inspect the element directly)
The CSS-Section on the right side might tell you about what really makes the item become blue ;)
Oh, and just a note:
As already mentioned you can use !important to "force" the styles to be applied, but if this is not absolutely necessary, i'd suggest to find the way to override this style on a clean way, since !important, if used to often, might result in a complete mess of your stylesheet.
regards
I'm not familiar with joomla but it may be inserting an inline style to whatever element you're trying to style. Right click on the element and use inspect element (firefox) or just inspect (chrome) to see if any styles were applied.
It'll look like <div class="" style="color: blue;">

Disable any :hover, :focus and :active css from being applied

I'm using jQuery Mobile to develop some html5 apps, and its a real pain to manually override every single small thing that gets applied to elements on hover, focus and active.
Is there some way to disable the application of these effect, across the board?
If there only were a way to do something like this in CSS?
*:focus, *:hover, *:active {
return; // this would stop any css effects on these events to be applied
}
You can "disable" some styles pretty easily but most will just be a ton of cat and mouse for you to make sure there's no styling applied.
You will need to cover specificity cases if something like an anchor tag might have cursor: pointer and text-decoration: underline as defaults while most other styles will not have this.
Although I recommend against this, if you need to make sure this works will least amount of work you can try adding the !important after each property like below:
*:hover {
outline: none !important;
}
In case you're looking for a "clean" slate to start with, you can use Normalize, and this will reset most of your styles and help them look nice in case browsers are styling them.

Adding CSS styles to everything but a focused textarea

Only CSS please!
Basically I want to apply some styles to everything on the page (or almost everything) except for a certain textarea when that certain textarea is :focused.
So, when I focus on the textarea, everything else gets an opacity: 0 or something like that.
I tried fiddling with :not() but I couldn't get it on quite work.
I also might want to expand this to say: apply some styles to everything on the page (or almost everything) except for a certain div when a certain textarea is :focused.
This is kinda an overcomplicated example I was trying to learn from:
http://tympanus.net/codrops/2012/01/09/filter-functionality-with-css3/
There is no way to ascend the DOM hierarchy using CSS, so what you'd want to do is make sure that the relevant textarea is a sibling of the container for anything that you want to have fade out. At that point you should be able to do something like:
textarea:focus ~ section.toFade {
opacity: 0.1;
}
It's an ugly fragile solution (bound by the limitations of CSS), so hopefully this is just an academic exercise.
The closest I could figure was:
*:not(textarea) {
color:red !important;
}
As *:not(textarea:focus) seems to break it.

Confused by CSS pseudo-class :active

I was looking here at CSS :active Selector.
The :active selector styles links to
active pages
That got me thinking, what the heck is an 'active page' in HTML/CSS terminology...
At this point I went to the w3docs Section : 5.11.3 The dynamic pseudo-classes: :hover, :active, and :focus.
The :active pseudo-class applies while
an element is being activated by the
user. For example, between the times
the user presses the mouse button and
releases it.
So I used one of the w3shools try it pages and hack together an example, substituting the following code, which you can just cut & paste and try.
<html>
<head>
<style type="text/css">
:focus,:active
{
outline-offset: 10px;
outline: solid;
}
</style>
</head>
<body>
<p>Click the links to see the background color become yellow:</p>
w3schools.com
wikipedia.org
<button type="button">Click Me!</button>
<form>
<input type="text"/>
</form>
</body>
</html>
The form field works for :focus. But the button or links don't work for :active.
Why is that? Is there something about 'active page' I'm not understanding that w3schools talked about.
I saw this nice tip when Googling for it, but I don't think it's related.
There is no concept of "active page" in CSS. In fact, the SitePoint Reference debunks this by saying:
The pseudo-class does not signify a link to the active, or current, page—that’s a common misconception among CSS beginners.
What the spec says is right: :active simply styles elements that are activated, e.g. clicked (as in the example given) or in some other way triggered such that the browser starts navigating to the link's target.
Note that it doesn't just apply to <a> elements; it may apply to any non-form-input element that's clicked. For instance, you can do this:
p:active {
color: red;
}
And any paragraph you click will flash its text red.
Note however that the exact definition and implementation is left up to the browser, but in general, you can rely on <a> elements having an activated state.
:active is the style given to an element (a or a button, for example) when the mouse is held down over it. You might have seen it visible on some sites when you click a styled button; when you actually click the button, it might change. This is the :active pseudo-class.
I've always used :active for links. The split second before the browser takes you to the page you just clicked on, the text would change to the color you called in a:active{ ... }
Example:
a:active { color:pink; font-weight:bold; }
Most browsers support it, but it's really not worth your time to style it. Back in the day of 56k dial up it was a nice thing to have to visually show that the link the user clicked was being loaded.

Resources