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

Issue with the navbar toggler not displaying the list items

When the screen is minimized, the toggle button appears. However, clicking it does not reveal the contents of the navbar on a small screen. I have tried loading jQuery before the bootstrap JS file as suggested by many, but it still doesn't work. Can s ...

The Bootstrap modal fails to open

I am currently working on implementing a Navbar button that triggers a Bootstrap modal containing a form. While looking for resources, I stumbled upon a useful script at https://gist.github.com/havvg/3226804. I'm in the process of customizing it to su ...

Change the appearance of text with a button click using JavaScript

Currently mastering JavaScript but encountering an issue. How do I change the text to underline when clicking on "Click Me"? <p id="demo" style=" text-decoration:none; ">Hello JavaScript!</p> <button type="button" onclick="document.getE ...

Creating two columns using React and Material-UI Grid while utilizing just one map function for iteration

I am facing a challenge with Material-UI Grid. I want to display the information from my map function in two columns instead of one. I tried adding another Grid item sm={6} and using the same map function, which resulted in two columns but with identical i ...

Having difficulty reaching a div element using either its id or class in a CSS stylesheet

I'm having trouble adding padding to the home section div and I can't seem to access it using either the id or class. Any suggestions on how to solve this issue would be greatly appreciated. Below is the CSS code: body { margin: 0 ; font ...

Accessing the current window dimensions using CSS

I'm experimenting with a way to set the background of the entire body element. <html> <head><head/> <body> <div class="wrapper"></div> </body> </html> How can I determine the height and width of the cu ...

Counting Numbers with jQuery Animation from Zero to Percentage Value

I am currently working on a project where I aim to incorporate a jQuery Animated Number Counter, starting from zero and ending at a specified percentage value. The values of the counter are dynamically retrieved from a database. function timer() { if ...

Having issues with my toggler functionality. I attempted to place the JavaScript CDN both at the top and bottom of the code, but unfortunately, it is still not

I recently attempted to place the JavaScript CDN at the top of my code, but unfortunately, it did not have the desired effect. My intention was to make the navigation bar on my website responsive and I utilized a toggler in the process. While the navbar di ...

I am looking to use flexbox and Vue.js to organize my sent messages in blue on the right and received messages in yellow on the left. How can I achieve this grouping effectively?

I've successfully built a messenger system using Vue.js with a Laravel backend. My goal is to display messages from myself (Jim, the logged in user) on the right side in blue and messages from Debbie (the recipient) on the left side in yellow, similar ...

Stylish Responsive Design with HTML and CSS Centering

I am tasked with creating a landing page that features an image at the top with text, followed by a couple of videos and a 2-pixel strip line image that must stay at the bottom. <html> ... <body> <img src="topimage.png" alt="" /> <t ...

How to Use CSS to Copy a Style Path and Conceal an Element within an iFrame

My goal is to adjust a division by including a width element in its CSS properties. I have attempted to use Chrome and Firebug to copy the CSS path, but it seems that the result is incorrect. After inserting it into my style.css file, the division remains ...

Employing IF conditions within a form to refine options

I am looking to streamline the options in my form based on the car and transmission selected. I have set up the form, but I am struggling with the JavaScript code. I need help figuring out how to only display certain color options when Car 1 is chosen alon ...

Is there a way to verify the file extension in a multi-input form during an upload process?

I am looking for a solution that requests users to upload 4 different files when filling out the form. Check out this example of one of the inputs: <div class="custom-file"> <input type="file" class="custom-file-input" accept="video/*" id="v ...

Jquery cascading menu

I'm currently experiencing difficulties in creating dropdown menus using jquery and css. Below is my HTML code: <nav class="topNav"> <ul> <li> <a href="#menu" class="menu-toggle"><img src ...

Creating an accessible order for the next and back buttons

Currently, I am working on designing the user interface for a checkout flow. Towards the end of the page, there are 3 buttons available - Next, Back, and Cancel. When viewed on a desktop, the button layout appears as follows: (Back) (Next) Cancel http ...

Creating a dynamic web application using Node.js, HTML, and MySQL arrays

After querying my database from MySQL Workbench, I need to convert the retrieved data into HTML code. connection.query( 'SELECT dealer_infor.dealer_id, dealer_infor.dealer_branch, dealer_infor.dealer_address, dealer_infor.dealer_tel, dealer_infor. ...

How to ensure the footer stays at the bottom of the page even when the Treeview control expands

Greetings Everyone! I'm facing an issue with keeping the footer pinned at the bottom of the page. The treeview control I'm using overlaps the footer when expanded. Can anyone assist in ensuring the footer remains at the bottom of the page? < ...

When resizing an anchor tag with a percentage in CSS, the child image width may not scale accordingly

In short, I have multiple draggable images on a map enclosed in anchor tags (<a><img></a>) to enable keyboard dragging. The original image sizes vary, but they are all too large, so I reduced them to 20% of their original sizes using the ...

Show the initially chosen option in a dropdown menu when updating a record using mysqli

I recently came across a PHP page that handles relocation requests and allows users to edit them as needed. While the add page features dropdown boxes, the edit page simply displays them as text fields. This makes it difficult for users to select a differ ...

What steps should be followed to effectively incorporate Google Fonts into a Material UI custom theme for typography in a React/TypeScript project

Hey there, I'm currently working on incorporating Google fonts into a custom Material UI theme as the global font. However, I'm facing an issue where the font-weight setting is not being applied. It seems to only display the normal weight of 400. ...