Superior method to ensure the child element's border overlaps with that of the parent

I am currently working on a project that requires a unique CSS effect:

Let me explain exactly what I am trying to achieve:

The main element (referred to as the title) has padding and a 2px bottom border. Inside this, there is a child element (known as title-inner) which also has padding and a 2px border but in a different color, creating an overlapping effect with its parent (title).

This is the structure I am using:

<div class="title">
    <div class="title-inner">Widget title</div>
</div>

I understand that achieving this effect will require adjusting the padding and margin properties of both elements, however, the CSS I have written so far does not respond well on mobile or when resizing the browser window (you can see it in action here: this fiddle).

Is there a more effective way to accomplish this CSS effect? Any suggestions?

Answer №1

Instead of applying padding to both the child and parent elements, consider giving the padding only to the child element. Then, add a negative margin to ensure that the borders overlap each other.

body {
    background: black;
    color: #333;
    font-family: Arial, sans-serif;
    margin: 1em;
}

.title {
    border-bottom: 3px solid #ccc;
}

.title-inner {
    display: inline-block;
    vertical-align: bottom;
    padding-bottom: 12px;
    margin-bottom: -3px;
    border-bottom: 3px solid #FACD98;
}
<div class="title">
    <span class="title-inner">Lorem Ipsum</span>
</div>

Answer №2

The most effective approach, in my opinion, involves utilizing background, divs, and width properties.

Check out the Animation Demo here

By using div elements instead of borders, you gain greater control over the layout of your elements.

<div class="title">
    <div class="title-inner">Lorem Ipsum</div>
    <div id='line'>
        <div id='load'></div>
    </div>
</div>

Once the div elements are in place, apply the following CSS styles:

body {
    background: black;
    color: #ddd;
    font-family: Helvetica, sans-serif;
    margin: 2em;
}
.title-inner {
    padding: 17px;
}
#line {
    background-color:purple;
    height:2px;
    width:100%;
}
#load {
    background-color:yellow;
    height:100%;
    width:0%;
}

Finally, enhance the functionality by adding some JavaScript code:

   function loop() {
        $('#load').animate({
            width: "50%"
        }, 1600);
        $('#load').promise().done(function () {
            $(this).width(0);
            loop();
        });

    }
loop();

Answer №3

One alternative approach is utilizing absolute positioning for the child element:

.heading {
    border-bottom: 2px solid;
    position: relative;
    padding-bottom: 20px;
    height: 20px;
}

.heading-content {
    display: inline-block;
    position: absolute;
    width: 50%;
    height: 100%;
    border-bottom: 2px solid #E64998;
}

It is worth noting that I included the 50% width based on your provided image example.

http://jsfiddle.net/v1ut1pyv/5/

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

The AJAX request for JSON data is functioning correctly in Firefox, but is experiencing compatibility issues with other web browsers

I recently completed a PHP page that generates a valid JSON document. The jQuery code used to fetch and display the data is quite straightforward: $.ajax({ url: "http://localhost:8888/rkm/json-jc", dataType: "json", success: function(data) { ...

The text within the button disappears when in the :before state

After implementing the code from this codepen, I decided to use the 3rd button design. .btn { line-height: 50px; height: 50px; text-align: center; width: 250px; cursor: pointer; } .btn-three { color: #FFF; transitio ...

Angular HttpClient does not support cross-domain POST requests, unlike jQuery which does

I am transitioning to Angular 13 and I want to switch from using jQuery.ajax to HttpClient. The jquery code below is currently functional: function asyncAjax(url: any){ return new Promise(function(resolve, reject) { $.ajax({ type: ...

Problems with WordPress Theme Rendering in Outdated Versions of Internet Explorer

Currently, I am developing a website for Chase on the Lake at http://chaseonthelake.com/. While everything looks perfect in FireFox, there are some display issues when viewing it in Internet Explorer. The dropdown transparency is not showing correctly, m ...

Insufficient width of images in the bootstrap carousel

Is there a different solution to this problem that hasn't been mentioned in the other posts? I've tried all the top solutions from those posts, but my issue still remains. In particular, the "example" code and "Stock" bootstrap carousel appear t ...

stopPropagation() halts the propagation in the opposite direction

I encountered an issue while attempting to create a non-clickable div. While it does stop propagation, it doesn't prevent what should be stopped and allows things that shouldn't be clickable. js //screen has set stopPropagation $("#smokeScree ...

Exploring Symfony2 controller integration with Javascript arguments

I am currently working on a project with Symfony2 and I have a question regarding receiving arguments from a template in a controller. My goal is to take the value of the argument and store it in the database. The argument's value will be generated by ...

Adapting Bootstrap components based on the screen size: Viewport-driven

Check out our Custom Sizing Solution, designed to offer sizing options tailored to different devices and viewports. Here's what we can do: For small devices, the width will be set at 100% For larger devices, the width will be adjusted to 75% For ext ...

Can someone explain the significance of '{}' within the function shown below?

I've been able to grasp most of this code, but I'm unsure about "{}". Can anyone clarify its meaning? var Toggle = function(section, expand) { this.section = section || {}; this.expand = expand | ...

Can individual column searching be implemented in datatable on the server side?

I am facing some challenges with implementing column filters in datatables server-side. The filter columns are visible but do not seem to be functioning properly. I have reviewed various resources such as the links provided below, but I still cannot resolv ...

Tips on traversing a JSON array in AngularJS using the ng-repeat directive

My service returns the following JSON data: {"categories":[{"category_id":"20","name":"Desktops"}, {"category_id":"18","name":"Laptops &amp;"},{"category_id":"25","name":"Components"}, {"category_id":"57","name":"Tablets"}, {"category_id":"17","name": ...

Converting JSON information into a mailto hyperlink

Can anyone help me figure out how to encode a mailto link properly with JSON data in the query parameters, ensuring that it works even if the JSON data contains spaces? Let's consider this basic example: var data = { "Test": "Property with spaces" ...

What steps can be taken to prevent a wrapper's CSS from affecting its enclosed elements?

My container div includes some opacity and auto height that I don't want to affect all elements within it. Is there a way to preserve the container's styles without impacting its contents? ...

Responses were buried beneath the inquiries on the frequently asked questions page

My FAQs page is in pure HTML format. The questions are styled with the css class .pageSubtitle, and the answers have two classes: .p1 and .p2. Here's an example: <p class="pageSubtitle">Which Award should I apply for?</p> <p class="p1" ...

Is there a way to apply -webkit-line-clamp to this JavaScript content using CSS?

i have a random-posts script for my blogger website <div class="noop-random-posts"><script type="text/javascript"> var randarray = new Array(); var l=0; var flag; var numofpost=10; function nooprandomposts(json){ var total = ...

Combining PHP and MySQL with a join statement

I am trying to calculate the average rating for each store in a directory list using MySQL's AVG function. Whenever someone reviews a store, they assign a rating ranging from 1 to 5. This rating is then inserted into the rating column of the reviews ...

Verify the form data before triggering the ajax call with just one click

Ensuring that all required areas are completed in a form is crucial. The form needs to be validated properly to either reject the request with a message showing what information is missing, or submit it successfully... The rejection process works well as ...

Are there any specific steps I should take to ensure that my server can support jQuery.getJSON when using a bookmarklet?

Currently, I am in the process of creating a bookmarklet that will require some user details to be input. After researching my options for cross domain communication, I have found that my best choices are either using jQuery.getJSON or adding a form and i ...

What is the best way to turn off the jQuery Kladr plugin?

Currently, I'm incorporating the jquery kladr plugin to facilitate address autocompletion specifically for Russia. I am wondering how to deactivate this plugin should a different country be selected? ...

Highlighting Navbar Items

Can anyone provide advice on how to highlight a navbar item when clicked? I'm unsure if I should use Angular or CSS for this. Any guidance would be greatly appreciated. <div class="collapse navbar-collapse" id="navbarNav"> <ul class ...