skip to Main Content

I am struggling to fix a problem in my HTML code, so I made a nice looking button using div command, class attribute and editing it in CSS, but the problem is I can click outside the width of the button, and it works, it opens the link, I can’t see the button, I mean I can see it but it’s width 300px not long as the screen, any help will be appreciated, thanks.

Here’s the HTML code just in case needed.

<a href="https://youtu.be/rMLzeRghtvY">
        <div class="rb">
            <h3>
                Razer Blade 14
            </h3>
        </div>
    </a>

Here’s the CSS code.

.rb {
    margin-top: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 200px;
    background-color: black;
    margin-left: auto;
    margin-right: auto;
    transition: 0.25s;
    border-style: inset;
    border-color: #00ff00;
    width: 100%;
    max-width: 800px;
    pointer-events: none;
}

.rb:hover {
    background-color: #222222;
    transition: 0.25s;
    border-color: #00cc00;
}

h3 {
    font-family: 'Courier New', Courier, monospace;
    font-size: 72px; color: #00ff00;
    text-align: center;
}

I tried making a button that will take me to an youtube link when clicking it, but the problem is I can click outside the width of the button, for the height it’s fine, I can only click inside the height of the button but the clickable width is really long but the visual width is just 800px.

3

Answers


  1. If you make a parent the flex item, you’ll lose the click issue.

    .makeflex{
      display: flex;
      align-items: center;
        justify-content: center;
    }
    .rb {
        margin-top: 50px;        
        height: 200px;
        background-color: black;
        margin-left: auto;
        margin-right: auto;
        transition: 0.25s;
        border-style: inset;
        border-color: #00ff00;
        width: 100%;
        max-width: 800px;
        pointer-events: none;
    }
    
    .rb:hover {
        background-color: #222222;
        transition: 0.25s;
        border-color: #00cc00;
    }
    
    h3 {
        font-family: 'Courier New', Courier, monospace;
        font-size: 72px; color: #00ff00;
        text-align: center;
    }
    <div class="makeflex">
    <a href="https://youtu.be/rMLzeRghtvY">
            <div class="rb">
                <h3>
                    Razer Blade 14
                </h3>
            </div>
        </a>
    </div>
    Login or Signup to reply.
  2. I don’t understand what your issue is. Are you asking why the entire div is an active link when you have the div inside the a anchor? Maybe you want the anchor to be only inside the h3?

    .rb {
        margin-top: 50px;
        display: flex;
        align-items: center;
        justify-content: center;
        height: 200px;
        background-color: black;
        margin-left: auto;
        margin-right: auto;
        transition: 0.25s;
        border-style: inset;
        border-color: #00ff00;
        width: 100%;
        max-width: 800px;
        pointer-events: none;
    }
    
    .rb:hover {
        background-color: #222222;
        transition: 0.25s;
        border-color: #00cc00;
    }
    
    h3 {
        font-family: 'Courier New', Courier, monospace;
        font-size: 72px; color: #00ff00;
        text-align: center;
    }
    <h3 class="rb">
      <a href="#">Razer Blade 14</a>
    </h3>
    Login or Signup to reply.
  3. try this:

    .rb {
      margin-top: 50px;
      display: block;
      margin-left: auto;
      margin-right: auto;
      width: 100%;
      max-width: 800px;
      box-sizing: border-box;
    }
    
    h3 {
      font-family: 'Courier New', Courier, monospace;
      font-size: 72px;
      margin: 0;
    }
    
    
    .rb a {
      display: block;
      background: black;
      line-height: 44px;
      padding: 75px;
      border: 3px solid #00ff00;
      box-sizing: border-box;
      text-align: center;
      color: #0f0;
      text-decoration: 1px underline #551a8b;
      transition: 0.25s;
    }
    
    .rb a:hover {
      background-color: #222222;
      border-color: #00cc00;
    }
    <div class="rb">
    <h3>
    <a href="https://youtu.be/rMLzeRghtvY">
            
                
                    Razer Blade 14
                
            
        </a>
        </h3>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search