Add distinct margins to the initial and final elements that are not related

In my HTML, I have a certain type of box with a margin: 10px; applied to all of them. These boxes are displayed in a row on the page using Bootstrap, and I'm trying to figure out how to remove the left margin from the first element and the right margin from the last element. The challenge is that the elements are not siblings and do not have a common parent. Here's an example of the HTML structure:

<div class='container'>
  <div class='col-md-2'>
    <div class='MY-CUSTOM-BOX'>
    </div>
  </div>
  <div class='col-md-5'>
    <div class='MY-CUSTOM-BOX'>
    </div>
  </div>
  <div class='col-md-5'>
    <div class='MY-CUSTOM-BOX'>
    </div>
  </div>
</div>

While :first-of-type applies to all boxes, there's uncertainty about how to approach :first-child because of the nested divs. Any suggestions on how to tackle this issue?

Answer №1

If you're looking to achieve this, consider using a combination of css selectors like the >, :first-child, and :last-child

:first-child > .MY-CUSTOM-BOX {
  margin-left: 0;
}

:last-child > .MY-CUSTOM-BOX {
  margin-right: 0;
}

This approach targets direct descendants of first and last child elements containing MY-CUSTOM-BOX. It works well when boxes are at the same level within their parent structure (e.g., container -> div -> MY-CUSTOM-BOX).

You could also reverse the order for potentially better outcomes depending on your nesting levels:

.container > :first-child .MY-CUSTOM-BOX {
  margin-left: 0;
}

.container > :last-child .MY-CUSTOM-BOX {
  margin-right: 0;
}

In this scenario, the first and last children of container will have any nested MY-CUSTOM-BOX elements with zero margins.

See a demonstration of both methods here: https://jsfiddle.net/ttakchr1/

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

Is it possible to connect ng-model with a style tag?

Is it feasible to create a basic template editor using angularjs where an input field with an ng-model can be connected to a style tag to control the css on an entire page rather than applying it to a specific element with inline styling? Could something ...

Vue - Embedding <div> elements in a recursive manner

I am attempting to recursively add a div element that includes a button in the following manner: <div v-for= "column in 25": key="column.id" class ="columna"> <div v-for="row in 10": key="row.id" ...

Adjust the ARIA value of a Bootstrap progress bar within a VueJS component on the fly

During the development of my website using VueJS and Bootstrap, I made the decision to utilize Vanilla Bootstrap instead of a specific VueJS framework like VueBootstrap or VueStrap. Currently, I am facing an issue while building a custom component that wr ...

Troubleshooting the non-functioning addEventListener in JavaScript

I am facing an issue where a function that should be triggered by a click event is not working and the console.log message does not appear <script src="e-com.js" async></script> This is how I included the javascript file in the head ...

What is the best way to apply one JavaScript code to two separate HTML elements?

I am currently experiencing an issue where only the first mp4 file is working properly, while the second one does not. I am considering removing the stylesheet part as it is proving to be difficult for me to handle. I lack the skills to implement other sol ...

Moving draggable items out of a div and placing them into a designated droppable area

I'm facing an issue with dynamically creating draggable "divs" within a "table" and trying to make them drop into multiple droppable areas. The problem is that when I drag the element, it leaves a gap and upon dropping, it lands in multiple places ins ...

The functionality of scrolling ceases to work once the bootstrap modal is closed

I am utilizing Bootstrap v3.1.1 to showcase a modal popup on one of my web pages. The following code shows how I have implemented it. <!--Start show add student popup here --> <div class="modal fade" id="add-student" tabindex="-1" role="dialog" a ...

Is there a way to automate tasks after successfully entering my credentials on the login page and being redirected to a new URL?

Recently, I set out to develop a straightforward program that would launch a Chrome website, navigate to my router's settings page, and adjust the internet speed based on my input. I coded this in Python 3.9 and incorporated the Selenium library for a ...

Looking for ways to ensure React is responsive and feeling a bit challenged by the React mentality?

I'm still getting the hang of ReactJS, and I've been facing some challenges with making React components responsive. For my app, I am utilizing react-tabs. It works well when the screen is wide, but I need to switch to a hamburger panel layout w ...

What is the best way to fill in the jquery treeselect widget?

I'm struggling with populating the jquery treeselect widget using a json file or any other method. Since I am new to jquery/javascript, I'm sure I must be missing some basics. Although I have obtained the plugin from https://github.com/travist/j ...

Initially, my PDF file does not get selected, except in the Internet Explorer browser

I am currently facing an issue with selecting PDF files in Internet Explorer. The process works smoothly in Google Chrome, but I encounter a problem when trying to select PDFs in IE. Specifically, when I attempt to select a PDF for the first time in Inter ...

What is the method for adjusting the border color of an ng-select component to red?

I am having an issue with the select component in my Angular HTML template. It is currently displaying in grey color, but I would like it to have a red border instead. I have tried using custom CSS, but I haven't been able to achieve the desired resul ...

What could be preventing this code from automatically scrolling to the designated class when a key is pressed?

When a key is pressed on the document, the default action is prevented and the element with the ID "show-all-win" is animated to scroll to the position of the element with the class "key_" plus the key code of the pressed key. This animat ...

Creating consistent widths for drop downs and text boxes in Bootstrap

I'm currently using VB.Net MVC, Razor, Bootstrap, and Visual Studio 2013. My clients have requested that the dropdowns match the width of the text boxes. Here is how I create my dropdowns: @<div class="row"> <div class="col-md-4"> ...

This is an alignment of the background image ('relative') to the right side

I am facing an issue with the header section of my website. The background-image is styled using the following CSS: .header { position: relative; min-height: 436px; background: url("../img/holland-canal.jpg") no-repeat; background-size: co ...

Transform a <ul> into an <ol> format (replacing bullets with numbers)

This question presents a challenge, but there may be a solution waiting to be uncovered. Presently, I am utilizing the Kendo UI panelbar for developing expandable lists. However, an issue surfaces when employing an <ol> as the sublist - in this scen ...

Using jQuery, it is possible to eliminate a div element without affecting its contents

<div class="a"> <div class="b"> <ul> <li>Element A</li> <li>Element B</li> </ul> </div> </div> How can I eliminate div class="b" solely? I require the presence of div a, u ...

The Javascript function I created references a variable that seems to be undefined in the code

Recently, I discovered a function that is triggered with an onclick event using "openNav()" from an HTML element. Below is the code snippet for this function: function openNav() { document.getElementById("nav-drawer").classList.add("nav-drawer-open"); ...

The CSS Grid does not align properly in the horizontal direction when displayed on mobile devices

I am currently working on a responsive layout where the header and footer should each take up around 5% of the screen space and remain fixed. The middle section should scroll based on the number of elements within it. Despite using the fr and % values, the ...

Default behavior in Fullcalendar 6 allows for rendering links on both days of the month and weekdays

Is there a way to customize the rendering of days and weekdays in Fullcalendar React? Currently, they are displayed as links by default (<a>{dayContent}</a>), but I'm looking to have them rendered as <div> or <span>. Any sugges ...