End Cap Positioned at an Angle on an Inline Element

Objective:

I have a design that requires an angled edge on inline elements containing variable text. If the text wraps onto multiple lines, the edge should only be applied to the last line, rather than the entire block of text.

My Solution:

http://codepen.io/anon/pen/CjLpG

  1. Insert a span element
  2. Use JavaScript to dynamically determine the height of the inline text upon page load
  3. Create a CSS trapezoid shape
  4. Utilize relative positioning to correctly position the edge

Current Challenges:

  1. The solution relies on JavaScript
  2. There is a slight discrepancy in alignment when resizing the screen or changing the text size. Different browsers (such as Safari and Chrome) interpret rounding differently, causing inconsistency.

Are there alternative methods to achieve this effect more effectively?

Unsuccessful Approaches:

  1. Exploring other complex positioning techniques
  2. Attempting various skewX transformations (not feasible for inline elements)

Implementation Code:

<mark>test text</mark><span class="edge">&nbsp;</span>
mark {
    background: #f00;
    padding: 0.1em;
}
mark + span {
    border-style: solid;
    border-color: #f00 transparent transparent transparent;
    border-right-width: 10px;
    border-bottom-width: 0;
    border-left-width: 0;
    position: relative;
    pointer-events: none;
}
$(window).load(function() {
    $('mark + span').each(function(i, span) {
        var $span = $(span);
        var $mark = $span.prev('mark');
        var markHeight = $span.innerHeight();
        var markPaddingTop = Number($mark.css('padding-top').replace(/px/, ''));
        var markPaddingBottom = Number($mark.css('padding-bottom').replace(/px/, ''));
        $span.css({
          top: markHeight + markPaddingTop,
          'border-top-width': markHeight + markPaddingTop + markPaddingBottom
        })
    });
});

Answer №1

Using jQuery for this effect may be unnecessary. You can achieve the same result with a simpler approach using absolute positioning and the :after pseudo class.

mark {
    display: inline-block;
    position: relative;
    background: #f00;
    padding: 0.1em;
    min-height: 50px;
    box-sizing: border-box;
}

mark:after {
    position: absolute;
    display: block;
    left: 100%;
    bottom: 0;
    content: '';
    width: 0; 
    height: 0; 
    border-top: 0 solid transparent;
    border-bottom: 50px solid transparent;
    border-left: 10px solid #f00;
}

50px is somewhat arbitrary, so feel free to adjust it to match your desired line height.

Check out this CodePen example

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

Having trouble with the menu toggle button on Bootstrap 4?

When using Bootstrap 4, the breadcrumb button may not function properly when the header becomes responsive. I have ensured that Bootstrap 4 CSS and JS are included in the project. Please assist me in resolving this issue. Code: .navbar { height:100 ...

Obtaining Output from within the $.ajax() Method

Is it possible to retrieve a value from within an $.ajax function in JavaScript? Here's the structure I'm working with: function getValue(){ var number = 0; $.ajax({ 'url':'/some/url', 'type':&apo ...

Highlighting a row upon being clicked

How can I implement a feature in my HTML page that highlights a row when selected by changing the row color to #DFDFDF? If another row is selected, I would like the previously selected row to return to its original color and the newly selected row to be h ...

Problem with pairing table rows and cells, slight misalignment of 1 pixel

In my attempt to align an icon and text side by side, with the icon on the left and the text on the right, I encountered an issue where the element's box seems to be consistently 1 pixel off. This misalignment is causing the text to not line up proper ...

Does jQuery always apply the last duplicate attribute in the attr() function if there are duplicates present?

I am feeling quite perplexed at the moment, as I wonder if giving a duplicate attribute in attr() will only apply the last one. For example: attr({style:'color:yellow',style:'font-family:arial',style:'color:yellow'}). In this ...

Switch between links with jQuery

I am looking to dynamically switch the classes on a few links. Take a look at the code below: <ul class="inline toggleNav"> <li><a class="active" id="imagesLink" href="#">Images</a></li> <li><a class="noLink" ...

Twitter Bootstrap column overflowing the container boundaries

Hey there, I'm encountering a couple of small issues. First, the col class is extending beyond the container area = https://i.sstatic.net/bvEAv.jpg Additionally, I need to add space between the two divs = https://i.sstatic.net/KWFAB.jpg When I try a ...

How can AJAX be utilized with both file and text inputs?

I have a basic form enclosed in a modal window, dynamically generated using jQuery: <form name="altEditor-form" id="altEditor-form" enctype="multipart/form-data" role="form"> <div class="form-group"> <div class="col-sm-5 col-md- ...

Is there a way to easily copy the content within the <code> tag to the clipboard?

I've attempted to use the following code, but unfortunately, it's not functioning properly. Is there a way for me to copy the contents inside the <code> tag to my clipboard? <pre id="myCode"> <code> console.lo ...

Tips for integrating Sass into a React application with bundle.js and bundle.css

I'm currently working on a React project that uses bundle.js/bundle.css, which are linked in the index.html like this: <script src="/bundle.js"></script> <link rel="stylesheet" href="/bundle.css" /> Ini ...

What is the best method for linking two ajax requests together?

Here is the code snippet I'm currently working with: <script> $(function () { $('#AdminCreateNewUserSubmit').on('click',function(){ $("#admincreatenewuserform").ajaxForm( { target: '# ...

The issue of JQuery mobile customizing horizontal radio buttons failing to function on physical devices has been identified

Not long ago, I posed a query on Stackoverflow regarding the customization of horizontal jQuery-mobile radio buttons. You can find the original post by following this link How to customize horizontal jQuery-mobile radio buttons. A developer promptly respon ...

Allow frontend JavaScript to retrieve a text file that is restricted to individual users

Trying to articulate my goal is challenging, but I'll do my best to clarify. My objective is to enable JavaScript (or an alternative solution) to retrieve data from a text file on a static site and utilize that data in some way. The challenge here is ...

Adding design to distinct element after clicking a button

Struggling with a JS/Jquery issue on my portfolio website, I admit that I am still just an average programmer. My portfolio website has five buttons, each representing a different project. For each project, there is a corresponding text description and an ...

Revamping various classes by applying a range of unpredictable background hues

I currently have a website located at . However, I am looking to change the background colors of the cards (when flipped) to random colors. My current approach is shown below: <script> function get_random_color() { var letters = '0123456789AB ...

How to give a stylish touch to your :after element with a css

I have a unique bubble design that pops up when hovering. I'm trying to add a border around the bubble, but I'm facing difficulty adding a border to the pointer of the bubble. The arrow is created using the .bubble:after selector. You can check ...

Is it possible to use a jquery selector to access dropdown options?

Having an issue with accessing a dropdown on my ASPX webpage using JavaScript. Here is the code I have tried: var dropDown = document.getElementById('myDropdown'); alert(dropDown.options.length); Unfortunately, this code is not working as inten ...

When the content within a dialog element exceeds the boundaries of the dialog, it will not be displayed

Issue Struggling with creating a dialog element that includes an x button for closing the <dialog> in the upper-right corner. My initial idea was to utilize absolute positioning and transforms to achieve this functionality, but encountered a setback ...

Acquiring the content between the start of a paragraph and a designated hyperlink with the help of jQuery

I'm currently working on a jQuery script to extract the text from within a paragraph starting from the beginning up to a specific link: <p> Lorem ipsum dolor sit amet, <a href="#">aliqua</a> consectetur adipiscing elit, sed do e ...

JavaScript Number Conversion with parseInt()

Struggling to execute a calculation that fills a text box based on an onblur event. Unfortunately, I keep getting NaN as the result. Since I am not very familiar with JavaScript, any guidance will be greatly appreciated. Please note that I am mainly focus ...