Tips for allowing a position:absolute <div> to expand as though it were containing a lengthy <p>innerText

This div automatically adjusts its width to fit the content inside, even if it extends beyond the page boundaries. Is there a way to make this div expand to fill the entire width of the page without needing a paragraph?

<div style="position:absolute;left:256px;background-color:blue;">
  <p>bla bla bla bla bla bla bla bla bla</p>
</div>

To see an example in action, check out this js fiddle: https://jsfiddle.net/qbu0u1s6/2

Answer №1

To achieve a responsive design, consider adjusting the width to 100% using calc:

<div style='width:calc(100% - 100px)'><p>Lorem ipsum</p></div>

https://jsfiddle.net/abc123/

Answer №2

<div style="position:absolute;left:256px;background-color:blue;">
  <p>
    bla bla bla bla bla bla bla bla bla 
  </p>
</div>

<div style="position:absolute;top:100px;left:256px;background-color:blue;word-wrap: wrap-word;">
  <p>
    bla 0 bla 1 bla 2 bla 3 bla 4 bla 5 bla 6 bla 7 bla 8 bla 9 
    bla 0 bla 1 bla 2 bla 3 bla 4 bla 5 bla 6 bla 7 bla 8 bla 9 
    bla 0 bla 1 bla 2 bla 3 bla 4 bla 5 bla 6 bla 7 bla 8 bla 9 
  </p>
</div>

<div style="position:absolute;top:200px;text-align: center;background-color:blue;width:100%;word-wrap: wrap-word;">
  <p style="width:100%">
    bla 0 bla 1 bla 2 bla 3 bla 4 bla 5 bla 6 bla 7 bla 8 bla 9 
    bla 0 bla 1 bla 2 bla 3 bla 4 bla 5 bla 6 bla 7 bla 8 bla 9 
    bla 0 bla 1 bla 2 bla 3 bla 4 bla 5 bla 6 bla 7 bla 8 bla 9 
  </p>
</div>

The issue arose when setting the width to 100% and left to 256px, causing the div to take up 100% of the screen width starting from 256px from the left.

To resolve this, simply update left: 256px;

For center alignment, use text-align: center;

<div style="position:absolute;left:256px;background-color:blue;">
  <p>
    bla bla bla bla bla bla bla bla bla 
  </p>
</div>

<div style="position:absolute;top:100px;left:256px;background-color:blue;word-wrap: wrap-word;">
  <p>
    bla 0 bla 1 bla 2 bla 3 bla 4 bla 5 bla 6 bla 7 bla 8 bla 9 
    bla 0 bla 1 bla 2 bla 3 bla 4 bla 5 bla 6 bla 7 bla 8 bla 9 
    bla 0 bla 1 bla 2 bla 3 bla 4 bla 5 bla 6 bla 7 bla 8 bla 9 
  </p>
</div>

<div style="position:absolute;top:200px;background-color:blue;left: 256px; width:calc(100%-256px);word-wrap: wrap-word;">
  <p style="width:100%">
    bla 0 bla 1 bla 2 bla 3 bla 4 bla 5 bla 6 bla 7 bla 8 bla 9 
    bla 0 bla 1 bla 2 bla 3 bla 4 bla 5 bla 6 bla 7 bla 8 bla 9 
    bla 0 bla 1 bla 2 bla 3 bla 4 bla 5 bla 6 bla 7 bla 8 bla 9 
  </p>
</div>

If you still prefer using left: 256px, utilize the calc() function in CSS

This will subtract 256px from the 100% width, providing the div with the width desired

If central text alignment is needed within the below div, add text-align: center to the <p> tag.

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

Applying CSS dynamically to a mat-cell in Angular 6

In my material table, I need to apply specific CSS classes based on the content of the cell. These are the CSS classes that I am using .status-code{ flex: 0 0 10% !important; width: 10% !important; } .status-code-success{ flex: 0 0 10% !impo ...

Is it possible to utilize JavaScript to access a .php file located in a separate directory?

Although I have been searching all day, I couldn't find a workout that specifically addresses my issue. I'm new to JavaScript and AJAX. Currently, I am exploring a web app called calendar.php located in the directory C:\xampp\htdocs&bs ...

How do I make a div's height equal to 100% of the height of its parent

I am trying to make a button stick to the bottom of a card no matter how much content is on it. I used the Bootstrap class mt-auto, which worked well. However, when I set the height of all divs to 100%, the row in the card body overflows from the lg breakp ...

Stop the page from scrolling when scrolling within a specific DIV to address the [Violation] warning appearing in the console

Initially, it may seem like a duplicate question that has been answered here, but there are additional aspects that need to be addressed. How do I resolve the following [Violation] warning in the Google Chrome console? [Violation] Added non-passive eve ...

Tips for ensuring an animation is triggered only after Angular has fully initialized

Within this demonstration, the use of the dashOffset property initiates the animation for the dash-offset. For instance, upon entering a new percentage in the input field, the animation is activated. The code responsible for updating the dashOffset state ...

Options for regular expressions in CSS: match letters regardless of case and search for all occurrences

Here is the HTML I am working with: <div> <img class="hi" src="http://i.imgur.com/f9WGFLj.png"></img> <img class="HI" src="http://i.imgur.com/f9WGFLj.png"></img> </div> Additionally, I have the following CSS co ...

Enhancing your design with animated borders on hover

This unique border animation caught my eye and sparked my curiosity. I'm interested to know if it can be achieved using only HTML5/CSS for a hover effect. Have you discovered any other visually appealing hover animations worth mentioning? ...

"Revamp the appearance of the div element upon clicking elsewhere on the

function replace( hide, show ) { document.getElementById(hide).style.display="none"; document.getElementById(show).style.display="flex"; } @import url('https://fonts.googleapis.com/css2?family=Allerta+Stenci ...

Tips for creating animated images with a mask or clip using HTML, CSS, and jQuery

I am attempting to animate the movement of an image under a stationary mask. I have 2 methods for achieving this: one using the mask property and the other using clip First method using Mask : View working script at http://jsfiddle.net/2Aecz/ or below & ...

Displaying text on hover and showing a text box when a link is clicked using CSS and JavaScript

Hovering over the text will display "Edit," and clicking on it will reveal a text box. This function will be used to update content using ajax. HTML: <table> <thead> <tr> <th> Name </th> <th> Age </th> ...

Trouble arises with the jQuery keydown function

Currently, I am encountering an issue with a straightforward jQuery code implementation. In addition to the problem description, I have included the HTML structure below, The current functionality works as expected in setting focus on the input field upo ...

The express-handlebars module is having trouble parsing HTML within the main layout file in an Express.js application

Hello, I am facing an issue with handlebars as it is not reading the HTML in my file. Here is a screenshot highlighting the problem along with the provided code. The folder structure is as follows: views layouts main-layout.hbs home.hbs https://i ...

How can I synchronize two webkit-animations at the same speed but with one covering a greater distance?

[Update: The solution involved creating two containers, with the second animation container configured to have left: 100%.] I designed a simple animation that moves a large gif image across the page horizontally, where the width of the gif is 1536px. Sin ...

Is it possible to customize the width of text color alongside a progress bar?

My Bootstrap 4 Website contains the following HTML code snippet: <div class="container"> <div class="row"> <div class="col-md-6 mx-auto> <h2>Example heading text</h2> <h6>Example subh ...

Align the label vertically with the corresponding input field

I am currently in the process of revamping an old HTML form to eliminate the use of tables. I found a solution on this site to give the label a fixed width, and here is how it looks in the current code: HTML <div id="form"> <label for="field ...

Dynamic scaling and positioning of multiple images in relation to each other using CSS

I am currently working on layering images over a large logo and making sure it remains responsive. I want the overlaid images to maintain their position on the logo even when the browser window size is reduced. Here is what my current browser display look ...

Is there a way to automatically populate my second dynamic dropdown list on page load?

I am currently working on a dynamic drop down menu that consists of two lists. When the page initially loads, the second drop down list is empty. However, I would like both lists to be displayed by default. You can see an example of the issue I'm faci ...

What is the process for applying a class in jQuery to empty HTML elements?

Working on a WordPress/PHP website and looking to dynamically add a class when child elements are empty? This is the HTML structure: <div class="featured-block"> <a href="/" class="featured-block__item cf"> <div class="featured-bl ...

Problem arising from animation not commencing at expected starting point

Creating a feature on an app that involves breathing exercises using CSS animations. The challenge I'm facing is ensuring the words "exhale" and "inhale" appear synchronously with the animation of a circle shrinking and enlarging. However, the animati ...

The new Facebook login button in my app seems to be glitchy as it fails to redirect users to the appropriate page after

I am working on a web application and have successfully integrated Facebook login from developers.facebook.com. However, I am facing an issue where after the user logs in with their Facebook credentials, they are not redirected to my application. Additiona ...