Dynamically Center Elements inside a Row - html

I have two Images with Links inside a row. Sometimes only one Image will be needed so I have added JSTL tags to verify that. The problem is when I'm displaying just one Image. Then they're not centered. Only when both the Images are available it's centered. How to tackle this?
<div class="row">
<c:if test="${content.facebookLink ne 'NO_FACEBOOK_LINK'}">
<div class="col-sm-3 col-sm-offset-3 col-xs-6">
<a href="${content.facebookLink}" target="_blank">
<img src="<c:url value="/resources/images/fb.png"/>">
</a>
</div>
</c:if>
<c:if test="${content.twitterLink ne 'NO_TWITTER_LINK'}">
<div class="col-sm-3 col-xs-6">
<a href="${content.twitterLink}" target="_blank">
<img src="<c:url value="/resources/images/twitter.png "/>">
</a>
</div>
</c:if>
</div>
Thanks.

Better way is to convert .col-sm-3.col-xs-6 to inline-block element and use text-center class to .row
.col-sm-3.col-xs-6 {
display: inline-block;
float:none;
width:auto;
}
img {
width: 100px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class=container>
<div class="row text-center">
<div class="col-sm-3 col-xs-6">
<a href="${content.facebookLink}" target="_blank">
<img src="https://placeholdit.imgix.net/~text?txtsize=28&txt=300%C3%97300&w=300&h=300">
</a>
</div>
<div class="col-sm-3 col-xs-6">
<a href="${content.twitterLink}" target="_blank">
<img src="https://placeholdit.imgix.net/~text?txtsize=28&txt=300%C3%97300&w=300&h=300">
</a>
</div>
</div>
</div>
<div class=container>
<div class="row text-center">
<div class="col-sm-3 col-xs-6">
<a href="${content.twitterLink}" target="_blank">
<img src="https://placeholdit.imgix.net/~text?txtsize=28&txt=300%C3%97300&w=300&h=300">
</a>
</div>
</div>
</div>

Found a simple solution. Omitted he columns from the dynamic elements.
<div class="row">
<div class="col-md-12">
<c:if test="${content.facebookLink ne 'NO_FACEBOOK_LINK'}">
<a href="${content.facebookLink}" target="_blank" style="display: inline">
<img src="<c:url value="/resources/images/fb.png"/>">
</a>
</c:if>
<c:if test="${content.twitterLink ne 'NO_TWITTER_LINK'}">
<a href="${content.twitterLinkk}" target="_blank" style="display: inline">
<img src="<c:url value="/resources/images/twitter.png "/>">
</a>
</c:if>
</div>
</div>
Works perfect.

Related

How do I fit an anchor tag (containing image) in bootstrap grid cell?

I have a 9-cell grid containing images (using bootstrap columns). The grid works fine and is responsive when it contains images only. However, my goal is to make the images clickable, hence I placed the <img> tag inside an anchor tag <a>, which resulted in the photos getting out of the grid order.
When I clicked on "Inspect" on Chrome, it showed the <a> as a large container, with the <img> centered inside of it. How do I make the <a> element adhere to bootstrap column sizes?
HTML:
<div class="container">
<div class="row">
<a href="#">
<img class="col-md-6 col-lg-4 gallery-photo" src="https://" alt="something">
</a>
<a href="#">
<img class="col-md-6 col-lg-4 gallery-photo" src="https://" alt="something">
</a>
<a href="#">
<img class="col-md-6 col-lg-4 gallery-photo" src="https://" alt="something">
</a>
</div>
</div>
CSS:
.gallery-photo {
object-fit: cover;
height: 300px;
margin: 1% 0;
}
The columns are supposed to be placed directly inside of a row.
So it should look something like this.
<div class="container">
<div class="row">
<div class="col-md-6 col-lg-4">
<a href="#">
<img class="gallery-photo" src="https://" alt="something">
</a>
</div>
<div class="col-md-6 col-lg-4">
<a href="#">
<img class="gallery-photo" src="https://" alt="something">
</a>
</div>
<div class="col-md-6 col-lg-4">
<a href="#">
<img class="gallery-photo" src="https://" alt="something">
</a>
</div>
</div>
</div>
Try adding the bootstrap columns to the a tag and not the img.
<div class="container">
<div class="row">
<a class="col-md-6 col-lg-4" href="#">
<img class="gallery-photo" src="https://" alt="something">
</a>
<a class="col-md-6 col-lg-4" href="#">
<img class="gallery-photo" src="https://" alt="something">
</a>
<a class="col-md-6 col-lg-4" href="#">
<img class="gallery-photo" src="https://" alt="something">
</a>
</div>
</div>
Bootstrap columns should be placed directly inside of a row.
Your links need to go inside of the bootstrap columns if you want them to be constrained by the bootstrap column sizes.
In addition, you should put width: 100% on your images for object-fit to work the way you want it to.
.gallery-photo {
object-fit: cover;
width: 100%;
height: 300px;
margin: 1% 0;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-md-6 col-lg-4">
<a href="#">
<img class="gallery-photo" src="https://placeimg.com/1100/750/nature" alt="something">
</a>
</div>
<div class="col-md-6 col-lg-4">
<a href="#">
<img class="gallery-photo" src="https://placeimg.com/1100/750/nature" alt="something">
</a>
</div>
<div class="col-md-6 col-lg-4">
<a href="#">
<img class="gallery-photo" src="https://placeimg.com/1100/750/nature" alt="something">
</a>
</div>
</div>
</div>

How to align three images on a row

There will be a certain amount of images in database which is unknown when this page is being redirected, which means I can't set the amount of div accordingly. What I am after is no matter how many images are there, I would like to display it in a manner of three images in a row, the fourth one will start from the left side of a new row. I try to use col-md-4 try to align three images on a row, but the fourth image is either on the center or right side of the next row ?
This is what I am after. Let's say there are 5 images in database, I would like it to be displayed like this
<div class="row col-md-12">
#foreach($images as $image)
<div class="col-md-4">
<img src="{{ $image->image }}" alt="{!! $image->title !!}" width="50%" height="auto" >
<div class="btn btn-primary edit-image-btn">Edit</div>
<div class="btn btn-danger delete-image-btn">Delete</div>
</div>
#endforeach
</div>
Problem
The height of your first image is greater than the height of images two and three, this means that when the fourth image is added it is added on a new line, but where there is first available vertical space.
You can actually incorporate both of the solutions below simultaneously, clearfix is the most appropriate and versatile, but there are some design benefits from the alternative solution.
clearfix Solution
Adding a div with .clearfix will force the grid system to create what visually appears to be a new row. This is the best solution, you can compare the index of the image and add a clearfix div every third image.
Alternative Solution
You could place your images as backgrounds to a div (rather than using img tags) and force these divs to be a set height using CSS. this isn't solving your problem, it is just avoiding it.
If you set the background-size: cover; background-position: center center; then all images would cover the available space, although you would lose some of the image this is the best solution design wise (my preference).
If having all of the image displayed is important then use background-size: contain;.
.col-xs-4 {
padding:4px;
}
.image-div {
height: 100px;
background-size:cover;
background-position: center center;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Ej0hUpn6wbrOTJtRExp8jvboBagaz+Or6E9zzWT+gHCQuuZQQVZUcbmhXQzSG17s" crossorigin="anonymous">
<h4>Current Problem</h4>
<div class="container">
<div class="col-md-12 row">
<div class="col-xs-4">
<img src="https://dummyimage.com/600x600/000/fff" width="100%" height="auto">
</div>
<div class="col-xs-4">
<img src="https://dummyimage.com/600x400/000/fff" width="100%" height="auto">
</div>
<div class="col-xs-4">
<img src="https://dummyimage.com/600x400/000/fff" width="100%" height="auto">
</div>
<div class="col-xs-4">
<img src="https://dummyimage.com/600x400/000/fff" alt="alternative" width="100%" height="auto" >
</div>
</div>
</div>
<h4>`clearfix` Solution</h4>
<div class="container">
<div class="col-md-12 row">
<div class="col-xs-4">
<img src="https://dummyimage.com/600x600/000/fff" width="100%" height="auto">
</div>
<div class="col-xs-4">
<img src="https://dummyimage.com/600x400/000/fff" width="100%" height="auto">
</div>
<div class="col-xs-4">
<img src="https://dummyimage.com/600x400/000/fff" width="100%" height="auto">
</div>
<div class="clearfix"></div>
<div class="col-xs-4">
<img src="https://dummyimage.com/600x400/000/fff" alt="alternative" width="100%" height="auto" >
</div>
</div>
</div>
<h4>Alternative Solution</h4>
<div class="container">
<div class="col-md-12 row">
<div class="col-xs-4">
<div class="image-div" style="background-image:url('https://dummyimage.com/600x600/000/fff'); " >
</div>
</div>
<div class="col-xs-4">
<div class="image-div" style="background-image:url('https://dummyimage.com/600x400/000/fff'); " >
</div>
</div>
<div class="col-xs-4">
<div class="image-div" style="background-image:url('https://dummyimage.com/600x400/000/fff'); " >
</div>
</div>
<div class="col-xs-4">
<div class="image-div" style="background-image:url('https://dummyimage.com/600x400/000/fff'); " >
</div>
</div>
</div>
</div>
You should not use row and col class names in the same div:
<div class='row'>
<div class="col-md-12">
#foreach($images as $image)
<div class="col-md-4">
<img src="{{ $image->image }}" alt="{!! $image->title !!}" width="50%" height="auto" >
<div class="btn btn-primary edit-image-btn">Edit</div>
<div class="btn btn-danger delete-image-btn">Delete</div>
</div>
#endforeach
</div>
</div>
Check below code and updated fiddle https://jsfiddle.net/dgmrsnb6/
<div class="container">
<div class="row images-wrapper">
<div class="col-md-4 col-sm-4">
<img src="https://dummyimage.com/600x400/000/fff" />
</div>
<div class="col-md-4 col-sm-4">
<img src="https://dummyimage.com/600x400/000/fff" />
</div>
<div class="col-md-4 col-sm-4">
<img src="https://dummyimage.com/600x400/000/fff" />
</div>
<div class="col-md-4 col-sm-4">
<img src="https://dummyimage.com/600x400/000/fff" />
</div>
<div class="col-md-4 col-sm-4">
<img src="https://dummyimage.com/600x400/000/fff" />
</div>
<div class="col-md-4 col-sm-4">
<img src="https://dummyimage.com/600x400/000/fff" />
</div>
<div class="col-md-4 col-sm-4">
<img src="https://dummyimage.com/600x400/000/fff" />
</div>
<div class="col-md-4 col-sm-4">
<img src="https://dummyimage.com/600x400/000/fff" />
</div>
</div>
</div>
<div class="row col-md-12">
#foreach($images as $image)
<div class="col-md-3">
<img src="{{ $image->image }}" alt="{!! $image->title !!}" width="100" height="auto" >
<div class="btn btn-primary edit-image-btn">Edit</div>
<div class="btn btn-danger delete-image-btn">Delete</div>
</div>
#endforeach
</div>

How to fill the gap of a div without using media query?

I'm using bootstrap cards to build this layout on desktop:
So far so good, at this width everything is ok as well:
But from this width to desktop width the container is not filled:
The HTML to build the card with the images is:
<div class="card h-60">
<div>
<div class="row box">
<div class="box-content formal">
<div class="col-md-12">
<img class="imageban" src="img/crm.png" />
<span><b>CRM</b></span>
</div>
</div>
<div class="box-right">
<a href="#">
<img class="img-responsive" src="img/img1.jpg"/>
</a>
</div>
</div>
</div>
<div>
<div class="row box">
<div class="box-content report">
<div class="col-md-12">
<img class="imageban" src="img/report.png" />
<span><b>Report</b></span>
</div>
</div>
<div class="box-right">
<a href="#">
<img class="imageban" src="img/img4.jpg"/>
</a>
</div>
</div>
</div>
<div>
<div class="row box">
<div class="box-content formal">
<div class="col-md-12">
<img class="img-responsive" src="img/formal_notices.png" />
<span><b>Formal Notices</b></span>
</div>
</div>
<div class="box-content text">
<a href="#">
Open the Formal <br>Notice Document <br>Library
</a>
</div>
</div>
</div>
</div>
And the CSS is only this line:
.row.box{
max-width: 345px;
}
I'm stuck in this, any help would be nice. Thanks.
You can remove the class .box and use the Bootstrap classes instead col-md-4 col-xs-12.

4 Centered Images HTML/ CSS BOOTSTRAP

--FIRST IMAGE IS DESIRE OUTPUT---
I got it to work on my screen but when I put it on bigger monitor doesn't seem to scale right. Any Recommendations ?
<div style="margin-left: 100px;margin-right: 100px;">
<div class="container-fluid">
<div class="row">
<div class="col-sm-3">
<img src="img/NUESTROS EVENTOS -1.png">
</div>
<div class="col-sm-3">
<img s src="img/NUESTROS EVENTOS -2.png">
</div>
<div class="col-sm-3">
<img src="img/NUESTROS EVENTOS -3.png">
</div>
<div class="col-sm-3">
<img src="img/NUESTROS EVENTOS -5.png">
</div>
</div>
</div>
This is what I get with this other code, centers but can't get rid the spaces.
---THIS IS WHAT I GET---
<div class="row" style="margin-bottom: 70px;">
<!-- column -->
<div class="col-xs-6 col-sm-5cols wow bounceInLeft ">
<div class="thumbnail ">
<img src="img/NUESTROS EVENTOS -1.png">
</div>
</div>
<!-- column -->
<div class="col-xs-6 col-sm-5cols wow bounceInLeft ">
<div class="thumbnail">
<img src="img/NUESTROS EVENTOS -2.png">
</div>
</div>
<!-- column -->
<div class="col-xs-6 col-sm-5cols wow swing ">
<div class="thumbnail">
<img src="img/NUESTROS EVENTOS -3.png">
</div>
</div>
<!-- column -->
<!-- column -->
<div class="col-xs-6 col-sm-5cols wow bounceInRight ">
<div class="thumbnail">
<img src="img/NUESTROS EVENTOS -5.png">
</div>
</div>
</div>
You can use inline-block elements, I have created inline-block class to wrap img and gave text-center class to row to align images in center.
.row-custom .inline-block{
display:inline-block;
}
.row-custom .inline-block img{
max-width:100px
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row row-custom text-center">
<div class="inline-block">
<img src="https://placeholdit.imgix.net/~text?txtsize=28&txt=300%C3%97300&w=300&h=300">
</div>
<div class="inline-block">
<img s src="https://placeholdit.imgix.net/~text?txtsize=28&txt=300%C3%97300&w=300&h=300">
</div>
<div class="inline-block">
<img src="https://placeholdit.imgix.net/~text?txtsize=28&txt=300%C3%97300&w=300&h=300">
</div>
<div class="inline-block">
<img src="https://placeholdit.imgix.net/~text?txtsize=28&txt=300%C3%97300&w=300&h=300">
</div>
</div>
Don't need to change or add more into CSS, You can use the Bootstrap class:
<div class="row">
<div class="col-sm-3">
<img class="img-responsive center-block" src="img/NUESTROS EVENTOS -1.png">
</div>
<div class="col-sm-3">
<img class="img-responsive center-block" src="img/NUESTROS EVENTOS -2.png">
</div>
<div class="col-sm-3">
<img class="img-responsive center-block" src="img/NUESTROS EVENTOS -3.png">
</div>
<div class="col-sm-3">
<img class="img-responsive center-block" src="img/NUESTROS EVENTOS -5.png">
</div>
</div>
Do these 3 things to achieve the desire result-
1. Add .img-responsive class to your img tags as its a way to make images responsive in bootstrap framework. You can also check this link for reference .
2. Add width="100%" to images to take up full space available in the image.
3. Add nopadding class to columns so that it will remove the padding space.
Update your code as below:
.nopadding {
padding: 0 !important;
margin: 0 !important;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="row">
<div class="col-xs-3 nopadding">
<img src="https://dummyimage.com/600x400/000/fff" width="100%" class="img-responsive">
</div>
<div class="col-xs-3 nopadding">
<img s src="https://dummyimage.com/600x400/000/fff" class="img-responsive" width="100%">
</div>
<div class="col-xs-3 nopadding">
<img src="https://dummyimage.com/600x400/000/fff" class="img-responsive" width="100%">
</div>
<div class="col-xs-3 nopadding">
<img src="https://dummyimage.com/600x400/000/fff" class="img-responsive" width="100%">
</div>
</div>
</div>

Collapse 4 bootstrap columns into 2

Considering this footer:
<footer id="footer" class="dark" style="background-color:#220E2F !important;">
<div id="footer-container_k" style="margin: auto; display:block;" class="row">
<div class="col-md-2 col-xs-6 col-md-offset-2" style="position: relative; left:8%;" id="first">
<div class="buffer">
<p>About</p>
<!--<p>Careers</p>-->
<p>Press</p>
<p>Blog</p>
</div>
</div>
<div class="col-md-2 col-xs-6" style="position: relative; left:2%;" id="second">
<div class="buffer">
<p>Contact</p>
<p>Support</p>
<p>Elexa USA</p>
</div>
</div>
<div class="col-md-2 col-xs-6" style="position: relative; right:4%;" id="third">
<div class="buffer">
<p>Z-Wave Products</p>
<a href="https://domeha.com/guardian" target="blank" ><p>Guardian Products</p></a>
<p>How-Tos</p>
<!--<a href="/news" target="blank" ><p>Dome News</p></a>-->
</div>
</div>
<div class="col-md-2 col-xs-6" style="position: relative; right:10%;" id="last">
<div class="buffer">
<a href="https://www.youtube.com/channel/UCuqjZpsNcaWkbH6T1_PTQ3Q" target="blank" ><p>YouTube</p></a>
<a href="https://www.facebook.com/DomeByElexa" target="blank" ><p>Facebook</p></a>
<a href="https://twitter.com/DomeByElexa" target="blank" ><p>Twitter</p></a>
<a href="https://www.linkedin.com/company/16181754"target="blank" ><p>LinkedIn</p></a>
</div>
</div>
</div>
How would I refactor this to collapse into two columns instead of one without positioning them, like I am now? Positioning seems hacky and seems to break very easily.
Remove all the extra styles you added on and they will stack. However the order will be wrong because of the stacking. To reorder try the push and pull approach, mobile first.
http://www.bootply.com/DoMgBuRpDj
<div class="col-md-2 col-xs-6 col-md-push-2" id="third">..</div>
<div class="col-md-2 col-xs-6 col-md-pull-2" id="second">...</div>