How to set background color of an image?

Hi!
I’m trying to create a header for a paper I’m working on. I’m using a transparent banner image with white text, which is impossible to read in my document. I want to set the background color behind the image to be a specific color, lets say gray. I couldn’t find any property in #image.This is what I have so far (not very much :)):

#image("banner.png", width: 100%)

Solved it! :grinning:

  rect(
    image("banner.png", width: 100%),
    width: 100%,
    fill: black
  )

Now to figure out how to place it absolutely, without considering page margins…

That sounds like a good solution, using rect or block to draw the background.
You can place things overlappingly or offset using the place function. The easiest way to draw on the whole page is using page.background, which gives you access to the page without margins already (so you don’t need to compensate for them in place’s dx and dy).

#set page(background: place(top + left, rect(image(..))))

If you need to, you could also use a conditional like if here().page() == 1 or something similar to only draw this page background on a particular page.

1 Like

Thank you!
I only saw counter(page).get().first(), which to me seemed a bit cumbersome. Appreciate the tip about here().page() == 1.
This is what I ended up doing:

#set page(background: context [
  #align(top, [
    #if here().page() == 1 [
      #rect(
        align(left, image("banner.png", width: 25%)),
        fill: black,
        width: 100%
      )
    ]
  ])
])

This is the result:

Edit: Changed {}to [], which is considered better practice

1 Like

Good. counter(page) is the page counter for display, which you can set and change as you want, and here().page() is a monotonic physical page counter, which is often used for even/odd page logic and so on, and it cannot be changed.

1 Like

Hi. Make sure you read How to post in the Questions category.

Try to use curly braces by default instead of square brackets. Create a dedicated section on dangers of using square brackets carelessly (multi-line) · Issue #6844 · typst/typst · GitHub

1 Like