splitting up the lengthy list into manageable chunks according to the screen dimensions

How can I make the blue color row-based list (the divs inside a div with id phrase) break on the next line based on the screen size? Any suggestions on additional changes needed in the following CSS?

#phrase {
            color: #fff;
            position: relative;
            z-index: 2;
            display: flex;
            flex-direction: row;
            margin: 2rem 0;
        }

Snippet of my code:

 $(".words").draggable({
        revert: function (event, ui) {
            var bRevertingPhrase = this.closest("#drop-em");

            if (!bRevertingPhrase.length) {
                var phraseID = $(this).data("id");
                var phraseHomeID = $(this).parent().data("id");

                //If the child and parent ids don't match, we move the child to the correct parent
                if (phraseID !== phraseHomeID) {
                    var correctCell = $("#phrase").find("div[data-id='" + phraseID + "']");
                    correctCell.prepend(this);
                }
            }
            return !event;
        }
    });
    $("#drop-em > div").droppable({
        drop: function (ev, ui) {
            $(ui.draggable)
                .detach()
                .css({ top: 0, left: 0 })
                .appendTo($(this).find(".content:empty"));
        }
    });
    $("#phrase > div").droppable({
        drop: function (ev, ui) {
            $(ui.draggable).detach().css({ top: 0, left: 0 }).prependTo(this);
        }
    });

    const myButton = document.querySelector("#move-text");
    myButton.addEventListener("click", () => {
        fill();
    });

    function fill() {
        const cells = document.querySelectorAll("#phrase > div > span");
        var destination = 0;
        var newLoc = "";
        cells.forEach((cell, index) => {

            newLoc = document.querySelector(
                ".item:nth-child(" + cell.dataset.destination + ") .content ");

            newLoc.append(cell);




            newLoc.classList.add("moved");
        });
    }
        html {
            box-sizing: border-box;
        }

        *,
        *:before,
        *:after {
            box-sizing: inherit;
        }

        body {
            counter-reset: columnCount 1 alphaCount;
        }

        h1 {
            text-align: center;
        }

        .wrap {
            display: flex;
            gap: 2rem;
        }

        .grid {
            margin: auto;
            display: grid;
            flex: 1 0 0;
            grid-template-columns: repeat(12, 1fr);
            padding-top: 1.5rem;
        }

        .item {
            position: relative;
            background-color: #f9f9f9;
            border: 1px solid grey;
            aspect-ratio: 1/1;
            counter-increment: columnCount;
            min-width: 0;
            transition: background 1s ease;
        }

        .item:nth-of-type(12n + 1) {
            counter-increment: alphaCount;
        }

        .item:nth-of-type(12n + 1)::before {
            content: counter(alphaCount, upper-alpha);
            position: absolute;
            display: flex;
            align-items: center;
            top: 0;
            bottom: 0;
            left: -1.75rem;
            color: red;
            pointer-events: none;
        }

        .item:nth-of-type(n + 13)::after {
            display: none;
        }

        .item::after {
            content: counter(columnCount);
            position: absolute;
            left: 0;
            right: 0;
            text-align: center;
            top: -1.75rem;
            color: red;
            pointer-events: none;
        }

        .content {
            display: flex;
            flex-direction: column;
            justify-content: center;
            width: 100%;
            height: 100%;
            overflow: auto;
            color: #000;
            padding: 1rem;
            word-wrap: break-word;
        }

        .words {
            cursor: move;
            transition: padding 0.5s ease;
        }

        .content:has(.ui-draggable-dragging) {
            overflow: visible;
        }

        .ui-droppable-active .content {
            overflow: visible;
        }

        .words.ui-draggable-dragging {
            background: blue;
            padding: 5px 10px;
            border-radius: 6px;
            z-index: 999;
            color: #fff;
            display: block;
            width: 50px;
            height: 50px;
            overflow: hidden;
        }

        #phrase {
            color: #fff;
            position: relative;
            z-index: 2;
            display: flex;
            flex-direction: row;
            margin: 2rem 0;
        }

        #phrase>div {
            margin: 0 10px 10px;
            padding: 5px 10px;
            background: #007bff;
            border: 2px solid #007bff;
            border-radius: 6px;
            color: #fff;
            min-width: 100px;
        }

        #phrase>div:empty {
            background: #fff;
            border-style: dashed;
            padding: 0px 25px;
            min-height: 30px;
        }

        .moved {
            animation: fade 3s ease;
        }

        @keyframes fade {
            0% {
                opacity: 0;
            }

            50% {
                opacity: 1;
                background: red;
            }
        }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
    
    <div id="phrase">

... [Your original HTML content continues here] ...

Answer №1

To ensure the div on the following line breaks correctly based on screen size, utilize the flex-wrap: wrap property.

#phrase {
    color: #fff;
    position: relative;
    z-index: 2;
    display: flex;
    flex-wrap: wrap;
    margin: 2rem 0;
}

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

Ways to avoid a bootstrap column from wrapping onto a new line when the parent width is decreased

https://i.sstatic.net/gKdAN.png I attempted to decrease the form width by applying padding on both sides of the form using 'px-5'. However, when I implemented this change, the column holding the '#lastName' input shifted to a new line. ...

A step-by-step guide on transferring data from an HTML file to MongoDB using Python Flask

I am currently developing a web application that involves uploading multiple CSV files and transferring them to MongoDB. To build this application, I have utilized Python Flask. To test out different concepts for the application, I have created a sample f ...

Numerous Selection Boxes

I came across this code snippet on the web. How can I insert the output into an email? Usually, I follow this format: $name = $_POST['name']; $email_body = "$name"; I want to incorporate the output of this code into my $email_body variable. Is ...

JavaScript enables users to store over 5 megabytes of data on their client devices

Is there a way to store more than 5mb in the client browser? I need this functionality across various browsers including Firefox, Chrome, Internet Explorer, Safari (iOS), and Windows Phone 8 Browser. Initially, localStorage seemed like a viable option as i ...

Ways to move an element beyond specified padding using CSS

In this image below, I have added a 10px padding to the container as it is usually needed. However, for the headings (blue) I do not want any padding. Is there a way to make the heading extend over the padding? I prefer not to give individual padding to e ...

The image fails to appear

Check out my website at www.wostokhr.pl I am having trouble getting the picture "pics/hero.jpg" to show up in the "div id="hero"" section with a JS parallax function. I have double-checked the HTML and CSS validators and everything seems to be correct. An ...

Monochrome Effect Triggered by Cursor Hover

I'm attempting to use Javascript with HTML5 canvas to convert an image to greyscale, but I seem to be missing something in my code. Can anyone spot the error? I feel like I'm very close! function grayscaleConversion(str) { // Access the Canv ...

What is the best approach to updating multiple rows instead of just the first row using the update button located on both sides using PHP and AJAX?

Starting fresh, so I might sound like a beginner with this question. How can I make use of the update button to update specific rows instead of just the top row? Every time I try to update, it only works for the top row. Here's the code snippet from ...

How can I adjust two overlapping divs for proper display?

It seems like I have two divs overlapping on the same line. The layout appears fine on a PC but not on an iPad or mobile device. Can someone please review the code and point out any mistakes I might have made? The issue is with the content overlapping the ...

Struggling to eliminate the scrollbar on a Material UI Dialog

I have a modal window that includes a keyboard, but I'm encountering some issues. Despite adding overflow:'hidden' as inline CSS, the scrollbar refuses to disappear. Furthermore, even when utilizing container-full padding-0 in Bootstrap, th ...

Trouble arises when trying to link IE7 with jQuery

I am currently testing a website with jQuery. There is a plugin that, when hovered over, slides down to reveal another banner, subsequently sliding the banner below it down as well. I have added a link on the bottom banner using z-index, and although it wo ...

Changing the background image within a CSS class on an imported Vue component

Is there a way to dynamically change the background-image on a CSS class within an imported component? I have successfully installed 'vue-range-slider' and imported RangeSlider. The setup for the range-slider is as follows: <template> ...

Making Bootstrap Carousel images slide seamlessly

Can someone help me with implementing Bootstrap carousel slides that display gif short clips when transitioning to the next page? I've searched through Bootstrap documentation and Stack Overflow for solutions, but I can't seem to get the slide to ...

Opacity transition in CSS does not function properly on a sibling selector when hovering over an element

I have created a responsive menu where the list items after the 4th element are set to display:none; opacity:0 on mobile devices. The 4th element is an icon and when you hover over it, the hidden menu items appear as a block list below using li:nth-child( ...

What causes the discrepancy in smoothness between the JavaScript animation when offline versus its choppiness when online, particularly on AWS

Recently I delved into game development using HTML5/CSS/JS and embarked on a small project. Check out the game here at this AWS storage link: If you open the game and press SPACE, you'll notice that the ball starts moving with occasional brief pauses ...

The appearance of the webkit-scrollbar is not reflecting the intended style

I have successfully created a wrapper for a styled scrollbar in React JS - import styled from '@emotion/styled'; export const ScrollbarWrapper = styled.div(() => ({ maxHeight: '65vh', overflowY: 'auto', '*::-web ...

Tips on changing the outline color by clicking

I'm working on a simple code where I need to change the outline color when a user clicks on a text field. <input type="text" id="box1" /> <input type="password" id="box2" /> <input type="email" id="box3" /> <input type="submit" ...

Is there a way to access a large HTML file?

Currently, I have been utilizing a Python script to compare data and the resulting differences are saved in an HTML file. However, the size of the file has ballooned to 158 MB due to significant differences, making it impossible for me to open in any brows ...

The oval shape of a Canvas circle in HTML5 is due to its width and height properties

var qcanvas = $('#canvas'); var canvas = ctl_canvas[0]; var context = canvas.getContext('2d'); qcanvas.css('border', '1px solid black'); qcanvas.css('width', 400); qcanvas.css('height', 75); Afte ...

Align the text at the center within a relative div without specifying its size

In the structure of my HTML, I have the following simplified layout: <table> <tr> <td> <div class="PromptContainer"> <span class="Prompt">Prompt text</span> </div> <input type="t ...