Skip to content

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.

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.

flowchart LR Utente(["🚶 User"]) subgraph Sistema ["Interactive Campus Map (Lesson 1)"] U1(["View the Campus Vector Map"]) end Utente --> U1 classDef actor fill:#1e3a8a,stroke:#3b82f6,color:#fff,stroke-width:2px; classDef usecase fill:#2563eb,stroke:#1d4ed8,color:#fff,stroke-width:2px; classDef system fill:#f8fafc,stroke:#64748b,stroke-width:2px,color:#0f172a; class Utente actor; class U1 usecase; class Sistema system;

Diagram created with Mermaid

The following figure shows the map we will create in SVG format.

OfficesClassroomsLabsCafeteriaGym

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>

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
  • 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.

sequenceDiagram actor User participant HTML as index.html participant CSS as style.css participant SVG as SVG Map (Embedded) participant Browser as Web Browser User->>Browser: Requests to open the site Browser->>HTML: Loads the index.html file HTML->>CSS: Requests the external stylesheet (link rel="stylesheet") CSS-->>Browser: Returns the style rules (.label, body, svg) HTML->>SVG: Interprets the structural layers (<g> and <rect>) Browser-->>User: Renders the page and displays the vector map

Diagram created with Mermaid

graph TD subgraph Client [Client Environment] Browser[Web Browser] end subgraph ProjectFiles [Project Files] HTML[index.html] -->|Links stylesheet| CSS[style.css] HTML -->|Embeds and structures| SVG[Map SVG Structure] subgraph SVG_Structure [SVG Composition] SVG --> Terreno[Ground Layer] SVG --> Percorsi[Paths Layer] SVG --> Edifici[Buildings & Labels Layer] end end Browser -->|Loads and renders| HTML %% Visual styles for the diagram classDef browser fill:#1e3a8a,stroke:#3b82f6,color:#fff,stroke-width:2px; classDef html fill:#2563eb,stroke:#1d4ed8,color:#fff,stroke-width:2px; classDef cssSvg fill:#059669,stroke:#047857,color:#fff,stroke-width:2px; classDef layer fill:#0d9488,stroke:#0f766e,color:#fff,stroke-width:1px; class Browser browser; class HTML html; class CSS,SVG cssSvg; class Terreno,Percorsi,Edifici layer;

Diagram created with Mermaid

Create the index.html file in HTML, linking the external CSS stylesheet in the header and adding the SVG map inside the body:

index.html
<!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>

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.

style.css
/* 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;
}

Open the Source Control panel in VS Code (Ctrl + Shift + G) 🔧:

  1. Create a new branch named lesson-1.
  2. Commit the files with the message: "Completed Lesson 1: SVG Map and style.css".
  3. Use Publish Branch / Sync to sync the lesson to your GitHub repository.

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, and layer-edifici organize the SVG elements.

Which attributes define the position and dimensions of a <rect> element?

  • x, y, width, and height.

    Correct: x and y indicate the position, while width and height indicate the width and height.

  • id, class, href, and src.
  • font-size, font-weight, fill, and text-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
  • .label

    Correct: the .label class 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 viewBox attribute.
  • By using <link rel="stylesheet" href="style.css" /> in the HTML header.

    Correct: the <link> element loads the external stylesheet specified by href.

  • By grouping the CSS in the SVG <g> tag.

Quiz progress

0 / 0 questions answered (0%)

0 correct answers