Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Onclick CSS button effect

I'm creating a CSS button and I'm trying to make an onclick effect: when the user clicks on the button it would push down the button text by 1px. My problem here is that it's pushing the whole bottom of the button. How would you proceed?

<div class="one">
    <p><a id="button" href=#>Button</a></p>
</div>

Here's the CSS:

#button {
    display: block;
    width:150px;
    margin:10px auto;
    padding:7px 13px;
    text-align:center;
    background:#a2bb33;
    font-size:20px;
    font-family: 'Arial', sans-serif;
    color:#ffffff;
    white-space: nowrap;
    box-sizing: border-box;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;

}

#button:active {
    vertical-align: top;
    padding-top: 8px;
}

.one a {
    text-decoration: none;
}
like image 993
Kimberley Furson Avatar asked Jan 23 '14 15:01

Kimberley Furson


People also ask

How do I create a button press effect in CSS?

We can use CSS transform property to add a pressed effect on the button when it is active. CSS transform property allows us to scale, rotate, move and skew an element.

Can we use OnClick in CSS?

To use CSS onClick, you'll essentially need to create a pseudo class. You'll use CSS selectors and the checkbox hack to produce something like an OnClick function. And you can get away with this if you just want to use only CSS to make some minor changes, like border width or border radius.

How do I change the color of a clicked button in CSS?

To change the button color on click in CSS, the “:active” pseudo-class can be used. More specifically, it can represent the button element when it gets activated. Using this class, you can set different button colors when the mouse clicks on it.

How do you keep active CSS style after clicking?

How do I keep an active CSS style after clicking an element? The :active selector is used to select and style the active link. A link becomes active when you click on it. The :active selector can be used on all elements, not only links.


2 Answers

You should apply the following styles:

#button:active {
    vertical-align: top;
    padding: 8px 13px 6px;
}

This will give you the necessary effect, demo here.

like image 119
Vangel Tzo Avatar answered Sep 17 '22 15:09

Vangel Tzo


Push down the whole button. I suggest this it is looking nice in button.

#button:active {
    position: relative;
    top: 1px;
}

if you only want to push text increase top-padding and decrease bottom padding. You can also use line-height.

like image 28
Zuhair Avatar answered Sep 20 '22 15:09

Zuhair