1

This CSS/HTML button has the exact same code as another HTML button that works correctly. It is supposed to translate 4 pixels down. The other HTML button does exactly this and despite having the exact same code the button's shadow decides to move upwards.

.back {
    font-size: 28px;
    color: #ecf0f1;
    text-decoration: none;
    border-radius: 5px;
    border: solid 1px #f39c12;
    background: #e67e22;
    text-align: center;
    -webkit-transition: all 0.1s;
    -moz-transition: all 0.1s;
    transition: all 0.1s;
    -webkit-box-shadow: 0px 6px 0px #d35400;
    -moz-box-shadow: 0px 6px 0px #d35400;
    box-shadow: 0px 6px 0px #d35400;
    font-family: "Futura";
    margin: -20px -50px;
    position: relative;
    top: 50%;
    left: 50%;
}

.back:hover {
    background: #ffad66;
    color: #ffffff;
    -webkit-box-shadow: 0px 6px 0px #e07f43;
    -moz-box-shadow: 0px 6px 0px #e07f43;
    box-shadow: 0px 6px 0px #e07f43;
}

.back:active {
    -webkit-box-shadow: 0px 2px 0px #d35400;
    -moz-box-shadow: 0px 2px 0px #d35400;
    box-shadow: 0px 2px 0px #d35400;
    transform: translateY(4px);
}
5
  • Can you provide the HTML, including the button that works as expected, and the button that does not? Commented Sep 21, 2018 at 0:35
  • <a href="google.com" class="back">Back</a> is the button that doesn't work, <input class="submit" type="submit" name="orderbtn" value="Order"></input> is the button that does. Both buttons have the exact same style coding posted originally, with the exception of different class names. Commented Sep 21, 2018 at 0:40
  • I've created the following jsFiddle - is this functioning as expected? jsfiddle.net/6bxjq7og/1 Commented Sep 21, 2018 at 0:41
  • Yes, although I copied the code and the problem still persisted. I figured it out though, I just made a button tag around the link tag and made the button a part of the class. Thank you. Commented Sep 21, 2018 at 0:45
  • you're welcome - I just provided an answer that may be a better fit for your project, which means you need not replace your link tag with the button alternative. Commented Sep 21, 2018 at 0:47

1 Answer 1

1

This is likely due to the default display:inline CSS rule that is applied to<a> elements. This will affect the vertical placement and animation behaviour of your button.

Try adding the following to your CSS: display:inline-block;

This will override the default display:inline of your back link, and should achieve the desired result:

.back {
    font-size: 28px;
    color: #ecf0f1;
    text-decoration: none;
    border-radius: 5px;
    border: solid 1px #f39c12;
    background: #e67e22;
    text-align: center;
    -webkit-transition: all 0.1s;
    -moz-transition: all 0.1s;
    transition: all 0.1s;
    -webkit-box-shadow: 0px 6px 0px #d35400;
    -moz-box-shadow: 0px 6px 0px #d35400;
    box-shadow: 0px 6px 0px #d35400;
    font-family: "Futura";
    margin: -20px -50px;
    position: relative;
    top: 50%;
    left: 50%;
    /* Add rule to specify inline-block display behavior */
    display: inline-block;
}

Alternatively, you can just use a <button> tag instead of a <a> tag. Using the button tag will mean that your original CSS will work as expected without the need for the update suggested in this answer:

<!-- 
Old approach
<a class="back">Test</a> 
-->

<!-- Alternative approach -->
<button class="back">Test</button> 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. However, I fixed the problem by creating a button tag around the anchor element, and made the class of the button .back. I will take what you said into consideration in the future though, as it could solve some problems.
@Gabe_0 please see updated answer, does this work for you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.