CSS: Very simple image gallery - css

I don't always do web development, but when I do CSS is the most frustrating thing I can imagine. I'm trying to create a simple class to hold an image together with description. I want to have two rows, each with three images. Very simple.
Here's my code for displaying images only, which works ok.
img {
position: relative;
margin: 0 auto;
max-width: 650px;
padding: 5px;
margin: 10px 0 5px 0;
border: 1px solid #ccc;
}
<h3>Screenshots</h3>
<p>
<img src="images/img1.png" width="200" height="140">
<img src="images/img2.png" width="200" height="140">
<img src="images/img3.png" width="200" height="140">
<img src="images/img4.png" width="200" height="140">
<img src="images/img5.png" width="200" height="140">
<img src="images/img6.png" width="200" height="140">
</p>
<h3>Installation</h3>
This shows images exactly as I want, three in each row and then there's a padding from < p > and then Installation section. To get description under images in the same div I changed img in css to div.img and htm code to:
<h3>Screenshots</h3>
<p>
<div class="img">
<a target="_blank" href="images/img1.png"><img src="images/img1.png" width="200" height="140"></a>
</div>
<div class="img">
<a target="_blank" href="images/img1.png"><img src="images/img1.png" width="200" height="140"></a>
</div>
<div class="img">
<a target="_blank" href="images/img1.png"><img src="images/img1.png" width="200" height="140"></a>
</div>
<div class="img">
<a target="_blank" href="images/img1.png"><img src="images/img1.png" width="200" height="140"></a>
</div>
<div class="img">
<a target="_blank" href="images/img1.png"><img src="images/img1.png" width="200" height="140"></a>
</div>
<div class="img">
<a target="_blank" href="images/img1.png"><img src="images/img1.png" width="200" height="140"></a>
</div>
</p>
<h3>Installation</h3>
One would have thought that the resulting webpage is going to be exactly the same as img in the first case and div.img in the second have the same attributes. It is not the case though. With this code I get each div stretched to the full width of the column and images are one under the other at the left side of the div.
I have also tried this code: http://www.w3schools.com/CSS/css_image_gallery.asp, but in this case I get Installation and all the text that follows go onto the images, ignoring < p > tag which has bottom margin.

To keep it simple: replace your divs with <span> elements, or set display: inline on your CSS for the divs. This will make them behave as you expected (as inline, not block elements).
Also:
You don't need the margin: 0 auto. It won't center inline elements as it does with block elements
You added position: relative, but don't seem to need it, either. Unless you intend to apply absolute position to something inside the div at a later point.
Here is a jsfiddle without these two properties, and adding display: inline to the divs: http://jsfiddle.net/3BwKY/.

Related

How to get scroll down for 2 images (left and right) at a time?

I made two divs that when I scroll down ; images can move at the same time.
I used a position:fixed tag.
Now the 1st image is scrolling down, but the 2nd is not.
When I use position:fixed tag for the 2nd image div, then that becomes part of 1st image.
How can I resolve this issue?
I want when I scroll down both images should scroll down from left and right - towards down position.
Get results on my site: Sagifill.com
Code:
<div style="width: 1300px; margin-left:auto; margin-right:auto;">
<div style="float:left; padding:0px 5px 0px 5px; position:fixed;">
<a href="anrdoezrs.net/click-7588066-11740922"; target="_top">
<img src="lduhtrp.net/image-7588066-11740922"; width="160" height="600" alt="" border="0"/></a>
</div>
<div style="float:right; padding:0px 5px 0px 5px; ">
<a href="anrdoezrs.net/click-7588066-11740922"; target="_top">
<img src="lduhtrp.net/image-7588066-11740922"; width="160" height="600" alt="" border="0"/></a>
</div> </div>
You don't need to float them with position:fixed. Try this:
<div style="padding:0px 5px; position:fixed; left: 0; top: 0;">
<a href="anrdoezrs.net/click-7588066-11740922"; target="_top">
<img src="lduhtrp.net/image-7588066-11740922"; width="160" height="600" alt="" border="0"/>
</a>
</div>
<div style="padding:0px 5px; position: fixed; right: 0; top: 0;">
<a href="anrdoezrs.net/click-7588066-11740922"; target="_top">
<img src="lduhtrp.net/image-7588066-11740922"; width="160" height="600" alt="" border="0"/>
</a>
</div>

Centering a child within a parent on a responsive flowing website wireframe

I am currently trying to build a responsive mobile website.
The second level div in the code below is not picking up the 100% width from it's parent div (which should be the maximum width of the device).
The child div needs to sit centered within the parent div on the device
<div style="width:100%; overflow:hidden; margin-left:auto; margin-right:auto; text-align:center; background:#0FF;">
<div style="max-width:200px; margin-left:auto; margin-right:auto; ">
<a href="http://website.com.au/contact.html" target="_blank" onmouseover="MM_swapImage('Image21','','images/make-an-enquiry-icon-2.png',1)" onmouseout="MM_swapImgRestore()">
<img src="images/make-an-enquiry-icon-1.png" alt="" name="Image21" max-width="100%" border="0" id="Image21" style="margin-left:auto; margin-right:auto; align:center;" />
</a>
<div align="center" style="width:100%;">
<a href="https://maps.google.com.au/maps?f=q&source=embed&hl=en&geocode=&amp" target="_blank" class="style41" style="text-align:left"><img src="images/add-plus-icon.png" width="14" height="9" border="0" />
View Larger Map
</a>
</div>
It's working, you might have something else going on in your other CSS.
Check this out. Same code as yours, i added a border to the inner div so it can be seen.
http://jsfiddle.net/LAfaR/
<div style="width:100%; overflow:hidden; margin-left:auto; margin-right:auto; text-align:center; background:#0FF;">
<div style="max-width:200px; margin-left:auto; margin-right:auto; border:solid 1px black ">
<a href="http://boxhillindoorsports.com.au/contact.html" target="_blank" onmouseover="MM_swapImage('Image21','','images/make-an-enquiry-icon-2.png',1)" onmouseout="MM_swapImgRestore()">
<img src="images/make-an-enquiry-icon-1.png" alt="" name="Image21" max-width="100%" border="0" id="Image21" style="margin-left:auto; margin-right:auto; align:center;" />
</a>
<div align="center" style="width:100%;">
<a href="https://maps.google.com.au/maps?f=q&source=embed&hl=en&geocode=&q=ll=-37.833988,145.132484&spn=0.017761,0.045748&z=14&iwloc=A" target="_blank" class="style41" style="text-align:left"><img src="images/add-plus-icon.png" width="14" height="9" border="0" />
View Larger Map
</a>
</div>

3 horizontal image layout issue

I have been trying to achieve getting 3 images side by side. I finally got it the exact way I want them laid out. A few ways I tried, one image would always move down on smaller screens. They way I have it now is the exact way I want but the code i used is a bit messed up(not even sure if correct but it works).
My code has 4 images and just 3 show up. Again it shows up the way I want. The image that doesn't show up is the 2 one in the code. I dont want the image to show up anyways but I know it shouldnt be in there either.
I struggle with this stuff so any help on how this should read correctly is appreciated.
When I remove the 2 image in the code they all go out of line.
<div id="content">
<center><div><br><br><br><br><br><br>
<img src="images/image1.png" width="30%" style="float:left;/> <img src="images/image2.png" width="50%" style="float:left;"/>
</div>
<img src="images/image3.png" width="30%"/><img src="images/image2" width="30%" style="float:right;"/>
</div>
</center>
<br><br><br><br><br><br><br><br><br></div>
</div>
<div id="content" width="100%">
<div>
<img src="1.jpg" width="30%" style="float:left"/>
<img src="2.jpg" width="30%" style="float:left"/>
<img src="3.jpg" width="30%" style="float:left"/>
</div>
</div>
Here is the simplest and clean code...
fiddle :http://jsfiddle.net/ag3Et/1/
css
#content{
width:100%;
float:left;
}
#content img{
width:32%;
margin-left:1%;
float:left;
}
html
<div id="content">
<img src="http://www.birds.com/wp-content/uploads/home/bird4.jpg"/>
<img src="http://www.hdwallpapers3d.com/wp-content/uploads/2013/05/HD-Birds-wallpaper-3.jpg"/>
<img src="http://www.oassf.com/en/media/images/birds_wallpaper.jpg"/>
</div>
This is the answer for your ques.. please mark it as an answer after using this code so that others can also use it in future...
<div id="content">
<center><div><br><br><br><br><br><br>
<img src="1.jpg" width="30%" style="float:left;"/>
<img src="2.jpg" width="30%"/>
<img src="3.jpg" width="30%" style="float:right;"/>
</div>
</center>
</div>
<br><br><br><br><br><br><br><br><br></div>
</div>
You should definitely brush up on your html and css.
You need to set fixed pixel values so your images can fit into place and stay as you would like them to.
CSS:
#content{ width: 960xpx; margin:30px auto; } //the margin is to center this whole block
img{ width: 320px; float:left;} //each image is defined as a fixed 320px
.clear{ clear: both; } //makes sure things don't overlap
HTML:
<div id="content">
<img src="images/image1.png"/>
<img src="images/image2.png"/>
<img src="images/image3.png"/>
<div class="clear">
<!-- SOME OTHER CONTENT HERE -->
</div>
</div>

change div size based on image orientation

I have been making a web landing page for my app, but it keeps squishing the landscape image and thinking its portrait. I want the div to auto adjust for the picture so if the height is greater than the width then the div should adjust to the image's size. How could I go about doing that.
Sample of the Code I am using to display the images:
<div id="favphoto">
<a href="/event/%event%/photo/%id%.jpg" target="_blank">
<img border="0" src="/event/%event%/photo/%id%.jpg" />
</a>
</div>
I want to make the div auto size to the correct image that gets called, if not am I just going about this wrong.
Setting the div to display:inline-block will cause it to be only as wide as the contents.
JSFiddle Example
<div class="favphoto">
<a href="#" target="_blank">
<img border="0" src="http://lorempixel.com/400/200/sports/" />
</a>
</div>
<div class="favphoto">
<a href="#" target="_blank">
<img border="0" src="http://lorempixel.com/200/400/sports/" />
</a>
</div>
.favphoto {
display:inline-block;
vertical-align:top;
}
.img {
display:block;
}

CSS: vertically centering imgs with varied height/width inside a statically positioned div

This is completely stumping me. I'm pretty new to CSS & DIVs so please bear with me.
I'm trying to make a DIV-based image gallery.
The layout is as follows...
<div id="container">
<div class="row">
<div class="img-container">
img
</div>
...more img-containers
</div>
...more rows
</div>
The images are all in thumbnail form but the thumbnails vary in size (but all less than 150x150px) which seems to rule out all of the vertical-align solutions I've found, especially when combined with the fact that the row divs and the container are statically positioned.
I'm sorry if this has been asked somewhere before but I have looked through both this site and Google for hours and haven't found anything that seems to work.
Thanks in advance for any help!
EDIT3: For reference, the site I'm working on is here: http://utsa.edu/honors/?page=international/china_new
EDIT1: #Robert: Thanks for the class/id suggestion. Changed that. I don't believe the vertical centering you suggested is going to work though. Since your images are all the same size the padding & margins center them. Mine are all different heights.
EDIT2: #Nowhere: That was the first thing I tried. From what I've read, it's not working because the containing DIVs aren't positioned absolute. I don't know though.
.img-container img {vertical-align: middle ;} works fine for me with images of different heights and widths. Their vertical centers are correctly aligned...
If you don't find a solution within CSS, you might want to consider having a program generate images from the existing thumbnails: you convert them to images of the size of the biggest ones, surrounding the smaller ones with a transparent border (in a format that supports transparency like PNG).
I would use a combination of floating each image container left within the row div and auto center the images within the image container.
Here's how I might do it. I recommend you change any div id to a div class if you plan on using it more than once in your document.
<html>
<head>
<title></title>
<style type="text/css">
body {
background-color: #666;
text-align: center;
}
#container {
background-color: #fff;
width: 960px;
margin: 0 auto;
overflow: hidden;
padding: 10px;
}
.row {
clear: both;
padding: 15px 0;
}
.img-container {
float: left;
width: 33%;
}
</style>
</head>
<body>
<div id="container">
<div class="row">
<div class="img-container">
<img src="images/image1.jpg" alt="image1" width="150" height="200" />
</div>
<div class="img-container">
<img src="images/image1.jpg" alt="image1" width="150" height="200" />
</div>
<div class="img-container">
<img src="images/image1.jpg" alt="image1" width="150" height="200" />
</div>
</div>
<div class="row">
<div class="img-container">
<img src="images/image1.jpg" alt="image1" width="150" height="200" />
</div>
<div class="img-container">
<img src="images/image1.jpg" alt="image1" width="150" height="200" />
</div>
<div class="img-container">
<img src="images/image1.jpg" alt="image1" width="150" height="200" />
</div>
</div>
<div class="row">
<div class="img-container">
<img src="images/image1.jpg" alt="image1" width="150" height="200" />
</div>
<div class="img-container">
<img src="images/image1.jpg" alt="image1" width="150" height="200" />
</div>
<div class="img-container">
<img src="images/image1.jpg" alt="image1" width="150" height="200" />
</div>
</div>
</div>
</body>

Resources