Achieve click event using just CSS - html

My question is: It is possible to achieve this example using only css? If not what would you do? Jsfiddle examples are really appreciated ;)
How to obtain also the slashes? Should i use an image or in css is possible? And the triangle that change when is clicked? I know it is possible to do it with Js maybe in css :after and :before would help me?
PS: Javascript to Hide Menu:
<script language="javascript">
function toggle() {
var ele = document.getElementById("toggleMenu");
var text = document.getElementById("displayMenu");
if(ele.style.display == "block") {
ele.style.display = "none";
text.innerHTML = "Menu";
}
else {
ele.style.display = "block";
text.innerHTML = "Hide";
}
}
</script>
<div class="menu-toggle"><div id="wrap"><a id="displayMenu" href="javascript:toggle();">Menu</a></div></div>
<div id="toggleMenu" style="display: none">
<div class="menu">
<ul><li> Home </li>
<li> Item </li>
</ul>
</div>
</div>

Usually I do something like this with images to achieve the click event with just css
<figure>
<img id="zoom" src="http://cargowire.net/Content/images/events/stackoverflow.jpg" alt="EE" />
<figcaption>
<ul>
<li>
Zoom In
</li>
<li>
Zoom Out
</li>
</ul>
</figcaption>
</figure>
and CSS:
figure { background: #e3e3e3; display: block; float: left;}
#zoom {
width: 0px;
-webkit-transition: width 1s;
}
#zoom:target {
width: 400px;
}
Check here: http://jsfiddle.net/dCTeW/ Maybe something similar can be done for menus too

It is perfectly possible, but only when the mouse hovers, not on click as far as I am aware. You will want to use CSS :hover states.
There is an in depth article here: http://csswizardry.com/2011/02/creating-a-pure-css-dropdown-menu/
and the demo for that article here: http://csswizardry.com/demos/css-dropdown/
If you want to use click then a small bit of jquery may help you something like:
$('.menu-item').click(function(){
$(this).find('hover-div').toggle()
})
http://api.jquery.com/toggle/
that is the Documentation for toggle which is what you need to achieve.

If you insist on using click in stead of hover, you could try to use :focus as suggested, but it would actually be a hack, and not considered correct use of HTML and css. Just for demonstration though, have a look at this: http://jsfiddle.net/9efMt/1/
As you can see, I use an input, with the :focus pseudo class and the + sibling selector. Nothing really wrong with that css, but putting the menu in an input is just not done!
I used jquery for the js in the, imo, correct example that is in the same fiddle. All i do is toggling a class when the menu link is clicked. It looks like this:
$('#menu2').click(function() {
$('#menu2-sub').toggleClass('active');
});
The css should be fairly straight forward.

Related

How to keep :active css style after click a button

Once the button is clicked I want it to stay with the active style instead of going back to normal style. Can this be done with CSS please? Im using blurb button from DIVI Theme (WordPress). Please help me!
code:
#blurb-hover.et_pb_blurb .et_pb_blurb_content
.et_pb_main_blurb_image .et-pb-icon:hover {
color: red !important; }
#blurb-hover.et_pb_blurb .et_pb_blurb_content
.et_pb_main_blurb_image .et-pb-icon:selected {
background-color: #ff4b46;
color: #fff; }
#blurb-hover.et_pb_blurb .et_pb_blurb_content
.et_pb_main_blurb_image .et-pb-icon:active {
color: white !important;
background-color: red;
width: 140px;
height: 100px; }
CSS
:active denotes the interaction state (so for a button will be applied during press), :focus may be a better choice here. However, the styling will be lost once another element gains focus.
The final potential alternative using CSS would be to use :target, assuming the items being clicked are setting routes (e.g. anchors) within the page- however this can be interrupted if you are using routing (e.g. Angular), however this doesnt seem the case here.
.active:active {
color: red;
}
.focus:focus {
color: red;
}
:target {
color: red;
}
<button class='active'>Active</button>
<button class='focus'>Focus</button>
<a href='#target1' id='target1' class='target'>Target 1</a>
<a href='#target2' id='target2' class='target'>Target 2</a>
<a href='#target3' id='target3' class='target'>Target 3</a>
Javascript / jQuery
As such, there is no way in CSS to absolutely toggle a styled state- if none of the above work for you, you will either need to combine with a change in your HTML (e.g. based on a checkbox) or programatically apply/remove a class using e.g. jQuery
$('button').on('click', function(){
$('button').removeClass('selected');
$(this).addClass('selected');
});
button.selected{
color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Item</button><button>Item</button><button>Item</button>
We're going to to be using a hidden checkbox.
This example includes one "on click - off click 'hover / active' state"
--
To make content itself clickable:
HTML
<input type="checkbox" id="activate-div">
<label for="activate-div">
<div class="my-div">
//MY DIV CONTENT
</div>
</label>
CSS
#activate-div{display:none}
.my-div{background-color:#FFF}
#activate-div:checked ~ label
.my-div{background-color:#000}
To make button change content:
HTML
<input type="checkbox" id="activate-div">
<div class="my-div">
//MY DIV CONTENT
</div>
<label for="activate-div">
//MY BUTTON STUFF
</label>
CSS
#activate-div{display:none}
.my-div{background-color:#FFF}
#activate-div:checked +
.my-div{background-color:#000}
Hope it helps!!
In the Divi Theme Documentation, it says that the theme comes with access to 'ePanel' which also has an 'Integration' section.
You should be able to add this code:
<script>
$( ".et-pb-icon" ).click(function() {
$( this ).toggleClass( "active" );
});
</script>
into the the box that says 'Add code to the head of your blog' under the 'Integration' tab, which should get the jQuery working.
Then, you should be able to style your class to what ever you need.

addclass on hover not working

I want to hover on an element (an imagemap area, actually but I made it a simple div for this example) and create an animation on a different div element. Since they're not child or sibilings I had to use java and addclass but it's not working. It looks like the trigger element is not recognized and if I hover it nothing happens
<div class="testHover">
<p>Hover me to change color</p>
</div>
<div id="timeLine">
<div id="primaGuerraMondiale">
<h2>Content</h2>
</div>
</div>
css
#primaGuerraMondiale {
background: green;
}
.animated {
color:white;
}
javascript
$('.testHover').hover(function() {
$('#primaGuerraMondiale').addClass('animated');
}, function() {
$('#primaGuerraMondiale').removeClass('animated');
});
Here is the fiddle https://jsfiddle.net/elisapessa/yzLe803n/
You need jQuery 1.9.1 and above to make it work. The code is right.
In the left hand panel in the jsfiddle, there is a section called "Add Resources". Click this, then add the URL into the field and click the + button. This will add your resource (JS or CSS) to the page. After that you click on run and check it:

making div invisible and after clicking link make it visible

I have two different div's I want to make one of them invisible and after clicking the link or button I want it to be visible and the other invisible. I don't know javascript so I know only HTML and CSS. Can I do that with only using HTML&CSS and How can I do that? Thanks.
You need to use jQuery for this.
Just add this line to your head tag:
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.2.min.js">
If your HTML is like this:
<div id="div1">This is div1</div>
<div id="div2">This is div2</div>
<button id="button1">Toggle divs</button>
CSS:
#div2 {
display:none;
}
At the bottom of your page, just before the closing tag </body> add the following JavaScript:
<script>
$("#button1").on("click", function () {
$("#div1, #div2").toggle();
}
</script>
Here's a link for a similar example:
http://api.jquery.com/toggle/#entry-examples

change the background colour of divs when clicked

I have tabs comprised of the following, When I clicked on any of the divs, I need background colour change to blue and if I clicke on other div, the previous tabs colour needs to set to original and new clicked div colour to be blue and so on:
This is my html:
<div class="zoom_controls">
<a class="db" id="prof_cpu_d" href="#" data-chart="line" data-range="1m">Real Time</a>
<a class="db" id="prof_cpu_w"href="#" data-chart="line" data-range="3m">Weekly</a>
<a class="db" id="prof_cpu_m" href="#" data-chart="line" data-range="6m">Monthly</a>
</div>
I have this:
.zoom_controls a:active {
background-color: #a6d1ff;
}
it does not seem to be working
how would I do this in css?
You need to use Javascript/jQuery to toggle the class. You can't do this with pure CSS.
Modify CSS
.zoom_controls a.active {
background-color: #a6d1ff;
}
jQuery
$('.zoom_controls a').on('click', function(event){
event.preventDefault();
$('.zoom_controls a').removeClass('active'); //Remove color for all with class .zoom_controls
$(this).toggleClass('active'); //Apply bgcolor to clicked element
});
Codepen sketch
Update
If you are, for some reason, thinking about a:hover, then you do it like this in CSS.
.zoom_controls a:hover {
background-color: #a6d1ff;
}
Otherwise, if you are looking to target the 'active' state of the link, you are doing it correctly.
The :active state only applies when the anchor is active -- that is, when the user is clicking on it. When that click ends, so does the state. What you want can't be done with pure CSS.
CSS:
.zoom_controls a.active {
background-color: #a6d1ff;
}
jQuery:
$('.zoom_controls a').on('click', function(ev) {
ev.preventDefault();
$(this).addClass('active').siblings('.active').removeClass('active');
});
JSBin demo
You can use the JS library jquery to select the active tabs and assign the the background color. The nice bit with using jquery is it's compatibility with other browsers.
It does work, but it's not doing what you want. Because active is only a dynamic pseudo-class that determines the styling of an active element (in this case any link inside the div with class zoom_controls when it's being clicked).
You might need JavaScript for this job, that or an unnecessarily complex CSS 3 solution.

How do you create a hidden div that doesn't create a line break or horizontal space?

I want to have a hidden checkbox that doesn't take up any space on the screen.
If I have this:
<div id="divCheckbox" style="visibility: hidden">
I don't see the checkbox, but it still creates a new line.
If I have this:
<div id="divCheckbox" style="visibility: hidden; display:inline;">
it no longer creates a new line, but it takes up horizontal space on the screen.
Is there a way to have a hidden div that takes up no room (vertical or horizontal?
Use display:none;
<div id="divCheckbox" style="display: none;">
visibility: hidden hides the element, but it still takes up space in the layout.
display: none removes the element completely from the document, it doesn't take up any space.
Since the release of HTML5 one can now simply do:
<div hidden>This div is hidden</div>
Note: This is not supported by some old browsers, most notably IE < 11.
Hidden Attribute Documentation (MDN,W3C)
Use style="display: none;". Also, you probably don't need to have the DIV, just setting the style to display: none on the checkbox would probably be sufficient.
Since you should focus on usability and generalities in CSS, rather than use an id to point to a specific layout element (which results in huge or multiple css files) you should probably instead use a true class in your linked .css file:
.hidden {
visibility: hidden;
display: none;
}
or for the minimalist:
.hidden {
display: none;
}
Now you can simply apply it via:
<div class="hidden"> content </div>
To prevent the checkbox from taking up any space without removing it from the DOM, use hidden attribute.
<div hidden id="divCheckbox">
To prevent the checkbox from taking up any space and also removing it from the DOM, use display: none.
<div id="divCheckbox" style="display:none">
In addition to CMS´ answer you may want to consider putting the style in an external stylesheet and assign the style to the id, like this:
#divCheckbox {
display: none;
}
To only hide the element visually but keep it in the html, you can use:
<div style='visibility:hidden; overflow:hidden; height:0; width:0;'>
[content]
</div>
or
<div style='visibility:hidden; overflow:hidden; position:absolute;'>
[content]
</div>
What may go wrong with display:none? It removes the element from the html completely, so some functionalities will be broken if they need to access the hidden element content.
Consider using <span> to isolate small segments of markup to be styled without breaking up layout. This would seem to be more idiomatic than trying to force a <div> not to display itself--if in fact the checkbox itself cannot be styled in the way you want.
Show / hide by mouse click:
<script language="javascript">
function toggle() {
var ele = document.getElementById("toggleText");
var text = document.getElementById("displayText");
if (ele.style.display == "block") {
ele.style.display = "none";
text.innerHTML = "show";
}
else {
ele.style.display = "block";
text.innerHTML = "hide";
}
}
</script>
<a id="displayText" href="javascript:toggle();">show</a> <== click Here
<div id="toggleText" style="display: none"><h1>peek-a-boo</h1></div>
Source: Here
display: none;
This should make the element disappear and not take up any space.
#divCheckbox {display: none;}
This should solve it