The positioning of the div in the Flex layout changes depending on the device - on desktop it appears below other elements, while

I am working on my first flex layout and I need help with achieving the desired placement for sections on both desktop and small devices (please refer to the attached photo).

The "License" div should change its order depending on screen width: on mobile devices it should be displayed last, but on desktop it needs to appear right after the text in the first column on the left.

Do you think this is achievable with just flex? Or should I consider using the grid layout model instead?

My flex example is simple except for the issue on desktop where I have had to use a workaround (like the ".break-column" CSS rule) to position the "license" div below due to the "flex-direction: row;" property.

Currently, the design works as intended on mobile devices but the "license" div is still shown last on desktop. It's possible that my HTML structure is incorrect. Any guidance in the right direction would be greatly appreciated!

Thank you in advance!

Link to Example

https://i.sstatic.net/nz0ii.png

Answer №1

To achieve the desired layout using flexbox, you have two options:

  1. One approach is to utilize JavaScript by adding a resize event listener on the window. When the screen width reduces below a certain threshold, move the .content-license element to the bottom of the webpage.
  2. Alternatively, you can duplicate the .content-license element. One instance can be placed within the .content__wrapper, while the other can be positioned at the end of the page. On larger screens, hide the duplicate at the end of the page, and on smaller screens, hide the one in the .content__wrapper.

The provided code snippet adopts the latter strategy, eliminating the need for JavaScript implementation.

.home__flex {
  display: flex;
  background: rgb(230, 253, 214);
  flex-wrap: wrap;
}

.cover__wrapper {
  display: flex;
  flex-direction: column;
  padding: 15px;
}

.content-license {
  padding: 15px;
}

.cover__wrapper .content-license {
  display: none;
  padding: 0;
}

.cover__wrapper img {
  width: 50%;
  height: 200px;
  align-self: center;
}

.links__wrapper {
  display: flex;
  flex-wrap: wrap;
  flex-grow: 1;
  padding: 15px;
}

@media (min-width: 550px) {
  .content-license {
    display: none;
  }
  .cover__wrapper .content-license {
    display: block;
  }
}

@media (min-width: 750px) {
  .cover__wrapper {
    flex-basis: 20%;
    padding: 35px;
  }
  .cover__wrapper img {
    width: 100%;
  }
}

@media (min-width: 850px) {
  .cover__wrapper {
    flex-basis: 20%;
  }
  .links__wrapper {
    padding: 0 35px;
  }
}
<div class="container__home">
  <div class="home__flex">
    <div class="cover__wrapper">
      <img class="cover" src="https://i.ibb.co/Kx6Hm9w/dang.png" alt="dang" border="0">
      <div class="content-descr">
        <p>
          Until recently, the prevailing view assumed lorem ipsum was born as a nonsense text. “It's not Latin, though it looks like it, and it actually says nothing, After magazine answered a curious reader.
        </p>
      </div>
      <div class="content-license">
        <h2>Licence</h2>
        <p>
          Licensed under a Creative Commons attribution-noncommercial license.
        </p>
      </div>
    </div>
    <div class="links__wrapper">
      <div class="table-of-contents">
        <h1>Content</h1>
        <ol>
          <li>Until recently, the prevailing view </li>
          <li>It's not Latin, though it looks</li>
          <li>Its ‘words’ loosely approximate the frequency</li>
          ... (additional list items)
        </ol>
      </div>
      <div class="other-links">
        <h2>Other</h2>
        <ul class="links">
          <li><a href="code">After magazine</a></li>
          <li><a href="errata.html">looks pretty cool</a></li>
          <li><a href="/">Lots of laugh</a></li>
        </ul>
      </div>
    </div>
    <div class="break-column"></div>
    <div class="content-license">
      <h2>Licence</h2>
      <p>
        Licensed under a Creative Commons attribution-noncommercial license.
      </p>
    </div>
  </div>
</div>

Answer №2

Your question is a bit unclear, but if you're looking to adjust the order of your divs using flexbox, you can utilize the order property along with @media queries. This will allow you to rearrange your elements based on screen size.

Here's an example:

.container {
    display: flex;
}

@media (min-width: 600px) {
    .div-1 { order: 2; }
    .div-2 { order: 1; }
    .div-3 { order: 3; }
}

This code snippet will change the order of the divs within the container at a screen width of 600px or larger. Make sure to assign each div a specific order value for this to work correctly.

For further guidance on flexbox, you can visit W3Schools Flexbox. You can also experiment with it using the W3Schools Editor.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I display only a horizontal scrollbar on the body content while avoiding an excessively long image header?

Currently, I have a 4000px width image as the header of my website. To hide the horizontal browser scrollbar, I am using the following CSS: html { overflow-x: hidden; } However, this method results in the horizontal scrollbar never appearing. I ac ...

VueError: Unable to sort ad performance data

Hi there, I'm encountering a typeerror in my code and need some assistance. My goal is to sort a simple table using the code below. <template> <div> <div class="row"> <h2>Ad performance</h2> </div> <d ...

What caused Safari to only show half of the page?

Whenever I browse my website using Safari, I noticed that if the total size of the content on the first page is less than 100vh, Safari ends up cutting off half of the page. To fix this issue, I added a CSS rule in the body setting min-height to 110vh wh ...

Chrome-derived web browsers are failing to properly display the <span> element when pre-wrap is configured

Consider the following snippet of HTML... <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <style> /* Code blocks */ div.code { white-space:pre-wrap; display: table; ...

Customizing active-class in Vuetify

How do I customize the appearance of my v-list-item-group when it is in the active state? <v-list> <v-list-item-group v-model="day" color="green"> <v-list-item v-for="(item, i) in items" :key="i"> <v-list-item-content& ...

What is the process for locating and displaying a database in WordPress?

I seem to be facing a challenge when it comes to developing a new functionality since I am relatively new to both WordPress and web development. My current goal is to incorporate a search feature into my project. The idea is that when a user enters a sea ...

Injecting a variety of wallpapers dynamically using css and html

As a beginner in CSS, HTML, and coding in general, I am currently working on creating my own start page with a local path that loads my bookmarks using an HTML file. The background-image control is handled by the style.css file. I have researched ways to c ...

I aim to utilize JavaScript to capture the constantly changing font color that is randomly generated

Is there a way to extract the font color from a table element? #FF0000 For example, it could be #568000 etc.. Here's my table <table class="mytable"> <th class=vertical bgcolor=#C0C0C0 align=center> <font color=#FF0000 size= ...

Position a button to be aligned with the bottom of its container

My goal is to have the button positioned at the bottom of the red div, nested inside it. .grand-parent { display: flex; flex-direction: row; flex-wrap: wrap; height: 300px; background-color: green; } .pa ...

Alignment issues with columns in Bootstrap

In my layout, I have three columns with a container in each column. However, I am experiencing an unusual display issue: https://i.sstatic.net/CLT1q.png An example of the desired display is shown here: https://i.sstatic.net/4dCru.png The three columns ...

Tips for eliminating the additional spacing that might occur between the logo and the content

Is there a way to ensure the white content is right up against the logo without any blank line in between? I've already set the margin-top to 0, but there's still space appearing. <body background="$request.getContextPath()/images/background. ...

Stop the form from refreshing upon submission using an Ajax call in AngularJS

Currently, I am in the process of developing a search form that requires two inputs: Job title and Location. These keywords are used to gather data from various websites. However, upon submitting the form, the page refreshes itself. To prevent this, I have ...

When the cursor hovers over an item, I would like to enlarge its width while simultaneously shrinking the width of two additional items

Here is a section that I have created, you can view the mockup here. The section is divided into 3 parts, each taking up around 33.3% of the width and floated. My goal is to make it so that when a specific element, like .col-one in my example, is hovered ...

Decrease the amount of empty space when adjusting the window size

Struggling to make a section of my website responsive and encountering an issue that's proving difficult to solve. The current setup involves 3 rectangular inline-block divs per "row" with a margin-right of 100px in a wrapper, all dynamically added. ...

The transition effect does not seem to be functioning correctly when adding a class

It seems like a common issue, but none of the solutions I've come across so far have helped. Everything appears to be standard and simple code, yet I can't seem to get it to work properly. I'm trying to create a dropdown menu where a hidden ...

Struggling to identify which css or bootstrap statement is impacting the arrangement of the form layout

I'm facing an issue with setting a border and background color behind a form. When I attempt to do so, the form is aligned to the right and I can't pinpoint which part of my code is causing this misalignment. Here's an example: https://jsfi ...

I'm new to this, but can someone explain why I'm being redirected to the register_db.php page when I click on the register button?

Why when I click the register button, it redirects me to the register_db.php page instead of sending the data to the database and keeping me on the same register.php page? I want the data to be sent to the database without changing the webpage. register.p ...

Struggles between ajax asp.net and jquery colliding

Within my project, I have incorporated numerous jquery files. After researching this issue yesterday, I came across a solution. I made the following adjustment: $(document).ready(function () { and substituted it with: function pageLoad() { An issue has ...

The iframe is being loaded in the center of the page rather than at the top

I am currently facing an issue with embedding wetransfer into a website using iframe. The problem is that when the page loads, it automatically scrolls past the header section of the site instead of loading the new page at the top. How can I prevent this ...

"Encountering a Challenge with Setting Up Forms on

I've recently started learning to code and I'm facing an issue with my form appearing stretched out. You can view it here: I initially thought it was due to margins, so I increased them. Then, I tried adjusting the width to 50%, but that didn&ap ...