Image example of what I need
I basically copied the code off of a YouTube video. I am a rookie so try and explain as easily as possible how to stack two images on top of each other.
They are the same width and same height images and need to be aligned horizontally and vertically.
.image {
position: absolute;
top: 50%;
left: 50%;
margin-top: -100px;
margin-left: -260px;
}
<div class="image">
<img src="car.png">
</div>
There are a few ways to do this. The most simple would probably be to edit your CSS to do the following:
CSS:
.image {
width: 100%; /* Image container is now full-width */
}
.image img {
margin: 40px auto; /* "auto" will center block elements */
display: block; /* Set images to be "block" so they obey our auto margin */
}
HTML:
<div class="image">
<img src="path/to/image1.jpg">
<img src="path/to/image2.jpg">
</div>
JSFiddle
For horizontal and vertical centering:
While some may prefer the flex method, I prefer the table-cell method for simple alignment. Try this:
CSS:
.image {
width: 100%;
height: 500px /* Modify this to fit your needs */
display: table;
}
.image .centered {
display: table-cell;
vertical-align: middle;
}
.image .centered img {
margin: 40px auto;
display: block;
}
HTML:
<div class="image">
<div class="centered">
<img src="http://fillmurray.com/360/100">
<img src="http://fillmurray.com/360/100">
</div>
</div>
JSFiddle
If the question is only to put second image under the first just br tag
If the question is to make on blank page 2 images in center one under one:
<table style="width: 100%; height: 100%; border-spacing: 0px; border-collapse: collapse; padding: 0px; margin: 0; border: 0;">
<tr style="height: 100%">
<td style="text-align: center; vertical-align: middle; height: 100%;">
<img src="1.jpg" /><br><br>
<img src="2.jpg" />
</td>
</tr>
</table>
For body and html you need also height: 100%
I'm pretty sure all positioning is possible with div so probably I'm complicated with tables but I was too tired to find the code for vertical positioning so I used tables in my work.
Related
Need help centering these images in CSS
I have been trying to center them by using a div id tag
<div id="centerLeftAboutPic">
<div class="single-about-detail clearfix">
<div class="about-img">
<img src="img/AttyRLev.jpg" alt="">
</div>
<div class="about-details">
<div class="pentagon-text">
<h1>R</h1>
</div>
<h3>Atty Rob Lev</h3>
<p>Click here to learn more about robert lev</p>
</div>
</div>
</div>
I also created a separate div ID for the second picture. Here is the CSS for one of the images. Both images have similar css.
#centerLeftAboutPic {
float: right;
width: 320px;
padding-left: 30px;
position: relative;
}
I am new to web developing so I am still confused on positioning. Thank you.
You can use the below in your css
text-align:center
snippet
#centerLeftAboutPic {
text-align:center;
padding-left:30px;
position: relative;
border:solid black;
}
img{
width:50px;
height:50px;
margin-left:70px;
}
<div id="centerLeftAboutPic">
<img class="img-responsive" src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcRH181kjqkxFXqYU4bTP8zdfiAfO4iceJrxA4lMPXMCKY61eX9v" /></a>
<img class="img-responsive" src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcRH181kjqkxFXqYU4bTP8zdfiAfO4iceJrxA4lMPXMCKY61eX9v" /></a>
<div>
</div>
If you want to center the image relative to its container then all you need to do is add this to its CSS.
#centerLeftAboutPic img {
margin-left: auto;
margin-right: auto;
display: block;
}
However it's only going to center it within the 320px container you gave it in #centerLeftAboutPic. If you adjusted that property to width: 100%; it will center it on the page.
Here's a fiddle for you. I set the width to 100% in the example, play around with it and you'll see what I mean: https://jsfiddle.net/v5k8rjy2/2/
If you want to center the entire #centerLeftAboutPic div you'll need to put the margins on the div its self and remove the float: right property. Here's a fiddle of that: https://jsfiddle.net/v5k8rjy2/5/
#centerLeftAboutPic {
width: 320px;
position: relative;
margin-left: auto;
margin-right: auto;
display: block;
}
I have a header with a div which have display:table; max-width: 800px. It should act as a frame to restrict the contents width. Inside the frame are images which auto-scale and are nested inside div's with display:table-cell.
Everything is working on Chrome and Mobile Safari, but Firefox and IE are not restricting the frame width.
jsFiddle
Can anybody help me, please ;(
Set the table to have table-layout: fixed and a width of 100%.
.frame {
display: table;
max-width: 800px;
width: 100%;
height: 300px;
margin: 0 auto;
padding: 10px;
background: #ccc;
table-layout: fixed
}
.item {
display: table-cell;
vertical-align: middle;
padding: 0 5px;
}
img {
max-width: 100%;
height: auto;
}
<div class="frame">
<div class="item">
<img src="http://lorempixel.com/250/250" />
</div>
<div class="item">
<img src="http://lorempixel.com/250/200" />
</div>
<div class="item">
<img src="http://lorempixel.com/250/100" />
</div>
</div>
Check this Fiddle
Replace max-width to width from image css, the reason behind this is max-width does not apply to inline elements, so you will get inconsistent behavior across browsers.
img {
width: 100%;
height: auto;
}
I have read all the other questions on Stackoverflow related to this topic, and none seem to fix my issue.
I have a nav bar at the top of my page that is fixed to the top left corner then a div in the centre containing an image and some texts.
My content is as so:
<div class="center">
<div id="logo"> <a class="home" href="index.html"> <img class="noborder" src="images/logocat.png" alt="" /> </a>
</div>
<div id="top">
<p class="toptext">TEXT</p>
</div>
<div id="contact">
<table border="0" align="center">
<tr>
<td><a href="http://be.net/crookedcartoon"></td>
<td><p class="contact-text1"> CROOKEDCARTOON#GMAIL.COM </p>
<p class="contact-text2"> Alex: TEXT </p></td>
<td><img class="noborder" src="images/seriesindex.jpg" onmouseover="this.src='images/serieshover.jpg'" onmouseout="this.src='images/seriesindex.jpg'" alt="" />
</td>
</tr>
</table>
</div>
<div id="about">
<p class="content-text-index">TEXT</p>
</div>
</div>
The CSS for the mentioned div is:
.center {
height: 984px;
width: 504px;
position:absolute;
top:50%;
left:50%;
margin-top:-492px;
margin-left:-257px;
padding:0px;
}
It wont centre vertically or horizontally there is no div or wrapper surrounding this div, or any conflicting css (as far as im aware) and it's becoming a pain.
Can anyone suggest a fix? I have tried the half margin/width etc idea and that is what is displayed above.
I notice you are using #center instead of '.center` in your CSS.
.center { /* as you have used a class in your HTML */
height: 984px;
width: 504px;
position:absolute;
top:50%;
left:50%;
margin-top:-492px;
margin-left:-257px;
padding:0px;
}
ok, forget everything and just use this CSS
html, body{ height: 100%; }
.center {
height: 984px;
width: 504px;
position:absolute;
top:50%;
left:50%;
margin-top: -492px;
margin-left: -257px;
padding:0px;
}
And let me know if it's working.
Use CSS flex-box : http://www.sketchingwithcss.com/samplechapter/cheatsheet.html
.center {
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row /* works with row or column */
flex-direction: row;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
}
You can go with CSS flex-box. and for old versions of IE you need use either this jquery solution http://dipaksblogonline.blogspot.in/2011/02/div-content-vertical-align-centermiddle.html
or use the display: table; property CSS to vertically align text to middle of div
You can apply a ghost element to fill the 100% of the height of a parent element.
.wrap {
text-align: center;
height: 500px;
background: #d4d4d4;
}
.wrap:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}
.center {
display: inline-block;
vertical-align: middle;
width: 300px;
}
for a markup like this:
<div class="wrap">
<div class="center">...</div>
</div>
An example: http://jsfiddle.net/LayyM/
I have in a problem with centering an image.Hence I have many div and in every div have two div that is divided into two.In those two div I will show two image.Each of these two div is 300 pixel.Problem is images may be small or 300 pixel and I have to show images center if small and if image is 300 then automatically sit on full div. Can anyone help me to do this?
Here is the code..
<div style="margin-top:10px;height: 300px;width: 600px;background: whitesmoke">
<div style="float:left;width: 300px;height: 300px;">
<img class="" src="images/productImages/medium/<?php echo $product1['product_image']?>" alt="image_01" width="" height="" />
</div>
<div style="float:right;width: 300px;height: 300px;">
<?php if(!empty($product2)) { ?>
<img class="" src="images/productImages/medium/<?php echo $product2['product_image']?>" alt="image_02" width="" height="" />
<?php } ?>
</div>
</div>
you have to position the parent with "relative" and than you can handle the IMGs with position "absolute" ;)
take a look at this fiddle
.CenterMe {
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
You should use display: table-cell; for the parent element and than use max-height and max-width properties for your img tag.. This way you can align the images vertically as well as horizontally.
Demo
div {
width: 300px;
height: 300px;
display: table-cell;
border: 1px solid #f00;
text-align: center;
vertical-align: middle;
}
div img {
max-height: 100%;
max-width: 100%;
}
But if you are looking to align images only horizontally and not vertically, than you won't need to do much, just declare text-align: center; on the parent element.
Use image as background and set position to center:
<div style="margin-top:10px;height: 300px;width: 600px;background: whitesmoke">
<div style="float:left; width: 300px; height: 300px; background-image: url('images/productImages/medium/<?php echo $product2['product_image']?>'; background-position: center center; background-repeat: no-repeat;">
</div>
<!-- etc. -->
</div>
just add text-align:center to your image container:
<div style="float:left;width: 300px;height: 300px; text-align:center">
<img class="" src="images/productImages/medium/<?php echo $product1['product_image']?>" alt="image_01" width="" height="" />
</div>
okay, considering your images and condition are both dynamic, modify your inline css to :
<div style="margin-top:10px;height: 300px;width: 600px;background: whitesmoke;text-align:center">
added : text-align:center
Fiddle
I have a thumbnails div:
How to place the selected div at the bottom?
Like so:
http://jsfiddle.net/jJzu5/
html:
<td>
<div class="thumbnail">
<img class="thumbnail" src="abc.jpg">
</div>
</td>
<td>
<div class="thumbnail">
<img class="thumbnail" src="def.jpg">
</div>
</td>
<td>
<div class="thumbnail">
<img class="thumbnail" src="ghk.jpg">
</div>
</td>
<td>
<div>
... // <-- this has to be at the bottom
</div>
</td>
css:
div.thumbnail {
width: 40px;
height: 40px;
margin-right: 10px;
position: relative;
float: left;
border: 1px #aaa solid;
}
img.thumbnail {
position: absolute;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
max-height: 90%;
max-width: 90%;
}
Update:
please look into the jsfiddle-link: http://jsfiddle.net/jJzu5/
vertical-align doesn't work there.
Update2:
uploaded a desired look (at the top)
Sorry, now that I understand what you mean. You just need to add a wrapper with display: table and then set the text div to display: table-cell and it should behave how you want:
div.wrapper { display: table;
}
div.downloads_thumbnail {
width: 50px;
height: 50px;
border: 3px #000 solid;
position: relative;
float: left;
}
div.thumbText {
display: table-cell;
vertical-align: bottom;
}
img.downloads_thumbnail {
position: absolute;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
max-height: 50px;
max-width: 50px;
}
and
<div class="wrapper">
<div class="downloads_thumbnail">
<img class="downloads_thumbnail" src="http://bower.io/img/bower-logo.png">
</div>
<div class="downloads_thumbnail">
<img class="downloads_thumbnail" src="http://bower.io/img/bower-logo.png">
</div>
<div class="downloads_thumbnail">
<img class="downloads_thumbnail" src="http://bower.io/img/bower-logo.png">
</div>
<div class="downloads_thumbnail">
<img class="downloads_thumbnail" src="http://bower.io/img/bower-logo.png">
</div>
<div class="thumbText">
...
</div>
</div>
http://jsfiddle.net/thespacebean/jJzu5/3/
Try the css:
vertical-align: bottom;
Docs: http://www.w3schools.com/cssref/pr_pos_vertical-align.asp
Are you using <tr> elements? If not, wrap your existing <td> elements in a <tr>. Then start a new <tr> and place your "selected div" in a <td> within it. It will start a new table row and appear below your existing markup.
Have you tried using margin-top? I am sure that would work, if doesn't then try adding some padding. So the text should come at the bottom.
In your fiddle the problem is, that the div with the images is at left, so you cannot place it infront of it. You can have a look there for yourself!
Since the images provided there were so large and not in a line, I gave it a position: absolute and aligned it there. Other approach is the margin.
You are using tr so the div will be inline i.e. table row (tr). This is why it floats up there.
Jsfiddle: http://jsfiddle.net/afzaal_ahmad_zeeshan/jJzu5/2/