Adjusts width based on the child element's dimensions and content height

I am trying to create a unique angled text flow where the width of each line is a set value --w. Below is the code I have so far:

* { margin: 0 }

html, body { display: grid }

body {
  --w: min(35em, 65vw);
  --f: 1/ 8;
  background: linear-gradient(90deg, transparent var(--w), pink 0);
  font: 1em ubuntu, sans-serif
}

.outer-wrapper {
  width: min-content;
  box-shadow: 0 0 0 2px red;
  background: lemonchiffon
}

span::before, span::after {
  --i: 0;
  --j: calc(1 - var(--i));
  --g: 
    linear-gradient(to right bottom, 
        hsla(50, 50%, 50%, var(--j)) 50%, 
        hsla(50, 50%, 50%, var(--i)) 0);
  float: left;
  height: 100%;
  aspect-ratio: var(--f);
  background: var(--g);
  shape-outside: var(--g);
  content: ''
}
span::after {
  --i: 1;
  float: right
}

.inner-wrapper {
  width: var(--w);
  box-shadow: inset 0 0 0 2px blue
}
<div class="outer-wrapper">
  <span aria-hidden="true"></span>
  <div class="inner-wrapper">
    <p>Gingerbread macaroon cake topping wafer cake dessert. Lemon drops cheesecake lemon drops lollipop biscuit tart. Chupa chups bonbon dragée tart pie. Oat cake cupcake bear claw pie ice cream ice cream. Jelly beans ice cream icing halvah cotton candy. Tiramisu dragée macaroon chocolate bar chocolate cake. Sweet roll wafer candy gummies donut wafer liquorice sugar plum. Icing gummies chocolate cake gummies caramels sweet roll. Tootsie roll jelly beans jelly chocolate jelly beans pie. Halvah lollipop lemon drops shortbread bonbon. Jelly biscuit bear claw cotton candy cotton candy chocolate bar soufflé donut lollipop. Fruitcake lemon drops marshmallow sugar plum cheesecake gingerbread. Sugar plum chupa chups...
    <p>Gingerbread macaroon cake topping wafer cake dessert. Lemon drops cheesecake lemon drops lollipop biscuit tart. Chupa chups bonbon dragée tart pie. Oat cake cupcake bear claw pie ice cream ice cream. Jelly beans ice cream icing halvah cotton candy. Tiramisu dragée macaroon chocolate bar chocolate cake. Sweet roll wafer candy gummies donut wafer liquorice sugar plum. Icing gummies chocolate cake gummies caramels sweet roll. Tootsie roll jelly beans jelly chocolate jelly beans pie. Halvah lollipop lemon drops shortbread bonbon. Jelly biscuit bear claw cotton candy cotton candy chocolate bar soufflé donut lollipop. Fruitcake lemon drops marshmallow sugar plum cheesecake gingerbread. Sugar plum chupa c...
    <p>Gingerbread macaroon cake topping wafer cake dessert. Lemon drops cheesecake lemon drops lollipop biscuit tart. Chupa chups bonbon dragée tart pie. Oat cake cupcake bear claw pie ice cream ice cream. Jelly beans ice cream icing halvah cotton candy. Tiramisu dragée macaroon chocolate bar chocolate cake. Sweet roll wafer candy gummies donut wafer liquorice sugar plum. Icing gummies chocolate cake gummies caramels sweet roll. Tootsie roll jelly beans jelly chocolate jelly beans pie. Halvah lollipop lemon drops shortbread bonbon. Jelly biscuit bear claw cotton candy cotton candy chocolate bar soufflé donut lollipop. Fruitcake lemon drops marshmallow sugar plum cheesecak...
  </div>
</div>

The issue I am facing is that I want the floated pseudo-elements on the sides to contribute to the --w width and expand the .outer-wrapper by one unit, as otherwise, the text overflows outside the red outlined box.

https://i.sstatic.net/anNXy.png

I am unsure how to achieve this using CSS alone. I know it can be done with JavaScript by detecting the pixel-valued height, storing it in a variable, updating it on resize, and recalculating from there. However, I would prefer a CSS-only solution.

Skewing the box is not a viable option in this scenario.

Answer №1

While it comes with its own limitations and may not be a perfect fit for your specific scenario, one approach you could consider is utilizing a hidden duplicate of the content in a narrower container to determine the necessary height. Then, applying height: 100% to give .outer-wrapper the required height. Below is an example of how you can achieve this:

* { margin: 0 }

html, body { display: grid }

body {
  --w: min(35em, 65vw);
  --f: 1/ 8;
  background: linear-gradient(90deg, transparent var(--w), pink 0);
  font: 1em ubuntu, sans-serif
}

.outer-outer-wrapper {
  height: auto;
  display: flex;
}

.height-setter {
  background: lightgreen;
  width: calc(var(--w) * (1 - var(--f)));
  visibility: hidden;
}

.outer-wrapper {
  width: min-content;
  box-shadow: 0 0 0 2px red;
  background: lemonchiffon;
  height: 100%;
}

span::before, span::after {
  --i: 0;
  --j: calc(1 - var(--i));
  --g: 
    linear-gradient(to right bottom, 
        hsla(50, 50%, 50%, var(--j)) 50%, 
        hsla(50, 50%, 50%, var(--i)) 0);
  float: left;
  height: 100%;
  aspect-ratio: var(--f);
  background: var(--g);
  shape-outside: var(--g);
  content: ''
}
span::after {
  --i: 1;
  float: right
}

.inner-wrapper {
  width: var(--w);
  box-shadow: inset 0 0 0 2px blue;
}
<div class="outer-outer-wrapper">
<div class="outer-wrapper">
  <span aria-hidden="true"></span>
  <div class="inner-wrapper">
    <p>Your custom content here...</p>
    <p>More content goes here...</p>
    <p>And even more content...</p>
  </div>
</div>
  
  <div class="height-setter">
    <p>Duplicated content for determining height...</p>
    <p>Additional duplicated content...</p>
    <p>More duplicated content...</p>
  </div>
</div>

Some potential issues to keep in mind:

  • The calculated height may not always be exact due to line break discrepancies. To mitigate this, you can add extra lines with padding at the bottom of .height-setter for better coverage.
  • Duplicate content exists within the code, which might not pose an issue for screen reader users as long as visibility: hidden is implemented correctly.
  • If .outer-outer-wrapper exceeds the width of its contents, additional adjustments like flex cropping or using an .outer-outer-outer-wrapper with overflow: hidden might be necessary.

Answer №2

Using Pure CSS Only

Note: Remember to duplicate your content in the HTML code as shown below

If you utilize a dynamic language to print your content, this process becomes simple.

This method is effective across all screen sizes, regardless of content size or padding. Additionally, feel free to adjust the values of --f and --w, everything will still function smoothly!

* {
        margin: 0;
    }

    html, body { display: grid }

    body {
        --w: min(35em, 65vw);
        --f: 15%;
        background: linear-gradient(90deg, transparent var(--w), pink 0);
        font: 1em ubuntu, sans-serif
    }

    .outer-wrapper {
        width: var(--w);
        box-shadow: 0 0 0 2px red;
        background: lemonchiffon;
        height: 100%;
        position: relative;
    }

    span::before, span::after {
        --i: 0;
        --j: calc(1 - var(--i));
        --g:
                linear-gradient(to right bottom,
                hsla(50, 50%, 50%, var(--j)) 50%,
                hsla(50, 50%, 50%, var(--i)) 0);
        float: left;
        height: 100%;
        width: var(--f);
        background: var(--g);
        shape-outside: var(--g);
        content: ''
    }
    span::after {
        --i: 1;
        float: right
    }

    .inner-wrapper {
        box-shadow: inset 0 0 0 2px blue
    }

    .inner-wrapper-hidden {
        width: calc(100% - var(--f));
        visibility: hidden;
        pointer-events: none;
    }

    .inner-wrapper-absolute {
        position: absolute;
        height: 100%;
        width: 100%;
        top: 0;
        left: 0;
    }
<div class="outer-wrapper">
<div class="inner-wrapper-absolute">
    <span aria-hidden="true"></span>
    <div class="inner-wrapper">
        <p>Your duplicated content here...</p>
        <p>More duplicated content...</p>
        <p>Additional duplicated content...</p>
    </div>
</div>
<div class="inner-wrapper-hidden">
    <p>Duplicated content goes here...</p>
    <p>More duplicated content...</p>
    <p>Additional duplicated content...</p>
</div>
</div>

Answer №3

To tackle this issue using only CSS, we can utilize mathematical calculations to determine the height of the .outer-wrapper. By solving a basic equation, we can find the new height based on the previous height, width, and aspect ratio.

The key point to consider is that the area occupied by the text remains constant in both scenarios – represented by the shaded regions. Therefore, adjusting the height of the outer-wrapper proportionally will maintain this equilibrium.

https://i.sstatic.net/JyBct.png

https://i.sstatic.net/2TSgQ.png

I encountered challenges setting the height attribute as specified for outer-wrapper, due to difficulty with the syntax. Any guidance or assistance provided to resolve this issue would be greatly appreciated.

.outer-wrapper {
    width: min-content;
    box-shadow: 0 0 0 2px red;
    background: lemonchiffon;
    height: calc((var(--w) + sqrt(var(--w) * var(--w) - 4 * var(--f) * var(--w)))/(2 * var(--f)));
}

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

Steps for adjusting menu link color after being clicked

I am attempting to modify the background color of visited links. Below is my header code: echo ('<ul>'); echo('<li><a href="'.$FromPage.'">Back</a></li>'); echo('<li><a href="Tal ...

Upon migrating from Vue CLI 2 to 3, an error is thrown stating: "Cannot locate element: #app" and an empty body is

Currently, I am in the process of transitioning my VueJS project from VueCLI 2 to version 3. After moving all the necessary files to the src folder, I attempted to view it in the browser by running npm run serve. However, I encountered a problem where the ...

Displaying cards horizontally on desktop and vertically on mobile using CSS

Context and Issue I am currently working on this specific page, with a focus on the "Top Artists" section. Desired Outcome: When viewed from a laptop, I aim for the cards to be displayed as vertical rectangles, where the ranking is at the top, followe ...

When using a Kendo Grid with a Checkbox as a column, the focus automatically shifts to the first

Whenever I select a checkbox in my Kendo grid, the focus automatically shifts to the first cell of the first row. How can I prevent this from happening? Here is the code I am currently using when a checkbox is checked: $('#approvaltranslistview' ...

What is the best way to make hover text that also copies when you highlight the main text?

Currently, I am utilizing a basic bootstrap tooltip for creating tables with jQuery. However, I require a specific functionality that the standard bootstrap tooltip does not provide. I need the ability to copy the base text and have it appear as "base tex ...

Utilizing Sessions Across Several PHP Forms

Hey there! I've got a query for you. When you kick off your PHP code with session_start(), do you need to keep adding the prior variables on subsequent form pages? I'm contemplating using the session() function for my GYM Sign UP Webpage. The f ...

Set the userID variable as the assigned value and the username variable as the ng-model attribute for a select

Seeking assistance with a coding dilemma. Currently working on an ASP.Net MVC and AngularJS application, where I am utilizing the select tag to display a list of users. Both the select tag and a div are connected via the same ng-model for real-time binding ...

What are the implications of using :root along with the universal selector in CSS to overwrite Bootstrap variables?

As I work on theming my website, which utilizes Bootstrap, I am making adjustments to the Bootstrap variables. Some variables are defined against the :root element in a way that allows me to easily override them: :root { --bs-body-color: red; } Howeve ...

Utilizing Rvest for extracting data from LinkedIn profiles

I've been trying to scrape my LinkedIn profile using Rvest, but I encountered a problem in the experience section. The XPath below was supposed to retrieve the experience section, but it's returning 0 nodes: Test <- read %>% html_nodes(xpa ...

Looking for assistance with implementing mouseover scrolling on an unordered list

I am attempting to create a scrolling effect on my UL element when the mouse hovers over it, but I am having trouble getting it to work. Here is the link to my fiddle: http://jsfiddle.net/MisterStorm/4ja9apqz/3/ and here is my code: $(document).ready( ...

Finding the image source of a clicked image

Is it possible to retrieve the image src after clicking on another image? $(document).ready(function() { $('img.getimage').click(function() { alert($(this).attr('src')); }); }); .sinistra { width: 150px; } .getimage { wi ...

The highcharts datetime format is not functioning properly within a Bootstrap 4 column

I am using highcharts to display a datetime chart within a Bootstrap 4 column like the example below: HTML: <section> <div class="container"> <div class="row"> <div class="col-md-8"> ...

Struggling with creating a slideshow - is the display set to none?

I've been working on a jQuery slideshow project and have encountered an issue where a hidden element is not showing up as expected. Below is the code I'm using: <section id="content"> <div id="jumbotron"> <div class="s ...

The request for http://localhost:3000/insert.js was terminated due to a 404 (Not Found) error

As someone new to web development, I am currently tackling a project where I'm having trouble loading the Javascript file insert.js. The HTML document upload.html resides in the public folder, while the Javascript file is located in the main folder. I ...

Display a pop-up window after a set amount of time and automatically close it when clicking on an

I am looking to implement a functionality where my popup box opens automatically after a specific time interval (e.g. 5000 milliseconds) and can be closed by clicking on an anchor tag placed inside the popup box. function openPopup() { $('.popup- ...

Creating an animated Gif using HTML and CSS styling

I am trying to create an effect where a .gif animation plays once when the mouse hovers over a specific image. I have one static image in PNG format and one animated gif. The goal is for the gif to play every time the mouse is hovered over the png image. ...

Display Listings Shortcode with Responsive CSS styling

I recently added a plug-in called Display-listings-shortcode and incorporated the columns-extension feature on my website, RitaNaomi.com. This allows for blog posts to be displayed in columns halfway down the homepage when viewed on a web browser. Initiall ...

What are the steps for adding a photo to the folder directory on my hosting server?

Hey there! I've been having some trouble uploading a photo from my website to the hosting server's directory (/public_html/upload/) using php. No matter what I try, the folder remains empty. I'm stuck and not sure where I'm going wrong. ...

The efficiency of a website that handles numerous files

I'm currently in the process of creating a website that allows users to order a customizable product. The ordering process involves 10 steps, each with various options to choose from. Right now, all the steps are handled in a single file (index.php) ...

The offset values of $(element) keep increasing indefinitely when they are updated repeatedly

After researching how to utilize JavaScript drag functionality to move elements, the goal is to dynamically adjust the cursor position within a square when dragged. In an attempt to simplify the process and avoid storing x and y offsets as separate variabl ...