How to Align Images in CSS & HTML
Ranked #64 in Internet, #1,509 overall
Graphics Layout Help for Newbies
Want to align an image on a web page? Need side-by-side graphics and text? This page should solve all your graphics and HTML questions!
This Image Tutorial Covers:
- How to Make an Image a Clickable Link
- How to Center an Image or Headline
- How to Align Pictures & Text Side by Side
- How to Add Margins Around Images
- How to Add Background Images
- How to Scale / Resize an Image
- How to Add Photo Credits Under Images
- How to Make a Multiple Images Gallery
Tip: Print out my HTML Graphics Cheat Sheet with most of the codes and tricks on this page!
This lens is part of Greekgeek's Graphics Tutorials Suite!How to Align Images • How to Upload Images
Where to Get Graphics (Legally!) • Free Squidoo Graphics
How to Fix Missing Images in Amazon Listings
Photoshop Tricks: How to Make Glossy Buttons • Add a 3D Frame to a Photo
How to Put an Image or Photo on a Webpage
HTML Code for "Place a Graphic RIGHT HERE"
HTML puts hidden codes in webpages that mark which parts are text, images, or links. On Squidoo, you can use HTML in Text Modules.Before we can align an image, we need an image! Upload a picture to a free image host like Photobucket or Picasa. Then get the image's URL (on Photobucket, it's the "Direct Link"; on Picasa, click "Embed Image", then "Image Only - no link".)
Use the <img> tag as a placeholder for your image on the page. The URL (location of the image ending in .gif, png. or .jpg) goes inside the tag, like this:
<img src="http://www.somedomain.com/imagename.jpg">
SRC is short for "source." For another example, here's the HTML code for the magician Squid at upper right:
<img src="http://www.istad.org/lenses/squids/magic-squid.jpg" style="width: 150px; float: right;">
Notice the extra style options tucked inside the <img> tag to set its size and position. For the rest of this tutorial, I'll teach you what options are available for aligning and resizing images.
How to Make an Image a Clickable Link
HTML Code For Turning an Graphic Into a Link
Link to my Greece Odyssey Lens
HTML Code:
<a href=http://www.squidoo.com/odyssey>Link to my Greece Odyssey Lens</a>
Do the same thing to turn an image into a link, but replace the text with an HTML image code:HTML Code:
<a href=http://www.squidoo.com/odyssey><img src=http://www.istad.org/squid/click-me.jpg></a>
TIP: Make it clear that an image IS a link, by making it look like a button or using a caption.
How to Center an Image or Headline
Simple HTML Centering (No CSS Auto-Margins on Squidoo)
<p align=center>(image goes here)</p>
which makes a paragraph -- including a blank line after it -- containing the image. For example:
HTML Code to Center an Image:
<p align=center><img src="http://www.istad.org/squidoo/align-images/computer-squid.jpg"></p>
To center a caption under an image, write <br> (break) to go to the next line, them write the caption BEFORE the end-of-paragraph code (</p>).
HTML Code for a Caption Under a Centered Image:
<p align=center><img src="http://www.istad.org/squidoo/align-images/computer-squid.jpg"><br>I Squidoo! Do You?</p>
The caption can include a link:
<p align=center><img src="http://www.istad.org/squidoo/align-images/computer-squid.jpg"><br>I <a href="http://www.squidoo.com">Squidoo</a>! Do You?</p>
How to Align Images, Text Side By Side
Now We're Getting into CSS (Style)
To place images and/or text side by side, we need to use CSS styles, which give us more control over layout and appearance. The style command lets us piggyback a bunch of fancy effects onto basic HTML.float pushes an image right or left, and then text (or another image) fills in beside it. So the code for this lightbulb squid graphic is:
<img src=http://www.istad.org/squids/idea-squid.jpg style="float: left">
You don't need to know what HTML and CSS are to use them, but if this alphabet soup is starting to feel intimidating, check out "What Are HTML and CSS?"
Note: I simplified the code in the above example. In fact, I had to do something else to this image -- add empty space to the right and below the image -- or it would be jammed against the text next to it. The next section explains how.
How to Add Margins Around Images
Do It With Style -- CSS Style, That Is
We could simply add the CSS command margin: 10px; to the style, and this would put a ten-pixel-wide buffer of white space around the graphic, so this text isn't bang-up against it.But if I'd done that, there'd be a margin on all sides of the graphic, pushing it away from the left-hand gutter.
Luckily, CSS lets us set each margin independently, if we choose:
Here's the CSS code for the example above:
<img src="http://www.istad.org/greece/templeHephaistosSm.jpg" style="float: left; margin-right: 30px; margin-bottom: 20px;">
Immediately after the code, write the paragraph you want next to the image.
To put the side-by-side image to the right of the text, change float: left; to float: right; and margin-right: to margin-left:
How to Add Background Images
Put a Picture Behind a Paragraph
Here's a close-up photo of the chair I'm sitting on. (It was handy.)With CSS style: background-image, any image can be tiled as the background for any webpage element. We usually use this trick with paragraphs, but it also works for lists, headers, etc.
Unfortunately, background images often make it hard to read text. Sometimes even changing the color of the text (see code below) doesn't help. This example demonstrates why you should choose background images with care, instead of just snapping a random photo!
Here's the code:
<p style="background-image: url(http://www.istad.org/squidoo/align-images/wicker-pattern.jpg); padding: 10px; color: white;">
One solution is to have the background repeat in only one direction, using repeat-x (only horizontal), repeat-y (only vertical), or no-repeat (useful when you've got a graphic fitted to the paragraph...but be careful; text may be a different size on different computers.)
Here, I've repeated the background image as a vertical strip, then added padding to the left side of the text. padding separates text from the edge of a paragraph, as opposed to margin, which is the space around the paragraph.
Here's the code for this background-image:
<p style="background-image: url(http://www.istad.org/squidoo/align-images/wicker-pattern.jpg); background-repeat: repeat-y; padding-left: 125px;">
I used 125px for the padding since my image was 100 pixels wide.
The better solution, of course, is to use a graphic that doesn't clash with the text over it. Here I lowered the contrast and raised the brightness of the image using Photoshop. The code is the same as the first example, except I didn't change the text color.
Cool Squidoo Tip!
See my Squidoo Themes and Color Codes lens for some fabulous backgrounds you can use that are built right into Squidoo.
I show you how to use background images in the Black Box module, like this!
How to Scale / Resize an Image
Style It With the Size Attribute
Your web browser doesn't do quite as good a job as a graphics program, but it can scale images! Specify either the height or the width, and the browser resizes the other dimension to match.In fact, I've been doing this throughout my tutorial, because my squid graphics are huge. (Graphics look better scaled down than up). I was concealing a part of the code in my examples above. Here's what I was really doing:
<img src=http://www.istad.org/lenses/squids/idea-squid.jpg style="float: right; width: 200px; margin-left: 15px; margin-bottom: 10px">
Note that you can also define width or height using percent. For example, width: 50% means "make the width of the image half the width of the paragraph."
Amazon Spotlight: Learn CSS the Easy Way
I got this book for my Mom
CSS: The Missing Manual
Amazon Price: $19.49 (as of 02/14/2012)![]()
When my Mom set up a blog to share her K-12 educational resources, she needed to learn how to build webpages. She already knew a few basics -- headers, paragraphs, links and images-- but her website needed more.
After examining all the books on web design in Buns & Noodle, I purchased this book for her to get her started. It worked! It taught her CSS (dressing up webpages, layout, techniques like side-by-side paragraphs and text). She still uses it as a reference guide.
How to Add Captions Under Aligned Images
This Code Makes Photo Credits Under Images
Earlier, I showed you how to center a caption under a centered image using HTML. Now, I'm going to show you my fanciest layout trick: a caption under a graphic that's left- or right-aligned!At right is a graphic pushed to the margin using float: right, with some margins separating it from this paragraph.
Here's the code:
<img src=http://www.istad.org/greece/templeHephaistosSm.jpg style="float: right; margin-left: 20px; margin-bottom: 10px;">

Temple of Hephaistos
© 2005 Ellen Brundige
Here's the CSS code for aligned image + caption:
<p style="float: right; width: 200px; margin-left: 20px; margin-bottom: 10px; font-size: 9pt; font-style: italic; text-align: center;"><br><img src="http://www.istad.org/greece/templeHephaistosSm.jpg" style="margin-bottom: 5px;"/>Temple of Hephaistos
© 2005 Ellen Brundige</p>
If you're having trouble figuring out what all the codes mean, just copy and paste the whole kit and kaboodle, then change three things to use it:
- Change the URL of my image to the URL of your image.
- Change the width (in pixels) to the width of your image.
- Change the caption. You can break it into multiple lines, as I did, or just use one line.
And what if you want the caption to be a clickable link, too?
How to Give a Photo Credit Under an Image
Great for Creative Commons Photos!

Kapoho, Hawai'i
Photo Credit: HVO
Use the "centered captions under images" trick above, then in the caption include an HTML link, like this:
<p style="float: left; width: 200px; font-size: 9pt; text-align: center; margin-right: 20px; padding-bottom: 0px;"><img src="http://www.istad.org/lenses/volcanoes/cinder-cone.jpg" alt="Cinder Cone, Kapoho Eruption, Hawaii" height="132" width="200">Kapoho, Hawai'i
Photo Credit:<a href="http://hvo.wr.usgs.gov/kilauea/history/1960Jan13/">HVO</a></p>
Again, note that I had to make the paragraph width the width of the graphic in pixels, so that the caption underneath is no wider than the image (it wraps around when it hits the right-hand edge of the graphic.)
Aligning Multiple Images
What if you want to have multiple images side-by-side in a virtual photo gallery? And what if you need captions?Go to page two for my handy HTML Templates: Side-by-Side Image Gallery. Includes a template you can download and save for later.
Guestbook
Please share and/or rate this page if you found it useful!-
Reply
-
notedbybarb
Jan 6, 2012 @ 10:32 pm | delete
- Thank you for these well-presented and very helpful tutorials. So thoughtful of you to share.
-
-
Reply
-
GrammaLinda
Jan 4, 2012 @ 1:57 pm | delete
- Excellent resource for anyone. I know CSS and have bookmarked this lens. SquidAngel blessings!
-
-
Reply
-
williemack58
Jan 3, 2012 @ 3:53 pm | delete
- Fantastic information, a bit overwhelming for a new squidadale, however I am sure in time it will become second nature.
-
-
Reply
-
IslandsTropicalMan
Jan 2, 2012 @ 5:56 am | delete
- very useful tips, thanks for sharing
-
-
Reply
-
ggalea Dec 26, 2011 @ 11:50 am | delete
- Really, great tricks with CSS, we can make more special our lens with those easy implementation.
Thanks by share,
-
- Load More
Greekgeek's Graphics Tutorial Suite
by Greekgeek
I've written several Squidoo Tutorials to help you!
BONUS! Free online text editor which helps you write CSS!
more »
- 255 featured lenses
- Top lens » Free Web Graphics: Where to Get Them (Legally!)
- This lens »
Won purple star

Explore related pages
- Finding and Using Images Online. Finding and Using Images Online.
- Clip Art and Graphics for Crafts, Scrapbooks, Blogs, Web Design Clip Art and Graphics for Crafts, Scrapbooks, Blogs, Web Design
- Free Clip Art for Teachers and Students Free Clip Art for Teachers and Students
- Free Web Graphics: Where to Get Them (Legally!) Free Web Graphics: Where to Get Them (Legally!)
- Free Images and How to Use Them Free Images and How to Use Them
- Photoshop Tutorial: 3D Photo Frame Photoshop Tutorial: 3D Photo Frame
