Button link section for card placement

The shaded region needs to be an <a> hyperlink. The dark area contains a <button> nested within the shaded region. This button should have an onClick event.

I would like to be able to click on any part of the shaded region to access the link attached to the <a> tag. When clicking on the dark area, I want the button action to be triggered. What is the recommended and accessible method to achieve this?

https://i.sstatic.net/ffbAZ.png

Answer №1

What do you think of this custom styling?

<style>
    .container{
      position: relative;
    }
    
    a{
      display: block;
      width: 300px;
      height: 150px;
      background-color: lightgray;
      border-radius: 1rem;
    }
    
    button{
      position: absolute;
      bottom: .5rem;
      left: .5rem;
      padding: .5rem;
      background-color: black;
      border-radius: .5rem;
      color: white;
    }
</style>

<div class="container">
   <a href="https://example.com/" target="_blank"></a>
   <button type="button" onclick="alert('Clicked!')">Click Me</button>
</div>

Check it out here: https://example.com/style-example

Answer №2

If you want to achieve this using JavaScript, as suggested by @ChrisG, you can utilize the .stopPropagation() or .preventDefault() methods.

Alternatively, for a CSS-only solution, you could create a card container within an anchor tag and a button like so:

<div class="card-container">
   <a href="#"></a>
   <button></button>
</div>

To position the button above the anchor tag, set the container's position: relative; and the button's position:absolute;. You may also need to adjust the z-index property to ensure it is displayed above.

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

JavaScript horizontal slider designed for a seamless user experience

Currently, I am in the process of developing a slideshow that includes thumbnails. While my slideshow is functional, I am facing an issue with limited space to display all thumbnails horizontally. I am considering implementing a horizontal slider for the t ...

Unable to populate dropdown menu with Firebase information

I am in the process of populating a dropdown menu with data from Firebase using Angular 9.1.9. Any help would be greatly appreciated. category.service.ts constructor(private db: AngularFireDatabase) { } getCategories() { return this.db.list(&apos ...

Encountering a React js error while in production mode, Minified React error #301

We are encountering an error on our Production environment that we are unable to resolve. The error reads: react-dom.production.min.js:216 Error: Minified React error #301. For more information, please visit here. Despite the fact that our code does not ...

The mobile device is not displaying the navigation menu (responsive) and columns correctly

@media only screen and (max-width: 960px) { .flexslider { margin-top: 100px; } .main-header { padding: 15px 0; } .main-header .logo { margin-top: 20px; } .service-item, .team-member { margin-bottom: 50px; } .our-skills ...

Display the information stored in an array onto the console using console.log in Angular with Typescript

.html file <div class="container" *ngFor="let info of (this.info || [])"> <h5 class="card-title" (click)="getInfo()">{{info.paragraph1}}</h5> </div> .ts file getInfo () { *****console.lo ...

When all y values are zero, Highcharts.js will shift the line to the bottom of the chart

Is there a way to move the 0 line on the y axis to the bottom of the chart when all values are 0? I attempted the solution provided in this post without success. Highcharts: Ensure y-Axis 0 value is at chart bottom http://jsfiddle.net/tZayD/58/ $(functi ...

Does an inverted v-cloak exist?

As stated in the VueJS documentation, the v-cloak directive serves to conceal uncompiled mustache bindings until the Vue instance is fully prepared. This means that a particular element, such as a div, can be hidden and only displayed once Vue is ready. I ...

The problematic max-width with srcset attribute

Here's the HTML markup I'm working with: <picture> <source srcset="img/home-page/placeholders/placeholder_2560.jpg" media="(max-width: 2560px)"> <source srcset="img/home-page/placeholders/placeh ...

Injecting a single dependency for all routes using $routeProvider

Here is the code snippet: $routeProvider .when("/page1", { controller: "MyController", resolve: {Strategy: "StrategyOne"}}) The code above waits for the resolution of the Strategy dependency before instantiating the "MyController" controller. In ...

Is there a way to manipulate a DOM element using a component?

import { FlagIcon, ChartIcon, ShareIcon, ConnectIcon, HearIcon, AnalyticsIcon, AddIcon, RemoveIcon, } from "../../icons/Icons"; import "./Sidebar.scss"; import ReactDOM from "react-dom"; const Sidebar = () =&g ...

Unable to utilize MUI Dialog within a ReactDOMServer.renderToStaticMarkup() call

I recently started using the DIALOG component for the first time, expecting it to seamlessly integrate into my setup. However, much to my disappointment, it did not work as expected. After spending a considerable amount of time troubleshooting the issue, I ...

The combination of various background patterns and varying levels of color transparency

Trying to solve a problem here. I have a basic website with a repeating pattern background, and I'm attempting to create a color overlay using multiple backgrounds in CSS. Here's the code I'm using: html,body { background: rgba(200, 54, 5 ...

I'm having trouble with jQuery recognizing the left-margin property. Is there a workaround for this issue?

I rarely use jquery, but I wanted to add animation to a side bar. The sidebar menu is 670px with a -670 left-margin. When the user hovers over it, I'd like the left-margin to change to 0px and reveal the hidden content. Upon mouseout, it should return ...

Is it possible to effortlessly download videos stored in Firebase directly from my website by simply clicking on an "a" tag?

Recently, I came across a web page that contained a list of videos. There was an icon resembling an arrow next to the "a" tag. You can view this image by clicking here. My intention was to download one of these videos hosted on Firebase by clicking on ...

`Achieving object placement on top of another object in ThreeJS`

Being new to ThreeJS, I am seeking help with a basic question. I have loaded multiple GLTF files into my scene and I need to position one object on top of another (but the position should be adjustable later on) For instance: https://i.sstatic.net/oJq1BIi ...

Add movement to elements as you scroll down the page with Angular

We are utilizing Angular.js combined with Bootstrap, and there is no jQuery included in our project. I am interested in implementing animate.css to create a fading effect for elements on the page as users scroll up and down. I came across an example that ...

Showing database table entry that corresponds to WordPress post ID

Seeking guidance on a project. I have created a website for a volleyball club using Wordpress and am collecting player profile data through a Google form/spreadsheet, which I then save as a .csv file and import into a custom table named 'profiles&apos ...

Display specific divs after selecting checkboxes and clicking the submit button

I need assistance with creating a page that contains multiple checkboxes for users to select. Once they click the submit button, specific divs should be displayed based on their checkbox selections. I am new to java coding and would greatly appreciate any ...

Incorporating CSS sprites within an <img> element

I have a sprite that contains various logos. I am trying to extract a specific logo using the following css .prog_logo { background-image:url('../../image/sprite_logo.png'); } .prog_logo_prime { background-position:0px -81px; width:52px; height: ...

Organize the structure of a JSON object in AngularJS to generate a hierarchical unordered list

Greetings! I am working with a RESTful API that returns a JSON object structured like this: { data: [ { id: 4, name: "Naranja", cant_portion: 2, portion_name: "Piezas", foodgroup_name: "Frutas" } ...