Is it considered valid in CSS to nest an ID within another ID, like for example "nesting #main inside #display img { ... }"?

When working with CSS, the code snippet

#main #display img { height: 80px }
targets all images within an element with the id display that is nested inside another element with the id main. However, there may be some confusion around whether this syntax is logical or valid since ids are typically considered to be globally unique identifiers.

A solution to this nesting dilemma can be found in SASS, where code can be organized hierarchically like so:

#main {
  width: 700px;

  #display {
    img {
      height: 80px;
    }
  }

This structure represents an "id within an id" relationship, which might make more sense conceptually for certain design scenarios.

An update: It's recommended to avoid situations where multiple elements share the same id value, as demonstrated here:

#main #display img { height: 80px; }
#sidebar #display img { height: 80px; }

In such cases, a better approach would be to simplify the selector by targeting the nested element directly:

#display img { height: 80px; }

Answer №1

Of course! When using one style sheet across multiple pages, there are situations where it can be beneficial ("for instance, if #display is nested inside #main, display images in a certain way, otherwise display them differently").

It's completely permitted without any issues.

Answer №2

This concept is logically sound. Although it may seem limiting, it could be exactly what you need.

For example, when applying the same CSS to numerous documents with varying structures.

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

FontAwesome symbols are not displaying on any chromium-based web browsers

I have been updating a theme from fontawesome 4 to 5, and everything seems to be working fine except for one specific icon that is displayed as a css pseudo-element. Even though I followed the guidelines in the FA docs, the icon still does not appear. It ...

Adjust padding for smaller devices in React using Material UI

In my grid layout, I have columns set to 3,6,3 and a spacing of 3 between them. On larger screens, the spacing between grids looks fine. However, as the screen size decreases, the spacing remains the same which is not visually appealing. What I am aiming ...

Creating a child div that is half the size of its parent

Is there a way for a child div to inherit half the width and half the height of its parent div without using specific values or viewport units? ...

What is the best way to implement a media query for changing styles on hover for an element

I am facing an issue with media queries where when I apply a hover effect to an element, such as a clickable link in the navigation bar, the new rules work as expected but also retain the default browser styles. For instance: nav ul li a:hover{ border:0; ...

Looking for ways to include CSS links in Sublime Text 2 program?

Currently, I am diving into the world of Sublime Text 2 and I must say, I am quite impressed with its capabilities. However, there are a few features that I miss from my previous editor (Microsoft Expression Web). One particular functionality I found extr ...

Creating a stylish border inside an image with CSS

Is it possible to add an inner line to all product images like shown in the example image?https://i.sstatic.net/EVIiM.png The HTML code I have is as follows: <div class="gallery"> <div class="picture"> <img id="main-product-img ...

Creating a Fixed Position Layout with Three Columns

Currently in the process of creating a 3-column website template and have been rearranging the divs to improve SEO. The issue I am facing is that the layout looks terrible when it's not fully loaded. Here is how it appears when it's not fully lo ...

What could be causing the pseudo elements to not show up on the divs?

I've applied a after pseudo element to divs, but when I check the debugger view, no pseudo elements are showing up. Even after looking at related posts, I still can't figure out why this is happening. I would really appreciate any feedback or c ...

Vuetify's data table now displays the previous and next page buttons on the left side if the items-per-page option is hidden

I need help hiding the items-per-page choices in a table without affecting the next/previous functionality. To achieve this, I've set the following props: :footer-props="{ 'items-per-page-options':[10], &apo ...

Ways to stop the click event from being triggered in JQuery

There is a single toggle switch that, when clicked, switches ON and a popup with close and OK buttons appears. However, if the toggle is clicked again, it switches OFF and disappears. The specific requirement is that with every click where the toggle switc ...

Utilizing CSS to create dynamic 3D cube transformations

Exploring the realm of CSS 3D transforms to showcase cubes and cuboids featuring cross-sections has been my current venture. In this endeavor, I'm aiming at compatibility with Chrome, Firefox, and MSIE 11. It has come to my attention that in order to ...

Whenever I attempt to execute gulp or gulp build locally, an AssertionError [ERR_ASSERTION] occurs, stating that the task "build" was never

Trying to execute gulp in my terminal is resulting in an assertion error. Despite uninstalling node and NPM, then reinstalling using brew, the issue persists. npm version: 6.13.4 node version: v12.14.1 Running MacOS Catalina 10.15.2 Error in Terminal g ...

Printing Strings in JavaScript IF Conditional Block

Hello there! I am looking for assistance with a JavaScript concept. Recently, I created some code where in the HTML file, there is a div element with an id of "GetID" where the string outputs are meant to be displayed. The JavaScript code looks something ...

Choose a unique text color

Having trouble with my HTML select form and modifying text color for specific options. In the provided example, I changed the color for the Tuesday option, but it only shows up when scrolling through the options. How can I make the color change visible for ...

Automatically scroll to each div or element on the page

Can anyone help me with implementing an auto scroll to each div or element when the user scrolls using the mouse? Here is the scenario: Imagine this structure of the page... <div id="main-wrap"> <div class="my-div-1"> <p>here goes ...

CSS: Centering images vertically within a fixed-positioned container, regardless of their dimensions

I am completely stuck on this problem. CSS and DIVs are new to me, so I appreciate your patience. My goal is to create a DIV-based image gallery with the following layout: <div id="container"> <div class="row"> <div class="img-container"&g ...

What is an alternative way to make elements overflow on top without relying on position: absolute when using overflow: hidden?

I'm currently exploring React and I'm interested in implementing an animation using react-collapsed to collapse the upper portion of a specific component. As of now, the container size is reduced to half of the child's size and overflow: hi ...

In search of the most optimal solution for converting PSD files to HTML/CSS

I am currently working on converting a layout into HTML/CSS, but I am facing challenges in determining the best way to structure the HTML/CSS for major content elements. For example, there is a different background block in the middle that should be under ...

The clickable areas for the href and onclick functions are extremely small and positioned inaccurately on the screen

The code below is supposed to load an image of a 'close window' button that should be clickable. However, when the page loads, the clickable area is only a couple of pixels wide and a pixel or two high, positioned just below or above the center o ...

Cannot locate the Django bootstrap files

Although I have set up Django with bootstrap by following tutorials, I am facing an issue where Django is unable to locate the static files even when I am replicating the steps shown in the tutorials. The structure of my Project is as follows: webshop ...