Click on button that is behind an element - html

I have a button that is in a div, that is behind another div. The second div overlaps the first by using the css: position: absolute;
Therefore the button is not clickable. Is there any way I can make it clickable like a normal button?
Example: jsfiddle
body {
background-color: blue;
}
.stack {
width: 320px;
height: 240px;
position: absolute;
top: 50%;
left: 50%;
background-color: white;
margin-top: -120px;
margin-left: -160px;
}
.background {
width: 320px;
height: 240px;
background-image: url('http://www.userlogos.org/files/logos/ps1d3r/apple-black-i.png');
top: 0px;
left: 0px;
position: absolute;
}
.card {
pointer-events: none;
width: 320px;
height: 240px;
background-image: url('http://2.bp.blogspot.com/-OIHQM4x8l0U/UEiDLQyiTRI/AAAAAAAAHFs/i1a6rkqQ8tQ/s320/floral+swirl.png');
top: 0px;
left: 0px;
position: absolute;
}
<div class="stack">
<div class="background" onclick="alert('background clicked');">
<button onclick="alert('bg-button clicked');" style="left:65px; top:65px; position: absolute;">This is a background button</button>
<div class="card">
<button onclick="alert('card-button clicked');">This is a card button</button>
<textarea style="left:100px; top:100px; position: absolute;">This is a card textarea</textarea>
</div>
</div>
</div>

You can use pointer-events:none; on .card. This will disable the click event on the .card div and user can click on the button behind it. More info here (MDN).
Here is an example showing how you can enable the click envent on an element hidden behind another one :
button {
margin: 50px;
}
button:focus {
background: red;
}
button:hover {
background: teal;
}
.inFront {
pointer-events: none;
position: absolute;
top: 25px; left: 25px;
right: 25px; height: 150px;
border: 3px solid red;
background: rgba(0, 0, 0, .5);
}
<button onclick="alert('button clicked');">I am a button behind the .inFront div</button>
<div class="inFront"></div>
In this example, the .inFront div is over the button but the pointer-events: none; property on the div allows the button to be clicked, focused and hovered.
Regarding your example, the drawback is that it will also disable the textarea and the "card button" so you will have to change your HTML and move both textarea and card button out of the .card div so they are still clickable. Here is a demo :
DEMO

Use z-index in this case.
<button onclick="alert('bg-button clicked');" style="left:65px; top:65px; position: absolute; z-index:1;">This is a background button</button>
DEMO
This positions the element in the depth field higher than everything else. The higher the number, the higher the stack order.
z-index: 1;
Though, z-index requires positioning such as position: absolute; or position: relative;.
Read a great article about Z-Index here.

Give the button a positive z-index
button {
position: absolute;
z-index: 1;
}

For those who have the same issue as I do where(not restructuring your HTML):
div 1 is on top of div 2
Both div 1 and 2 needs to be clickable/interactive
However div 2 should be infront of div 1
Apply the following codes to div 2:
div2 {
position: absolute; // to manipulate position
z-index: 999; // manipulating the position, putting it in front of div1
pointer-events: visible; // making it interactive, clickable
}

Related

How to increase the clickable area of a button?

I need to increase the clickable height of a button to 48px. The button has a background color, and I want to increase the clickable height without actually setting the height of the button (i.e. the increase in clickable area should be invisible / empty space around the button). I've tried increasing the padding and setting background-clip: content-box, however this means that the edges of the button are no longer rounded. What is the recommended way to achieve this?
I'd work with a pseudo element here:
button {
--clickable-space-around-button: -15px;
position: relative;
}
button::after {
content: "";
left: var(--clickable-space-around-button);
right: var(--clickable-space-around-button);
bottom: var(--clickable-space-around-button);
top: var(--clickable-space-around-button);
position: absolute;
}
<div style="padding: 20px;">
<button onclick="console.log('clicked')">Click me</button>
</div>
If your issue was rounded corner then use transparent border and padding-box not content-box
button {
background:red padding-box;
font-size:25px;
padding:10px 20px;
border:25px solid transparent;
border-radius:35px;
outline:none;
}
button:active {
background-color:blue;
}
<button>text</button>
Here is what I found for your request, I hope it will be useful.
I can suggest adding an invisible before (tinted with a color so that
the area is visible, you just need to remove this color):
.link {
position: relative;
display: block;
margin:50px auto;
height: 1px;
width: 1px;
background: pink;
cursor: pointer;
}
.link:before {
content: '';
position: absolute;
display: block;
top: -15px;
left: -15px;
right: -15px;
bottom: -15px;
background: rgba(255, 0, 0, 0.06);//цвет убрать
}
<a class="link" href="#"></a>
Original Answer: https://ru.stackoverflow.com/a/702645
Or you also can just try margin
Have a good day!

Sticky div in CSS

I would like a div to be pushed down (see "Search while I move the map" in the screenshot), to float above the map, but so that if I use any constants for margin-top or top, then that's relative to the parent div (map), not the browser window.
How can I do so? Website link I have tried to add position: relative; to the parent #map but this is what I get (the map gets hidden):
This is my CSS code:
#map {
#searchCheckboxContainer {
position: absolute;
display: inline-table;
white-space: nowrap;
margin-top: 24px; // sure, this works, but it's 24px *from the browser window*
top: 0px; // any way to make it relative to the parent div (map)?
left: 50%;
transform: translateX(-50%);
background: rgb(255, 255, 255) !important;
}
#searchCheckboxContainer {
height: 40px;
}
}
HTML:
<div id="map" v-cloak>
<div id="searchCheckboxContainer">
<div id="searchCheckbox">
<input id="checkbox" class="form-check-input" type="checkbox" value="" id="defaultCheck1">
<label class="form-check-label" for="defaultCheck1">
Search as I move the map
</label>
</div>
</div>
<div id="mapid"></div>
</div>
Adding as an answer to avoid the comment noise:
All of the #map element children are positioned absolute. So, essentially they aren't in the normal document flow and will not affect the height of the #map div.
You need to add:
position: relative;
height: 100vh (or whatever)
To your #map div.
Then, to your #searchCheckboxContainer, add a z-index: 100 //could be anything but that worked
This will put the box above the map.
I assume you need it to look like that:
In this case you need to modify the following:
#map {
position: relative;
height: calc(100vh - 86px); // The height of header on mobile and you need to add responsive media queries to handle it.
}
#map #searchCheckboxContainer{
position: absolute;
display: inline-table;
white-space: nowrap;
margin-top: 0;
bottom: 0;
left: 0;
top: auto;
transform: none;
background: #ffffff !important;
z-index: 2;
width: 100%;
padding: 15px;
}
#searchCheckbox .form-check-input{
position: relative;
margin-top: 0;
margin-left: 0;
}
#map #mapid{
height: 100%;
width: 100%;
position: absolute;
background-color: yellow;
z-index: 1;
}

Having a <button> tag completely over a <div> element

I have tried to make a tag that completely covers a div element and when clicked, the button element would disappear.
HTML:
I have tried setting height and width to 100%, but this does not fill the area entirely... how do I fix this?
#startstorybutton {
height: 100%;
width: 100%;
position: absolute;
top: 7.5vw;
right: 20vw;
z-index: 10;
background-color: #f4511e;
color: white;
Font-size: 50px;
font-style: italic;
transition: all 0.6s;
cursor: url(cursors/select.PNG), pointer;
}
#story {
background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/3/30/Three_little_pigs_%28geograph_4633727%29.jpg/1200px-Three_little_pigs_%28geograph_4633727%29.jpg");
background-size: 100%;
height: 40%;
width: 80%;
margin: 0 auto;
}
<div id="story">
<div>
<p id="storytitle">The Three Little Pigs</p>
<div>
<p id="storybox"></p>
</div>
<div id="storybuttonsection">
<button type="button" id="backbutton"><span>Go back</span></button>
<p id="pagenumber">121212</p><button id="continuebutton"><span>Continue</span></button>
</div>
</div>
<button type="button" id="startstorybutton">Start the story!</button>
</div>
You have to position the button element on top of the "hidden element", if you set the main element position to relative you can then position the child element absolute over it (top: 0; right: 0) with the height and width to 100%.
For hide the button, the easy way is to add a listener event to the button and then hide it (or remove it) with javascript.
#startstorybutton {
height: 100%;
width: 100%;
position: absolute;
top: 0;
right: 0;
z-index: 10;
background-color: #f4511e;
color: white;
Font-size: 50px;
font-style: italic;
transition: all 0.6s;
cursor: url(cursors/select.PNG), pointer;
}
#story {
background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/3/30/Three_little_pigs_%28geograph_4633727%29.jpg/1200px-Three_little_pigs_%28geograph_4633727%29.jpg");
background-size: 100%;
height: 40%;
width: 80%;
margin: 0 auto;
position: relative;
}
<div id="story">
<div>
<p id="storytitle">The Three Little Pigs</p>
<div>
<p id="storybox"></p>
</div>
<div id="storybuttonsection">
<button type="button" id="backbutton"><span>Go back</span></button>
<p id="pagenumber">121212</p><button id="continuebutton"><span>Continue</span></button>
</div>
</div>
<button type="button" id="startstorybutton" onclick="this.style.display = 'none'">Start the story!</button>
</div>
I am not very sure of your HTML structure. But as you described your problem I have created an example for you.
You need t make changes in your HTML to make it simple and straightforward.
The story div contains the button, which will be absolutely positioned to fit the entire div.
I have thrown in a small bit of jQuery to show how it will look like.
$(function(){
$("button.overlay").click(function(){
$(this).hide();
});
});
.story{
width: 250px;
height: 100px;
border: 1px solid black;
text-align:center;
position: relative;
}
.overlay{
position: absolute;
top:0px;
left:0px;
height: 100%;
width:100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="story">
Here goes the story!
<button class="overlay">read story button</button>
</div>

Styling span:hover to make span element remain visible

I've set up span elements to be visible only when buttons are hovered over. I'd like the span to continue to be visible if the cursor is then moved onto the span element. (This already happens in Chrome by default.) I thought I could do that just with span:hover {visibility: visible}, but it doesn't work. Do I need to add something else as well, or is something else wrong?
reduced CSS:
.tooltipT {
position: absolute;
}
.tooltipT span {
position: absolute;
width: 0px;
visibility: hidden;
}
.tooltipT:hover span, .tooltipT span:hover {
visibility: visible;
width: 400px;
}
.tooltipT:hover span, .tooltipT span:hover {
bottom: 16px;
left: -200px;
}
reduced HTML:
<div class="pix">
<button class="tooltipT" style="top: 50%; left: 50%;">1
<span>blah
</span>
</button>
<img src="img/MIPs.jpg" alt="Melt-in-Place Station details">
</div>
It's live here, the image with the span element involved is half-way down the page, it is used on the pink button in the middle.
I put it on Codepen and am playing with suggested solutions there.
You'll need JavaScript here because the minute you move off the button (to move towards the now visible span), the button is no longer being hovered and so the CSS class no longer applies. This should do it:
button.addEventListener("mouseover", function(){
span.setAttribute("class", "tooltip");
});
Obviously, replace button with the id of your button and tooltip with the name of the class that should be applied when the button is moused over.
It does work if you let the span outside of the button. In this case you'll need the element ~ element or element + element selector to show up the span when hovering the button.
body {
background: honeydew;
}
.tooltipT {
position: absolute;
background: tomato;
width: 50px;
height: 50px;
}
span {
position: absolute;
width: 50px;
height: 50px;
visibility: hidden;
background: gold;
border: 3px solid black;
top: calc(40% - 40px);
left: calc(50% + 40px);
}
span:hover, .tooltipT:hover ~ span {
visibility: visible;
}
button {
border: 3px solid black;
top: 40%;
left: 50%;
}
button:hover {
background: red;
}
<button class="tooltipT">0</button>
<span>_blah</span>

CSS: Exclude text from background overlay

I have a big image with a dark overlay covering the front of my webpage. I want to add a div filled with bright text on top of the overlay.
Is there a way to position the div so as to exclude it from the overlay?
HTML:
.overlay {
position: absolute;
background-color: rgba(0, 0, 0, 0.45);
top: 70px;
left: 0px;
right: 0px;
bottom: -200px;
z-index: 1;
}
.about-us {
background-image: url("img.jpg");
width: 1100px;
height: 731px;
display: block;
position: relative;
}
<div class="about-us">
<div class="overlay">
<div class="intro">
<h2>Catchy title</h2>
<p>Small Para</p>
<h1>More txt</h1>
</div>
</div>
</div>
Something like this:
h2 {
background-color: white;
z-index: 2;
}
Assuming that was what you wanted on top.
Use a pseudo element instead of an overlay element.
.about-us:before {
content:"";
position: absolute;
background-color: rgba(0,0,0,0.45);
top: 70px;
left: 0px;
right: 0px;
bottom: -200px;
}
Also you need to explicitly specify position property for intro element in able to interact with the content:
.intro {
position: relative;
}
See example here