Changing Layout - FlexBox is your Friend

Changing Layout - FlexBox is your Friend

In this article, we will show you how to change the layout of certain parts of your LiaScript course easily with FlexBox. FlexBox is a powerful layout tool that allows you to create complex layouts with minimal code. It is supported by all modern browsers and is easy to use.

What is FlexBox?

FlexBox, or the Flexible Box Layout Module, is a CSS layout model that provides a more efficient way to design complex layouts without relying heavily on float or positioning. It allows elements within a container to be automatically arranged in a way that makes them responsive and adaptable to different screen sizes, making it a great tool for modern web design.

The primary advantage of FlexBox is its ability to distribute space within a container, even when the size of the items is unknown or dynamic. FlexBox also makes it simple to create responsive layouts that adapt to different screen sizes and orientations, which is particularly important for mobile-first design.

Creating a FlexBox Layout

To demonstrate how FlexBox works, let’s create a simple example where we want to arrange several items within a container. We’ll start by defining a container and some child elements, and then apply FlexBox properties to control their layout.

Step 1: Setting Up the Flex Container

First, we need to define a container that will hold our items. We do this by assigning a display: flex; property to the container class, which turns it into a FlexBox container. Here’s how it looks in CSS:

.flex-container {
    display: flex;
    flex-wrap: wrap; /* Allows the items to wrap as needed */
    align-items: stretch;
}
  • display: flex: This property turns the .flex-container into a FlexBox container, meaning its child elements will follow FlexBox rules for layout.

  • flex-wrap: wrap: By adding the flex-wrap: wrap; property, we allow the items within the container to wrap onto the next line if there isn’t enough space to fit them all in one row. This makes our layout more flexible and responsive.

  • align-items: stretch: This property aligns the items along the vertical axis by stretching them to fill the container’s height, ensuring that all items have a uniform height.

Step 2: Defining the Flex Items

Next, we define the individual items that will go inside our flex container. Each item will be given a class .flex-child, and we’ll apply some styling to control how they behave within the flex container:

.flex-child { 
    flex: 1;
    margin-right: 20px; /* Adds space between the columns */
}
  • flex: 1: This property tells the .flex-child elements to take up an equal share of the available space within the container. The value 1 means each child will occupy an equal portion of the container, making it ideal for creating evenly spaced columns or elements.

  • margin-right: 20px: We add a margin-right property to create space between the items, ensuring that they don’t appear too close to one another. This spacing makes the layout cleaner and more visually appealing.

Step 3: Making the Layout Responsive

To ensure that our layout adapts well to smaller screens, such as mobile devices, we can use a media query to adjust the styles when the viewport width is below a certain threshold. In this case, we’ll make the items stack vertically and remove the margin between them on devices with a screen width of 600 pixels or less:

@media (max-width: 600px) {
    .flex-child {
        flex: 100%; /* Makes the child divs take up the full width on slim devices */
        margin-right: 0; /* Removes the right margin */
    }
}
  • flex: 100%: On smaller screens, each .flex-child will take up the full width of the container, causing the items to stack vertically rather than sitting side by side. This ensures that the layout remains readable and user-friendly on mobile devices.

  • margin-right: 0: The margin is removed on smaller screens to prevent unnecessary spacing, allowing the items to use the full width of the screen.

How FlexBox Enhances Your LiaScript Course

By integrating FlexBox into your LiaScript course, you can easily create dynamic and responsive layouts that adapt to different devices and screen sizes. Whether you’re arranging images, text blocks, or interactive elements, FlexBox provides a powerful and intuitive way to manage the layout with minimal code.

By utilizing the @style macro or by importing an external CSS file, you can apply FlexBox properties to specific elements within your LiaScript course. In the following example we have used a section with a class flex-container and then added the class flex-child to each paragraph within a HTML-comment. Of course this could also be nested divs or other elements. With the additional inline styling, you can also define min and max widths, heights, and other properties to further customize the layout.

If you grab the vertical bar between the editor and the preview, you can see how the layout changes when the screen size is reduced. This is a great way to test how your layout will look on different devices and ensure that it remains readable and visually appealing across all screen sizes.

More FlexBox Childs

In Flexbox there is this nice property called flex:, which allows you to distribute the available space equally among all child elements. A value of 1 means that all child elements will have the same width. If you set the value to 2, the child will have twice the width compared to the others with flex: 1. 3 would mean three times the width and so on. This is a great way to create flexible layouts that adapt to different screen sizes and content lengths.

The following example therefor create predefined classes flex-child-1, flex-child-2, flex-child-3, till flex-child-8. These classes can be used to create beautiful newspaper like layouts with different column widths. We had to use a little bit of inline styling to ensure that the columns are not too thin, but you can adjust the values to your needs.

Switch to the split view and grab the vertical bar to see how the layout changes when the screen size is reduced.

Of course, you can use also HTML-tags like divs to add these class configurations, which can contain multiple Markdown-blocks. But do not make it too complex 😏 … in most cases the base LiaScript-Markdown is enough.

Content first

More FlexBox Properties

Here are some additional properties that you can experiment with to further customize your layout:

Container Properties
  1. display: flex … This property turns the container into a FlexBox, allowing its child elements to be laid out according to FlexBox rules.

  2. flex-direction … Defines the direction in which the child elements are placed within the container.

    Common values:

    • row (default): Child elements are placed left to right.
    • column: Child elements are placed top to bottom.
  3. justify-content … Aligns child elements along the main axis (horizontal by default).

    Common values:

    • flex-start: Child elements align at the start.
    • center: Child elements are centered.
    • space-between: Child elements are spaced evenly with the first at the start and the last at the end.
  4. align-items … Aligns child elements along the cross axis (vertical by default).

    Common values:

    • stretch (default): Child elements stretch to fill the container.
    • center: Child elements are centered.
    • flex-start: Child elements align at the top.
  5. flex-wrap … Controls whether child elements wrap onto multiple lines.

    Common values:

    • nowrap (default): All child elements are on one line.
    • wrap: Child elements wrap onto new lines as needed.
  6. gap … Sets the space between child elements, controlling both horizontal and vertical gaps.

Child Properties
  1. flex … Applied to the child elements. A shorthand property that controls how the child elements grow, shrink, and their base size within the container.

    Common usage:

    • flex: 1: Makes child elements grow to fill the available space equally.
  2. align-self … Applied to the child elements. Allows an individual child element to have a different alignment from the other children.

    Values: Same as align-items (e.g., flex-start, center, stretch).


Related Posts

LiaScript: Crash-courSe in 13 Akten (German)

Dieses Tutorial führt in 13 „kurzen“ Schritt-für-Schritt Videos in die Kursentwicklung mit LiaScript ein, von der Installation des Editors hin zu den wichtigsten Gestaltungsmöglichkeiten.

Read More

Offene Bildungsmaterialien auf twillo finden und teilen

Twillo ist ein Portal, das sich auf offene Bildungsmaterialien (OER) spezialisiert hat, insbesondere im Bereich der Hochschullehre.

Read More

LiaScript 0.14 - Partial Documentation (Korean)

This is an automated translation of the LiaScript LiaScript into the Korean language.

Read More