Reputation: 7635
I would like to add a top-right button on each slide that would be over the carousel-control
link.
How can I do that?
Below you can see that the buttons are under the carousel-control
.myButton {
position: absolute;
top: 0;
right: 0;
z-index: 999; /*not working */
}
.carousel {
height: 400px;
}
.carousel-inner {
width: 100%; /*this must stay */
height: 100%; /*this must stay */
/* z-index: 1; //this make the carousel-control unclickable */
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active text-center">
<button type="button" class="myButton">Btn</button>
Slide 1
</div>
<div class="item text-center">
<button type="button" class="myButton">Btn</button>
Slide 2
</div>
<div class="item text-center">
<button type="button" class="myButton">Btn</button>
Slide 3
</div>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
Upvotes: 0
Views: 3418
Reputation: 406
u can set inner carousels z-index to higher than link to bring it to front then to make links clickable set the inner carousels "pointer-events" to none this will make the carousel to transfer the click event to the element behind it, then to make the button clickable you should set buttons pointer-events to auto, remember you should set pointer-events property of every children of carousel to auto.
.myButton {
position: absolute;
top: 0;
right: 0;
z-index: 999; /*not working */
pointer-events: auto;
}
.carousel {
height: 400px;
}
.carousel-inner {
pointer-events: none;
z-index:10;
width: 100%; /*this must stay */
height: 100%; /*this must stay */
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active text-center">
<button type="button" class="myButton">Btn</button>
Slide 1
</div>
<div class="item text-center">
<button type="button" class="myButton">Btn</button>
Slide 2
</div>
<div class="item text-center">
<button type="button" class="myButton">Btn</button>
Slide 3
</div>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
Upvotes: 1
Reputation: 341
I know your feeling and it also happen to me, until this moment i don't have the ideal answer. so i made and alternate solution, which to make the carousel height smaller.
i know my alternate solution will not fully answered your question, but hopefully it will help you in some way.
.myButton{
position:absolute;
right:0px
}
.carousel-control{
margin: auto auto;
height:100px;
width:100px;
background:#ff0000 !important;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Carousel Example</h2>
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner">
<div class="item active">
<button type="button" class="myButton" onclick="alert('slide1')">Btn</button>
<img src="https://www.w3schools.com/bootstrap/la.jpg" alt="Los Angeles" style="width:100%;">
</div>
<div class="item">
<button type="button" class="myButton" onclick="alert('slide2')">Btn</button>
<img src="https://www.w3schools.com/bootstrap/chicago.jpg" alt="Chicago" style="width:100%;">
</div>
</div>
<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
</body>
</html>
Upvotes: 1
Reputation: 13
Z-index only works for positioned elements on the same level as each other. you should set it on carousel-inner class
.carousel-inner{
z-index:999;
}
Edit: Try like this
<html>
<link type="text/css" id="dark-mode" rel="stylesheet" href="">
<style type="text/css" id="dark-mode-custom-style"></style>
<head>
<style>
.carousel-inner .item .myButton {
position: absolute;
top: 0;
right: 0;
}
.carousel {
height: 400px;
}
.carousel-inner {
width: 100%; /*this must stay */
height: 100%; /*this must stay */
}
.carousel-inner .item {
z-index: 9999; /*not working */
}
.carousel-inner .carousel-control {
z-index: 990 !important;
}
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class=""></li>
<li data-target="#carousel-example-generic" data-slide-to="1" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="2" class=""></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item text-center">
<button type="button" class="myButton">Btn</button>
Slide 1
</div>
<div class="item text-center active">
<button type="button" class="myButton">Btn</button>
Slide 2
</div>
<div class="item text-center">
<button type="button" class="myButton">Btn</button>
Slide 3
</div>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
<script type="text/javascript">
</script>
<div class="as-console-wrapper">
<div class="as-console"></div>
</div>
</body>
</html>
Upvotes: 1
Reputation: 61
Place button tag with class "myButton" before the div tag with class "item active text-center". I changed it for Btn1 in the below code.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<style type="text/css">
.myButton {
position: absolute;
top: 0;
right: 0;
z-index: 999; /*not working */
}
.carousel {
height: 400px;
}
.carousel-inner {
width: 100%; /*this must stay */
height: 100%; /*this must stay */
/* z-index: 1; //this make the carousel-control unclickable */
}
</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<button type="button" class="myButton">Btn1</button>
<div class="item active text-center">
Slide 1
</div>
<!-- <button type="button" class="myButton">Btn2</button> -->
<div class="item text-center">
<!-- <button type="button" class="myButton">Btn2</button> -->
Slide 2
</div>
<!-- <button type="button" class="myButton">Btn3</button> -->
<div class="item text-center">
<!-- <button type="button" class="myButton">Btn3</button> -->
Slide 3
</div>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</body>
</html>
Upvotes: 2