Can you explain the significance of the characters '& > * + *' in JSX (CSS)?

When using material UI, the progressbar demo includes lines of JSX code like this:

'& > * + *': {
      marginTop: theme.spacing(2),
    },

Can you explain what the selector '& > * + *' represents in this context?

Answer №1

That is known as the lobotomized owl technique. It allows you to select every element that has an immediately preceding sibling, excluding the first one within a parent.

ul > * + * {
  background: red;
  margin-top: 2rem;
}
<ul>
 <li>1</li>
 <li>2</li>
 <li>3</li>
 <li>4</li>
 <li>5</li>
 <li>6</li>
</ul>

As demonstrated in the code snippet, this technique adds margin-top and background styles to all li elements except for the first one.

It's useful for easily adding margin between elements without needing additional styling to remove margin from the first or last element.

This CSS feature is independent of React or JSX.

Learn more about it here, or watch a video tutorial on it here.

Answer №2

it acts as a selector to target all sibling elements.

<div class="wrapper">
    <p>1</p>
    <p>2</p>
<div>

The second paragraph has a background color of red.

Please note that the first paragraph does not meet the criteria.

.wrapper {
  width: 100%;
  height: 100%;
  font-size: 16px;
  & > * + * {
    background-color: red;
  }
}

In this context, & refers to the parent selector, the > symbol indicates the child combinator, and the + specifies the adjacent sibling combinator.

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

Learning how to integrate Next.js, React.js, and Redux into an HTML page for an enhanced user

My current project is built on Asp.Net MVC, but the technology used is not crucial. I integrated react.js and redux for searching a section of my html page using a cdn link. Now, I am considering deploying the server side of the application with next.js. ...

When inner pages in Next.js reload, they display the same content as the home page upon page refresh

During the development of our website using Next Js, we encountered an issue where the home page and inner pages load correctly locally and in the deployed build on AWS S3. However, when refreshing an inner page or opening it in a new tab, the content of ...

Dividing React Router into individual files

I am facing an issue while trying to implement routes in my react project with three separate components: Window.js import React, { Component } from 'react'; import SideBar from "../SideBar/SideBar"; import MainBody from "../MainBody/MainBody"; ...

When the progress bar is clicked, the background color changes and then changes back again

https://www.w3schools.com/code/tryit.asp?filename=FG1ZE0NJ4ZX7 https://i.stack.imgur.com/Bnd0k.png I have created a progress bar that resembles the screenshot provided. When I hover over it, the color changes to green. However, I am looking for assistanc ...

In React, retrieve the value of the previously clicked radio button when a new one is

My MUI radio buttons are set up to gather filter criteria for an object. After filtering the object, I display the filtered results in an owl carousel. However, I have encountered an issue where clicking on a radio button always selects the previously cl ...

Tips for creating a responsive Youtube embedded video

Check out this website for a good example: If you take a look, you'll notice that the header youtube video is responsive - it automatically resizes when the window size changes. Here are the <iframe> codes: <iframe id="bluetube-player-1" fr ...

What is the reasoning behind having blank space between the buttons?

Can anyone help me understand why there is visible space between the buttons in the code example provided below? Despite removing margins and paddings, the whitespace persists. <body> <div class="button-container"> <button>1& ...

Sophisticated method in JavaScript to conceal and reveal div elements

My knowledge of front-end web development is strongest in HTML and CSS, but when it comes to JavaScript, I feel like there must be a more efficient way to achieve the functionality I want. On my website, I have a set of <li> items that, when clicked ...

React MUI: Dynamic and Adaptive sidebar

I'm currently working on integrating React Material UI: Persistent + Responsive drawer to create a responsive layout for mobile devices with a persistent drawer for larger screens. I've come across some code that almost fits my requirements, but ...

Div element remains fixed in width while zooming in

I'm encountering an issue with a centered div, where the width is set to 50% and the max-width is set to 100%. When I zoom in, the width remains constant but the height increases. Despite setting the max-width to 100%, why does this occur? And most im ...

What could be causing the state to continuously appear as null in my redux application?

Currently, I am in the process of developing a basic contacts application to gain expertise in React and Redux. The main focus right now is on implementing the feature to add a new contact. However, I have encountered an issue where the state being passed ...

Tips on transferring information to the graphical user interface

Here is my code snippet: signup.post('/signup', urlendcodedParser, async(req: Request, res: Response) => { const username = req.body.username; const password = req.body.password; const age = req.body.age; const email = req ...

Color Theme in Electron React with Material UI

Recently integrated electron and React with Material UI into my project. I am facing an issue with the color property in the app-bar component, as it is removing all colors and displaying it as white. Even though I copied the code from the documentation, I ...

Set a maximum height for an image without changing its width dimension

I am facing an issue with an image displayed in a table. The image is a graph of profiling information that can be quite tall (one vertical pixel represents data from one source, while one horizontal pixel equals one unit of time). I would like to set a ma ...

Uncovering the HTTP Cookie in Node.JS with JWT

Let me preface this question by saying it may sound repetitive, but I appreciate your patience in hearing me out: I'm currently tackling a project involving a React Frontend and a Node Backend. User authentication is managed using JWT tokens. However ...

Navigating the enum data model alongside other data model types in Typescript: Tips and Tricks

In my different data models, I have utilized enum types. Is it possible to compare the __typename in this scenario? enum ActivtiyCardType { Dance, ReferralTransaction, } type ActivityCardData = { __typename:ActivtiyCardType, i ...

Show two spans, each underneath the other

I am looking to showcase two span elements, one below the other. Take a look at my HTML code snippet <img class="profile-photo margin-0" data-ng-if="!question.isOpen" ng-src="{{question.profilePicId ? que ...

Customize your Material UI theme colors with SASS variables

I am exploring the idea of customizing the theme of Material UI in a dynamic way by incorporating SASS Variables that would update the Material UI theme automatically. When designing my page with Sass, I typically use variables to define colors: $primary ...

Enhance the brightness and add a subtle glow to an image inside a div element when hovering over the div

Is there a way to create an effect where the image within a div brightens when hovering over the entire div without affecting any other elements? Here is my code, any suggestions or improvements are appreciated. #textimg{ background-image: url( ...

Switching tabs in React using Material UI checkboxes causes the check mark to disappear visually

I've been struggling with this issue for quite some time - I have checkboxes (50) rendered within a tab, and after checking them, switching tabs, and then going back, the value is stored but the checkbox loses its visual appearance. <Tabs value={se ...