Maybe a pretty simple question. I have a polyline shapefile that I've symbolized through categories, and I wish for the labels to automatically be set to the same color as it's corresponding line segment. Therefore, if I end up changing the symbology in the future the labels will update accordingly. Is this possible, maybe through the expression feature in the labeling options? (I'm very new to qgis).

2 Answers 2


There are a couple of solutions that spring to mind. The first is to create a field in your data that you can use an expression to set the RGB value dependent on your data. Then use this as the expression for both the fill color and text color (accessed by the little 'Data defined' tab beside each color picker).

Another alternative would be to use rules-based symbology and duplicate identical rules for rules based labeling (requires QGIS 2.12 or above). By combining this with layer-level variables you can simplify the process of changing your rules. See the example here. You don't need to use layer-variables, but it can be helpful.

  • Awesome, this is just what I needed to get started. Thanks! May 24, 2016 at 9:42
  • It’s much more easy to use @symbol_color!
    – katagena
    Dec 4, 2021 at 18:17

Here is one solution, I have tested on QGIS3.x

  • Open the layer properties.

  • Select Menu Label

label menu

  • Select the color formula box and
    Color change

    • Select Edit...
    • Type in the code area: @symbol_color
    • And apply the changes

PS: For some usages, I prefer a contrast color for the text. In this case I replace the previous formula with this one:

    /* Contrast color for text */  
    if (  
    color_part(@symbol_color,'red')*0.299 +      
    color_part(@symbol_color,'green')*0.587 +  

See this link for source of the formula

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.