Issues with jQuery causing responsive CSS triangles to fail implementations

Recently, I stumbled upon the interesting concept of CSS triangles created using element borders. I decided to incorporate this design into one of my custom Wordpress themes, but I encountered a problem. I wanted to add a triangle at the bottom of a div, like a banner, but the div contained user-set text which made it impossible to hard code the width of the box and the triangle. My solution was to use jQuery to dynamically adjust the triangle size based on the parent div's width. However, while attempting to set the borders according to half the total parent width, I noticed that the box would resize as elements loaded (mainly due to slow font loading). I wrapped the script to trigger on element resize, but it didn't seem to work properly. This resulted in the triangle appearing larger than the box it was meant to be under. You can observe this issue here:

Here is the code snippet:

HTML

<div id="logo-box">
    <h1><?php bloginfo('name'); ?></h1>
    <div id="logo-box-point"></div>
</div>

CSS

#logo-box{
    background:#374140;
    display:inline-block;
    padding:20px;
    position:relative;
}

#logo-box h1{
    color:#f2f2f2;
}

#logo-box-point{
    content: ' ';
    width:0px;
    height:0px;
    border-left: 0px solid transparent;
    border-right: 0px solid transparent;
    border-top:25px solid #374140;
    border-bottom:0;
    position:absolute;
    bottom:-25px;
    left:0px;
    z-index:2;
}

jQuery

$(function(){
    function triangleSize(){
            var logoBoxWidth = $("#logo-box").width();
            $("#logo-box-point").css({"border-left-width":logoBoxWidth/2, "border-right-width":logoBoxWidth/2});
    }
    triangleSize();
    $("#logo-box").resize(function(){
            triangleSize();
    });
});

I also experimented with the .change() function, but it didn't yield the desired effect. Any insights on what might be causing this content jump or why the function isn't triggering? I suspect it could be related to the inline block display property, but I'm unsure how to resolve it since display:inline-block is essential for the correct display of the div.

Answer №1

When it comes to resizing an element, remember that resize won't work in certain cases. Instead, try using the following code snippet:

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

http://jsfiddle.net/TDcuD/

Please note that your code may not have taken the padding of the DIV into consideration, as I've addressed in the provided fiddle example.

Answer №2

You have the option to accomplish this using basic CSS as well

 div
    {
    width: 0px;
    height: 0px;
    border-style: solid;
    border-width: 30px 338px 0 338px;
    border-color: #007bff transparent transparent transparent;
    line-height: 0px;
    _border-color: #007bff #000000 #000000 #000000;
    _filter: progid:DXImageTransform.Microsoft.Chroma(color='#000000');
    }

VIEW LIVE DEMO

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

Positioning child divs using CSS

I am trying to arrange the #inner2 and #inner3 divs to float next to each other while also adjusting to fit perfectly within the #outer div when the browser window size changes. Unfortunately, inner3 is not floating correctly as it should be and is overlap ...

"What is the best way to display an image from a table and then enlarge it to full size when a button is clicked, all

1. Understanding the Concept In my page, I have a table listing various images with unique names that are successfully displayed in the table. My goal is to display the selected image in full screen within a popup window when a button is clicked. 2. Que ...

When interacting with Chrome, the div gets automatically blurred upon being clicked

While working on the development of our website, we stumbled upon something peculiar. We have a set of divs displayed on the screen, resembling the layout of Pinterest. Upon clicking any of these divs, the content within that particular div is loaded into ...

What steps can I take to ensure that the upper and left sections of a modal dialog remain accessible even when the size is reduced to the point of overflow?

One issue I'm facing is with a fixed-size modal dialog where part of the content gets cut off and becomes inaccessible when the window shrinks to cause an overflow. More specifically, when the window is narrowed horizontally, the left side is cut off ...

Iterate through jQuery loop, clear styles, and iterate again

My goal is to animate the "top" position of an HTML element and then reset the style once the function has finished running before starting the loop again. I'm not an expert in this field, and I believe there must be a more efficient way to achieve t ...

Ways to customize the border of the ListItemButton when it's in a selected state using Material UI?

I'm currently working on a feature that involves highlighting a ListItemButton with a specific background color when selected. However, I now want to take it a step further and add an outline or border color around the ListItemButton when it is select ...

.cshtml fusion turns sour

My attempt to create a loop that displays small boxes led me to the following code snippet with unexpected results: (code) : <div class="row"> <div class="col-lg-3 col-xs-6"> <!-- small box --> <!-- please change the addr ...

Mixing two elements for a continuous animation without the use of JavaScript

I'm interested in creating an animation that cycles between "0% interest free credit" and "free delivery". I am attempting to achieve this without the use of JavaScript, but so far I can only make the first element fade away without the second one fad ...

Uncovering Twig's Power: Navigating JSON Key Values

In my current project, I am utilizing OctoberCMS and Twig to fetch dynamic data and translations from JSON files for the front end. Here is an example snippet of code: <div class="wrapper items"> <div class="items"> {% for suggestion in ...

Capturing the input value from a text box within a table cell, saving it in an array using Javascript, and then transmitting it back

I have a PHP-generated table displayed on my website. The table consists of two rows - the first row contains headers, and the second row contains text boxes inside each cell. My goal is to allow users to input values into these text boxes, then retrieve t ...

Having trouble transmitting parameters through ajax

I have been attempting to use ajax to send variables to a php page and then display them in a div, but unfortunately, it's not functioning as expected. Below is the HTML code: <div id="center"> <form> ...

access the ckeditor within the frame

I have a CKEditor in a frame and am experiencing an issue. When I try to console log from the browser, it won't work until I inspect near the frame and perform the console log again. [This is the CKEditor] https://i.stack.imgur.com/fWGzj.png The q ...

Using jQuery to iterate through JSON data obtained from a web service

In this code snippet, I am attempting to retrieve a JSON response from a PHP page and then iterate through it to display the name field of each JSON object. However, for some reason, nothing is being alerted out. <html> <head> <title>A ...

Allow users to edit the textarea only to input values, not from

I am trying to achieve a specific functionality with two input fields and one textarea. Whenever I type something in the input fields, their values are automatically populated in the textarea using .val(). Currently, this feature is working as intended, b ...

Define the width of jqgrid

I have been utilizing SmartAdmin jqgrid to showcase data in tabular format. The number of columns in the datatable changes dynamically each time. I am converting the DataTable to a JSON object and assigning it to the jqgrid. However, it appears that jqgrid ...

How can I reverse the names displayed in ng-repeat when I click?

When utilizing the orderby filter in angularjs, I want to be able to sort the data only when the button is clicked. If the button is not clicked, the sorting order should not be displayed. <tr ng-repeat="tools in toolsfilter | orderBy:orderByField:reve ...

To enable the standard PayPal 'donate' button functionality, you can remove the submitHandler from jQuery Validate dynamically

I need to insert a PayPal donate button into the middle of an AngularJS donation form, specifically by nesting the PayPal generated form tags within the donation form tags. This donation form is utilizing an older version (1.12) of jQuery Validate, which ...

Calculate the total sum of values in a MySQL column and then group the results

I'm currently working on a quiz website and looking to create a leaderboard. The user scores are stored in a database table named 'user_record' with the following structure: 'user_id' - varchar(254) -holds the user id for a score ...

What steps should I take to incorporate Google sign-in on my website and gather user information efficiently?

I am looking to add the Google sign-in button to my website. While I am knowledgeable in HTML, PHP and JavaScript are not my strong suits. My goal is to allow users to sign in with their Google account and securely store their information on my database th ...

My website always fits on smaller resolution screens, but annoyingly, a horizontal scroll bar still appears

My website seems to have a horizontal scroll bar on smaller resolution screens, even though it fits. Any suggestions on making it fit better? If you're using a laptop, you'll notice that the scroll bar moves too far right to just show blank grey ...