1

How do i get a new line in a Geoserver style CSS ?

{
label: [name] (need new line here) [pop];
}

Details: [name],[pop] are attributes of the layer

Is there a comprehensive CSS documentation with examples for Geoserver? I want the label to look like this

1

I've never tried it in CSS but in SLD you would use a CDATA block containing the new line:

    <ogc:PropertyName>Name_1</ogc:PropertyName><![CDATA[
]]><ogc:PropertyName>Name_2</ogc:PropertyName>

so you could try:

* {
  stroke-width: 0.2;
  label: [STATE_ABBR]<![CDATA[
]]>[OTHER_VAL];
  label-anchor: 0.5 0.5;
  font-family: "Times New Roman";
  font-fill: black;
  font-style: normal;
  font-size: 14;
}

There is a lot of CSS documentation in the GeoServer manual including the Cookbook, and a tutorial and even a full workshop.

UPDATE

It turns out that it is easier than I thought you just need to quote the return:

* {
  fill: lightgrey;
  stroke: black;
  stroke-width: 2;
  label: [STATE_NAME]'
    '[STATE_ABBR];
  label-anchor: 0.5 0.5;
  font-family: "Times New Roman";
  font-fill: black;
  font-style: normal;
  font-size: 14;
}

which gives this image:

enter image description here

  • I tried the label: [STATE_ABBR]<![CDATA[ ]]>[OTHER_VAL]; it did not work; instead its throwing an exception asking to write a CQL expression in between < >. I have gone through the documentation but did not find anything related to new line in the label. Thanks for the suggestion though – Shreyes Shiv Sep 19 '17 at 5:22
  • I guess the "indentation" you added is also getting into the map :-) – Andrea Aime Sep 19 '17 at 10:11
  • probably - I didn't notice that – Ian Turton Sep 19 '17 at 10:19
  • @iant you are very close to the answer, where you delete the indentation and the New line is applied with no indentation in the second line. Now the next question is how to control the spacing between the two lines? – Shreyes Shiv Sep 21 '17 at 4:49
1

I had the same problem and it worked like this for my CSS style:

* {
 label: [PART1][PART2];
 -gt-label-auto-wrap: 1;
}

For me it worked with '1', maybe you need to adapt this value.

If you want it to work in your SLD style I think you need to really add the return between the square brackets in your document. So write:

<Label>
<ogc:PropertyName font-family='normal'>PART1</ogc:PropertyName><![CDATA[
]]>
<ogc:PropertyName>PART2</ogc:PropertyName>
</Label>

instead of:

<Label>
<ogc:PropertyName font-family='normal'>PART1</ogc:PropertyName><![CDATA[]]>
<ogc:PropertyName>PART2</ogc:PropertyName>
</Label>
  • { label: [PART1][PART2]; -gt-label-auto-wrap: 1; } this is good suggestion but, if there are two words in the first attribute the auto-wrap wraps the contents of the [PART1] that too. – Shreyes Shiv Sep 21 '17 at 4:47
  • Have a look at this post on how to split labels: Split label style in GeoServer CSS or SLD @iant and Andrea Aime suggested a solution by using offset/displacement that should work for you as well. – Michaela Sep 21 '17 at 6:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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