Finding the best way to transfer text between DIV elements?

I have a dilemma involving two DIV elements positioned absolutely on the sides of an HTML page, much like this EXAMPLE:

<div class="left">
</div>
<div class="right">
</div>

These are styled using the following CSS:

.left{
    position:absolute;
    left:10px;
    top:10px;
    width:100px;
    height:100px;
    background:red;
}
.right{
    position:absolute;
    right:10px;
    top:10px;
    width:100px;
    height:100px;
    background:blue;
}

My question is, is there a way to insert text into the left DIV and allow any excess text to flow into the right one? I am open to alternative solutions that achieve the same result, regardless of whether they involve these specific DIV elements.

NOTE: While I would prefer a pure CSS solution, it appears unlikely. Therefore, I am seeking a pure JavaScript solution without relying on libraries or frameworks.

Answer №1

CSS Regions (still in the 'draft' phase) aims to address this issue:

The CSS regions module enables content to flow through multiple areas known as regions. These regions do not have to be adjacent in the document order. The CSS regions module offers a sophisticated content flow mechanism, which can work together with positioning techniques specified by other CSS modules like the Multi-Column Module [CSS3COL] or the Grid Layout Module [CSS3-GRID-LAYOUT] for placing the regions where content flows.

For more information and tutorials, visit https://www.adobe.com/devnet/archive/html5/articles/css3-regions.html

Answer №2

Here's a solution using the fixed-width method. The space between the two columns will be equal to the width of the main div.

Check out the Fiddle

<div class="container">
    <div class="sides">Insert your text here.<div>
    <div class="main"></div>
</div>

If you prefer variable width, you'll need to use JS or jQuery.

Update:

In this case, I utilized jQuery as I found it easier than using pure JS to solve this issue.

function setGap() {
    var width = $(".main").width();
    $(".sides").css({
        "-moz-column-gap": width + "px",
            "-webkit-column-gap": width + "px",
            "column-gap": width + "px"
    });
}
$(window).resize(setGap);
setGap();

Updated Fiddle Link

Update 1:

function setGap() {
    var width = document.getElementsByClassName("main")[0].offsetWidth;
    var elem = document.getElementsByClassName("sides")[0];
    var style = elem.getAttribute("style");
    if (typeof style != "null") {
        style =
            "-moz-column-gap:" + width + "px; -webkit-column-gap:" + width + "px; column-gap:" + width + "px";
        elem.setAttribute("style", style);
    }
    else {
        style +=
            "-moz-column-gap:" + width + "px; -webkit-column-gap:" + width + "px; column-gap:" + width + "px";
        elem.setAttribute("style", style);
    }
}
window.onresize = setGap;
setGap();

Another Updated Fiddle Link

Answer №3

Up until now, in the year 2012, it has been impossible to achieve this effect using only CSS or CSS3 (without resorting to 2 separate elements).

However, with the help of JavaScript, you can duplicate the content and apply the scrollTop function on the appropriate element:

SEE LIVE DEMO

var d = document,
    $left  = d.getElementById('left'),
    $right = d.getElementById('right'),
    leftH  = $left.offsetHeight;

$right.innerHTML = $left.innerHTML +'<p style="height:'+ leftH +'px;" />';
$right.scrollTop = leftH;

In order to make this work effectively, an empty paragraph is also being added to adjust the scrolling position of the right element.

Note: Remember to include overflow:hidden; in your ID elements #left and #right

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

Issues with the functionality of the shopping cart are causing a disruption

This is the coding for the online shopping cart: <!DOCTYPE html> <html lang="en-us"> <head> <meta charset="UTF-8" /> <title>Online Shopping Cart</title> <script src="jquery-3.1.1.min.js"></script> ...

Are there any distinctions between these two compact React components?

Let's compare these two components: function App1 () { return <button onClick={() => null}>Click me</button> } function App2 () { const fn = () => null; return <button onClick={fn}>Click me</button> } The on ...

When defining a class property in TypeScript, you can make it optional by not providing

Is there a way to make a property on a Class optional without it being undefined? In the following example, note that the Class constructor takes a type of itself (this is intentional) class Test { foo: number; bar: string; baz?: string; construc ...

Incorporate a jQuery userscript on Firefox that includes a link to a form

Attempting to incorporate a search link into an online form using a userscript with jQuery, specifically for Firefox. I typically work in Chrome, so I'm encountering some challenges with compatibility in Firefox. However, I need this to be functional ...

Writing and altering content within the <code> element in Chrome is unreliable

Utilizing a WYSIWYG Editor with contenteditable functionality allows users to input "code snippets" using a <code> element. Here is an example: <div contenteditable="true"> <p> This is a paragraph with an <code>inline s ...

What could be causing my jQuery animation to lack smoothness?

I am experiencing an issue where, after a delay, my element completely jumps across the screen to its new position when I animate its height. Strangely, this problem only occurs when using pixel values and not percentages. Unfortunately, I need to use pi ...

"Steady layout of grid for the navigation bar and

Currently, I am in the process of developing a control panel with the use of HTML and CSS. To structure the page, I opted for a grid layout. However, I encountered an issue where the navbar and sidebar do not stay fixed on the screen despite trying various ...

Using the class for jQuery validation as opposed to the name attribute

I am looking to implement form validation using the jquery validate plugin, but I am facing an issue with using the 'name' attribute in the html since it is also used by the server application. Specifically, I want to restrict the number of check ...

What is the best way to transmit extra data when tunneling a TLS connection?

I have developed a basic HTTP proxy that utilizes the HTTP CONNECT method for HTTP tunneling. const http = require('http'); const https = require('https'); const pem = require('pem'); const net = require('net'); con ...

Creating a Dual Linear Gradient Background with Tailwind: Step-by-Step Guide

Can you use two linear backgrounds on a single element in Tailwind CSS? I found this CSS code that seems to achieve it: background-image: linear-gradient(to right, #f8fafb 50%, #161616 50%), linear-gradient(to right, #161616 50%, #f8fafb 50%); I att ...

Angular2 - Incorporating a New Attribute

I am working with the following Angular2 code: <ngx-datatable-column prop="id" name="ID"> <template ngx-datatable-cell-template let-row="row" let-value="value"> <a [routerLink]="['/devicedtls',r ...

Enhancing TextFields with React Material Margins

I am facing a simple issue where I attempted to add marginButtom to the TextFields but for some reason, it is not applying any margin. Please take a look at this codesandbox link CLICK HERE CODE <label>Schedule</label> <Te ...

Incorporate JavaScript to include a new class and attribute

Apologies for any language barriers.. I grasp that it involves using addClass and removeClass, but I am uncertain about how to write the terms. I need to ensure that if the screen resolution is less than 768 pixels, then the attributes id="dLabel" type="b ...

Incorporating a division using the data() method

Currently, I am in the process of generating a list of flash SWFs. The data for this list is retrieved through an ajax call, which returns a JSON object. To populate the rows with data, I utilize my makeAppRow function. makeAppRow = function(myData) { ...

Trigger a function from a Bootstrap modal

Here is a preview of my HTML code : <form action="form2_action.php" method="post"> <table> <tr> <td>First Name </td> <td>:</td> <td><input type="text" id="f ...

The disappearance of the overlay background due to modal reload in NextJS dynamic routing

I am working on a simple NextJS app where clicking on a page opens a modal with updated URL but without triggering a new navigation. The content inside the modal is displayed, while the actual page location is reflected in the URL and refresh takes the use ...

Encountering a "Token Error: Bad Request" when attempting to call the callback URL

Every time I try to invoke the callback URL with google-OAuth2, I encounter the following error: Error Traceback: TokenError: Bad Request at Strategy.OAuth2Strategy.parseErrorResponse (G:\projects\oauth\node_modules\passport-oauth ...

Store the output of a mysql query in a variable for future reference

As I work on developing a backend API for a private message system, I have encountered a challenge with one of my functions. The issue arises when I attempt to store the result of an asynchronous call in a variable for future use. Here is the code snippet ...

The 3D hover effect is malfunctioning in the Firefox browser

I've been working on a 3D hover effect and my code is functioning properly in Opera and Chrome, but for some reason it's not working in Firefox. I've even attempted using vendor prefixes, but to no avail. The strange thing is that when I rem ...

Steps for implementing a reset button in a JavaScript slot machine game

I need assistance with implementing a reset button for my slot machine game prototype written in JS. I attempted to create a playAgain function to restart the game by calling the main game function, but it's not working as expected. Do I need to pass ...