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

Items seem to vanish into thin air and become immovable when attempting to relocate them

I am attempting to create a unique grid layout with 3x3 dimensions, where each grid item represents a fragment of a single image. These items should have the capability to be dragged and dropped inside another 3x3 grid, in any desired sequence. I have hit ...

Is it possible to execute the Go compiler within a web browser?

Just discovered that Go can be compiled to WebAssembly. How awesome! According to this Go documentation, the Go toolchain is actually written in Go itself. This got me thinking, is it possible to run the Go compiler in a browser? Can I create a website w ...

What is the best way to handle an AJAX request within an if-else statement?

Attempting to utilize an if-else condition in order to make a jQuery Ajax call to an API. Having trouble understanding why the ajax function is being called even though it should be in the else statement. Below is the code snippet: if (e.value == null | ...

Javascript problem: Trouble with event.clientX position

I found a great resource on learning javascript at this website, I'm having trouble understanding the code snippets in function XY(e, v), specifically these two lines: event.clientX + document.documentElement.scrollLeft and event.clientY + document ...

Issues with Linear-Gradient functionality in NativeScript 8 on Android devices

I recently added a linear-gradient to an image in my NativeScript 8 app. Surprisingly, it seems to work perfectly on iOS, but I'm encountering some issues on Android. Despite trying solutions like using -webkit-linear-gradient(), the desired effect is ...

Utilizing the jQuery slideToggle method on the specified target element

I'm encountering an issue with jQuery slideToggle, and here's the code I have: $(".dropdownmainmenu").click(function(){ $(this).children(".showmenu").slideToggle(); $(this).toggleClass("activemainmenu"); $(this).find(".showarrowmainmen ...

Getting rid of any excess space between columns within the same row in Bootstrap following a line

Exploring Bootstrap 3. When you test the following HTML and CSS code, everything appears as expected with no space above 768px. However, upon reaching that breakpoint, the divs start stacking on top of each other, which is fine. But suddenly, a mysterious ...

What is the best way to add a color swatch image using Javascript?

I'm facing a challenge in Shopify where I need to assign corresponding background images to color swatches. Currently, my icons are set up with the correct links for each color, but they are missing their respective images, similar to this example. I ...

Is there a way to bring in a variable from the front end script?

Is it possible to transfer an array of data from one HTML file to another? If so, how can this be done? Consider the following scenario: First HTML file <script> let tmp = <%- result %>; let a = '' for (const i in tmp){ ...

Performing condition checks within the foreach loop and displaying numerous results in a single row

I have a list of results from searching certain parameters and I need to print them out. When using a foreach loop to print the results, I want to ensure that if the purchase dates are the same, they should be printed in the same row. How can I achieve thi ...

Is there a way to enhance the appearance of a TextField in JavaFX to resemble the sleek design of Android or material UI?

Is there a way to create a window like this in JavaFX? Take a look at the textfield shown in the image below... I recently came across the Jphonex framework that allows for this type of display. However, when I package it into a Jar file and try to run it ...

Issues with the Dropdown Menu

I'm currently working on a dropdown menu and I've encountered two issues. When I hover over the links in the main navigation bar (such as 'about' and 'connect'), only the actual words are clickable, not the entire area tha ...

Positioning elements within a Bootstrap column: Fixed versus Absolute positioning

Striving to maintain the absolute positioning of the right column as users scroll through the left column has proven to be a challenging task. Despite exploring various solutions from stackoverflow and google, none seem to effectively address this issue. ...

What is the best way to adjust the CSS height of an HTML select element to 100% within a table cell in Internet Explorer

Is it possible to make a select (dropdown list) element fill out 100% of the table cell height? I was successful in getting it to work in Firefox and Chrome, but unfortunately not in IE (Internet Explorer). Here is the test HTML code: <table> & ...

What is the best way to retrieve user groups in Django?

My goal is to investigate the user groups associated with a user in Django. However, when I check the console, I encounter the message: Uncaught ReferenceError: user is not defined at 6/:643:33 The purpose of the function is to redirect the user based ...

Leveraging the power of map in an Angular typescript file

I've been attempting to populate a Map in Angular by setting values dynamically. When certain buttons are clicked, the onClick function is invoked. typeArray: Map<number,string>; Rent(movieId: number){ this.typeArray.set(movieId,"Rental ...

The troubleshooting issue with jQuery animate Scrolltop malfunctioning

My goal is to use jQuery to scroll the page to a specific div when a button is clicked. Despite not seeing any errors in the JavaScript console, the page does not actually scroll to the desired location. I have tried placing the jQuery js file before and a ...

The checkbox is displayed as selected after being clicked in conjunction with another checkbox

In the tree structure, there are checkboxes that behave strangely when clicked. I have already read a similar discussion here. The problem I am encountering is that when I click on an item, it gets checked but the console does not show it as checked immed ...

Is it possible to reorganize the order of elements in a

My goal is to create a flex column layout where the image appears first. However, I'm facing an issue with the code below, as it only changes the order when the flex direction is set to row (image on the left). It does not work for rows. </head> ...

Is there a way to change an HTML document into a Word file?

I am looking for recommendations on libraries that can help me save HTML documents in memory as Word .DOC files. Any suggestions for both closed and open source options are welcome. Additionally, could someone provide links to these libraries based on the ...