• We just launched and are currently in beta. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

Creating Image Gallery with HTML and CSS

fjnfunvtuntu

Digital Asset Preservation Specialist
F Rep
0
0
0
Rep
0
F Vouches
0
0
0
Vouches
0
Posts
48
Likes
125
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2 1000 XP
Images gallery for web pages can be created with plain HTML, JavaScript, and CSS. Easiest one is with HTML but that's not good if we want to add new images. Image Gallery with HTML and CSS is quite easy and we can add new images to the gallery with ease. It looks beautiful too by using CSS. This program creates the thumbnails and displays the enlarged image when a mouse is moved over a thumbnail.

Following are the steps to create the Image Gallery.

Part 1: Styling with Cascading Style Sheet.

This code can be written inside or in external css file.

Step 1: Style for the document body.
  1. body {

  2. background

    :

    #222

    ;

    //changes background

    color of the body

  3. margin-top

    :

    20px

    ;

    // top

    margin of the document expressed in pixel.

  4. }


Step 2: Style for displaying Title.

  1. h3 {

    //title is displayed using <h3>

    tag

  2. color

    :

    #eee

    ;

    //color of the title

  3. font-family

    :

    Verdana;

    //font family for the Title

  4. }


Step 3: Style for displaying thumbnail.


This style is used for creating the thumbnails i.e displaying images in small size. Margin property of CSS is used to specify the amount of space to be left between a border and the image or any content around it. It is specified with four values as top, right, left, bottom(top margin, right margin etc) or if the the margin should be equal on all the four sides then it can be specified with single value. Same rule is used for specifying padding which is nothing but space between border and the content around which the border is drawn.

  1. .thumbnails

    img {

    //thumbnails is the name of style class for <img>

    tag.

  2. height

    :

    80px

    ;

  3. border

    :

    4px

    solid

    #555

    ;

    //border

    around the thumbnail is 4

    pixel wide and color used is the one with 555

    color code.

  4. padding

    :

    1px

    ;

    // amount of space

    left

    between the image

    edge and it's border

  5. margin: 0 10px 10px 0; // amount of space left between a border and the image or any content around it.

  6. }

Next is the CSS style when a mouse is moved over the thumbnail. The color is changed and cursor appearance is changed to pointer.
  1. .thumbnails

    img:

    hover

    {
  2. border

    :

    4px

    solid

    #00ccff

    ;

  3. cursor

    :

    pointer

    ;

  4. }


Step 5: Style for the enlarged image


For the enlarged image height is changed to 500 px and border is drawn around the image. This class name used here is preview.

Part 2: Adding and Displaying the Images:

One main division is created inside the document and other two divisions are created inside the main division for thumbnails and images. This division is aligned in the center of the web document.
  1. <html

    ><div

    class

    =

    "gallery"

    align

    =

    "center"

    ></

    html

    >


Step 1: Adding Title


For displaying title

tag is used, for which style is already created.
  1. <html

    >
  2. <h3

    >

    Cute Babies</

    h3

    ><br

    /

    ></

    html

    >

Step 2: Creating and Displaying Thumbnails

Division is created with tag and onmouseover attribute to specify that images should be enlarged when mouse is moved over the thumbnail. Id is applied to each image thumbnail.

  1. <img

    onmouseover

    =

    "preview.src=img1.src"

    id

    =

    "img1"

    src

    =

    "Babies/image001.jpg"

    alt

    =

    "Image Not Loaded"

    /

    >

  2. <img

    onmouseover

    =

    "preview.src=img2.src"

    id

    =

    "img2"

    src

    =

    "Babies/image002.jpg"

    alt

    =

    "Image Not Loaded"

    /

    >

When mouse is moved over the thumbnail, value of src attribute of the image(


tag) with "preview" id will be changed to that of src of the thumbnail(image) with specified id, i.e same image will be displayed.

Step 3: Division for enlarged image:

Division is created and style of preview class css style is applied. In the beginning first image is loaded.
  1. <div

    class

    =

    "preview"

    align

    =

    "center"

    >

  2. <img

    id

    =

    "preview"

    src

    =

    "Babies/image001.jpg"

    alt

    =

    "No Image Loaded"

    /

    >
  3. </

    div

    >

Note: src attribute value format is for Linux OS, for windows OS should be used.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

343,109

343,117

Top