Expand boundaries of nested elements

Having trouble styling nested DIVs (check out this example). Is there a solution?

I'm working with dynamically rendered nested DIVs (each with class="box"), like so:

<div class="box" id="1">
  other content
  <div class="box" id="2">
    <div class="box" id="3">
    </div>
  </div>
  other content
  <div class="box" id="4">
  </div>
</div>
other content

My goal is to have each of these DIVs display a bottom border, like this:

<style>
div.box {border-bottom: solid 1px gray;}
</style>

However, I've run into an issue when the bottom borders of two adjacent nested DIVs meet (e.g. box 2 and 3 or box 1 and 4). This results in a thicker gray line than intended.

Is it possible to collapse the borders of these neighboring nested DIVs for a cleaner look?

I did try adding border-collapse: collapse, but unfortunately, that didn't solve the problem.

Answer №1

border-collapse property applies exclusively to table and inline-table elements.

To create overlapping borders, you can experiment with setting the margin-bottom property to a negative value for .box elements, like this:

Check out the Example Here

div.box {
    border-bottom: solid 1px gray;
    margin-bottom: -1px;
}

Answer №2

The border collapse issue persists, I managed to fix it using your JsFiddle example. However, the solution may need adjustment since your DIVs are generated dynamically.

div.box > div.box {
    border-bottom: solid 1px gray;
}

div.box > div.box > div.box:last-child {
    border-bottom: none;
}

Answer №3

One way to control borders in CSS is by using the border-collapse property, which is specifically designed for tables.

However, if you are working with div elements instead of tables, you can achieve a similar effect by utilizing the :last-child selector in CSS3. This allows you to remove the border on the last .box element.

For example:

.box:last-child{border:none;}

Answer №4

There are multiple ways to achieve this:

1) Mimic table behavior with div elements.

HTML

<div class="wrapper2">
    <div class="row"></div>
    <div class="row"></div>
</div>

SCSS

.wrapper2 {
    display: table; border-collapse: collapse; margin-bottom: 15px;
    .row {
        width: 200px; height: 100px; background: green; border: 1px solid red; display: table-cell;    
    }
}

2) Style specific divs by removing borders from one side.

HTML

<div class="wrapper">
    <div class="row"></div>
    <div class="row"></div>
    <div class="clearfix"></div>
</div>

SCSS

.wrapper {
    width: 404px; margin-bottom: 15px;
    .row {
        width: 200px; height: 100px; background: green; border: 1px solid red; float: right;
        &:first-child { border-left: 0; }
    }
    .clearfix { clear: both; }
}

3) Overlap divs to create a unique layout.

HTML

<div class="wrapper3">
    <div class="row"></div>
    <div class="row move-left"></div>
    <div class="clearfix"></div>
</div>

SCSS

.wrapper3 {
    width: 404px; margin-bottom: 15px;
    .row {
        position: relative; width: 200px; height: 100px; background: green; border: 1px solid red; float: left;
        &.move-left { left: -1px }
    }
    .clearfix { clear: both; }
}

All 3 examples can be viewed on jsfiddle

Choose the method that best suits your project requirements.

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

What is the best way to dim and display a loading GIF in the middle of a table?

Is it possible to apply a transparent grey effect to an entire <table> and display a loading image in the center? I am interested in running some AJAX calls where the table is rendered inaccessible, with a grey overlay and a loading GIF positioned a ...

What is the best way to transform a rectangular image into a square using CSS?

It's a common misconception that CSS can directly modify images, hence the quotation of "crop." My goal is to take rectangular images and apply CSS to give them a square appearance without altering the image itself. Essentially, I want to transform ...

What is the correct way to include '?i#efix' in a font directory path in a Rails application?

It is suggested to include ?#iefix at the end of .eot font paths in order to resolve another erratic behaviour issue in IE: @font-face { font-family: 'MyWebFont'; src: url('webfont.eot'); /* IE9 Compat Modes */ src: url(&ap ...

Emphasize specific dates on the jQuery UI calendar using a range selector

I am looking for a jQuery UI calendar that has an inline date range selector and the ability to highlight or disable specific dates. You can find my code example here Currently, I have a range selector set up, but all dates appear normal. What I need is ...

Troubles with Sizing in Twitter Bootstrap Modal

One issue I have encountered with my application is the varying widths of modals. Setting the width inline works well, but when the window is compressed it can cause part of the modal to be off screen, rendering it useless at smaller resolutions such as th ...

Is it possible to create a CSS-only negative border radius?

I'm reaching out for help with a specific issue. I want to create a unique negative border radius for my navigation links, similar to the design shown in this image: http://prntscr.com/9vi0b5 Instead of using images like in the example, I'm look ...

various types of font colors within the text box

If I have a text box with the content "eg. 'one'" Can I set the font color of "eg." to color:black and "'one'" to color:red? Below is the HTML code: <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" Text="eg.-one"> ...

Attempting to embed an image within the datepicker input

After successfully implementing the datepicker to select a date, I decided to add a buttonImage option for opening the datepicker on image click. However, I encountered an issue when trying to place my calendar image inside my input element, floated to th ...

I am noticing that "display: none;" is somehow being inserted automatically when the page renders. Where should I investigate to identify the root cause of this issue?

I have encountered an issue with a form element (a checkbox) that I integrated into a UI form. When the page loads, the Chrome F12 debugger indicates that display: none; has been applied as a style to the element: element.style { display: none; } This ...

The loaded sprite file is currently unused and a duplicate request is being sent for it

I have a CSS code snippet that looks like this: .is--stratus .icon__nav-threats.is--gray { background: url('/resources/img/navIcons_RiskInsight.png') no-repeat -1px -41px; width: 30px; height: 30px; } .is--stratus .icon__nav-thr ...

The scaling of the slick carousel image is not working as expected

Having an issue with the Slick carousel where images are not resizing based on browser size (original size is 300x228px). Just to clarify: When I shrink the browser window, the number of slides decreases but the images remain the same size. I've bee ...

Is there a way to customize the checkbox styles and icons in Material-UI React when hovering over them?

Incorporating material-ui into my current project, I've customized a Checkbox to display in red color. My goal is to have the checked Icon appear only when a user hovers over the Checkbox. However, it should remain hidden when not hovered. Unfortunat ...

Tips for positioning icons on the navigation bar using Bootstrap: How to display the icon on the right side and at the top of the

I am trying to position the arrow icon on the right side of the navigation bar, currently it looks like this: https://i.sstatic.net/35wef.png But I want it to look like the screenshot below: https://i.sstatic.net/lvvAM.png <div id="sidebar-wrapper"& ...

Button with CSS accordion menu

Seeking assistance as I am new to web design and experiencing an issue with my accordion button. I am trying to implement an animation that will display my <ul> when clicking within the .container element. The animation works fine, but I am having tr ...

Having issues with ::before pseudo-element, how can I style it differently based on the content of the <div>?

Assuming the following HTML elements: <p>Paragraph.</p> <div class="note">Naked Note Div.</div> <div class="note"> <p>Paragraph in Note DIV.</p> <p>Second Paragraph in Note DIV.</p& ...

Adjust the size of the font for the placeholder text

I've been attempting to adjust the font size of the placeholder text. I added the font size property to the listed classes below, but for some reason, it's not taking effect. Could you please advise me on how to resolve this issue so that I can ...

Having trouble with your jQuery dropdown menu? :)

I am currently developing my own drop-down menu and below is the code that shows hidden sub-menus: jQuery('ul li').hover(function(){ jQuery(this).children('ul').stop().show().animate({ opacity: 1 }); }, function() { jQuery(this).ch ...

HTML Techniques: Flipping the Script - Writing Characters from Right to Left

In order to showcase the temperature in degrees Celsius, I have implemented a placement technique using absolute positioning. Consequently, the °C symbol will persist in its designated spot. To achieve the desired presentation, the text should be displaye ...

Aligning Text Vertically with an Image in ul/li List Items

Apologies if this question has already been asked, but I've checked several similar inquiries in an attempt to merge the solution without much luck. I currently have a bootstrap row with two col-md-6's. The first column contains an image, while t ...

Issue with navigation menu button functionality on mobile devices

Whenever I attempt to access my website on a small device, the bootstrap navbar is supposed to collapse. However, the button doesn't seem to be working. Any assistance would be greatly appreciated as this is the one issue I'm struggling with. I&a ...