How to Center an Image - html

Currently I have an image saved in my project of a map that I would like to include in my solution, my first website. I'm very new to ASP.NET and CSS but have some HTML experience. I am having trouble figuring out how to center and stretch the image appropriately so that regardless of the size of the browser window, the center of the map always remains centered accordingly.
Please advise!
<section>
<div class="container-fluid">
<div class="row">
<img src="/img/additional/map.png"/>
</div>
</div>
</section>
EDIT 1:
I've found on http://getbootstrap.com/css/#images the following information
Responsive images
Images in Bootstrap 3 can be made responsive-friendly via the addition of the .img-responsive class. This applies max-width: 100%;, height: auto; and display: block; to the image so that it scales nicely to the parent element.
To center images which use the .img-responsive class, use .center-block instead of .text-center. See the helper classes section for more details about .center-block usage.
SVG images and IE 8-10
In Internet Explorer 8-10, SVG images with .img-responsive are disproportionately sized. To fix this, add width: 100% \9; where necessary. Bootstrap doesn't apply this automatically as it causes complications to other image formats.
<img src="..." class="img-responsive" alt="Responsive image">

Added class="img-responsive center-block" according to http://getbootstrap.com/css/#images .
<section>
<div class="row">
<img class="img-responsive center-block" src="/img/additional/map.png"/>
</div>
</section>

Just simply add center tag to the image.
<section>
<div class="container-fluid">
<div class="row">
<center>
<img src="/img/additional/map.png"/>
</center>
</div>
</div>
</section>

If are you using bootstrap, you can do it:
<div class="text-center">
<img src="..." class="rounded" alt="...">
</div>

You could also use pure CSS, for example:
.
center {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
}

add class center-block
Ref : here
e.g : <img class="center-block" src="/img/additional/map.png"/>

Related

Why can't I fix my image to the left side of my bootstrap website?

I have tried using ml-auto class, float: left, the float-left class, and fixed-left class, but none of those have worked in fixing my image to the left. I don't want to use margin since that's not responsive on different sizes(some white-space is present there most of the time). I have looked at other people who had the same question, but the answers to those were just the owner having a typing mistake, and after looking through mine, I haven't found any.
Please leave an explanation or a link so I can learn which of these methods to use to push my image to the right and which is more suitable for different situations. Here's my JSfiddle, this includes all of my HTML and CSS.
https://jsfiddle.net/1kdtjmh8/
Here is the code for the image I'm having issues with
<div class="container">
<div class="row">
<div class="col-lg img-col">
<img class="img-fluid home-image ml-auto" src="HomeImage.jpg" alt="">
</div>
Here is the CSS
.home-image{
height: 100vh!important;
width: auto!important;
display: block;
float:left;
}
You are using container which has a max width of 1140px which is creating the space. you can replace that with container-fluid and this should solve the issue
Please go through below codepen.io URL you will find the working solutions for your query.
It can resolved with two resolutions:-
By making the container class to container-fluid and keep all the css and html as it is.
Visit this Link:- https://codepen.io/nimesh049/pen/NWjaxaY
.userImage{
height: 150px!important;
width: auto!important;
float:right;
}
<div class="container">
<div class="row">
<h1>Image Holder</h1>
<div class="col-lg img-col">
<img class="d-block userImage " src="https://www.pngfind.com/pngs/m/610-6104451_image-placeholder-png-user-profile-placeholder-image-png.png" alt="" />
</div>

Setting Images Side by Side & responsive in bootstrap

I have written a code where 4 images are kept inside div side by side in cols.
Below is the code :
<div class="container-fluid">
<div class="row">
<div class="col-xs-3 col-sm-3 col-md-3">
<img src="image2.jpg" alt="1" style="width:100%; height:70%;" class="img-responsive" />
</div>
<div class="col-xs-3 col-sm-3 col-md-3">
<img src="image3.jpg" alt="1" style="width:100%; height:70%;" class="img-responsive"/>
</div>
<div class="col-xs-3 col-sm-3 col-md-3">
<img src="image4.jpg" alt="1" style="width:100%; height:70%;" class="img-responsive"/>
</div>
<div class="col-xs-3 col-sm-3 col-md-3">
<img src="image5.jpg" alt="1" style="width:100%; height:70%;" class="img-responsive"/>
</div>
</div>
</div>
This looks like this in full size window :
enter image description here
Now when the window size is minimized, the images looks like this :
enter image description here
I want the image to look the way it looks in the full size even after minimizing without the image getting stretched.
I was able to get the view of above images without it getting stretched when i removed the style: width:100%; height:70%; but then in full sized window the images had blank spaces between each other.
Please help me to get this sorted.
Using
class="img-responsive"
will make your images responsive across devices. In specific, and as per Bootstrap's documentation, it will add
max-width: 100%;
height: auto;
display: block;
to your image elements.
You will need to lose your inline styles
style="width:100%; height:70%;"
as they override everything else (Protip: never use them).
Lastly, if you don't want the blank spaces between images you'll need to force remove the padding for each column. Example:
div.col-xs-3 {
padding: 0;
}
Here is a sample codepen.
#FredRocha hey Fred, I tried using only img-reponsive & losing the inline styles but then the images were looking like this in full screen window size:
Example image
Maybe adding img-responsive class to image will fix the problem.

responsive img with boostrap

I need to make a logo in my footer responsive, and it should be centered. I thought I could solve it the following way with bootstrap:
HTML
<!-- Footer -->
<div class="container-fluid">
<div class="row">
<div class="landing img-responsive footer">
<div>
<img src="~/img/logo-white-small.png" />
</div>
</div>
</div>
</div>
CSS
.landing.img-responsive.footer {
margin: 0 auto;
}
But is that incorrect?
I would suggest you to use
<center><img src="~/img/logo-white-small.png" /></center>
or alternatively
<div style="text-align:center" class="img-responsive">
<img src="~/img/logo-white-small.png" />
</div>
Also check the space between .landing .img-responsive .footer between these classes in you css.
You can use:
img {
display: table;
margin: auto;
}
To center the image, while adding the .img-responsive class part of bootstrap to make it responsive.
https://jsfiddle.net/c79bstuu/
User img-responsive class for responsive image.
Use center-block class to horizontallyalign an element center.
<img src="~/img/logo-white-small.png" class="img-responsive center-block" />
it will make your image responsive as well as horizontally center aligned.
anyhow if you want vertically align then you should use display: table-cell for child div and display:table for parent div. You also need to remove float property from child div.

Bootstrap responsive image will not stay in center

I'm trying to line up three pictures within a bootstrap grid. All of the images were centered, which is what I'm trying to, before I added a img-responsive class to one of the images. I do however want the images to be responsive while also making them a tiny bit larger than they are already. Tried numerous things such as "width: 200%;" however without success
How it looks
I created a "center-all" class within my css that should center all the three of them, and it did work, just until I added the img-responsive class to the red one. The class was not added to any of the other images.
.center-all {
text-align: center;
align-items: center;
align-content: center
vertical-align: center;
}
JSFiddle
https://jsfiddle.net/8a6ee7f5/
Images doesn't work in Fiddle ofc.
What I want to achieve:
I want the images to be 2 times larger than what they currently are and I want them to be responsive while still keeping them centered.
Thanks in advance!
I found the answer to my question(s).
When adding the img-responsive class to the image it moves to the left. By adding "center-block" to the class to the image it should stay in the center.
<img src="..." class="img-responsive center-block">
Now I just need to find the answer on how to upscale my pictures to 200%.
EDIT:
I found the following to upscale my images:
CSS
.wrapper {
width: 40%;
}
and added that to my image class.
HTML
<img src="..." class="img-responsive center-block wrapper">
And that fixed it.
So bootstrap already has an element centering class called .text-center you can just add this class to the row wrapping your items and everything in that row will be centered. Then instead of using .img-responsive you can add a width to the image instead or give the image a percentage width. It will maybe look something like the following hopefully this is what you are looking for:
Here is a fiddle Updated Fiddle
note: I have added a border-radius of 50% to your images because I assume you want them to be round. If you want to use 100px you can change it back.
Css:
#values img{
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
border-radius: 50%;
width: 80%;
}
#val1 {
background-color: #792c24;
}
#val2 {
background-color: #4f5e6d;
}
#val3 {
background-color: #665e25;
}
Html:
<div class="container" id="values">
<h3 class="fancy">Values</h3>
<div class="row text-center">
<div class="col-sm-4">
<h4>Independent</h4>
<img id="val1" src="http://placehold.it/300x300">
</div>
<div class="col-sm-4">
<h4>Team Player</h4>
<img id="val2" src="http://placehold.it/300x300">
</div>
<div class="col-sm-4">
<h4>Dedicate</h4>
<img id="val3" src="http://placehold.it/300x300">
</div>
</div>
</div>

Responsive image align center bootstrap 3

I do a catalog using Bootstrap 3. When displayed on tablets, the product images look ugly because of their small size (500x500) and a width of 767 pixels in the browser. I want to put the image in the center of the screen, but for some reason I can not. Who be will help solve the problem?
There is .center-block class in Twitter Bootstrap 3 (Since v3.0.1), so use:
<img src="..." alt="..." class="img-responsive center-block" />
If you're using Bootstrap v3.0.1 or greater, you should use this solution instead. It doesn't override Bootstrap's styles with custom CSS, but instead uses a Bootstrap feature.
My original answer is shown below for posterity
This is a pleasantly easy fix. Because .img-responsive from Bootstrap already sets display: block, you can use margin: 0 auto to center the image:
.product .img-responsive {
margin: 0 auto;
}
Add only the class center-block to an image, this works with Bootstrap 4 as well:
<img src="..." alt="..." class="center-block" />
Note: center-block works even when img-responsive is used
Just use .text-center class if you're using Bootstrap 3.
<div class="text-center">
<img src="..." alt="..."/>
</div>
Note: This doesn't work with img-responsive
This should center the image and make it responsive.
<img src="..." class="img-responsive" style="margin:0 auto;"/>
I would suggest a more "abstract" classification. Add a new class "img-center" which can be used in combination with .img-responsive class:
// Center responsive images
.img-responsive.img-center {
margin: 0 auto;
}
Simply put all the images thumbnails inside a row/col divs like this:
<div class="row text-center">
<div class="col-12">
# your images here...
</div>
</div>
and everything will work fine!
You can use property of d-block here or you can use a parent div with property 'text-center' in bootstrap or 'text-align: center' in css.
Image by default is displayed as inline-block, you need to display it as block in order to center it with .mx-auto. This can be done with built-in .d-block:
<div>
<img class="mx-auto d-block" src="...">
</div>
Or leave it as inline-block and wrapped it in a div with .text-center:
<div class="text-center">
<img src="...">
</div>
You can still work with img-responsive without impacting other images with this style class.
You can precede this tag with the section id/ div id/class to define a order within which this img is nested. This custom img-responsive will work only in that area.
Suppose you have a HTML area defined as:
<section id="work">
<div class="container">
<div class="row">
<img class="img-responsive" src="some_image.jpg">
</div>
</div>
</section>
Then, your CSS can be:
section#work .img-responsive{
margin: 0 auto;
}
Note: This answer is in relation to the potential impact of altering img-responsive as a whole. Of course, center-block is the simplest solution.
Try this code it will work for small icons too with bootstrap 4 because there is no center-block class is bootstrap 4 so try this method it will be helpful. You can change the position of the image by setting the .col-md-12 to .col-md-8 or .col-md-4, it's upto you.
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="text-xs-center text-lg-center">
<img src="" class="img-thumbnail">
</div>
</div>
</div>
</div>
Try this:
.img-responsive{
display: block;
height: auto;
max-width: 100%;
margin:0 auto;
}
.Image{
background:#ccc;
padding:30px;
}
<div class="Image">
<img src="http://minisoft.com.bd/uploads/ourteam/rafiq.jpg" class="img-responsive" title="Rafique" alt="Rafique">
</div>
#media (max-width: 767px) {
img {
display: table;
margin: 0 auto;
}
}
To add to the answers already given, having the img-responsive in combination with img-thumbnail will set display: block to display: inline block.
<div class="col-md-12 text-center">
<img class="img-responsive tocenter" />
</div>
.
<style>
.tocenter {
margin:0 auto;
display: inline;
}
</style>
<div class="text-align" style="text-align: center; ">
<img class="img-responsive" style="margin: auto;" alt="" src="images/x.png ?>">
</div>
you can try this.
You can fix it with defining margin:0 auto
or you can use col-md-offset also
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<style>
.img-responsive{
margin:0 auto;
}
</style>
<body>
<div class="container">
<h2>Image</h2>
<div class="row">
<div class="col-md-12">
<p>The .img-responsive class makes the image scale nicely to the parent element (resize the browser window to see the effect):</p>
<img src="http://www.w3schools.com/bootstrap/cinqueterre.jpg" class="img-responsive" alt="Cinque Terre" width="304" height="236">
</div>
</div>
</div>
</body>
</html>
The more exact way applied to all Booostrap objects using standard classes only would be to not set top and bottom margins (as image can inherit these from parent), so I am always using:
.text-center .img-responsive {
margin-left: auto;
margin-right: auto;
}
I have also made a Gist for that, so if any changes will apply because of any bugs, update version will be always here:
https://gist.github.com/jdrda/09a38bf152dd6a8aff4151c58679cc66
So far the best solution to accept seems to be <img class="center-block" ... />. But no one has mentioned how center-block works.
Take Bootstrap v3.3.6 for example:
.center-block {
display: block;
margin-right: auto;
margin-left: auto;
}
The default value of dispaly for <img> is inline. Value block will display an element as a block element (like <p>). It starts on a new line, and takes up the whole width. In this way, the two margin settings let the image stay in the middle horizontally.
2021.09 from a project:
<div class="d-flex" style="height: 60px; width: 60px;">
<img alt="ddd" src="myurl" class="m-auto"/>
</div>