01. Creating an SVG Map
In this lesson, we lay the foundations for our map portal: we create the SVG vector graphic structure within the HTML page and separate visual styles into an external stylesheet (style.css).
The final result is a vector map organized into logical layers (ground, paths, and buildings) and styled with a clean, responsive design.
Introduction
Section titled “Introduction”Use Case Diagram
Section titled “Use Case Diagram”The Use Case Diagram describes the user’s essential interaction in the first phase of the project: the user opens the portal and views the campus’s static vector map, whose graphic and style resources are rendered responsively by the browser.
Diagram created with Mermaid
The Campus Map in SVG
Section titled “The Campus Map in SVG”The following figure shows the map we will create in SVG format.
Layer organization (stacking order)
Section titled “Layer organization (stacking order)”In SVG, the order in which the code is written determines the visual stacking order of elements on the screen:
- The first element written appears in the background.
- Subsequent elements are drawn on top of it.
We group the map elements into three main logical layers using the <g> tag (group):
<svg viewBox="0 0 800 600" xmlns="http://www.w3.org/2000/svg"> <!-- Layer 1: Ground (background) --> <g id="layer-terreno">...</g>
<!-- Layer 2: Pedestrian paths --> <g id="layer-percorsi">...</g>
<!-- Layer 3: Campus buildings --> <g id="layer-edifici">...</g></svg>Project file structure
Section titled “Project file structure”At the end of this lesson, the project folder will contain two main files in its root directory:
Directorycampus-map/
- index.html Main HTML page containing the SVG structure
- style.css Stylesheet for the page layout and building labels
Sequence Diagram
Section titled “Sequence Diagram”-
Purpose: Illustrates the chronological order of events as the browser loads the page and initializes static resources.
-
How to read it: Time flows vertically from top to bottom.
Diagram created with Mermaid
Component Diagram
Section titled “Component Diagram”Diagram created with Mermaid
index.html file code
Section titled “index.html file code”Create the index.html file in HTML, linking the external CSS stylesheet in the header and adding the SVG map inside the body:
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>University Campus Map - Lesson 1</title> <!-- Link to the external CSS file --> <link rel="stylesheet" href="style.css" /> </head> <body> <h1>🗺️ University Campus Map</h1>
<!-- Embedded SVG Map Structure --> <svg viewBox="0 0 800 600" width="800" height="600" xmlns="http://www.w3.org/2000/svg" > <!-- LAYER 1: GROUND (Green lawn) --> <g id="layer-terreno"> <rect x="0" y="0" width="800" height="600" fill="#7ec850" /> </g>
<!-- LAYER 2: PEDESTRIAN PATHS --> <g id="layer-percorsi"> <rect x="50" y="270" width="700" height="60" fill="#d1d5db" rx="4" /> <rect x="180" y="80" width="50" height="440" fill="#d1d5db" rx="4" /> <rect x="570" y="80" width="50" height="440" fill="#d1d5db" rx="4" /> </g>
<!-- LAYER 3: CAMPUS BUILDINGS --> <g id="layer-edifici"> <!-- Administrative Offices --> <g id="edificio-uffici"> <rect x="70" y="80" width="100" height="140" fill="#1e3a8a" rx="6" /> <text x="120" y="150" class="label">Offices</text> </g>
<!-- Classroom Building --> <g id="edificio-aule"> <rect x="70" y="380" width="100" height="140" fill="#2563eb" rx="6" /> <text x="120" y="450" class="label">Classrooms</text> </g>
<!-- Laboratories --> <g id="edificio-laboratori"> <rect x="630" y="80" width="100" height="140" fill="#7c3aed" rx="6" /> <text x="680" y="150" class="label">Labs</text> </g>
<!-- Cafeteria --> <g id="edificio-mensa"> <rect x="240" y="180" width="140" height="80" fill="#ea580c" rx="6" /> <text x="310" y="220" class="label">Cafeteria</text> </g>
<!-- Gym --> <g id="edificio-palestra"> <rect x="630" y="380" width="100" height="140" fill="#dc2626" rx="6" /> <text x="680" y="450" class="label">Gym</text> </g> </g> </svg> </body></html>Creating the style.css stylesheet
Section titled “Creating the style.css stylesheet”To keep the web architecture organized, we separate the page layout from the style rules. Create a file named style.css in the same project folder and paste in the following code. The CSS stylesheet lets us separate presentation from the HTML/SVG structure.
/* general page styles */body { font-family: system-ui, -apple-system, sans-serif; background-color: #f8fafc; margin: 0; padding: 20px; display: flex; flex-direction: column; align-items: center;}
h1 { color: #0f172a; margin-bottom: 20px;}
/* SVG container styles */svg { background-color: #ffffff; border: 2px solid #cbd5e1; border-radius: 12px; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08); max-width: 100%; height: auto;}
/* styles for building text labels */.label { font-size: 14px; font-weight: bold; fill: #ffffff; text-anchor: middle; dominant-baseline: middle; pointer-events: none;}Further development
Section titled “Further development”Saving the first branch to Git / GitHub
Section titled “Saving the first branch to Git / GitHub”Open the Source Control panel in VS Code (Ctrl + Shift + G) 🔧:
- Create a new branch named
lesson-1. - Commit the files with the message:
"Completed Lesson 1: SVG Map and style.css". - Use Publish Branch / Sync to sync the lesson to your GitHub repository.
Summary
Section titled “Summary”Knowledge check
Section titled “Knowledge check”Your answers to this page's quizzes are saved in your browser's local storage and remain there between visits.
What is the purpose of the <svg> element in the campus map?
- Link the stylesheet to the HTML page.
- Define the map’s vector graphics.
Correct: the map is built with SVG elements such as groups, rectangles, and labels.
- Manage saving the project to GitHub.
What does the viewBox attribute of the SVG element define?
- The coordinate area of the SVG graphic.
Correct:
viewBox="0 0 800 600"defines the map’s coordinate system and proportions. - The path to the external CSS file.
- The browser’s background color.
What is the purpose of the <g> tag in the map structure?
- Automatically draw the map border.
- Define the coordinates of the
viewBox. - Group related elements into logical layers.
Correct: groups such as
layer-terreno,layer-percorsi, andlayer-edificiorganize the SVG elements.
Which attributes define the position and dimensions of a <rect> element?
-
x,y,width, andheight.Correct:
xandyindicate the position, whilewidthandheightindicate the width and height. -
id,class,href, andsrc. -
font-size,font-weight,fill, andtext-anchor.
Why are the page styles placed in style.css?
- To completely replace the HTML and SVG structure.
- To separate presentation from the structure of the page.
Correct: linking the CSS file allows layout and visual appearance to be managed separately.
- To store building data in vector format.
Which CSS class is applied to the building labels?
-
#edificio -
layer-edifici -
.labelCorrect: the
.labelclass in the CSS file defines the appearance and alignment of the SVG labels.
How is style.css linked to the index.html page?
- By placing the CSS in the
viewBoxattribute. - By using
<link rel="stylesheet" href="style.css" />in the HTML header.Correct: the
<link>element loads the external stylesheet specified byhref. - By grouping the CSS in the SVG
<g>tag.
Quiz progress
0 / 0 questions answered (0%)
0 correct answers
Your score
0%
0 / 0 correct answers
Answered: 0 / 0 (0%)
STICS © 2026 by Carlo Scocco is licensed under CC BY-NC 4.0