What is the best way to prevent text wrapping once the viewport border is reached?

Consider the following code snippet (feel free to click and drag outside the text area) - the text wraps when it reaches the viewport border.

let isCaptureActive = false;
let offset = {
    x: 0,
    y: 0
}
const fooDOM = document.querySelector('#foo');
const bodyDOM = document.body;
window.addEventListener('mousemove', e => {
    if(!isCaptureActive) return;

    offset.x += e.movementX;
  offset.y += e.movementY;
  
    fooDOM.style.left = offset.x + 'px';
  fooDOM.style.top = offset.y + 'px';
});
window.addEventListener('mousedown', e => {
    if(e.target !== fooDOM) isCaptureActive = true;
});
window.addEventListener('mouseup', e => {
    isCaptureActive = false;
});
#foo {
  position: absolute;
  background-color: wheat;
  min-width: 250px;
  max-width: 500px;
}
<div id="foo">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras porta nisl justo, id rutrum lorem cursus in. Nullam dictum lobortis lorem, vitae facilisis lectus. Donec ut eros lacinia, suscipit nisl ut, convallis diam.
</div>

How can we make the text not be affected by the viewport border?

UPDATE: I need the ability to specify both the min-width and max-width attributes for the element.

Answer №1

let isCaptureActive = false;
let offset = {
    x: 0,
  y: 0
}
const fooDOM = document.querySelector('#foo');
const bodyDOM = document.body;
window.addEventListener('mousemove', e => {
    if(!isCaptureActive) return;

    offset.x += e.movementX;
  offset.y += e.movementY;
  
    fooDOM.style.left = offset.x + 'px';
  fooDOM.style.top = offset.y + 'px';
});
window.addEventListener('mousedown', e => {
    if(e.target !== fooDOM) isCaptureActive = true;
});
window.addEventListener('mouseup', e => {
    isCaptureActive = false;
});
#foo {
  position: absolute;
  background-color: wheat;
  min-width: 250px;
  max-width: 500px;
}
#pg {
    width: 100vw;
}
<div id="foo">
<p id='pg'>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras porta nisl justo, id rutrum lorem cursus in. Nullam dictum lobortis lorem, vitae facilisis lectus. Donec ut eros lacinia, suscipit nisl ut, convallis diam.
</p>
</div>

I encapsulated the content within a <p> tag and adjusted its width to 100vw.

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

How can I use jQuery to create an onClick event that expands text content on the right side of a portfolio

Hi there, I'm attempting to achieve a similar effect as the hover action in my first fiddle. I'd like to use jQuery click/toggle to expand the content instead of displaying it on hover. I've been working with basic addClass using jQuery/CSS ...

The validator on the W3 Schools website flagged an error for me

Has anyone encountered this particular validation error from W3 School before? I have a basic IMG element that is throwing an unusual error on line 43, column 112. I am completely baffled. Error Line 43, Column 112: Attribute target is not allowed on the ...

How can I align icons to the right and display text below each one?

I need to display icons with their names below them in a row from left to right, with proper spacing. Each icon should have its name displayed underneath it, for example "Icon1" under icon1 and so on. The tierGo element does not contain any CSS styles. ...

How to customize the hover and active properties of a checked checkbox in Vue 3 using Tailwind CSS and PUG?

It seems that checkboxes have a level of sophistication that I never anticipated, or perhaps my brain just can't quite grasp the nuances of checkboxes. After spending a significant amount of time researching and experimenting with checkboxes in a Vue ...

interaction & portal

I am currently managing a website that includes an iframe. Both the main site and the embedded site within the iframe are verifying the $_SESSION["login"]. Therefore, if the session expires, the user will be automatically logged out. The issue I'm fa ...

The expected functionalities of jQuery animation are not being achieved

Here is my jQuery script: <script> $(document).ready(function () { $("#tt").mouseover(function () { $("#aa").animate({ padding: "5px 0 5px 100px", backgroundColor: "#7EEFF7", }); }); $("#tt").mouseout(function () { $( ...

Chrome users may experience a jumping effect when using the jQuery .animate()

When I use a jQuery .animate() function to move an absolute positioned container, Firefox, Internet Explorer, and Opera correctly move the container to the top. However, in Chrome, the container jumps to the top and then moves down 200px from the margin. ...

Set the style of the mat-select element

I'm having an issue with my select option in Angular Material. The options look fine, but when I select one, the strong tag disappears. Can anyone help me style only that part? Thank you in advance. <mat-select formControlName="projectId" ...

Using asp.net MVC along with jQuery and Razor to toggle checkbox selections within a group

I am attempting to toggle the checkboxes within a group. My goal is to click on the corresponding parent (id=50, id=51), and then have all its child checkboxes also be clicked. I have tried using the closest and find jQuery functions with some modification ...

When the radio button is selected, show a variety of buttons

I'm facing an issue with rendering different buttons for two radio buttons within a view. Here is the rendered HTML code for the radio buttons: <input checked="checked" id="Isattending_0" name="Isattending" type="radio" value="Yes" /> <inpu ...

Queueing up file downloads through a web browser

When trying to download multiple files from a server, I am required to queue them up instead of downloading in parallel. Unfortunately, I do not have access to the download server, so my only option is to work with the browser. Is there an API available t ...

Display a Bootstrap Popover upon hovering over an alert message

I have created a custom notification window using the bootstrap alert class. The string has been trimmed for display, but I want the full message to appear in a popover when the user hovers over it. Below is the code snippet used for displaying notificatio ...

Using Special Fonts in Visual Studio 2013

Encountering a small issue with Visual Studio 2013. My application, developed using html and javascript, utilizes a custom font. When attempting to run the application on a different computer that does not have the font installed, it fails to display corr ...

Adjust the height of the element to match the parent's height

I am working on a design with a structure like this: <section> <h3>Title:</h3> <p>Content that may span multiple lines.</p> </section> section { background: #5E5E5E; overflow:hidden; } h3 { backgro ...

The button functionality gets hindered within the Bootstrap well

I'm trying to figure out what's wrong with my code. Here is the code: https://jsfiddle.net/8rhscamn/ <div class="well"> <div class="row text-center"> <div class="col-sm-1">& ...

How can I access the label of an input field?

With the for attribute, we can connect a label to a control, such as an <input>, so that clicking on the label focuses on the control. That's pretty neat. Here's a question that might sound a bit silly. How about the reverse? Is there a wa ...

Switching between two states of a single 'td' element within a column

I am trying to implement a feature where only specific elements in the fourth column of a four-column table toggle when clicked. For example, clicking on an element in the fifth row third column should toggle the corresponding element in the fifth row four ...

Troubles with CSS in MUI Component Integration

How can I successfully implement a vertical timeline using Material UI in React? I've set it up, but the CSS isn't functioning as expected. Is there an error in my implementation? List of Dependencies: "@emotion/react": "^11.11.1& ...

How does the browser interpret the CSS property transform: none?

Does the transform: none result in matrix(1,0,0,1,0,0) or a different outcome? I am looking to reset the transform; what is the best approach for this? <style> .box{ height: 100px; width: 100px; background-color: red; transition: 0.5s; transform: ...

Tips for creating different CSS print layouts within a single-page application

I am working on a single page application with multiple sections that need to display a list of images on separate pages. I was considering using iframes for this task, but I'm looking for advice from someone who has experience with similar situations ...