Nest DIVs within a DIV located beneath the initial DIV

My goal is to have two DIVS (one grey and one red) placed inside a black DIV underneath another black DIV when the window is resized to less than 1024px. Refer to the example provided below or view the attached image.

I could really use some guidance on this, as I am currently feeling quite lost.

This is how I envision it for screens wider than 1024px:

<div id="black">
    <div id="grey"></div>
    <div id="red"></div>
</div>

This is how I want it to appear on screens narrower than 1024px:

<div id="black"></div>
<div id="grey"></div>
<div id="red"></div>

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

Answer №1

Avoid replicating the information.

#black{background:black;overflow:hidden;}

#grey, #red{
  min-height:100px;
  margin:2%;
  float:left;
  width:47%;
}

#grey{background:gray;margin-right:1%}
#red{background:red;margin-left:1%}

@media (min-width:1024px){
  #black{padding-top:100px;}
  #grey, #red{
    float:none;
    width:auto;
    margin:0;
  }
}
<div id="black">
  <div id="grey"></div>
  <div id="red"></div>
</div>

Answer №2

I'm sorry, the request as written is not feasible.

CSS does not allow for moving items outside of their designated containers; it only permits reformatting within the existing structure.

As an alternative, you could duplicate the black div and toggle between them using media queries to adjust based on screen size.

Another option is utilizing jQuery or JavaScript to relocate DOM elements, as CSS alone cannot accomplish this task.

Answer №3

To create a responsive layout using only CSS and media queries, you can utilize two container <div> elements to manage the logic:

@media (max-width: 1024px) {
  .large { display: none; }
  .small { display: block; }
  .black { height: 100px; }
}

@media (min-width: 1025px) {
  .large { display: block; }
  .small { display: none; }
  .red, .grey { float: left; }
  .black:after { content: ""; display: table; clear: both; }
  .red { width: calc(50% - 5px); margin-left: 10px; }
  .grey { width: calc(50% - 5px); }
}

.large {
  height: 200px;
}

.small {
  height: 200px;
}

.black {
  background-color: black;=
  height: 100px;
  padding: 10px;
  box-sizing: border-box;
}

.red {
  background-color: red;
  height: 100px;
}

.grey {
  background-color: grey;
  height: 100px;
}
<div class="large">
  <div class="black">
    <div class="grey"></div>
    <div class="red"></div>
  </div>
</div>

<div class="small">
  <div class="black"></div>
  <div class="grey"></div>
  <div class="red"></div>
</div>

For optimal viewing of the above snippet, consider opening it in full page mode or visit this codepen link

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

Focusing on the Div element to set the background color

I'm attempting to target the specific div that will allow me to add a background color to the highlighted red area below. I've tried using this code #yui_3_10_1_1_1370470471782_587, but I'm not sure if it's the correct one. If someone ...

What is the best way to ensure that only one accordion tab remains open at a time, preventing it from closing unless a different tab

How can I ensure that only one accordion tab is open at a time, automatically closing any others that are currently open? Here is my current code for the accordion: $('[data-bs-toggle="collapse"]').on('click', function(e) { if ($( ...

Using Jquery to determine if a div is empty, excluding any content before or after it

I'm looking to use JQuery to determine if a Div is empty without taking into account any content added before or after it. When checking for emptiness, I want to ignore anything placed before or after the div. $(function(){ if($('#content&ap ...

Alert popup onclick feature is malfunctioning

SOLVED: I finally figured out the issue and it is now working perfectly. I had to manually switch to Chrome instead of using the Brackets live viewer. I want an alert box to pop up when the "Home" link is clicked on my website. I tried creating a separate ...

The alignment and sizing issues with stacking icons using fontawesome are still unresolved

I have been exploring the use of stacked icons with fontawesome, following a helpful blog post by Dave Gandy. Although stacking is working well, I am facing an issue aligning a stacked icon with a non-stacked icon (which is fa-5x). It's challenging t ...

Escaping from its container, text breaks free with the power of CSS

Experiencing difficulties with CSS flex layout that are proving challenging to resolve. HTML: <div class="image-view"> <div class="info"> <span class="label title">d37ci4x.jpg</span> <span class="label scale ...

How to continuously animate images in HTML using Bootstrap

I want to showcase 7-8 client images in a continuous loop using a <marquee> tag. The issue is that there is a gap between the last and first images. Here is the HTML code I have: <marquee> <ul> <li><a href="#&q ...

Tips for arranging divs in the exact way you want

Can you assist me in creating a layout like the one shown in the image below? I have attempted to achieve it, but my understanding of CSS is lacking and the layout needs to be completed quickly... I experimented with CSS properties such as float, overflow ...

The dropdown menu disappears when I hover over the tab, making it impossible for me to navigate it in the react app

My navigation component includes a Games tab with a dropdown menu that appears when you hover over it. However, I'm facing an issue where the dropdown menu closes before the user can transition from hovering over the games tab to the actual dropdown m ...

Struggling to position elements next to a floated div?

I'm facing an issue where I have a floated div with a margin of 200px from the right side, and I am trying to place a table on that empty space. Below is the CSS for the div: #testo { border-right: solid black 1px; background-color: white; mar ...

Is there a way to disable the backspace keycode in an input field?

I need to disable all keys in input field, this is what I have so far window.onkeypress = function(event) { event.preventDefault(); if (event.charCode && (event.charCode < 48 || event.charCode > 57)) { return false; } } <input type="t ...

Creating a text-filled alphabet design using HTML, CSS, and JavaScript: A step-by-step guide

I have a creative project in mind where I want to design various shapes resembling English alphabets, and each shape should include text. I already have an image file illustrating my idea. My objective is to accomplish this using only html css javascript. ...

choose a selection hopscotch

Is there a way to prevent the input field from jumping out of alignment when an option is selected in these q-select inputs? I want to fix this issue so that the field remains in line with the horizontal alignment. https://codepen.io/kiggs1881/pen/RwENYEX ...

Detect if the user is using Internet Explorer and redirect them to a different

My web application is having trouble rendering in Internet Explorer. In the meantime, I would like to detect if the user is using IE and redirect them to a different page specifically for IE visitors. What is the best way to accomplish this? Should I use ...

Issue with Firefox causing Bootstrap form to appear incorrectly

I recently created a customized form using Bootstrap, but unfortunately, I'm encountering an issue with Firefox. Despite functioning perfectly on other browsers, the radio buttons are being pushed off the edge of the page and aren't displaying pr ...

Pictures failing to appear in css/jquery slider

I have a total of 12 divs that are configured to display a thumbnail along with some text dynamically retrieved from a database query. When displayed individually, they appear fine in a column format with a border around them showcasing the thumbnail and t ...

When the "disabled" property is enabled, the <Select> element prevents the scrollbar from appearing

I've encountered an issue where disabling a "select" element also disables the scrollbar in Internet Explorer, while it functions perfectly in Chrome and Firefox. Is there a workaround for this problem? <select size="5" style="height:100px" disa ...

The horizontal alignment of a jQuery div is off-center

There is a div element with a width: 50% and some text inside. Using jQuery, I am calculating the widths of both. Then, I attempted to horizontally center the text within the div using this jQuery code: var wrapperWidth = $("#wrapper").width(); var textW ...

What is the best way to display content next to an image, as well as below the image once it finishes?

Currently, I am working on customizing my blog posts in WordPress. My goal is to have the content of each post displayed next to the post thumbnail, and once the image ends, the content should seamlessly flow underneath it from the left side. I have imple ...

Discovering the initial word of a string using jQuery

I'm currently facing an issue with jQuery that I need help solving. Here's the scenario: I am creating tooltips and positioning them directly under a specific trigger using CSS. Everything is functioning correctly, but there is one problem: In ...