🍎
Introduction to Web Development
  • Introduction To Web Development
  • Tools
    • Notes: Web Development Process
    • Notes: Replit
    • Notes: GitHub
    • Notes: Replit + GitHub Integration
    • GitHub Pages Hosting
  • Week 1: HTML
    • Notes: HTML
    • Notes: Working With Images
    • Project: Hello World
    • Project: Subject of Interest
  • Week 2: CSS
    • Notes: CSS
    • Using CSS for Layout
      • Notes: Box Model
      • Notes: Flexbox
      • Notes: Responsive Layout
    • Project: Styling Subject of Interest
    • Project: Image Gallery
  • Week 3: JavaScript and HTML Forms
    • Notes: JavaScript
    • Notes: Processing HTML Forms with JavaScript
    • Project: Dynamic Style Change
    • Project: Mad Libs
  • Week 4: Web Site Using Bootstrap
    • Notes: Anatomy of a URL
    • Notes: Content
    • Notes: Bootstrap
    • Notes: Overriding Bootstrap
    • Notes: Favicons
    • Notes: License
    • StartBootstrap Templates
    • Project: Bootstrap Website
  • Wrap Up
  • Videos
Powered by GitBook
On this page
  • Image Element
  • Properties of Images and Aspect Ratio
  • How to get Free Images
  • Compressing Images
  • Uploading Images to Replit
  • Image File Types
  • Resources
  1. Week 1: HTML

Notes: Working With Images

PreviousNotes: HTMLNextProject: Hello World

Last updated 10 months ago

Image Element

The image element uses the img tag to embed an image on a web page. There are 2 required attributes:

  • src is the path to the image and it can be a local path or a URL

  • alt is the text displayed if the browser settings do not allow for rendering images or the path to the image doesn't return an image.

Images will render if the browser settings allow for it and the value supplied in src returns an image, but if they don't, the text supplied in the alt attribute will render instead. In the example below, the src is pointing to a website that returns HTML an not text, so the alt attribute value is displayed instead.

<img src="http://www.wikipedia.com" alt="Dog Barking" >

There are more attributes associated with the image tag. We'll use the height and width attributes to solve a problem in the next section.

Properties of Images and Aspect Ratio

The properties of images that web developers need to know to serve up the best-looking image are:

  • The size is usually expressed as width x height in pixels

  • The resolution determines the clarity of the image and is generally expressed in DPI, or "dots per inch."

  • The metadata includes the filename, URL, and other data depending on the camera, like location, time, and date.

We'll focus on the size properties as these are important in layout. A layout design will often specify the size of the image in terms of width and height. The original image may be much larger than the design specifies. Also, the original image may not have the same aspect ratio as the image displayed on the web page.

The HTML img element allows us to specify the height and width of the image, but we don't want to select a height and width that creates a different aspect ratio than the original image, or the image will appear skewed.

Let's say our design looks like this, and we need an image with a width of 600px and a height of 400px.

We can set the height and width of any image to these values in the image tag, but if we don't maintain the aspect ratio, the image will look skewed. The original dimensions of this image are 5759 x 6930. The height is larger than the width, yet we are telling the browser to render at 600 x 400 where the width is greater than the height. The effect of skewing is to see the image "stretched" in on e of the dimensions, in this case width.

The actual image looks like this.

How can we resolve this problem? The easiest way is to set the image height to the design height, 400px, and the width to "auto." The image will not fill the space, but it will retain its aspect ratio.

<img height="400" width="auto" src="./tree.jpg"/>

How to get Free Images

We'll be using images in our projects. You can use your photos, but accessing free photos online is helpful if you want to collect various pictures on a given subject. Here are three websites that will allow you to download pictures and use them without charge:

If you are a photographer, you can upload your images to these websites and share your photos with others there.

https://picsum.photos/600/400

Placeholder Images

Compressing Images

The images you download from the web are often huge. The skewed image above that was downloaded from Pexels was 5759 x 6930, and the file size was 9.2 megabytes. Large images load very slowly on a web page and this interferes with user experience. Some users will leave a web page before it's loaded if it takes too long to load.

One technique for optimizing image file size is image compression. Image compression can reduce an image's quality, so there can be a tradeoff between quality and time to load the image. Compression algorithms have improved over the years.

Uploading Images to Replit

To upload a file to Replit, click on the three dots and select Upload file. Then, navigate to the location on your local hard drive to select the file. If you downloaded the file from an online source, it will likely be located in the /Download directory.

Upload the file from the Download directory to replit..

Create a new folder to store uploaded images. Call it "images".

Drag the image file that was uploaded into the /images folder.

Replacing "Hello World" with an image tag. By placing the dot slash./ in front of the folder name we are directing the browser to start looking for the images folder from the current code location.

 <img height="400" width="auto" src="./images/skewed-min.jpg" alt="tree">

Image File Types

The file extension on images reveals the format used for the image. Image file formats are continually evolving. The png and jpg (or jpeg) extensions are the most common and have existed for many years. Different formats provide different features. For example, if your image needs transparency, you will need a webp, avif, or PNG format.

You'll want to use a gif or avif if you need animation.

Read about image file formats in the resources.

Resources

We'll see more ways to work with images. Cropping an image to a desired height will fix the problem. You can use tools like Preview on the Mac and the Photos app on Windows 11 to crop images. There are also online solutions like that can help with image cropping.

During development, when you want to focus on coding HTML and CSS, you can use an image service that provides random images in the dimensions you need for your page design. Try . With Lorem Picsum, you add the width and height in the URL to get the exact dimensions you need. For example, if you need a 600 x 400 image, you would use this URL for the src attribute.

Random 600 x 400 Image from Lorem Picsum

The term "Lorem Picsum" is a take-off from the term "Lorem Ipsum". This term refers to placeholder text and is used by designers and developers when they need to fill up a certain space on a web page with text. There are many Lorem Ipsum generators. Look at to learn more about placeholder text and how to generate it.

If you just want to fill up a particular dimension, look at .

You can use online tools like to compress your images. You start by uploading the image, and then after the tool compresses the image, you download it. The image on the right is the compressed image (6.2 MB), and the image on the left is the original image (9.2MB). The loss in quality is worth the faster load time.

You can also look at to compress images. It has a limit of 5 MB for the original image.

Cloudinary
Pexels
Unsplash
Pixabay
Lorem Picsum
https://loremipsum.io/
Fake Images Please
https://imagecompressor.com/
https://tinypng.com/
W3C Image Tag
MDN Image File Formats
The alt attribute value is displayed when the image is not available or can't be rendered
Design showing image with 600x400 Dimension
Skewed Image
Image Rendered with Original Dimensions
Set Height to 400px and Width to Auto to Retain Aspect Ratio
Compressed Image on the Left and Uncompressed image on the right
Upload file to replit.com
Nabiate to the Download Directory to find the file
Uploaded File will be located in the directory that you were positioned in
Create a new folder to store images
Use a new folder /images to store uploaded images
Rendering the uploaded image form the images folder
Transparent Image Example
Git Example
Using Fake Images Please:
https://fakeimg.pl/600x400