Image inside a row with the right aspect ratio #3603
-
QuestionHello, 👋 I'm trying to put an The main takeaway is that due to the use-case of my application, in our case, the width of the image should not be hard-coded since we don't know the dimensions of the image but the aspect ratio should remain. Here is an MRC of my layout: from nicegui import ui
WIDTH = 300
HEIGHT = 600
ROW_HEIGHT = 64
MY_SVG = f"https://placeholder.pics/svg/{WIDTH}x{HEIGHT}"
def my_layout() -> ui.row:
with ui.row().style(f"max-height: {ROW_HEIGHT}px") \
.classes("w-full border flex-nowrap") \
.style("border-color: red; align-items: center") as row:
ui.button(icon="menu")
ui.space()
with ui.row().style("padding: 0px;"):
ui.button(icon="search")
return row Here are a few of the attempts I've tried: # Attempt 1
with my_layout():
ui.image(MY_SVG) # Just an image. # Attempt 2
with my_layout():
ui.image(MY_SVG).style("max-width: 100%; max-height: 100%;") # Try to limit the size of the image. # Attempt 3
with my_layout():
ui.image(MY_SVG).style(f"height: {ROW_HEIGHT}px").props("fit='contain'") \
.style("border-color: blue").classes("border") # Match the height of the parent row using 'contain'. # Attempt 4
with my_layout():
ui.image(MY_SVG).style(f"height: {ROW_HEIGHT}px").props("fit='shrink-down'") # Match the height of the parent row using 'shrink-down'. # Attempt 5
with my_layout():
ui.image(MY_SVG).style(f"height: {ROW_HEIGHT}px; width: {ROW_HEIGHT}px") # Attempt 6
with my_layout():
ui.image(MY_SVG).style(f"height: {ROW_HEIGHT}px; width: {ROW_HEIGHT}px").props("fit='contain'") \
.style("border-color: blue").classes("border") # Match the height of the parent row using 'contain'. # Attempt 7
with my_layout():
ui.image(MY_SVG).style(f"height: {ROW_HEIGHT}px; width: {ROW_HEIGHT}px").props("fit='shrink-down'") # Match the height of the parent row using 'shrink-down'. # Attempt 8
with my_layout():
ui.image(MY_SVG).style(f"max-height: {ROW_HEIGHT}px; max-width: {ROW_HEIGHT}px").props("fit='contain'") \
.style("border-color: blue").classes("border") # Match the height of the parent row using 'contain'. # Attempt 9
with my_layout():
ui.image(MY_SVG).style(f"height: {ROW_HEIGHT}px; width: auto").props("fit='contain'") # Match the height of the parent row using 'shrink-down'. Needless to say I've scoured through Quasar documentation, but I can't figure for the life of me how to do this right. Any guidance would be appreciated. 😁 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @PoshoDev, I see two possibilities:
In both cases you need to set a height to the |
Beta Was this translation helpful? Give feedback.
Hi @PoshoDev,
I see two possibilities:
Use QImg's "height" prop instead to set the image height, "fit=scale-down" and "position" to place it inside the occupied space:
Alternatively you can use
ui.interactive_image
, which behaves a bit more ordinarily when it comes to layout and sizing:In both…