Can a child Div remove a section of the parent div's border?

Is it possible to use CSS alone to make a child div's border "override" or "remove" part of its parent's border? I want certain rows to have no border without changing the DOM structure.

Keep in mind that the innerNoBorder div has no background color.

This is an example of what I've tried (which did not work)--

Html:

<div class="outerBorder">
    <div class="inner">hello</div>
    <div class="innerNoBorder">world</div> <!--want this to remove/override outerBorder -->
    <div class="inner">!</div>
</div>

CSS:

.outerBorder {
    border: 2px solid black;
}

.innerNoBorder {
    border-left:none;
    border-right:none;
}

Check out the JSFiddle demo.

Answer №1

The border property does not inherit from the parent, so your attempt will not work... but it seems you already realized that.

You can achieve overlapping of the parent div by applying a negative margin to the child element. Keep in mind that you must also give the child a background-color for this to work effectively.

.innerNoBorder {
    background: yellow;
    margin-left: -2px;
    margin-right: -2px;
}

Check out the updated Fiddle here

Alternative Approach

Same concept, but with a white border:

.innerNoBorder {
    border: 2px solid white;
    border-width: 0 2px;
    margin-left: -2px;
    margin-right: -2px;
}

See the updated Fiddle for this alternative approach

Answer №3

Sorry, but that is not a viable solution.

Have you considered removing the left border from the .outerBorder element instead?

.outerBorder {
  border-left-width: 0;
}

Alternatively, you could try positioning the .innerNoBorder div to cover the left side of its parent by using the following CSS:

.innerNoBorder {
margin-left: -2px;
border-left: 2px solid white;
}

Answer №4

If you want to remove borders using pseudo elements, you can try the following method:

Here is the HTML code:

<div class="outerBorder">
    <div class="inner">hello</div>
    <div class="innerNoBorder">world</div>
    <!-- This will remove the border or override outerBorder -->
    <div class="inner">!</div>
</div>

And here is the CSS code:

.outerBorder {
    border: 2px solid black;
}
.innerNoBorder {
    border-left:none;
    border-right:none;
    position:relative;
}
.innerNoBorder:after {
    position: absolute;
    top: -50%;
    left: -2px;
    bottom: -50%;
    border: 1px solid white;
    z-index: 45478;
    content:"";
}

You can adjust the height of the "no-border" area by changing the top and bottom values in the :after pseudo element.

Check out the demo here: http://jsfiddle.net/lotusgodkk/bc6uLmhx/7/

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

Unlocking the power of setting dynamic meta content

When using EJS for templating in my node.js webserver, everything has been running smoothly except for one issue. After integrating the head file into a page, I encountered the following error: SyntaxError: Unexpected identifier in /home/runner/superstrap/ ...

Is there a difference in the functionality of CSS :invalid between Chrome and Firefox?

I am facing a challenge with styling my invalid elements in HTML forms. It is simple in Chrome, but Firefox seems not to recognize my :invalid pseudoclass. To see the difference, try opening the following code snippet in both Chrome and Firefox: <html& ...

Adjust the color and font style of the title text in the dialog box

I need assistance in altering the font and color of the title within a dialog box. I am looking to modify the font, size, and color. What steps should I take? Below is the code snippet: ivworknggroup.setOnClickListener(new OnClickListener() { ...

Hiding the Previous or Next Button on the First and Last Item in a Twitter Bootstrap Carousel

Is there a way to hide the previous button on the first item and hide the right button when the carousel reaches the last item? I've tried different solutions but couldn't get it to work. Any help in understanding how to implement this for a Boot ...

Minimize the vertical gap between menu items when they wrap onto a new line

When examining the screenshot of the website, you'll notice a considerable space between the first and second rows of menu items. I'm aiming to minimize this spacing as much as possible. The following CSS pertains to this issue: .gva-navigatio ...

Slider Footer Player for Rdio

I am currently working on replicating the interactive player footer design used by rdio (). When you click on the footer, a panel elegantly slides up to reveal additional content. Another example of this functionality can be seen on the website teehan lax; ...

Issue with displaying text and aligning content to the left or right in tables on mobile devices

Looking to design a table using Bootstrap and include some pull-left and pull-right content in a column? Check out this code snippet: http://jsfiddle.net/Zoker/sgdfgkvL/ <td> <div class="pull-left">Some content</div> <div cla ...

Issue with border rendering on triangles in CSS on IE8

I currently have a horizontal navigation bar with triangle borders inserted using :before and :after for each list item. This creates the illusion of a white bordered point on the right side of each list item. The issue I'm facing is that this design ...

Box Pattern with Pop-Up Modal

Check out the grid of squares I've coded below: <div class='square-container'> <div class='square'></div> <div class='square'></div> <div class='square'></div& ...

What is the best way to evenly distribute the elements of my navigation bar using percentage values?

Hello, this is my first attempt at creating a website and I am currently working on designing a navigation bar with evenly spaced list items and dropdown boxes. While I have successfully implemented the dropdown boxes, I am struggling to evenly distribute ...

"Recently, I included a function called 'test' in the code, but I encountered an error stating that it was not recognized as a function. Can someone provide assistance

Issue Detected A 'TypeError' has been thrown: ".goal_test".click is not defined as a function Feel free to collaborate on the code by clicking this link: Please note that my modified code is enclosed within asterisk symbols. ...

CSS fixed positioning involves positioning an element relative to the

My website includes a div positioned below the header that contains quick links. I have implemented the solution from http://davidwalsh.name/persistent-header-opacity to ensure this div remains on-screen at all times. However, I would like it to appear a ...

I am looking to showcase a series of icons linked together by connecting lines

I have successfully designed the layout and added icons, but I am facing difficulty in creating connecting lines between them. I attempted to utilize CSS borders and pseudo-elements, yet I cannot achieve the desired outcome. If anyone could offer a CSS-ba ...

Options for configuring an HTML form

Experience our dynamic product slider in action! Visit this link to see it live. Get ready to enhance your website with this HTML Code: <link href="css/style.css" rel="stylesheet"> <script src="js/jquery.min.js"></script> <script sr ...

What is the best way to include a specific stylesheet for Blackberry Curve devices based on certain conditions

Is there a method to include a custom style-sheet specifically for the 'Blackberry curve 2007'? Or perhaps implement a redirect that can identify this device and direct them to a different .html page? I'm looking to create a simplified versi ...

Could a css style be applied to a parent element based on the status of its child element?

In my specific context, this is the HTML code I have: <div class='table'> <div> <div *ngFor='let product of this.market[selectedTab].products | orderBy:"id"' class='itemlist' [ngClass]="{' ...

JavaScript causing issues with CSS transform translate functionality

const openBtn = document.querySelector('.open') const closeBtn = document.querySelector('.close') const navContainer = document.querySelector('.nav-container') openBtn.addEventListener('click', () => { openBt ...

Achieving a persistent footer at the bottom of the page within Material Angular's mat-sidenav-container while using the router-outlet

I am looking to keep my ngx-audio-player fixed at the bottom of the screen, similar to what you see on most music streaming websites. I currently have a structure with divs and various elements for dynamic content and playing music. The issue is that the ...

Having trouble getting CSS headers to work smoothly with iPhone?

After creating a web application with CSS, the initial GUI was exactly how I wanted it to be. However, within 3 seconds of loading the page, the GUI would change. The HTML code for my header is as follows: <div id="Slideshow"> <img src="image/s1 ...

The HTML code is failing to apply the style to all rows except for the first one in the sql query result

When retrieving all the rows from a SQL database with a query and using a loop to iterate through them, I encountered an issue where only the first row was receiving the specified CSS style. The remaining rows were not applying the style as expected. < ...