Bootstrap carousal: open the clicked image - html

I am making a lightbox - carousel - modal page with Bootstrap 3. So far, all is going very well, except for one thing:
When the carousel starts, it always starts at the very first image of the page, not the image that I click on.
In other words: if I click on image 1 then image 1 is opened, if I click on image 2, then image 1 is opened, if I click on image 3, then image 1 is opened.
After that, the carousel works like it should, but what I want is, that when I click on image 2, that image 2opens, and if I click on image 3, that image 3 opens.
Some code:
<ul class="cropped-images">
<li class="la active"><a data-content="No 1" href="#lightbox" data-toggle="modal" data-slide-to="0"><img src="images/thumbs/1.jpg" alt="No 1"></a></li>
<li class="la"><a data-content="No 2" href="#lightbox" data-toggle="modal" data-slide-to="1"><img src="images/thumbs/2.jpg" alt="No 2"></a></li>
<li class="la"> etc... </ul>
<div id="lightbox" class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">TITLE</h4>
</div>
<div id="carousel-example-generic" class="carousel slide">
<!-- Wrapper for slides -->
<div class="carousel-inner">
<div class="item active">
<img src="images/large/1.jpg" alt="...">
<div class="carousel-caption">One Image</div>
</div>
<div class="item">
<img src="images/large/2.jpg" alt="...">
<div class="carousel-caption">Another Image</div>
</div>
... etc...
<!-- Controls -->
controls code
</div>
</div>
</div>
</div>
Any suggestions please?

You can use Bootstrap Carousel to create your own custom lightbox, something as shown that I've tried to show in the snippet below, please have a look:
$('.content-holder .thumbnail').each(function(i) {
var item = $('<div class="item"></div>');
var itemDiv = $(this).parents('div');
var title = $(this).parent('a').attr("title");
item.attr("title", title);
$(itemDiv.html()).appendTo(item);
item.appendTo('.carousel-inner');
if (i === 0) { // set first item active
item.addClass('active');
}
});
/* activate the carousel */
$('#modalCarousel').carousel({interval: false});
/* change modal title when slide changes */
$('#modalCarousel').on('slid.bs.carousel', function () {
$('.modal-title').html($(this).find('.active').attr("title"));
});
/* when clicking a thumbnail */
$('.row .thumbnail').click(function(){
var idx = $(this).parents('div').index();
var id = parseInt(idx);
$('#myModal').modal('show'); // show the modal
$('#modalCarousel').carousel(id); // slide carousel to selected
});
.thumbnail {
height: 100px;
margin: 6px;
}
.thumbnail {
margin-bottom: 6px;
}
.carousel-control.left {
background-image: none;
position: absolute;
top: 50%;
left: 0;
width: 25px;
height: 100%;
transform: translateY(-50%);
}
.carousel-control.right {
background-image: none;
position: absolute;
top: 50%;
right: 0;
width: 25px;
height: 100%;
transform: translateY(-50%);
}
.item .thumbnail {
margin: 0 auto;
}
body {
margin: 20px !important;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="content-holder">
<div class="container">
<div class="row">
<div class="col-lg-2 col-sm-3 col-xs-6">
<a href="#">
<img class="thumbnail img-responsive" src="http://placehold.it/200x200" alt="" title="">
</a>
</div>
<div class="col-lg-2 col-sm-3 col-xs-6">
<a href="#">
<img class="thumbnail img-responsive" src="http://placehold.it/300x300" alt="" title="">
</a>
</div>
</div>
</div>
</div>
<!-- lightbox modal -->
<div class="modal" id="myModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title">Lightbox</h3>
</div>
<div class="modal-body">
<div id="modalCarousel" class="carousel">
<div class="carousel-inner"></div>
<a class="carousel-control left" href="#modalCarousel" data-slide="prev">
<i class="glyphicon glyphicon-chevron-left"></i>
</a>
<a class="carousel-control right" href="#modalCarousel" data-slide="next">
<i class="glyphicon glyphicon-chevron-right"></i>
</a>
</div>
</div>
</div>
</div>
</div>
<!-- end lightbox modal -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
Hope this helps!

Try to something like this.
Just replace below line in your html code
<div id="carousel-example-generic" class="carousel slide">
to
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">

Related

How to make carousel-caption appear on top?

I have a simple bootstrap 4 carousel, I would like a carousel caption to appear on top instead of bottom something like this below.
update: HERE IS JSFIDLE WITH FULL CODE
carousel demo
Here is what I have tried so far
HTML
<div class="modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<div class="pull-left">My Gallery Title</div>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div id="demo" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ul class="carousel-indicators">
<li data-target="#demo" data-slide-to="0" class="active"></li>
<li data-target="#demo" data-slide-to="1"></li>
<li data-target="#demo" data-slide-to="2"></li>
</ul>
<!-- The slideshow -->
<div class="carousel-inner">
<div class="carousel-item active">
<div class="carousel-caption">
<h3>Heading 1</h3>
</div>
<img src="https://placeimg.com/600/400/nature/1" alt="Los Angeles">
</div>
<div class="carousel-item">
<div class="carousel-caption">
<h3>Heading 2</h3>
</div>
<img src="https://placeimg.com/600/400/nature/2" alt="Chicago">
</div>
<div class="carousel-item">
<div class="carousel-caption">
<h3>Heading 3</h3>
</div>
<img src="https://placeimg.com/600/400/nature/3" alt="New York">
</div>
</div>
<!-- Left and right controls -->
<a class="carousel-control-prev" href="#demo" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a class="carousel-control-next" href="#demo" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>
</div>
<div class="modal-footer">
<button class="btn-sm close" type="button" data-dismiss="modal">Close</button>
<!--end modal-footer-->
</div>
<!--end modal-content-->
</div>
<!--end modal-dialoge-->
</div>
<!--end myModal-->>
</div>
CSS
.carousel-caption {
position: absolute;
right: 15%;
top: -42px;
left: 15%;
z-index: 999999999;
padding-top: 20px;
padding-bottom: 20px;
color: #fff;
text-align: center;
}
Unfortunately, with this solution, my carousel caption disappear.
What do I need to do to get what I want?
change css of .carousel-caption
.carousel-item .carousel-caption {
position: static;
padding-top: 20px;
padding-bottom: 20px;
text-align: center;
}
working fiddle here
Change the value of top to 0. The carousel caption will move to top of your carousel exactly opposite to carousel indicators.
Try using the inspect element and check the position of your concern element.
Use property relative in div with class ".carousel-item"
.carousel-item{
position:relative;
}
.carousel-caption {
position: absolute;
top: 0;
left: 0;
width:100%;
text-align:center;
z-index: 999999999;
padding-top: 5px;
color: #fff;
}
Full working example in codepen:
https://codepen.io/anon/pen/bJygvj

Slide on Bootstrap 3 Carousel error

I'm working with a Bootstrap 3 carousel. Here is what I have done far, I want it to look similar to this.
Why does the slide preview from column 2 anytime I click the arrows? Is there a way to hide the preview from showing up when clicking next/previous
Anytime I click past two slides, it errors out and doesn't go back to the first slide.
<style type="text/css">
/* #information, #video {
width: 50%; //CSS styling for table tags.
} */
.linebreak {
display: none;
}
.image {
float: left!important;
}
.carousel slide {
width: 400px;
}
.text {
width: 45%!important;
float: right!important;
right: 14px!important;
/* top: 10px!important; */
margin-right: 35px!important;
/* margin-top: 20px!important; */
}
.carousel-inner {
overflow: visible;
}
.carousel img {
border-radius: 0px;
width: 140px;
margin-right: 10px;
margin-left: 80px;
}
.carousel-control active {
position: fixed;
}
.carousel {
margin-top: 40px;
}
.firstcolumn {
width: 1000px;
}
/*.video-container {
/*position:relative;
padding-bottom:315px;
padding-top:10px; /* width: 100% ;* max-width: 500px; padding-right:0px;
/*max-height:530px; overflow: auto;
border: none;
}
.video-container img {
position:absolute;
top:0;
left:0;
width:100%;
max-width: 500px;
max-height:530px;
height:100%;
margin: 0px;*/
/*}*/
</style>
<div class="firstcolumn">
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6"> <!-- DIV 1 BEGIN -->
<div id="myCarousel" class="carousel slide" data-ride="carousel" data-interval="false">
<!-- 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>
</ol>
<div class="carousel-inner">
<div class="item active">
<img class="image" src="https://www.evantage-technology.com/wp-content/uploads/2015/04/product_office365.jpg" alt="Los Angeles">
<div class="text">Office 365 is the brand name Microsoft uses for a group of subscriptions that provide productivity software and related services.</div>
</div>
<div class="item">
<img class="image" src="http://office365.ecolearning.eu/img/office_icos/delve-logo.png" alt="Chicago">
<div class="text">Microsoft Office Delve is a data visualization and discovery tool that incorporates elements of social networking and machine learning with the search capability of the Microsoft Office 365 suite.</div>
</div>
<!-- <div class="item">
<img src="ny.jpg" alt="New York">
</div>
</div> -->
<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" data-slide="prev" style="margin-top: 50px">
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next" style="margin-top: 50px">
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
</div> <!-- DIV 1 END -->
<div class="secondcolumn">
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
<center><h3>WHAT DO YOU WANT TO DO?</h3></center>
<br/>
<div class="row">
<div class="col-sm-6" align="center">.col-sm-3</div>
<div class="col-sm-6" align="center">.col-sm-6</div>
</div>
<div class="row">
<div class="col-sm-6" align="center">.col-sm-3</div>
<div class="col-sm-6" align="center">.col-sm-6</div>
</div>
</div>
</div>
</div>
</div>
If you make use of the Bootstrap Grid inside your Carousel component you can avoid using any customized CSS. You just need to structure your HTML accordingly:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 col-md-6">
<div id="myCarousel" class="carousel slide" data-ride="carousel" data-interval="false">
<!-- 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>
</ol>
<div class="carousel-inner">
<div class="item active">
<div class="row">
<div class="col-xs-4"><img class="img-responsive" src="https://www.evantage-technology.com/wp-content/uploads/2015/04/product_office365.jpg" alt="Los Angeles"></div>
<div class="col-xs-8">Office 365 is the brand name Microsoft uses for a group of subscriptions that provide productivity software and related services.</div>
</div>
</div>
<div class="item">
<div class="row">
<div class="col-xs-2"><img class="img-responsive" src="http://office365.ecolearning.eu/img/office_icos/delve-logo.png" alt="Chicago"></div>
<div class="col-xs-10">Microsoft Office Delve is a data visualization and discovery tool that incorporates elements of social networking and machine learning with the search capability of the Microsoft Office 365 suite.</div>
</div>
</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>
<div class="col-xs-12 col-md-6">
<h3 class="text-center">WHAT DO YOU WANT TO DO?</h3>
<div class="row">
<div class="col-sm-6 text-center">A</div>
<div class="col-sm-6 text-center">B</div>
</div>
<div class="row">
<div class="col-sm-6 text-center">C</div>
<div class="col-sm-6 text-center">D</div>
</div>
</div>
</div>
</div>
You may need to expand the snippet to view its responsive nature. I would also point out that you were using elements like <center> which were depreciated, or attributes like align="center" on your <div> elements. You should use CSS for this sort of positioning, which with respect to Bootstrap would involve applying the text-center class to the element whose contents you wish to be centered.

Bootstrap modal-within-modal image carousel issue

I have a modal that will house an image gallery. When the first modal is opened, there are thumbnails, and when a thumbnail image is clicked, a secondary modal will open on top of the first modal, which will have the larger images in a carousel (of course whichever thumbnail is clicked, that corresponding large image will open in the second modal's carousel position).
Right now, when I click either thumbnail, the second modal opens up, but it shows both of the thumbnails, instead of the larger image of the corresponding thumbnail. Very weird, I seem to have it right by looking at the code, but not sure why it's not working.
Here is the HTML:
<!-- kitchens modal -->
<div class="modal kitchens-modal fade" id="kitchens-modal" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<!-- modal header -->
<div class="modal-header">
<h1>KITCHENS</h1>
<h3>Your dream kitchen is so close...</h3>
</div>
<!-- end modal header -->
<!-- modal body -->
<div class="modal-body">
<div class="row">
<div class="col-lg-2 col-sm-3 col-xs-6">
<a href="#">
<img class="thumbnail img-responsive" src="./assets/img/kitchens/kitchen.jpg" alt="Kitchen" title="Kitchen">
</a>
</div>
<div class="col-lg-2 col-sm-3 col-xs-6">
<a href="#">
<img class="thumbnail img-responsive" src="./assets/img/kitchens/kitchen1.jpg" alt="Kitchen two" title="Kitchen two">
</a>
</div>
</div>
</div>
<!-- end modal body -->
<!-- modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
<!--end modal footer -->
</div>
</div>
</div>
<!-- end kitchens modal -->
<!-- inner kitchen gallery modal -->
<div class="modal" id="myModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title"></h3>
</div>
<div class="modal-body">
<div id="modalCarousel" class="carousel">
<div class="carousel-inner">
</div>
<a class="carousel-control left" href="#modaCarousel" data-slide="prev">
<i class="glyphicon glyphicon-chevron-left"></i>
</a>
<a class="carousel-control right" href="#modalCarousel" data-slide="next">
<i class="glyphicon glyphicon-chevron-right"></i>
</a>
</div>
</div>
</div>
</div>
</div>
<!-- end inner kitchen gallery modal -->
And the CSS:
.thumbnail {
height: 100px;
margin: 6px;
}
.thumbnail {
margin-bottom: 6px;
}
.carousel-control.left,
.carousel-control.right {
background-image: none;
margin-top: 10%;
width: 5%;
}
And lastly, the JS:
$('.row .thumbnail').on('load', function() {
}).each(function(i) {
if(this.complete) {
var item = $('<div class="item"></div>');
var itemDiv = $(this).parents('div');
var title = $(this).parent('a').attr("title");
item.attr("title", title);
$(itemDiv.html()).appendTo(item);
item.appendTo('.carousel-inner');
if (i === 0) { // set first item active
item.addClass('active');
}
}
});
/* activate the carousel */
$('#modalCarousel').carousel({interval: false});
/* change modal title when slide changes */
$('#modalCarousel').on('slid.bs.carousel', function () {
$('.modal-title').html($(this).find('.active').attr("title"));
});
/* when clicking a thumbnail */
$('.row .thumbnail').click(function(){
var idx = $(this).parents('div').index();
var id = parseInt(idx);
$('#myModal').modal('show'); // show the modal
$('#modalCarousel').carousel(id); // slide carousel to selected
});
I got this from a modal gallery I found online, and modified it a bit, but it just won't work! When I open the modal, and click on the thumbnail, the second modal just has all the thumbnails there, instead of the one image.
You can try something like:
$('.kitchens-modal .thumbnail').each(function(i) {
var item = $('<div class="item"></div>');
var itemDiv = $(this).parents('div');
var title = $(this).parent('a').attr("title");
item.attr("title", title);
$(itemDiv.html()).appendTo(item);
item.appendTo('.carousel-inner');
if (i === 0) { // set first item active
item.addClass('active');
}
});
/* activate the carousel */
$('#modalCarousel').carousel({interval: false});
/* change modal title when slide changes */
$('#modalCarousel').on('slid.bs.carousel', function () {
$('.modal-title').html($(this).find('.active').attr("title"));
});
/* when clicking a thumbnail */
$('.row .thumbnail').click(function(){
var idx = $(this).parents('div').index();
var id = parseInt(idx);
$('#myModal').modal('show'); // show the modal
$('#modalCarousel').carousel(id); // slide carousel to selected
});
.thumbnail {
height: 100px;
margin: 6px;
}
.thumbnail {
margin-bottom: 6px;
}
.carousel-control.left {
background-image: none;
position: absolute;
top: 50%;
left: 0;
width: 25px;
height: 100%;
transform: translateY(-50%);
}
.carousel-control.right {
background-image: none;
position: absolute;
top: 50%;
right: 0;
width: 25px;
height: 100%;
transform: translateY(-50%);
}
.item .thumbnail {
margin: 0 auto;
}
body {
margin: 20px !important;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="btn btn-primary" data-target="#kitchens-modal" data-toggle="modal">Open Kitchen Modal</div>
<!-- kitchens modal -->
<div class="modal kitchens-modal fade" id="kitchens-modal" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<!-- modal header -->
<div class="modal-header">
<h1>KITCHENS</h1>
<h3>Your dream kitchen is so close...</h3>
</div>
<!-- end modal header -->
<!-- modal body -->
<div class="modal-body">
<div class="row">
<div class="col-lg-2 col-sm-3 col-xs-6">
<a href="#">
<img class="thumbnail img-responsive" src="http://placehold.it/200x200" alt="Kitchen" title="Kitchen">
</a>
</div>
<div class="col-lg-2 col-sm-3 col-xs-6">
<a href="#">
<img class="thumbnail img-responsive" src="http://placehold.it/300x300" alt="Kitchen two" title="Kitchen two">
</a>
</div>
</div>
</div>
<!-- end modal body -->
<!-- modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
<!--end modal footer -->
</div>
</div>
</div>
<!-- end kitchens modal -->
<!-- inner kitchen gallery modal -->
<div class="modal" id="myModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title"></h3>
</div>
<div class="modal-body">
<div id="modalCarousel" class="carousel">
<div class="carousel-inner">
</div>
<a class="carousel-control left" href="#modalCarousel" data-slide="prev">
<i class="glyphicon glyphicon-chevron-left"></i>
</a>
<a class="carousel-control right" href="#modalCarousel" data-slide="next">
<i class="glyphicon glyphicon-chevron-right"></i>
</a>
</div>
</div>
</div>
</div>
</div>
<!-- end inner kitchen gallery modal -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
Hope this helps!

Control Bootstrap Carousel Slides using Buttons

I want to control the scroll of this carousel using menu bar where there are buttons 1,2,3,4,5 when I click them the carousel should slide to 2nd slide or 3rd slide like that.
I have used HTML, Bootstrap, CSS.
<div class="container-fluid">
<div class="row" id="row1">
<div class="col-sm-12" style="top: 10%; bottom: 50%;">
<h1>AS</h1>
</div>
</div>
<div class="row" id="row2">
<div class="col-sm-4" style="top: 10%; bottom: 50%; background-color: white; height: 7.4vh">
<h1 style="color: black">AS</h1>
</div>
<div class="col-sm-8" style="top: 10%; bottom: 50%; background-color: black">
<div class="nav">
<ul>
<li class="contact">1</li>
<li class="contact1">2</li>
<li class="about">3</li>
<li class="about1">4</li>
<li class="tutorials">5</li>
</ul>
</div>
</div>
</div>
<div class="row" id="row3">
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox" style="vertical-align: top; position: absolute; background-color:green">
<div class="item active" id="one">
<h1>ABC</h1>
<div class="item" id="two">
<img src="img/B.jpg" alt="Chania">
</div>
<div class="item" id="three">
<img src="img/C.jpg" alt="Flower">
</div>
<div class="item" id="four">
<img src="img/C.jpg" alt="Flower">
</div>
<div class="item" id="five">
<img src="img/C.jpg" alt="Flower">
</div>
</div>
<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon glyphicon-menu-left" aria-hidden="true"></span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon glyphicon-menu-right" aria-hidden="true"></span>
</a>
</div>
</div>
<div class="row" id="row4">
<div class="col-sm-12" style="top: 10%; bottom: 50%;">
<button class="button" onclick="Contact">Contact</button>
</div>
</div>
</div>
I'm not sure... Please check. Is it what you want to achieve?
You can place images in different slides.
Then use the .click() method to handle the click JavaScript event.
And call the .carousel(number) method:
Cycles the carousel to a particular frame (0 based, similar to an array).
See also:
the .eq() method
Reduce the set of matched elements to the one at the specified index.
the .index() method
If no argument is passed to the .index() method, the return value is an integer indicating the position of the first element within the jQuery object relative to its sibling elements.
the .find() method
Get the descendants <...> filtered by a selector <...>
the .children() method
Get the children of each element in the set of matched elements, optionally filtered by a selector.
the .parent() method
<...> traverses to the immediate parent of each of these elements in the DOM tree and constructs a new jQuery object from the matching elements.
<...> travels a single level up the DOM tree.
the .prev() method
Get the immediately preceding sibling <...>
For example: http://codepen.io/glebkema/pen/qayLYY
var myCarousel = $('#myCarousel');
var myNav = myCarousel.prev();
myNav.find('li > a').click(function() {
var newIndex = $(this).parent().index();
myCarousel.carousel( newIndex );
});
myCarousel.on('slid.bs.carousel', function () {
var newIndex = $(this).find('.carousel-inner>.active').index();
var newLI = myNav.children().eq( newIndex );
if ( !newLI.hasClass('active') ) {
myNav.find('li.active').removeClass('active');
newLI.addClass('active');
}
});
/* Make the images wide and responsive. */
.carousel-inner img {
height: auto;
width: 100%;
}
/* Count nav-pills */
.nav {
counter-reset: number;
padding: 12px 0;
}
.nav > li > a:before {
content: counter(number);
counter-increment: number;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div class="container">
<ul class="nav nav-pills">
<li role="presentation" class="active"></li>
<li role="presentation"></li>
<li role="presentation"></li>
<li role="presentation"></li>
<li role="presentation"></li>
<li role="presentation"></li>
</ul>
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="https://via.placeholder.com/900x300/c69/fff/?text=1" alt="">
</div>
<div class="item">
<img src="https://via.placeholder.com/900x300/9c6/fff/?text=2" alt="">
</div>
<div class="item">
<img src="https://via.placeholder.com/900x300/69c/fff/?text=3" alt="">
</div>
<div class="item">
<img src="https://via.placeholder.com/900x300/c33/fff/?text=4" alt="">
</div>
<div class="item">
<img src="https://via.placeholder.com/900x300/099/fff/?text=5" alt="">
</div>
<div class="item">
<img src="https://via.placeholder.com/900x300/f93/fff/?text=6" alt="">
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

Html display a legend

i'm working on a leaflet map and i want to add a legend on it by using a modal to do that.
In my modal i want to use a :
<ul><li>
and each ligne of my ul can be an horizontal line with a text or an icon with a text.
exemple :
-----(red) : Polyline of users
-----(green) : Polyline of the race
(icon) : marker of users ....
I don't know how to draw a red line in html/css to do something like that.
I have tried the <hr> but it's always take all the line.
thanks for your help.
I include the code of my modal :
<div id="infoModal" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Informations</h4>
</div>
<div class="modal-body">
<h2>Legende : </h2>
<ul class="list-group">
<li class="list-group-item">
<div class="col-md-4">
<hr width="25%" size="10">
</div>
<div class ="col-md-8">
TEST
</div>
</li>
</ul>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Fermer</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
Example (From Comment Section)
You could use flexbox and a pseudo-element.
.list-group-item {} .list-group-item div {
display: flex;
align-items: center;
}
.list-group-item div::before {
content: '';
flex: 1;
height: 5px;
background: red;
margin-right: 1em;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="modal-body">
<h2>Legende : </h2>
<ul class="list-group">
<li class="list-group-item">
<div class="col-sm-12">
<img src="http://lorempixel.com/image_output/abstract-q-c-25-25-2.jpg" alt="">
</div>
</li>
</ul>
</div>
Try this:
.my-divider {
position: relative;
left: 100px;
}
.my-divider::before {
content: "";
position: absolute;
top: 50%;
left: -100px;
width: 100px;
border-bottom: 1px solid red;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<ul class="list-group">
<li class="list-group-item">
<div class="row">
<div class="col-md-offset-4 col-md-8 my-divider">TEST</div>
</div>
</li>
</ul>
Values are a bit arbitrary so change those to suit.