Setting a precise length for CSS borders in nested elements

I have a series of nested div-elements organized like this

<div class="main">
    <div class="sub1">
        <div class="sub2">
            Hello Worlds!!
        </div>
    </div>
</div>

They come with the following styles

.main {
    width:200px;
    border-bottom : solid 1px #0000FF;
 }

.sub1 {
    width:300px;
    border-bottom : solid 3px #00FF00;
}

.sub2 {
    border-bottom : solid 3px #FF0000;
}

Wondering if there is a way to make the border length of the "sub2" class 200px, matching that of the main class?

Here is the example: http://jsfiddle.net/1Lygjmvj/

Answer №1

To achieve this, the most effective approach is to simply assign a width of 200px to the .sub2 element. Since you are already aware of the width of your .main element, there is no need to complicate things.

Your CSS styles should be straightforward like so:

.main {
    width:200px;
    border-bottom : solid 1px #0000FF;
}

.sub1 {
    width:300px;
    border-bottom : solid 3px #00FF00;
}

.sub2 {
    border-bottom : solid 3px #FF0000;
}

However, if it's absolutely necessary to adjust the width based on the parent element due to its dynamic width, JavaScript will come to the rescue. If you are using jQuery, you can utilize this snippet:

$(function () {
    var elements = $(".sub2");
    for (var i = 0; i < elements.length; i++) {
        $(elements[i]).width( $(elements[i]).parent().parent().width() );
    }
});

This script will dynamically set the width of all elements with the .sub2 class to match the width of their parent element upon page load.

Answer №2

Experiment with the following:

#primary {
   max-width:150px !important;
   border-bottom : solid 1px #FF00FF;
}

#section1 {
    width:250px;
    border-bottom : solid 2px #FFFF00;
}
#section2 {
    width:180px;
    border-bottom : solid 2px #00FFFF;
}

Answer №3

To enhance the structure, you can insert an empty element below sub2 and transfer sub1's styles to it:

#section {
  width:150px;
  border-bottom : solid 12px purple;
}

#subsection2 {
  border-bottom : solid 10px orange;
}

#subsection3 {
  width:250px;
  border-bottom : solid 8px pink;
}
<div id="section">
  <div id="subsection1">
    <div id="subsection2">
      Greetings Earthlings!!
    </div>
    <div id="subsection3"></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

Items positioned to the left will not overlap

Having trouble with floating elements? Need to ensure that box 3 aligns under box 1 when resizing the browser window? Use this HTML template: <div class="box"> <p>box 1</p> <p>box 1</p> <p>box 1</p> & ...

Overlapping columns with bootstrap images

Struggling to make three images responsive using Bootstrap columns. While they appear responsive, the issue is that the images overlap, except for the last column which displays correctly. <div class="row"> <div class="col-sm-2 offset-sm-3" ...

A guide on how to align Hamburger CSS in the center, center text, and include a

For a while now, I've been attempting to achieve three specific things with a Hamburger menu, but so far, I've had no luck. This Hamburger menu is responsive, transitioning into an X symbol upon clicking and moving to the right of the page with a ...

Connection and labels are malfunctioning on handheld devices

There are two functioning links on the website, but they become unresponsive when accessed from a mobile device. The first link leads to a DayZ map at dayz.xam.nu, while the second link directs users to a shop at dayzmsk.dayzplay.ru. The issue seems to be ...

How can the color of glyphicons be adjusted within an AngularJS ng-class that is constantly changing?

I am currently facing an issue with my function called lessThan(a,b) that checks if "a" is less than "b" and returns a boolean value. I have added a glyphicon to a form to display a check mark when true and an X when false, but I can't get the colors ...

What methods can I employ to utilize CSS on a table row that contains a specific class in an HTML

Is there a way to make the CSS affect <tr class="header"> with the class of header? CSS table.table-bordered tr:hover{ background-color: #C0C0A9; } table.table-bordered tr { background-color: #C0C0D9; } Any suggestions on what I ...

image appears hidden by object displaying a YouTube video due to z-index property

I am attempting to overlay a png image on top of an object element that contains a YouTube video. See the code snippet below: <object> scripts... </object> <img src='src' style='position:absolute;top:-10px;z-index:99;' ...

CSS code to modify background color upon mouse hover

I am currently working on creating a navigation menu. Check out the code snippet here: http://jsfiddle.net/genxcoders/ZLh3F/ /* Menu */ .menu { height: 100px; float: right; z-index: 100; } .menu ...

Resetting the countdown timer is triggered when moving to a new page

In my current project, I am developing a basic battle game in which two players choose their characters and engage in combat. The battles are structured into turns, with each new turn initiating on a fresh page and featuring a timer that counts down from ...

Having trouble getting Bootstrap CSS to work with ASP.net?

After using the bootstrap alerts class, I noticed that the view is not what I expected - https://i.sstatic.net/HqAFV.jpg Instead of that, I tried - https://i.sstatic.net/hooZy.jpg To fix this issue, I included the reference under the head tag like so: ...

Changing the color of placeholder text in MUI 5 TextField

Looking to customize the text color and placeholder text color in my MUI TextField component to be green https://i.sstatic.net/NZmsi.png The documentation doesn't provide clear instructions, so I attempted a solution that didn't work: <TextF ...

My website is constantly showing HTML code instead of rendering properly

the text and my other content are appearing on the website. after passing through a function that formats it into lines and stanzas, this is how it looks: [ 'If you can talk with crowds and keep your virtue,', 'Or walk with K ...

Getting JSON with duplicate keys in JavaScript can be achieved by parsing the data using a custom function

I am attempting to retrieve JSON from a URL, but I have encountered an issue where the response object is removing duplicate keys. Is there a way to fetch the JSON without eliminating these duplicates? Below is my JavaScript code: $('document'). ...

What is the best way to position my inline-flex element at the bottom of the page?

I'm trying to understand how flex-inline works. I want to make the height expand to 100% and reach the bottom of the screen without overcrowding it with content. html,body{margin:0;padding:0} body{background:black;} #sidebar{ display: inline-f ...

Disappearance of Chrome scroll bar without overflow-y condition

The live version of the page can be accessed here. It is divided into three main parts: Left sidebar Centered content Right sidebar To ensure that the right sidebar is scrollable, I have used overflow-y: scroll; right: -17px; to hide the scrollbar. Bot ...

Issue with Tinymce Editor: Content does not refresh when clicking another button within the form

I have a form set up as shown below: <form method="post" action="example.com" id="form"> <textarea id="content">{{$content_from_database}}</textarea> <button type="submit">Update</button> </form> Within the form, I ...

Can data be pulled from parse.com using Objective-C and then displayed on a website?

I am currently using parse.com as a backend service for my iOS app and have hired someone to create a website interface using HTML and CSS. Despite parse.com offering different options for sharing data between the app and website, such as through a JavaScr ...

Two neighboring sections containing dynamic elements. One section automatically adjusts its size to fit the content, while the other allows

I need to display 2 adjacent boxes with dynamic content rendered using Angular. The Container should have the same height as Box1. The height of Box2 may vary due to its dynamic nature, but it should never be taller than Box1. If it does become higher, a s ...

Ways to align various paragraphs within a single Bootstrap row

Looking to include notes on the right side of each input field? Check out this example I put together using a bootstrap layout. <head> <meta charset="utf-8"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4 ...

Creating interactive labels in a histogram that update based on the user's input

Currently, I have operational code in place that takes input data and generates a histogram based on set thresholds. When the code is executed, the histogram changes dynamically as the threshold slider is adjusted. However, there seems to be an issue wit ...