Can I have an onclick effect in CSS?

后端 未结 12 1707
悲&欢浪女
悲&欢浪女 2020-11-21 06:42

I have an image element that I want to change on click.


This works:

#btnLeft:hover {
    width:7         


        
相关标签:
12条回答
  • 2020-11-21 07:13

    Warning! Particularly simple answer below! :)

    You actually can have a change that persists (such as a block/popup that appears and stays visible after a click) with only CSS (and without using the checkbox hack) despite what many of the (otherwise correct) answers here claim, as long as you only need persistence during the hover.

    So take a look at Bojangles and TylerH's answers if those work for you, but if you want a simple and CSS only answer that will keep a block visible after being clicked on (and even can have the block disappear with a followup click), then see this solution.

    I had a similar situation, I needed a popup div with onClick where I couldn't add any JS or change the markup/HTML (a truly CSS solution) and this is possible with some caveats. You can't use the :target trick that can create a nice popup unless you can change the HTML (to add an 'id') so that was out.

    In my case the popup div was contained inside the other div, and I wanted the popup to appear on top of the other div, and this can be done using a combination of :active and :hover:

    /* Outer div - needs to be relative so we can use absolute positioning */
    .clickToShowInfo {
        position: relative;
    }
    /* When clicking outer div, make inner div visible */
    .clickToShowInfo:active .info { display: block; }
    /* And hold by staying visible on hover */
    .info:hover {
        display: block;
    }
    /* General settings for popup */
    .info {
        position: absolute;
        top: -5;
        display: none;
        z-index: 100;
        background-color: white;
        width: 200px;
        height: 200px;
    }
    

    Example (as well as one that allows clicking on the popup to make it disappear) at:

    http://davesource.com/Solutions/20150324.CSS-Only-Click-to-Popup-Div/

    I've also inserted a code snippet example below, but the positioning in the stackoverflow sandbox is weird so I had to put the 'click here' text after the innerDiv, which isn't normally needed.

    /* Outer div - needs to be relative so we can use absolute positioning */
    	.clickToShowInfo {
    		position: relative;
    	}
    	/* When clicking outer div, make inner div visible */
    	.clickToShowInfo:active .info { visibility: visible; }
    	/* And hold by staying visible on hover */
    	.info:hover {
    		visibility: visible;
    	}
    	/* General settings for popup */
    	.info {
    		position: absolute;
    		top: -10;
    		visibility: hidden;
    		z-index: 100;
    		background-color: white;
    		box-shadow: 5px 5px 2px #aaa;
    		border: 1px solid grey;
    		padding: 8px;
    		width: 220px;
    		height: 200px;
    	}
    	/* If we want clicking on the popup to close, use this */
    	.info:active {
    		visibility: hidden;	/* Doesn't work because DCEvent is :active as well */
    		height: 0px;
    		width: 0px;
    		left: -1000px;
    		top: -1000px;
    	}
    <p />
    <div class="clickToShowInfo">
    	<div class="info">
    		Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua
    	</div>
    	Click here to show info
    </div>
    <p />

    0 讨论(0)
  • 2020-11-21 07:15

    Edit: Answered before OP clarified what he wanted. The following is for an onclick similar to javascripts onclick, not the :active pseudo class.

    This can only be achieved with either Javascript or the Checkbox Hack

    The checkbox hack essentially gets you to click on a label, that "checks" a checkbox, allowing you to style the label as you wish.

    The demo

    0 讨论(0)
  • 2020-11-21 07:16

    I have the below code for mouse hover and mouse click and it works:

    //For Mouse Hover
    .thumbnail:hover span{ /*CSS for enlarged image*/
        visibility: visible;
        text-align:center;
        vertical-align:middle;
        height: 70%;
        width: 80%;
        top:auto;
        left: 10%;
    }
    

    and this code hides the image when you click on it:

    .thumbnail:active span {
        visibility: hidden;
    }
    
    0 讨论(0)
  • 2020-11-21 07:17

    The closest you'll get is :active:

    #btnLeft:active {
        width: 70px;
        height: 74px;
    }
    

    However this will only apply the style when the mouse button is held down. The only way to apply a style and keep it applied onclick is to use a bit of JavaScript.

    0 讨论(0)
  • 2020-11-21 07:18

    Okay, this maybe an old post... but was first result in google and decided to make your own mix on this as..

    FIRSTLY I WILL USE FOCUS

    The reason for this is that it works nicely for the example i'm showing, if someone wants a mouse down type event then use active

    THE HTML CODE:

    <button class="mdT mdI1" ></button>
    <button class="mdT mdI2" ></button>
    <button class="mdT mdI3" ></button>
    <button class="mdT mdI4" ></button>
    

    THE CSS CODE:

    /* Change Button Size/Border/BG Color And Align To Middle */
        .mdT {
            width:96px;
            height:96px;
            border:0px;
            outline:0;
            vertical-align:middle;
            background-color:#AAAAAA;
        }
        .mdT:focus {
            width:256px;
            height:256px;
        }
    
    /* Change Images Depending On Focus */
        .mdI1       {   background-image:url('http://placehold.it/96x96/AAAAAA&text=img1');     }
        .mdI1:focus {   background-image:url('http://placehold.it/256x256/555555&text=Image+1');   }
        .mdI2       {   background-image:url('http://placehold.it/96x96/AAAAAA&text=img2');     }
        .mdI2:focus {   background-image:url('http://placehold.it/256x256/555555&text=Image+2');   }
        .mdI3       {   background-image:url('http://placehold.it/96x96/AAAAAA&text=img3');     }
        .mdI3:focus {   background-image:url('http://placehold.it/256x256/555555&text=Image+3');   }
        .mdI4       {   background-image:url('http://placehold.it/96x96/AAAAAA&text=img4');     }
        .mdI4:focus {   background-image:url('http://placehold.it/256x256/555555&text=Image+4');   }
    

    JS FIDDLE LINK: http://jsfiddle.net/00wwkjux/

    So why am i posting this in an old thread, well because the examples here vary and i thought to provide one back to the community which is a working example.

    As already answered by the thread creator, they only want the effect to last during the click event. Now while this is not exact for that need, its close. active will animate while the mouse is down and any changes that you need to have last longer need to be done with javascript.

    0 讨论(0)
  • 2020-11-21 07:29

    If you give the element a tabindex then you can use the :focus pseudo class to simulate a click.

    HTML

    <img id="btnLeft" tabindex="0" src="http://placehold.it/250x100" />
    

    CSS

    #btnLeft:focus{
        width:70px;
        height:74px;
    }
    

    http://jsfiddle.net/NaTj5/

    0 讨论(0)
提交回复
热议问题