I'm trying to create a responsive div list
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class="container">
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-6">
<a href="#">
<img src="https://dummyimage.com/250x250/000/fff">
</a>
</div>
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-6">
<a href="#">
<img src="https://dummyimage.com/250x250/000/fff">
</a>
</div>
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-6">
<a href="#">
<img src="https://dummyimage.com/250x250/000/fff">
</a>
</div>
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-6">
<a href="#">
<img src="https://dummyimage.com/250x250/000/fff">
</a>
</div>
</div>
As you can see, from above i have four divs and when i only have 3 divs, they will in left
Is it possible do it dynamically when there is only 3 div
and they will be centered ?
If yes, how can i do that. Thanks in advance and sorry for my bad english
To test, try removing a column.
For All Resolutions:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class="container">
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-6">
<a href="#">
<img src="https://dummyimage.com/250x250/000/fff">
</a>
</div>
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-6">
<a href="#">
<img src="https://dummyimage.com/250x250/000/fff">
</a>
</div>
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-6">
<a href="#">
<img src="https://dummyimage.com/250x250/000/fff">
</a>
</div>
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-6">
<a href="#">
<img src="https://dummyimage.com/250x250/000/fff">
</a>
</div>
</div>
<style>
.col-xs-6:first-child:nth-last-child(3),
.col-xs-6:first-child:nth-last-child(3) ~ .col-xs-6 {
width: 33.3%;
}
</style>
For Mobile Resolution Only:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class="container">
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-6">
<a href="#">
<img src="https://dummyimage.com/250x250/000/fff">
</a>
</div>
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-6">
<a href="#">
<img src="https://dummyimage.com/250x250/000/fff">
</a>
</div>
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-6">
<a href="#">
<img src="https://dummyimage.com/250x250/000/fff">
</a>
</div>
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-6">
<a href="#">
<img src="https://dummyimage.com/250x250/000/fff">
</a>
</div>
</div>
<style>
@media (max-width:767px) {
.col-xs-6:first-child:nth-last-child(3),
.col-xs-6:first-child:nth-last-child(3) ~ .col-xs-6 {
width: 33.3%;
}
}
</style>
Explanation:
`.col-xs-6:first-child:nth-last-child(3)`
For The column that is the first column, and also the 3rd from the last column
, .col-xs-6:first-child:nth-last-child(3) ~ .col-xs-6
and for all adjacent siblings of said column (every column that comes after)
width: 33.3%;
take up 1/3 of the width.
If there are 4 or more columns, or less than 3 columns, the first child can't be 3rd from the last, so we only change the width when it's 3. No flexbox needed (although I love flexbox), no polyfills needed, no bs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With