HTML & CSS Intro Course: Adding Images
Master HTML Image Integration and Best Practices
Common Image File Formats
HTML Image File Types
JPEG
Ideal for photographs and complex images with many colors. Provides good compression for smaller file sizes.
PNG
Perfect for images with transparency or sharp edges. Maintains high quality but larger file sizes.
GIF
Supports animation and transparency. Limited color palette makes it suitable for simple graphics.
SVG
Vector format that scales perfectly at any size. Excellent for logos and simple illustrations.
Index.html is a special name reserved for your homepage. When visitors go to your website's address without specifying a page, the web server automatically looks for and displays index.html.
Adding Images to HTML
Organize Files
Create an images folder to store all your image files (JPEG, PNG, GIF, or SVG)
Use Relative Links
Link to images using relative paths, just like linking to other pages within your site
Write Image Tag
Use the img tag with src attribute pointing to your image file in the images folder
Add Alt Text
Include alternate text for accessibility and while images are loading
Block-level vs Inline Elements
| Feature | Block-level Elements | Inline Elements |
|---|---|---|
| Examples | Headings, Paragraphs | Images, Words |
| Display Behavior | Stack vertically | Line up horizontally |
| Layout Impact | Create new lines | Flow with text |
Always include alt text that describes what the image shows. For decorative elements that don't affect page meaning, use empty alt attributes rather than omitting them entirely.
Setting Image Dimensions
Common Screen Resolutions
Google ranks pages partly based on loading speed. Using oversized images from digital cameras can hurt your search rankings due to slow page load times.
Proper Image Gallery Setup
Create Thumbnails
Resize images to small dimensions for gallery display (e.g., 190x145px)
Optimize Full Size
Resize large versions to match common screen sizes (1280-1920px wide)
Compress Files
Reduce file quality appropriately to decrease file size without sacrificing visual quality
Use Separate Files
Maintain different files for thumbnails and full-size images rather than scaling one image
Additional HTML Tags
Break Tag (br)
Creates a line break within the same element without adding extra spacing. No closing tag required.
Horizontal Rule (hr)
Inserts a horizontal line across the page. Can be styled with CSS or sized with HTML attributes.
Image Implementation Checklist
Keeps project structure clean and files easy to find
Ensures images work when site is moved or deployed
Essential for accessibility and SEO
Improves page load speed and user experience
Prevents layout shifts during page loading
Different browsers handle broken images differently
Key Takeaways