Problem encountered when trying to generate a custom-shaped div with a width set to 100

I am currently working on creating a diagonal div at the bottom of another div. I want it to have a design similar to this: .

I encountered an issue when I tried adding a 100vw value to my border-left, as it caused a horizontal scrollbar to appear on my website. I attempted to use jQuery to find the width of the body, which worked initially. However, resizing the browser did not update the border width. Any suggestions on how to resolve this?

     var actualInnerWidth = $(".background-gradient").prop("clientWidth"); 
$('.background-gradient').css({
   'border-top':'50px solid red',
   'border-left': actualInnerWidth + 'px solid transparent'
}); 
    .banner {
   min-height: 50vh;
   margin: 0;
   padding:0;
   background-color: red;
 }
.background-gradient {
   width:100%;
   height:50px;
   background-color:yellow;
 }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div class="jumbotron banner">
</div>
<div class="background-gradient">
</div>

You can view the jsfiddle for this project here: https://jsfiddle.net/b49mmm9a/1/

Answer №1

Give this a shot, I believe you'll like it

.box {
  background-color: skyblue;
  margin-top: 40px;
  padding: 1% 20px;
  /* Added a percentage value for top/bottom 
            padding to keep 
            the wrapper inside of the parent */
  -webkit-transform: skewY(-5deg);
  -moz-transform: skewY(-5deg);
  -ms-transform: skewY(-5deg);
  -o-transform: skewY(-5deg);
  transform: skewY(-5deg);
}

.box>.wrapper {
  -webkit-transform: skewY(5deg);
  -moz-transform: skewY(5deg);
  -ms-transform: skewY(5deg);
  -o-transform: skewY(5deg);
  transform: skewY(5deg);
}
<div class="box">
  <div class="wrapper">
    <h1>This is a heading</h1>
    <p>This is a sub-heading</p>
    <p>

      How do I draw a Diagonal div?
      </a>
    </p>
  </div>
</div>

http://jsbin.com/daruruhola/edit?html,css,js,output

Answer №2

Below is a straightforward JS/jQuery snippet.

function addBorder(){
      var width = $(".background-gradient").prop("clientWidth"); 
      $('.background-gradient').css({
        'border-top':'50px solid red',
        'border-left': width + 'px solid transparent'
            }); 
        }

$(document).ready(function(){
    addBorder();
});

      $(window).resize( function(){
        addBorder();
      });

View the code on CodePen: https://codepen.io/sajiddesigner/pen/PjBRxQ

Answer №3

.container {
  width: 100%;
  overflow: hidden;
}
<div class="container">
  <div class="banner"></div>
<div class="background-gradient"></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

View saved local storage information

I have an inquiry regarding saving data using localStorage for a shopping cart feature. I am able to list the data on one page through console.log but am facing difficulties in calling the data on another page. Can anyone assist with this? The Product page ...

Using AJAX to dynamically edit and update PHP data

The information displayed on my index.php page is fetched from a database using the mysqli_fetch_array function. The data is presented in a table format with fields like Name, Age, Email, and Update. An edit button allows users to modify the data. Here is ...

Tracking the latency of WebSockets communications

We are in the process of developing a web application that is highly responsive to latency and utilizes websockets (or a Flash fallback) for message transmission to the server. While there is a fantastic tool known as Yahoo Boomerang for measuring bandwidt ...

Avoid the selection of child elements in jQuery

I have been trying to create a dropdown box similar to the "Most Recent" dropdown on Facebook. My goal is to have a wrapper that includes both the element that triggers the dropdown and the dropdown box itself. Here is the structure I have set up: <di ...

Guide to using Ajax to send a form and receive text in response

Check out my code on Fiddle: $("form.signupform").submit(function(e) { e.preventDefault(); var data = $(this).serialize(); var url = $(this).attr("action"); var form = $(this); // Added this line for reference $.post(url, data, function(data) { $(for ...

Spring MVC returns 404 error for AJAX POST Request

I am currently working on a project that involves submitting a JSON object using AJAX to a Spring controller. However, I keep encountering a 404 error upon submission. Can someone please pinpoint what the issue might be: Here is my AJAX Request : $.a ...

HTML block failing to implement Bootstrap styling

I am struggling to integrate Bootstrap into Drupal. I have used the 'row' class for the contact ID and added col-sm-12 for title and desc, but it is not working as expected. The width of the container seems off. Also, the accordion.js script work ...

What is the best way to incorporate a wav file across all browsers using Django?

I am having trouble playing a wav file in Django. I tried using the audio tag and it worked fine in Chrome, but doesn't work in Firefox. When I created an HTML file in Apache, it worked with Firefox. However, I can't figure out why it's not ...

Exploring the range of switch-case statements in JavaScript

// IMPLEMENTING A NEW FUNCTION [NEW CODE] $('.enter-button').on('click', function (e) { e.preventDefault(); var searchValue = $('.a').val(); var listOfLocations = $('.b').text().trim(); var listSi ...

Learn how to create a responsive flickr embedded video with Bootstrap!

I'm struggling to make the embedded video responsive. I attempted the solution mentioned in a Stack Overflow post but it didn't work for me. The video's width remains the same and doesn't scale down properly, appearing out of bounds on ...

Struggling to retrieve user input from a textfield using React framework

I'm having trouble retrieving input from a text-field in react and I can't figure out why. I've tried several solutions but none of them seem to be working. I even attempted to follow the instructions on https://reactjs.org/docs/refs-and-th ...

An unanticipated issue has arisen: an unexpected token was encountered while using jsonp

I have been trying to make a jsonp call to the API located at . Despite my thorough research on jsonp, I can't seem to figure out what I'm doing incorrectly. Chrome browser keeps displaying the error message 'Uncaught SyntaxError: Unexpected ...

Ways to retrieve only the chosen option when the dropdown menu features identical names

How can I retrieve only the selected value from the user, when there are multiple select options with the same class name due to dynamic data coming from a database? The goal is to submit the data using Ajax and have the user select one option at a time. ...

Tips for dynamically updating the value of a variable in Vue by importing a JavaScript file

There is a file for an app that imports ymaps.js where YmapsComponent.vue is declared. import '../js/ymaps.js'; import { createApp } from 'vue'; const app = createApp({}); import YmapsComponent from './components/YmapsComponent.vue ...

Eliminate the prior click action from the document object model

I am working with a set of elements that each have unique IDs and contain a span tag with the same image. For example: Under each element, there is a save icon. When a user clicks on it, the icon changes to a non-save icon. The issue arises when I click ...

Exploring the possibilities of Vue.js and Bootstrap 4 on the PlayStation 4 internet browser

Are there any workarounds for achieving Vuejs and Bootstrap 4 compatibility on the Playstation 4's Internet browser? When I access my website, it only displays our background color and nothing else. I am open to suggestions. Is there a fix for this i ...

How can Google Cloud Vision be optimized to enhance OCR capabilities?

Recently, I decided to put Google cloud vision's OCR to the test, only to be disappointed by the subpar results. Despite my documents being in French, the OCR seemed to struggle with detecting apostrophes and commas accurately. For instance, when usin ...

I am experiencing an issue where the data on the server is not being updated when I click the "back" button in the browser using jQuery, Ajax,

Here's the issue at hand: Upon clicking the "purchase" button, it changes color, updates the link, and adds the product to the cart: $(document).ready(function(c) { $('.item_add').click(function (event) { var addButton = $(this).c ...

Updating a React event as it changes with each onChange event

Let's address a disclaimer before diving into the issue - for a quick look, visit this pen and type something there. The Scenario This is the JSX code snippet used in my render method: <input value={this.state.value} onChange={this.handleCh ...

Finding a character that appears either once or thrice

In my work on JavaScript regex for markdown formatting, I am trying to match instances where a single underscore (_) or asterisk (*) occurs once (at the beginning, end, or surrounded by other characters or whitespace), as well as occurrences of three under ...