Ensure consistent switching of flexbox directional layout

I'm attempting to create a unique layout using only css3. My challenge is to achieve this without explicitly setting sizes, allowing the layout to flow freely. I am utilizing flexbox in my css for this purpose. After an automatic wrap, I want the layout direction of the smaller items to change.

https://i.sstatic.net/DB5H9.png https://i.sstatic.net/1n60P.png

I've almost achieved this behavior by incorporating @media(orientation: portrait). However, I still face size restrictions on the red box. There is also a third state where the yellow items should be displayed horizontally next to the red box.

div {
  min-width: 50px;
  min-height: 50px;
  margin: 8px;
  justify-content: flex-end;
}

#body {
  background: lightgray;
  padding: 8px;
  display: flex;
  flex-wrap: wrap;
}

#box {
  background: lightpink;
  min-width: 150px;
  height: 220px;
  flex-grow: 1;
}

.item {
  background: lightyellow;
}

#wrap {
  margin: 0;
}

@media(orientation: portrait) {
  #wrap {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
  }
}
<title>Example</title>
<div id="body">
  <div id="box"></div>
  <div id="wrap">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
  </div>
</div>

Please note that all sizes are purely cosmetic.

I am curious if it's achievable to have the item box automatically wrap when in landscape mode. Alternatively, is there a way to style the red box to fill the entire horizontal viewport?

Answer №1

Make sure to include display:block on the div with id body and insert the following code in your media query:

@media(orientation: portrait) {
#wrap { display: flex; flex-wrap: wrap; justify-content: flex-end;border:solid red;}
#body{display:block;}
}

View a resizable window example here

Snippet

<!DOCTYPE html>
<meta charset="utf-8">
<style type="text/css">
div { min-width: 50px; min-height: 50px; margin: 8px; justify-content: flex-end; }
#body { background: lightgray; padding: 8px; display: flex; flex-wrap: wrap; }
#box { background: lightpink; min-width: 150px; height: 220px; flex-grow: 1; }
.item { background: lightyellow; }
#wrap { margin: 0;border:solid green; }
#break{}
@media(orientation: portrait) {
#wrap { display: flex; flex-wrap: wrap; justify-content: flex-end;border:solid red;}
#body{display:block;}
}
</style>
<title>Example</title>
<div id="body">
<div id="box"></div>
<div id="wrap">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div></div>

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

The issue of MUI components overlapping arises during window resizing

I am currently working on a chat component that includes both the chat display and message input fields. function Chat() { const chatBoxStyles = { bgcolor: "red", height: "70vh", mt: "1rem" }; const messageIn ...

Strategies for detecting changes in multiple HTML select elements with jQuery

I currently have three select HTML tags and I would like to filter my table using the selected values (group of selected tags) with an AJAX request. For example, filtering by Gender, Location, and Season. My question is how can I achieve this without dup ...

Troubleshooting Problems with CSS Media Queries on Various 1280 screens

I'm facing an issue with creating a responsive layout using CSS media queries to adapt to different screen resolutions. Even though I've set up Media Queries for specific resolutions like: /*1280 x 1024*/ /*1280 x 960*/ /*1280 x 800*/ /*1280 x 7 ...

When trying to set the focus on the first item in a list using HTML and Angular, the focus unexpectedly shifts to the second

I've been tackling a UI requirement where the focus needs to be set on the first element of a list item constructed from an array of objects when the tab key is pressed for the first time. Subsequent tab key presses should cycle through the list items ...

Tips on effortlessly updating the URL of your website

Despite seeing the question asked multiple times, I still find myself struggling to understand how to modify a URL or create a new HTML page that seamlessly integrates into a website without redirecting users. I am curious about achieving the functionalit ...

What is the best way to enclose an h2 with a designated class, along with the following two elements, inside a wrapper class?

Looking to select a specific block of code to wrap with a class using the jQuery .wrapAll method. I need to target the <h2> element and the two elements that follow it. Unfortunately, the elements before and after this block are inconsistent, ruling ...

ReactJS creating trouble with incorporation of CSS in Table

I've noticed some strange behavior with the alignment of my table data. It seems to be all over the place. How can I fix this issue? Is there a way to specify column widths and ensure the text starts from the same position? Take a look at the table b ...

Adjusting canvas size to match content dimensions

I posed a similar inquiry and it was categorized as a duplicate even though the suggested duplicate did not provide an answer to my question. Edit: The purported duplicate was [stackoverflow.com/questions/17211920/make-canvas-height-auto][1] I am utilizi ...

Tips for integrating Rate Yo into the database

I have been attempting to integrate Rate Yo into the database, but I am consistently encountering an error message: Undefined index: rating in C:\xampp\htdocs\bill\bill\add_rate.php on line 57 Below is the code that I have trie ...

"Aligning two images side by side in a horizontal

[enter image description here][1]I am trying to place two images on my website, but I am struggling to vertically align them properly. I have attempted using line-height and vertical alignment, however, I cannot seem to pinpoint the issue or identify any m ...

Extract the HTML content from a file stored on the computer

Currently, I am utilizing Google App Engine alongside Python. My goal is to retrieve the tree structure of an HTML file located within the same project as my Python script. Despite attempting numerous approaches, such as using absolute URLs (for example ht ...

Troubleshooting: Why the TableTools basic usage example is not functioning as

After replicating the code from http://datatables.net/release-datatables/extensions/TableTools/examples/simple.html in my Visual Studio project, I organized the files by saving two css files as style1.css and style2.css, along with three js files named sc ...

The dropdown menus in Bootstrap are expanding outside the screen when opened

When I click on the dropdown in the Navbar on the right side, the menus open up far to the right and are not visible until I scroll horizontally. Here is the code snippet: <nav class="navbar navbar-expand-lg navbar-light bg-light"> < ...

A guide to smoothly transition box-shadow with jQuery's addClass() function

UPDATED I have a div with the class .shadow-circle-lg: <div class="shadow-circle-lg"></div> To add a different border styling to the div, I am using another class called .circle-highlight: .circle-highlight{ box-shadow: 0 0 0 1px rgba(0,0, ...

The distance separating the top navigation bar from the sub-navigation menu

I have designed a navigation menu with a subnavigation section, view it on JSFiddle with subnavigation. My challenge is to create a 1px solid white separation between the top navigation (with yellow background) and the subnavigation (with red-colored backg ...

Attempting to align a navigation block next to an SVG image

On my website, I envisioned a photoshopped image featuring a SVG picture of a book on the left and a "navigation block" on the right. The navigation block consists of links that redirect to other pages but it's not displaying properly. You can view th ...

The jQuery UI Sortable functions are being triggered at lightning speed

I am currently working on a project where users can create a seating chart, add rows and tables, and move the tables between different rows. The functionality for adding rows and moving tables already exists in the code. However, I am facing an issue where ...

Display flash messages consistently in a designated area on the webpage instead of appearing at the top of the page in Flask

{% with messages = get_flashed_messages(with_categories=true) %} {% if messages %} {% for category, message in messages %} <div class="alert alert-{{ category }}" style=" ...

A trio of versatile sections within a box, one designed to accommodate text overflow

Trying to create a CSS layout where three texts are placed side by side without wrapping, but once the first text is too long, it needs to be truncated. Check out the image below for more details. Methods I have attempted so far: Floated positioning of ...

React - CSS Transition resembling a flip of a book page

As I delve into more advanced topics in my journey of learning React and Front Web Dev, I discovered the ReactCSSTransitionGroup but learned that it is no longer maintained so we now use CSSTransitionGroup. I decided to create a small side project to expe ...