HTML labels in Context

“Hyper Text Markup Language (HTML) are sets of markup symbols or codes which is used to define the contents of a webpage and they can be utilised to extent labelling in QGIS”

HTML can be used in QGIS for rendering complex labels thereby enhancing what the labelling engine in QGIS provides. HTML labels can be accessed in QGIS map composer and also used for tooltips.
In this module, we will explore how HTML can be used to generate complex and beautiful labels in the map composer.

You try:

Goal: To learn how to use HTML and functions to label map elements

  • Load the administrative layer in the exercise data.
  • Symbolize the layer by choosing any cartography that is pleasing for your eyes.
  • Zoom to the extent of the admin layer.
  • Add a new map layout.
  • Add a map label using the Title text.
  • Play around with the properties of the label and change the font size, font colour etc.
  • Use the HTML label expression to highlight the label in different text and colour.
  • After entering the expression tick the option Render as HTML.

To Do

  • Change the background colour of each word using HTML
  • Add effects for the Title text.

Hint Follow the example expression Effects expression

Name Expectation

Title

Map showing provincial areas within South Africa

HTML label

<p>Map showing <span style="color:blue;font-weight:bold">provincial </span>areas within<span style="color:red;font-weight:bold"> South Africa</span></p>

Effects

<span style="color:white;text-shadow: 1px 1px 2px black, 0 0 25px blue, 0 0 5px darkblue;">Map showing provincial areas within South Africa</span>

More about HTML labelling

QGIS has an advanced labelling engine which can use expressions, data defined properties to make complex label rules. Labelling features have more options as opposed to mapping labels in map composer. Each composer label has its properties which can be customised but the customisation affects the whole label. In some cases, users want to fine-tune the label by separating it based to highlight specific text and the HTML labelling allows us to achieve this effect. Rendering labels as HTML allow for nice cartographic visualisation and add more options to the QGIS labelling engine. One useful scenario for rendering labels as HTML is when you want to create a composite label that contains different types of text sizes and colours.

Check your knowledge:

  1. Which of the following statement is incorrect:

    1. HTML labels cannot be combined with expressions
    2. A logo can be inserted as an HTML when the image is fetched from the internet
    3. Text labels can be converted into HTML labels in map composer.
  2. HTML labels can be used when your map consists of vector layer only :

    1. True
    2. False

This lesson was funded by Kartoza