Lists within lists and the use of percentages

There is a common belief that separating design and functionality is beneficial. Let's focus on the separation between HTML and CSS, specifically when organizing a menu hierarchy.

The issue with nested <ol>, <ul>, and <li> elements is that styling them based on percentage in a responsive design setting can be counterintuitive.

The Challenge:

I have a multi-layered list structure like this:

<ul>
  <li>
    <a>Item 1</a>
    <ul>
      <li>
        <a>Submenu 1</a>
          <ul>
            <li>
              <a>Submenu 1 (1)<a>
            </li>
            <li>
              <a>Submenu 1 (2)<a>
            </li>
            <li>
              <a>Submenu 1 (3)<a>
            </li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>

If I want to style this list relative or absolute to the body for responsiveness, I would start by styling the top ul element to flow down the rest of the list:

body > ul{

  position:relative;

  width:20%;

}

This works well initially. However, if I need to give absolute positions or relative widths/heights to deeper nested <li> elements, they will inherit the styles from the top-level <ul> (because it was defined as relative). This means each item needs unique styling calculations to maintain consistent sizing relative to the window. The subsequent lists cannot simply use the same percentage styling as the parent—they require customized adjustments.

This approach goes against the core idea of separating form and style. HTML should dictate data relationships, while CSS determines visualization and offers flexibility in how forms are presented.

My Queries:

  1. Is there a way to disregard a parent coordinate space set as fixed, relative, or absolute so that 100% equates to 100% of the body?

  2. Am I overlooking fundamental principles in the HTML/CSS relationship and its effective utilization?

  3. Will pursuing web development lead me to pulling out all my hair by age 35?

Note:

I seek an answer that brings peace, not just a temporary browser fix. If the solution is "it can't be done," please explain why and perhaps reflect positively on this limitation.

Answer №1

It's fascinating to see the challenges you're tackling with your project. While I haven't personally encountered a similar situation before, your insights are thought-provoking. It seems there may be some misunderstanding regarding how inheritance works in CSS. Simply applying relative positioning to an element doesn't change how its child elements derive their widths - percentages always refer back to their parent element's width. Embracing the nuances of CSS can indeed be like finding inner peace, and delving deeper into web development might just lead to pulling out hair! Perhaps investing in a stress ball could help ease the frustrations. Height calculations in CSS can be particularly tricky and unreliable.

When using percentages, it's crucial to be precise in your styles to avoid unintended consequences like font sizes shrinking. Placing percentage-based styles at the end of the styling hierarchy within the node tree and keeping more general styles higher up can prevent issues with parent-child relationships.

With careful planning, most layout requirements can be met, although it may require some strategic thinking at times.

Regarding relative and absolute positioning, nested elements will always base their position on the nearest ancestor with relative or absolute positioning. Unless utilizing JavaScript for dynamic calculations, this behavior is inherent in CSS.

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

Merging HTML Array with jQuery

I am working with input fields of type text in the following code snippet: <input type="text" minlength="1" maxlength="1" class="myinputs" name="myinputs[]" > <input type="text" minlength="1" maxlength="1" class="myinputs" name="myinputs[]" > ...

The information about the property is not appearing on the screen

I included the following CSS: nav label:AFTER { content: " ▾"; } However, when viewed in Chrome, it appears like this: content: " â–¾"; I have already added <meta charset="utf-8"/> to my JSP page and @CHARSET "utf-8"; in my CSS file. ...

The value in the textarea will stay unchanged even after the form has been submitted

I recently resolved my previous issue, but now I am seeking advice on how to prevent a textarea from clearing its input after submitting a form. You can view the jsFiddle here: http://jsfiddle.net/rz4pnumy/ Should I modify the HTML form? <form id="for ...

Obtaining a CSS element again to be utilized for boundary checking purposes

Is it possible to retrieve a CSS element? I recently found myself in a situation where I needed to add some code to an Adobe Edge project, specifically involving the manipulation of margin-top and margin-left properties. While experimenting with moving an ...

I found it challenging to select a particular choice using Selenium in Python

My goal is to use Selenium and Python to select the "Yazar" option within this particular HTML tag. However, I have encountered a problem where I am unable to successfully click and choose the "Yazar" option. Can anyone provide guidance on how to achieve t ...

How can you align an icon to the right in a Bootstrap4 navbar while keeping it separate from the toggle menu?

Looking to utilize the Twitter Bootstrap framework for structuring my navbar with distinct "left", "middle", and "right" sections, where the middle portion collapses beneath the navbar-toggler (burger menu) when space is limited. For a self-contained exam ...

eliminate the offspring of a component (chessboard)

Hey there! I'm currently working on developing a chess game and I could really use your expertise to help me solve an issue. In my code, when I try to move a piece in the game, this is what happens: 1. First, I remove the existing piece from its cu ...

Using jQuery to dynamically insert an HTML element into a table

I'm trying to insert a hyperlink into an empty HTML table. This function works perfectly fine on Firefox and Chrome, taking only 0.2 seconds to load. However, when I try it on IE9, it takes at least 3 minutes to display the link. The issue seems to be ...

Spring REST service prevents Cross-Origin Requests with AJAX

Having Trouble Accessing Spring REST Service My spring service @RequestMapping(value = "/MAS/authenticate", method = RequestMethod.POST) public ResponseEntity<Map<String, String>> authenticate(@RequestBody Subject subject) { Map<String ...

Refresh the Google Maps location using GPS coordinates

Currently, I am working with an Arduino that has a GPS chip and processing NMEA strings with Python. I have an HTML file set to auto-refresh every x seconds to update the marker's position. However, I would like to update the position information with ...

What is the reasoning behind jQuery UI employing CSS classes as opposed to pseudo-classes?

In this detailed explanation found on this website, it is discussed why jQuery UI uses CSS classes like .ui-state-active that are applied through JavaScript, rather than utilizing existing CSS pseudo-classes like :active. What is the reasoning behind thi ...

Creating a personalized CSS class for TYPO3 menu links

I am currently in the process of creating a brand new website using TYPO3 6.2 and incorporating the Bootstrap Package. My goal is to be able to assign custom CSS classes to menu links directly from the backend, and then display these CSS classes within my ...

"Create a dynamic HTML newsletter utilizing tables similar to the responsive design principles

Looking to create a mobile-responsive newsletter using my style.css <table cellpadding="0" cellspacing="0" border="0" align="center" width="600" bgcolor="#fff"> <tbody> <tr> <td style="padding-bottom:5px" valign="top"&g ...

triggering a button click event in HTML

Below is the creation of a div: <div class="area1"> </div> By using AJAX, I was able to alter the HTML for the aforementioned div with this call: $('#abutton').click(function(e) { e.preventDefault(); ...

The Order of Transitions Reversed in CSS

I have a unique situation that I'm hoping to get some help with. I've been trying to create an effect where I move text away and lift up a gray box, and then when my mouse moves away from the gray box, the box goes down first before the text move ...

I am wondering how to use the value assigned to a variable's textContent as an argument for player input in my JavaScript code

i am currently developing a JavaScript project to create a user interface for my rock, paper, scissors game. Currently, the game only runs in the console and prompts the player for input. So far, I have implemented three buttons (one each for rock, paper, ...

ng-view is the culprit behind the website's fatal error

Encountering a "RangeError: Maximum call stack size exceeded" in the console while trying to recreate a basic routing example from w3schools. The crash seems to be linked to <div ng-view></div> in index.html. Despite making minimal changes from ...

CSS - sticky header causing issues when resizing the browser window

I'm currently facing an issue with the layout of my navbar. While I've managed to create a fixed navbar that stays at the top, the content on the page seems to be hidden underneath it. To address this problem, I attempted to add a 5% margin from ...

Using JQuery to Enhance the Highlight Effect: Tips and Tricks

Exploring the functionality of the "highlight" JQuery effect: http://docs.jquery.com/UI/Effects/Highlight You have the ability to modify the background color of any DIV element with a fade in/out effect. However, the provided example demonstrates trigge ...

Is the use of the auto image attribute height/width impacting rendering performance?

Currently, I am facing an issue with setting the height and width attributes for an image as I do not have access to its dimensions from the server. One solution I am considering is setting both the width and height values to auto. For example: img { ...