Create an angle-div element that can be expanded or collapsed without relying on

I'm currently experimenting with creating a diagonal div that adjusts its height based on the content within it. While I can extract height values using JavaScript, I am curious if achieving this effect is possible exclusively through CSS.

Thus far, I've managed to craft the desired design and appearance, but it relies on two hardcoded CSS height values -

.content-slice{ min-height: 300px;}
.slice:after {border-top: 300px solid #fff;}

Here's an image depicting my objective - https://i.sstatic.net/seX7N.png

My HTML Markup -

<div class="content-slice slice-left">
   <div class="slice-bg">
      <div class="slice">

      </div>
      <div class="container">
         <div class="row">
            <div class="col-md-6 content-hold">
              <p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
              </p>               
            </div>
            <div class="col-md-5 col-md-offset-1">

            </div>
         </div>
      </div>
   </div>
   <div class="clearfix"></div>
</div>

My CSS Styles -

.content-slice{
   min-height: 300px;
   transition: all ease 350ms;
   position: relative;
   margin: 50px 0px;
   z-index: 10;
}

.slice-bg{
  background: #fff;
  min-height: 100%;
  position: absolute;
  width: 100%;
  color: #fff;
}

.content-hold{
  color: #fff;
}

.slice{
  width: 60%;
  background-color: #373737;
  min-height: 100%;
  position: absolute;
  color: #fff;
}
.slice:after {
  content: '';
  width: 0;
  height: 0;
  background-size: cover;
  border-top: 300px solid #fff;
  border-left: 130px solid #373737;
  position: absolute;
  right: 0;
}

View my JSFiddle demonstration here - https://jsfiddle.net/09vywcuj/11/

Answer №1

You can streamline your code by using padding and background properties like this:

p {
  padding: 20px 100px 20px 20px;
  background:linear-gradient(to top right,transparent 50%,white 51%) 100% 0/80px 100% no-repeat;
  background-color: #373737;
  color: #fff;
}
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent pulvinar magna porta luctus scelerisque. Aenean eleifend luctus odio, vel laoreet justo aliquet ut. Curabitur rutrum dictum lacinia. Integer vel pulvinar neque. Donec a fringilla justo.
  Integer vulputate tempor ipsum, sed iaculis lectus laoreet faucibus. Integer bibendum nisl ut leo porttitor mattis. Aenean non massa in libero mattis condimentum sed sed risus.</p>

Or if you prefer a transparent look, you can use this code:

body {
 background:pink;
}

p {
  padding: 20px 100px 20px 20px;
  background:
  linear-gradient(#373737,#373737) 0 0/calc(100% - 80px) 100% no-repeat,
  linear-gradient(to top right,#373737 50%,transparent 51%) 100% 0/80px 100% no-repeat;
  color: #fff;
}
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent pulvinar magna porta luctus scelerisque. Aenean eleifend luctus odio, vel laoreet justo aliquet ut. Curabitur rutrum dictum lacinia. Integer vel pulvinar neque. Donec a fringilla justo.
  Integer vulputate tempor ipsum, sed iaculis lectus laoreet faucibus. Integer bibendum nisl ut leo porttitor mattis. Aenean non massa in libero mattis condimentum sed sed risus.</p>

If you want to achieve a similar effect using Bootstrap, you can add a pseudo-element for the overflowing part:

body {
  background: pink;
}

p {
  padding: 20px 100px 20px 20px;
  background: linear-gradient(#373737, #373737) 0 0/calc(100% - 80px) 100% no-repeat, linear-gradient(to top right, #373737 50%, transparent 51%) 100% 0/80px 100% no-repeat;
  color: #fff;
  position: relative;
}

p:before {
  content:" ";
  display: block;
  position: absolute;
  top: 0;
  bottom: 0;
  right: 100%;
  left: -10000px;
  background: #373737;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<div class="container">
  <div class="row">
    <div class="col-xs-12">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent pulvinar magna porta luctus scelerisque. Aenean eleifend luctus odio, vel laoreet justo aliquet ut. Curabitur rutrum dictum lacinia. Integer vel pulvinar neque. Donec a fringilla
        justo. Integer vulputate tempor ipsum, sed iaculis lectus laoreet faucibus. Integer bibendum nisl ut leo porttitor mattis. Aenean non massa in libero mattis condimentum sed sed risus.</p>
    </div>
  </div>
</div>

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

Placing a div with absolute positioning inside another div with absolute positioning raises the question of how it functions when the outer div is not set to position: relative

Trying to grasp the concept, I experimented with the following setup: <div id="Outer"> <div id="Inner"></div> </div> In this scenario, the outer div has a relative position and the inner div has an absolute position. This allo ...

Creating MySQL query results in Node.js manufacturing process

I am looking to format the MySQL result in Node.js in a specific way. The desired result format should look like this: [ { "m_idx" :1 , "contents" : { "m_name" : "a", ...

Display the child component exclusively after the data has been successfully loaded

Within my parent component, I have the following structure: <v-container fluid> <ResultsPanel ref="results" /> </v-container> The ResultsPanel component consists of: <template v-if="showResults"> <v-container > IM SHOW ...

What is the best way to create a map in React that allows for changing the state without affecting all elements?

When working with a JSON file containing various values, one of them being "iframe" which can hold either "si" (yes) or "no" based on whether it should include an iframe. With this value (yes/no), I need (this.props.tabsiframe === 'yes') to deter ...

Modify the button text when it is hovered over

I am attempting to modify the text displayed on a button when hovering over it in a React component from Ant Design. However, I have not been successful so far. Button <div className={ status == "verified" ? `${styles.btn1} ${styles.btn1C ...

Creating a debounce functionality by isolating it from a keydown event listener

I have a table with selectable rows using up and down arrows. When a row is selected, an ajax call is made to fetch the record. To prevent users from spamming ajax requests, I implemented a debounce function called from a directive triggered by a keydown ...

Steer clear of utilizing Number() when working with scientific notation

I am attempting to perform the following task Number("0.00000000000122") yields 1.22e-12 However, my goal is to convert that number from a String to a Number. console.log(Number("0.00000000000122")) ...

Activate only one option group at a time

<select name="location"> <optgroup label="West Coast"> <option value="1">Los Angeles</option> <option value="2">San Francisco</option> <option value="3">Seattle</option> &l ...

Executing a js.erb template while submitting a form with AJAX in a Rails application

My form setup looks like this: <div class= "parent-container"> <%= form_with scope: @company, url: companies_path, html: { class: "form-inline", remote: true, "data-type" => :js, id: "new-company-create" }, local: true do |f| %> <d ...

Deactivate collapsing of active element in Bootstrap 5 accordion without using jQuery

Check out this example of a Bootstrap 5 Accordion element: <div id="accordion-1" class="accordion" role="tablist"> <div class="accordion-item"> <h2 class="accordion-header" role=& ...

Ways to improve performance when loading multiple $.ajax({...}) functions in Asp.net?

In my ASP.NET web application, I am calling multiple functions on page load. All functions are run on the page ready method. See the code snippet below: $(document).ready(function(){ func1(); func2(); func3(); //.... and so on. } ...

Trigger a re-render of specific components upon clicking a button in React

Within my server file, there is a function that retrieves a specific set of data based on an ID. app.get('/musicbyid', function(req, res){ var query = req.query.term.toLowerCase(); music.find({ _id: query }, function(err, allMusic){ ...

The jQuery autocomplete feature presents all choices regardless of what is typed into the input field

I'm currently working on a jQuery script that retrieves a JSON response and creates individual "player" objects based on the data received. These player objects are then added to the availablePlayers array, which serves as the data source for the aut ...

Execute a sudo command using an html button

Looking for a way to create a simple website with HTML buttons on my Raspberry Pi that can execute sudo commands. I attempted the following method: <?php if (isset($_POST['button'])) { exec('sudo reboot'); } ?> ...

Iterate through an array containing objects that may have optional properties, ensuring to loop through the entire

I need help iterating through an array of objects with a specific interface structure: export interface Incident { ID: string; userName1?: string; userName2?: string; userPhoneNumber?: string; crashSeverity: number; crashTime: number; } Here ...

`The ng-binding directive seems to be malfunctioning while ng-model is functioning properly

Currently, I am in the process of learning how to utilize Angular (1.3.10). My objective is to create two input fields that will specify the suit and value for a hand of playing cards. In my attempts to achieve this, I have encountered an issue. When I man ...

Downloading a CSV file using a Django/JQuery GET request

Seeking answers related to Python and jQuery, but luck has not yet been on my side! I am attempting to download a CSV file sent by a server via an AJAX GET call. I have managed to successfully download it through a blocking GET request, but I aim to achie ...

What is the best way to eliminate the comma at the end of the final array

<% for(var i=0; i < 10; i++) { %> <a href="#"><%= related[i] %></a><% if(i !== 9) { %>, <% } %> <% } %> Displayed above is some code that includes a loop to display related items. The goal is to remove the comm ...

Tips for integrating Google WebKit with AngularJS

Looking to enhance my application with Google WebKit functionality. My goal is to create a feature similar to Gmail's where hovering over the "+" symbol expands to display options such as "insert photos" and "insert links". I'm just starting out ...

Can I use Javascript to access a span element by its class name?

Similar Question: How to Retrieve Element By Class in JavaScript? I am looking to extract the text content from a span element that does not have an ID but only a specific class. There will be only one span with this particular class. Can anyone guide ...