Path: blob/main/doc/tutorials/intermediate/advanced_layouts.md
2012 views
Advanced Layouts
Responsive Layouts with FlexBox
So far when we have talked about responsive layouts we have primarily focused on simple width
/height
responsiveness of individual components, i.e. whether they will grow and shrink to fill the available space. For a truly responsive experience however we will need responsive layouts that will reflow the content depending on the size of the screen, browser window or the container they are placed inside of, much like how text wraps when there is insufficient width to accommodate it:
Panel offers one such component out of the box, the FlexBox
layout.
FlexBox
is based on CSS Flexbox and supports many of the same options, such as setting flex_direction
, flex-wrap
, align_items
and align_content
.
Distributing proportions
To achieve more complex layouts, i.e. specific proportions between different components we can use the flex
property on the children of our FlexBox
, e.g. here we declare that the green Spacer should be three times as wide as the red and blue components.
To learn more about this read this guide on controlling ratios of flex items.
Exercise
Using only the styles
of the components, distribute the two dataframe
s and the markdown
such that the dataframe
s are both 3 times wider than the markdown
, then center the markdown
vertically.
You might find inspiration in this guide on aligning items in a flex container.
:::{dropdown} Solution
:::
:::{note} Getting the FlexBox
styles
right can be tricky. If you need help try posting a minimum, reproducible example on Discourse. :::
Media queries
To achieve layouts depending on the overall screen/browser width, e.g. to have a different layout depending on whether we are working on a desktop or a mobile we can use media queries. Media queries allow us to apply different rules depending on a min-width
or max-width
, e.g. the example below will force the flexbow into a column layout when the viewport is below a size of 1200px:
Try changing your browser width to see how the layout changes from row based to column based.
Exercise: Challenge
This exercise is a bit more free-form and can be solved in many ways.
Please generate a layout that is both responsive and visually pleasing starting from the below code.
:::{dropdown} Solution
This is just an example solution. The exercise can be solved in many ways.
COMING UP
:::