skip to Main Content

I have a button that should direct me to a certain page, and when I click it, it works. However, it only works when I click the actual text inside the button, not anywhere on the button itself.

I was able to find some CSS online for making a button, and I just edited it a bit, changed the color, radius, margins, and some other attributes. Here is the current CSS:

.btn {
  display: inline-block;
  cursor: pointer;
  border-radius: 10px;
  overflow: hidden;
  background-color: black;
  font-size: 1.19rem;
  text-align: center;
  padding: 12px 24px;
  margin-left: 45vw;
  margin-right: 45vw;
  border: 3px solid black;
  outline: 3px solid black;
}
<div class="btn">
  <a href="<insert-url-here>">Text Here</a>
</div>

Before I changed the CSS a bit, I could click anywhere on the button to go to the URL. However, after just changing the color and margins, which I don’t think would cause this problem, I have to click on the text to go the URL, or else the button does nothing.

2

Answers


  1. css:

    .btn {
      display: inline-block;
      cursor: pointer;
      border-radius: 10px;
      overflow: hidden;
      background-color: black;
      font-size: 1.19rem;
      text-align: center;
      padding: 12px 24px;
      margin-left: 45vw;
      margin-right: 45vw;
      border: 3px solid black;
      outline: 3px solid black;
    }
    

    html:

     <a href="<insert-url-here>" class="btn">
          Text Here
        </a>
    
    Login or Signup to reply.
  2. This will be much easier if you wrap a button component in a link (though a div would also work). That makes everything inside the a element go to that link when clicked.

    <a href='<insert-url-here>'>
        <button type='button' class='btn'>Text here</button>
    </a>
    

    This will also allow you to simplify your CSS a good bit.

    .btn {
      cursor: pointer;
      border-radius: 10px;
      font-size: 1.19rem;
      padding: 12px 24px;
      margin-left: 45vw;
      margin-right: 45vw;
      border: 6px solid black;
    }
    

    Depending on how you’re using it, you should also look into centering your element with margin: auto.

    Lastly, the default button text color is black so you’ll also want to either change the text color or change your black background, otherwise it will just look like a black box.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search