Top method for transforming an HTML button into a hyperlink

Having some trouble with my code here. I currently have a Button that has an arrow hack, but I want it to display the link on the status bar instead. However, simply changing it to a href link doesn't work as expected. How can I convert this to a href link while still keeping the arrow? Here is my code snippet:

JSFIDDLE

HTML:

<button type="button" class="btn-lg btn-default my-button my-button-active">My Button</button>

CSS:

.my-button {
    color: #fff ;
    background-color: #444346;
    font-size:15px;
    padding: 20px 0px;
    border:none;
    margin-right:20px;
    position: relative;
    outline:0;
    width:31%;
}


.my-button-active:after {
    content:'';
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -10px;
    width: 0;
    height: 0;
    border-top: solid 7px #444346;
    border-left: solid 7px transparent;
    border-right: solid 7px transparent;
}

.my-button-active:hover:after {
    content:'';
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -10px;
    width: 0;
    height: 0;
    border-top: solid 7px #e6e6e6;
    border-left: solid 7px transparent;
    border-right: solid 7px transparent;
}

Answer №1

To make it work, you can simply change the button to an anchor tag and include either display: block; or display: inline-block; in the .my-button class.

Check out this demonstration on JSFiddle: http://jsfiddle.net/samirkumardas/60n0ruyj/2/

Answer №2

Convert your HTML code to an anchor tag:

<a class="btn-lg btn-default my-button my-button-active" href="">Click here

Update the .my-button selector with:

.my-button {
    display: block;
    position: relative;
    color: #fff;
    background-color: #444346;
    font-size:15px;
    padding: 20px 0;
    text-align: center;
    border: none;
    margin-right: 20px;
    outline: 0;
    width: 31%;
}

Answer №3

Simply update your button to an anchor tag and make adjustments to your CSS.

div {
  margin: 30px;
  
}

a.my-button {
  color: #fff ;
  background-color: #444346;
  font-size:15px;
  padding: 20px 0px;
  border:none;
  margin-right:20px;
  position: relative;
  outline:0;
  width:31%;
  display: inline-block;
  text-align: center;
}

.my-button:hover {
  text-decoration: none;
}


.my-button-active:after {
  content:'';
  position: absolute;
  top: 100%;
  left: 50%;
  margin-left: -10px;
  width: 0;
  height: 0;
  border-top: solid 7px #444346;
  border-left: solid 7px transparent;
  border-right: solid 7px transparent;
}

.my-button-active:hover:after {
  content:'';
  position: absolute;
  top: 100%;
  left: 50%;
  margin-left: -10px;
  width: 0;
  height: 0;
  border-top: solid 7px #e6e6e6;
  border-left: solid 7px transparent;
  border-right: solid 7px transparent;

}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<div>
  <a href="#" type="button" class="btn-lg btn-default my-button my-button-active">My Anchor Button that directs to another page</a>
</div>

Answer №4

When you switch from using a <button> element to an <a> element, it’s crucial to remember to add the CSS property display: block; to ensure proper formatting. This is because <a> tags are typically inline elements and don’t naturally support padding and margins.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tap, swipe the inner contents of a <div> element without being inside it

(Make sure to check out the image below) I'm facing an issue with a <div> on my website. It's not taking up the full width of the page, and the scrolling functionality within the <body> is not working as expected. I just want the cont ...

changing the visible style of a div element using JavaScript

I'm having an issue with a link on my webpage that is supposed to show or hide a <div id="po" style="display:none;">some html</div> element. The first time I click the link, the div displays properly. However, after tha ...

Setting the CSS background-position to be fixed relative to its parent element

I have a question that may be a bit challenging. I'm not entirely sure if it's possible, but I feel like I remember seeing or doing something similar in the past. I am currently working on creating a horizontal menu using a div block sized 980x36 ...

Troublesome Outlook display problem with responsive email template

Could someone take a look at the following code snippets and point out any errors that I may have made? While testing my template on litmus, everything appears to be fine. However, when I send a test email to my outlook account, the template displays ver ...

How can I create responsive buttons with icons using material-ui?

I'm working with a material-ui Button that has a startIcon, and I need to hide the button text on smaller screens while still showing the icon. One common solution would be to use the useMediaQuery hook to identify the browser size and render a diffe ...

Text within Twitter Bootstrap colliding with each other

I am having trouble creating a large text header for my website using Twitter Bootstrap. The span12 column is not adjusting to the size of the text as I would like it to. When I resize the browser window, instead of the text wrapping onto a new line, the ...

Building an audio volume meter in Reactjs: A step-by-step guide

Looking for suggestions on creating a visually appealing volume meter bar using Reactjs and Material ui. I have a basic implementation with LinearProgress, but now I want to upgrade it with a new design. Any tips on how to achieve this using Material-ui? A ...

Adjust the $scope variable depending on the size of the screen

Currently, I have a sidebar that collapses based on the value in $scope, specifically when $scope.open is set to true. However, I've noticed that if the menu is left open and viewed on mobile devices or small windows, it can look quite messy. To addre ...

What causes the z-index value to stay the same even after it has been modified using the .css() function?

The z-index property defaults to auto, as explained in the MDN documentation. Despite setting the z-index to 1 using the following code: $("body").css("z-index", 1); Upon retrieving the value of z-index with this code: $("body").css("z-index"); The re ...

attempting to utilize staggerTo function in GSAP

Struggling to animate my images with greensock so they start from bottom: 0, have opacity 1, and stagger in. My code seems valid but I must be missing something after looking at it for so long. I suspect the find() function is not returning what I expect ...

Issue: ng-select dropdown content is not visible when clicking the down arrow after dynamically searching for a result

Currently, I have implemented a ng-select dropdown for dynamically searching data. However, one issue that I've encountered is that when the search results are displayed and you click on the down arrow, the content is not visible as expected. You ca ...

Finding the index number of a DIV element when it is clicked

Here is an example of the HTML structure I am working with: <div id="preview-wrapper"> <div class="dz-preview dz-image-preview"> <a class="rotate-a" href="javascript:void(0);"> <img class="rotate" src="public/a ...

Create a visual layout showcasing a unique combination of images and text using div elements in

I created a div table with 3 images and text for each row, but I'm struggling to make all the text-containing divs uniform in size. How can I achieve this? Also, I need to center this table on the screen, and currently using padding to do so. Is there ...

Offspring containers fail to adjust in size relative to their parent container

I am working with a parent div that contains 5 child divs. The parent div does not fill the entire width of the window, but the 5 children share the same space and properly fill their parent. <div class="parent"> <div class="child ...

Angular 6 CSS spacing dilemmas

We recently made the switch from Angular 5 to Angular 6 and noticed that there is no spacing between the buttons/icons, etc. We are looking to bring back the spaces between the buttons and icons. I have recreated the issue below. As you can see in the Ang ...

Text gently appearing and disappearing within a block

I’m looking to create a smooth fade effect for the content inside a block that is being dynamically changed by script. Instead of an instant change, I want the old content to fade out and the new content to fade in gradually without using jQuery — just ...

Text overlay on image sliders inside div containers

Is there a way to display div elements on top of an image slider, rather than behind it? I've tried using z-index and position: absolute with no success. Here is an example: HTML: <div id="header"> <img src="assets/images/header1.png" / ...

The issue of footer overlapping the login form is observed on iOS devices while using Safari and Chrome

Unique ImageI am currently working on an Angular 8 project with Angular Material. I have successfully designed a fully functional login page. However, I am encountering a problem specifically on iOS devices such as iPhones and iPads, whether it is Safari o ...

Troubleshooting Website Navigation Bar and Layout with HTML, CSS, and Bootstrap5

I am facing a challenge with navbar positioning. My navbar items are missing after adding the image below. I have attempted to adjust the element positions but to no avail :( I also tried to create a fixed-top navbar, but it didn't work either :( I ...

stop the leakage of CSS and JS from the subtree to the document through the inverse shadow DOM mechanism

My page contains dynamic HTML content that I want to incorporate. The dynamic content consists of only HTML and CSS, without any JavaScript. However, I have some custom global CSS styles and JS logic that need to be implemented along with this dynamic con ...