centering image in div - html

I have a square <div> (70px x 70px) which will contain an image of a variable dimensions(Square, landscape or potrait). I want this image to be symmetrically centered inside the <div>. how do I get it..?
<div class="img-polaroid" style="width: 70px; height: 70px; background-color: black; text-align:center;">
<image src='.base_url("images/store/images/".$image->image).' />
</div>
The actual size of the image can be greater than 70px x 70px. But it should fit symmetrically in the center.
I also have to make it cross-browser compatible..
Help Appreciated...

Must it be an <img> element?
You can set the image as background of the <div> with background-position:center center - this is very easy to do, not using javascript and cross-browser..
Use it like this:
<div class="img-polaroid" style="width: 70px; height: 70px; background:url('.base_url("images/store/images/".$image->image).') center center no-repeat black; text-align:center;"></div>
Check this Working jsFiddle. In terms of browser support I've never come across a browser that doesn't support this.
It is sure to work on:
Netscape 6+
Mozilla 1+
Firefox 1+
Internet Explorer 4+
Opera 3+
Safari 1+

This will do the trick for u
.img-polaroid img{display:block; margin:0 auto;}

<div class="img-polaroid" style="width: 70px; height: 70px; background-color: black; text-align:center;">
<image src='.base_url("images/store/images/".$image->image).' />
</div>
should be
<div class="img-polaroid" style="width: 70px; height: 70px; background-color: black;display:table-cell;vertical-align:middle;text-align:center;">
<image src='.base_url("images/store/images/".$image->image).' />
</div>
add display:table-cell;vertical-align:middle;

Use display: table-cell; for your div with your text-align: center; and vertical-align: middle;
Below is the code:
<div class="img-polaroid" style="width: 70px; height: 70px; background-color: black; border: 1px solid yellow; text-align:center; vertical-align: middle; display: table-cell;">
<image src="http://www.gelifesciences.com/gehcls_images/GELS/Link%20Images/Product%20Link%20Images/ImageScanner%20III%2050x50.jpg" />
</div>
and here is the example: http://jsfiddle.net/eUGb6/
Here you can find more about compatibility issues:
vertical-align text-align display

It's very simple to do it vertically/horizontally alignment.
First build a holder div:
.holder {
display: table;
}
Then build a container div:
.container {
display: table-cell;
vertical-align: middle;
text-align: center
}
And in your container, it would be shown everything centered.
I didn't test it yet.. but should work ;p
<div class="holder">
<div class="container">
<img src="YOURIMAGE" />
</div>
</div>

Its easy :)
.img-polaroid {
display:table-cell;
vertical-align: middle
}
check fiddle http://jsfiddle.net/9hppL/2/

You can use the display:inline-block and :after for div, like this:
div {
width: 70px;
height: 70px;
text-align: center;
border: 1px solid green;
}
div:after {
content:"";
display: inline-block;
width:0;
height: 100%;
vertical-align: middle;
}
img {
vertical-align: middle;
}
Please view the demo.Click here, you can look at other solutions.

This is your ultimate guide to centering things in CSS> http://www.w3.org/Style/Examples/007/center
Here is a working jsfiddle: http://jsfiddle.net/9hppL/
.img-polaroid {
display: table-cell;
vertical-align: middle
}
#image {
background: blue;
width: 50px;
height: 50px;
margin-left: auto;
margin-right: auto;
}

Related

Height of div is greater than image

I am a rookie for front-end development. Recently, I wrote codes like:
<div style="background-color:red">
<img src='https://www.logaster.com/blog/wp-content/uploads/2013/06/jpg.png'>
</div>
The height of image(logo.jpg) is 80px, but the height of div is 82px. Why?
You can show image like a block to fix that,
<div>
<img style="display:block" src='logo.jpg'>
</div>
<div style="height:your height; width:your witdh;">
<img src='logo.jpg'>
</div>
To change the height or width you can do what i did above with inline style. or give the div a class or give the div an id and style it in an external stylesheet.
You need to write proper css to achieve this.
<div class="wrap">
<div class="box1">
<img src="http://www.placekitten.com/500/500">
</div>
</div>
.box1 {
width:auto;
height: auto;
max-width: 600px;
max-height: 300px;
background-color:chocolate;
padding:5px;
display: inline-block;
}
.box1 img {
vertical-align: top;
max-width: inherit;
max-height: inherit;
}
.wrap {
border: 1px dotted gray;
margin: 1.00em 0;
text-align: center;
}
JsFiddle : https://jsfiddle.net/nikdtu/75nu1a4m/

issues using display: inline-block

I'm somewhat experienced in html/css coding, but more in the "old school" sense.
My question: When I use display: inline-block on two divs, they stack on top of each other, despite the fact that they have a defined width and height. and lots of room to float beside.
Any advice would be greatly appreciated.
(I would attach file to view but it is long, more than willing to zip and send for help)
Cheers
Please see this simple example may be help you to resolve your problem.
Here is a sample div, add this to your code base and see the output
<style type="text/css">
.te {
display: inline-block;
border: solid #0000FF 1px;
width: 100px;
height: 100px;
}
</style>
<div style="text-align: center;">
<div class="te">
hello
</div>
<div class="te">
hello there
</div>
</div>
Demo Fiddle
You can just float them left.
css:
.block1 {
display: inline-block;
float: left;
background-color: blue;
width: 100px;
height: 100px;
}
.block2 {
display: inline-block;
float: left;
background-color: green;
width: 100px;
height: 100px;
}
html:
<div class="block1"></div>
<div class="block2"></div>
JSFiddle demo

Assigning horizonatal div's height dynamically

Hi I have an issue in assigning height dynamically to div areas.
Following is a demo layout type that I need to generate.
According to Dynamic content 1 and Dynamic content 2 the Gray color div automatically adjust its height as I have defined Height:auto in that layout. Thats fine.
The problem that I encounter is how to dynamically set the same height for side Div layouts(which I have indicated in Orage color). I tried with the following HTML and CSS but it doesn't work to get the height automatically in orange colored divs...
following is the HTML and CSS i tried with.
<div style="margin: auto; width: 510px; height: auto;">
<div style="display: inline-block; width: 20px; height: 100%; background-color: Orange; vertical-align: top;">
</div>
<div style="display: inline-block; width: 450px; height: auto; vertical-align: top; margin-left: -4px; margin-right: -4px; background-color: #bebebe;">
<div style="margin: auto; width: 200px; height:50px; background-color:Green; text-align:center"> Dynamic content 1 </div>
<div style="margin: auto; width: 200px; height:50px; background-color:Olive; text-align:center"> Dynamic content 2 </div>
</div>
<div style="display: inline-block; width: 30px; height: 100%; background-color: Orange; vertical-align: top;">
</div>
</div>
Following is the result I am getting for the above HTML.
It doesn't assign the height property automatically for the side divs. Even for the Right hand side div.
Not sure what I am doing wrong and would grateful if can guide me in where I am going wrong in this case. Thanks....
NOTE
The side orage colred divs are not borders.
1) add display:table to the container div. and
2) give your 'orange' divs display:table-cell
(Chrome doesn't require step 2, but other browsers like FF and IE do.)
FIDDLE
Try:
<div style="text-align:center; margin: auto; width: 510px; height: auto; background-color: Orange; vertical-align: top;">
<div style="display: inline-block; width: 450px; height: auto; vertical-align: top; margin-left: -4px; margin-right: -4px; background-color: #bebebe;">
<div style="margin: auto; width: 200px; height:50px; background-color:Green; text-align:center"> Dynamic content 1 </div>
<div style="margin: auto; width: 200px; height:50px; background-color:Olive; text-align:center"> Dynamic content 2 </div>
</div></div>
http://jsfiddle.net/dgjas/3/

Vertically aligned image and text div

UPDATE: The answers have got me close, but they still don't align vertically as the text div is larger, how can I make them both the same height and therefore align?
I would like to have two DIVs next to each other, one containing an image and one containing text, both sitting in a container DIV.
The image should be 15% of the width of the container div, with the text using the remaining 85%
The image and text should be aligned vertically within their respective DIVs, so it looks like they are aligned with each other.
I've tried to work this out but can't seem to do it! Can anyone help?
#picture {
float: left;
width: 15%;
line-height: auto;
}
#text {
width: auto;
padding-left: 16%;
line-height: auto;
vertical-align: middle;
margin-top: auto;
margin-bottom: auto;
}
#text p {
display: inline-block;
vertical-align: middle;
line-height: normal;
}
and
<div id="quotes">
<div id="picture">
<img style="width: 100%; vertical-align: middle" src="tom.jpg" >
</div>
<div id="text">
<p>"Christiaan was one of the stand out candidates throughout, therefore there was no hesitation in offering him a place on this highly sort after scheme..."</p>
</div>
</div>
Here's a fiddle with your code in it: http://jsfiddle.net/hQ6Vw/1/
The only changes I made was to assign matching top/bottom margins to the img and p tags. I think that will give you the effect you're looking for.
If you use float and verticl-align, those two won'nt work together.
Float extract itself from regular flow and go slide on one side or the other on top of next line right after any content within the regular flow.
Vertical-align works:
in betweem inline-boxes (inline-block-level element or displayed so with display:inline-block;)
inside td or it's CSS default display : display:table-cell;
here jsfiddle #TXChetG updated
Using display:inline-block; http://jsfiddle.net/GCyrillus/hQ6Vw/2/
Using display:table/* table-cell*/;
http://jsfiddle.net/GCyrillus/hQ6Vw/3/
This should get you close:
<div>
<div style="background: grey; width: 15%; float:left"></div>
<div style="background: blue; width: 85%; float:left"></div>
</div>
Replace the grey background div with your image and the blue with your text.
Check this out
HTML:
<section>
<div id="one"></div>
<div id="two"></div>
</section>
CSS:
section {
width: 80%;
height: 200px;
background: aqua;
margin: auto;
padding: 10px;
}
div#one {
width: 15%;
height: 200px;
background: red;
float: left;
}
div#two {
margin-left: 15%;
height: 200px;
background: black;
}
Is this what you mean?
html
<div class="container">
<div class="images">
<img src="http://jsfiddle.net/img/logo.png" style="background-color:black">
</div>
<div class="text">
Example
</div>
</div>
<div class="container">
<div class="images">
<img src="http://jsfiddle.net/img/logo.png" style="background-color:black">
</div>
<div class="text">
Example
</div>
</div>
css
.container {
clear: both;
}
.images {
width: 15%;
float: left;
vertical-align: text-top;
}
.text {
width: 85%;
float: right;
vertical-align:text-top;
}
Why not just set the #text p display to display: inline or display:block; or use margins to align them?
<div id="quotes">
<div id="picture">
<img src="tom.jpg" />
</div>
<div id="text">
<p>"Christiaan was one of the stand out candidates throughout, therefore there was no hesitation in offering him a place on this highly sort after scheme..."</p>
</div>
</div>
Display the container div as table and the text and image divs as table-cell to make them the same heights. You can then centre the image vertically through vertical-align:middle.
#quotes {
display:table;
}
#picture {
width: 15%;
display:table-cell;
vertical-align: middle;
}
#text {
display:table-cell;
width:85%;
padding-left: 16%;
}
#picture img {
width: 100%;
}
http://jsfiddle.net/X3WsV/1/

Vertical-align: middle - img in div (different size picture)

I was not successful CSS. Should be in the center of the picture (different size) in the div. See an example:
And examples of html and css - http://jsfiddle.net/KJXUt/
HTML:
<div class="box">
<a href="#" class="pic">
<img src="http://pics.posternazakaz.ru/pnz/product/small_x2/97/61/ccc0980cde08f9032cd665f7104aca10.jpg" alt="" width="300" height="130" />
</a>
</div>
<div class="box">
<a href="#" class="pic">
<img src="http://www.22.ru/ow_userfiles/plugins/photo/photo_preview_137.jpg" alt="" width="140" height="140" />
</a>
</div>​
CSS:
body {
background: #D6E8FF;
}
.box {
border: #555 1px solid;
margin-right: 10px;
width: 100px;
height: 100px;
overflow: hidden;
display: table-cell; /* <-- problem */
vertical-align: middle; /* <-- problem */
}
img {
max-width: 100px;
height: auto;
}
​
How to correct style? Help, please. Thanks in advance
The term to make a picture smaller without re-sizing it is called "cropping", to directly achieve your answer, a different css property: background will be used. And the div's will be removed. #Heliio's method is great if you need <div> tags.
A code sample is as following
HTML:
<a href="#" class="pic pic1">
</a>
CSS:
.pic {
display:inline-block;
border: #555 1px solid;
margin-right: 10px;
width: 100px;
height: 100px;
}
.pic1{
background: transparent url(http://mw2.google.com/mw-panoramio/photos/medium/62256112.jpg) -20px -200px no-repeat;
}
Note that the css has some negative values, these are extremely tedious to modify, but it's really just -((imagewidth-100)/2) x -((imageheight-100)/2)
http://jsfiddle.net/KJXUt/9/
When you use display: table-cell; it's forcing to fixed size to the cells. Maybe it's not a good solution.
I dont understand, if want really it, but:
body {
background: #D6E8FF;
}
.box {
border: #555 1px solid;
float:left;
width: 100px;
height: 100px;
overflow: hidden;
}
img {
max-width: 100px;
}
Example here: http://jsfiddle.net/KJXUt/1/
Or, if prefer: http://jsfiddle.net/KJXUt/3/
This is a simple technique to center the image vertically in .box
http://codepen.io/AntonNiklasson/pen/pkaov
To add more boxes next to eachother, add float: left to .box