Ensure that blocks with ribbon attachments are perceived as equal in size

I have been utilizing the bootstrap grid system to generate different-sized "blocks" on my webpage. Some of these blocks require a ribbon (such as "new" products). The challenge lies in how I create these ribbons, which involves wrapping the block's contents with padding around it to allow the ribbon to seamlessly integrate and give the desired effect.

However, this approach also leads to the boxes being slightly smaller than intended due to the additional padding required for the ribbon.

To see how it currently appears, check out this demo: fiddleLink

CSS

.row {
    padding: 5px;
    background: #efefef;
}

.block {
    position: relative;
    padding: 5px;
}

.block-inner {
    background: #fcc;
    padding: 15px;
}

div.ribbon {
    z-index: 1;
    position: absolute;
    top: 2px;
    left: 0;
    width: 88px;
    height: 86px;
}

div#no-ribbon {
    background: #ccf;
}

div.ribbon-text {
    position: absolute;
    top: 25px;
    left: -15px;
    width: 100px;
    height: 20px;

    text-align: center;
    font-style: italic;
    font-size: 16px;
    color: white;

    transform: rotate(-45deg);
}

HTML

<div class="container">
    <div class="row">
        <div class="col-xs-3 block">
            <div class="ribbon">
                <div class="ribbon-text">New!</div>
            </div>
            <div class="block-inner">
                <p>Some awesome text and imagery, whatever</p>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-xs-3" id="no-ribbon">
            <p>A block with its "actual" size</p>
        </div>
    </div>
</div>

Screenshot

The issue is evident in the misalignment of the visible left borders. While technically sound, from a user's perspective, it appears odd.

Is there a way to ensure that the left border of a block with a ribbon aligns perfectly with blocks without a ribbon?

Answer №1

Adjusting the placement of the inner block by making it relatively positioned and slightly widening it, while also shifting the ribbon 5 pixels to the left:

.block-inner {
    background: #fcc;
    padding: 15px;

    // newly added css
    position: relative;
    left:-5px;

    //Also increase the width of the inner block
    width:96.7%;
}

UPDATE

 div.ribbon {
        z-index: 1;
        position: absolute;
        top: 2px;

        // shift the ribbon left by -5px as well
        left: -5px;
        width: 88px;
        height: 86px;
    }

Answer №2

Update Complete

To adjust the positioning of the block, you can use margin-left: -5px;.

.block-inner {
    background: #fcc;
    padding: 15px;
    /* Apply this */
    margin-left: -5px;
}

div.ribbon {
    margin-left: -5px;
}

div.ribbon + .block-inner {
    margin-right: -5px;
}

Preview:

Fiddle: https://jsfiddle.net/kpopsj5x/5/

Answer №3

Your HTML and CSS structure needs some reorganization for better presentation.

#no-ribbon .block-inner {
  background: #ccf none repeat scroll 0 0;
}
<div class="container">
  <div class="row">
    <div class="col-xs-3 block">
      <div class="ribbon">
        <div class="ribbon-text">New!</div>
      </div>
      <div class="block-inner">
        <p>Some amazing content with images</p>
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col-xs-3 block" id="no-ribbon">
      <div class="block-inner">
        <p>Displaying a block in its actual size</p>
      </div>

    </div>
  </div>
</div>

Check out the updated version on JSFiddle

Answer №4

My recommendation is to keep it simple; there's no need to add a new HTML element for the "New!" badge. Instead, you can create an image with text:

and then use a pseudo-element like this:

.row {
    padding: 5px;
    background: #efefef;
}
.block-inner {
    background: none white;
}
.block {
    position: relative;
    padding: 5px;
}
.block-inner {
    background: #fcc;
    padding: 15px;
}
.badge-new::before {
    z-index: 1;
    position: absolute;
    content: ' ';
    top: -3px;
    left: 10px;
    width: 88px;
    height: 86px;
    background-image: url('https://i.sstatic.net/quoTy.png');
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
    <div class="row">
        <div class="col-xs-3">
            <div class="block-inner badge-new">
                <p>Some awesome text and imagery, whatever</p>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-xs-3">
            <div class="block-inner">
                <p>A block with its "actual" size</p>
            </div>
        </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

Using CSS to showcase div elements with varying dimensions

How can I arrange divs with varying heights to be positioned close together, regardless of their height, and with a specific margin similar to buildings? I am aiming for a layout like the one on this website . I tried using float:left but it only float ...

HTML two-row horizontal list with truncation

My dilemma involves a horizontal list of items that expand the full width of their container and wrap onto the next line. However, I only want to show two lines of items initially, with a "show more" link to reveal additional rows in increments of two unti ...

Prevent the clustering of advertisements using jQuery

Check out my code in action here! jQuery(document).ready(function(){ var insertionTemplate = jQuery(".hiddenAdBox").eq(0).html(), insertionTarget = jQuery('ul'), insertionTargetChildren = insertionTarget.find('li'), ...

The reasons why script tags pointing to "localhost:3000" become unsuccessful

Currently in the process of revamping my company's web services, we are separating our static web files (HTML, CSS, JS) from our authentication server and API service. To maintain backward compatibility during our migration plan, we need to keep the o ...

Styling Django's buttons and search bars to appear inline using CSS

In my Django template, I have the following HTML code: <div id = "search_form"> <form action="" method="get" id = "search-form"> {% csrf_token %} {{ form.as_p }} {{ form.media }} </form> ...

The background image is not displaying

I am having trouble getting my background image to show up on my webpage. I have included a link to a screenshot of my image directory below for reference: image directorydirectory /*Moto G4, Galaxy S5*/ @media only screen and (min-width: 360px) { * { ...

Dynamically include new events

Here is the code snippet I have in a JS file: $(".dropmenu").on("click", function(event){ event.preventDefault(); $(this).parent().find("ul").slideToggle(); }); On my webpage, I'm using the following code: var append="<ul> ...

Top suggestions for maintaining the Firefox extension popup open when clicking the navigation button

As I work on developing a Firefox extension using jetpack, I have encountered an issue. It seems that when I click on the navigation button, a popup opens but then closes automatically when I click elsewhere. Is there a way for me to override this automa ...

Designing an immersive full-screen experience with scrollable columns using Bootstrap

Trying to achieve a full-screen layout that occupies 100% of the viewport with a sticky header and footer, along with columns in the main content area that can be individually scrolled. I've been experimenting with using classes like .h-100 and .flex ...

What are the steps to interact with a marquee using Selenium?

Currently, I am faced with an application that displays a list of upcoming vessels in a marquee format. I am looking for a way to click on the specific data using Selenium with Java. Any advice or alternative solutions would be greatly appreciated. ...

Angular: Customizing table cell color based on condition

I am having trouble with changing the cell color of an object in my html table if a certain variable changeColor is true. I am utilizing angular for this task. <tr ng-repeat="list in results"> <% if (!{{list.changeColor}} ) %> <% { ...

Add PHP functionality to insert a jQuery array of select2 tags

After browsing numerous threads on various platforms regarding this issue, I have yet to find a solution. Therefore, I am reaching out for help once again. I am currently utilizing the select2 jQuery plugin and aiming to insert tags into an SQL database us ...

How can we prevent the HTML escaping of text children in React.createElement?

Let's examine the call to React.createElement: React.createElement('span', null, null, ['&gt;&lt;',]) When executing this code, React will escape the characters &gt; and &lt;. Is there a method to prevent these sp ...

"I am experiencing an issue with a tick symbol not displaying correctly in my

I have created an XSLT file where I have implemented the following code snippet. To display a tick symbol in front of "Hello World," I have used ✓. The purpose of this XSLT is to parse an XML response and convert it into HTML format. <td colspan ...

Can you assist me in finding certain Japanese Unicode characters?

I need help finding the Unicode Katakana characters for three Japanese symbols: https://i.sstatic.net/72V5g.png Although I found one of them, the closest matches I could locate are: ⽊ の ⺶ &#12106; &#12398; &#11958; Som ...

Do browsers interpret flex containers differently from one another?

I'm in the process of creating a website. Within the content area, there are flex-boxes containing images. The number of images can vary (up to 5), and I need to calculate the width of each image to ensure they all fit. Oddly enough, this works perfe ...

Equal-height columns in a responsive grid system

I'm currently facing an issue with a layout that seems simple I am working on a design featuring 4 headline boxes. In the "desktop" view, all 4 boxes need to be of equal height. The same applies when viewed across 2 boxes in the "tablet" mode. Howeve ...

The width of Highcharts increases proportionally to the growth of the chart's width

I want to create a highcharts graph where the width increases as data points increase. Currently, I have: I am using vuejs with highcharts, but it should work similarly with jquery or other frameworks. <div class="col-md-6" style= ...

Adding a component to a grid row with a click action

I have a grid of items and I want to display another component that takes up the full width below the clicked item when it's selected. Here is an illustration of what I'm trying to accomplish: https://i.sstatic.net/5XIvf.png Within my button-li ...

One Cursor Multiplying, Leaving Another Stranded in the Top Left Corner of the Screen

Lately, I've been facing a strange problem with my mouse cursor where it duplicates; one of the cursors appears to be fixated at the top left-hand side of the screen while the other cursor functions normally. Here's a visual example for clarity: ...