'Dynamic' styling in Next.js using conditional CSS

I am exploring ways to style elements based on their parameters. I have been searching for terms like "conditional css react" and "implementing conditional styles in react with css".
Here is a snippet of my code:
e.g.

<div class="ChatComponent_chatText__n2g6S">
    <span class="ChatComponent_message__e9Kqh" data-author="me">test me</span>
    <span class="ChatComponent_message__e9Kqh" data-author="other">test others</span>
</div>

And the accompanying CSS:

.message {
    background-color: #eef5f8;
    padding: 0.5em;
    border-radius: 10px;
    flex-grow: 0;
    border-bottom-left-radius: 0;
}

.message [data-author="me"] {
    background: linear-gradient(to right, #363795, #005C97);
    color: white;
    -webkit-align-self: flex-end;
    -ms-flex-item-align: end;
    align-self: flex-end;
    border-bottom-right-radius: 0 !important;
    border-bottom-left-radius: 10px !important;
}

Check out the rendered image below:
https://i.stack.imgur.com/G43DX.png

Answer №1

Upon closer inspection, it became apparent that the issue stemmed from the space between the css selector and the parameter. The corrected code snippet is as follows:

.message {
    background-color: #eef5f8;
    padding: 0.5em;
    border-radius: 10px;
    flex-grow: 0;
    border-bottom-left-radius: 0;
}

.message[data-author="me"] {
    background: linear-gradient(to right, #363795, #005C97);
    color: white;
    -webkit-align-self: flex-end;
    -ms-flex-item-align: end;
    align-self: flex-end;
    border-bottom-right-radius: 0 !important;
    border-bottom-left-radius: 10px !important;
}

The resulting image can be viewed below:
https://i.stack.imgur.com/q65VG.png

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

What is the best way to upload an object in React using fetch and form-data?

Currently, I am facing an issue where I need to send a file to my express app as the backend. The problem lies in the fact that my body is being sent as type application/json, but I actually want to send it as form-data so that I can later upload this file ...

Maintain the scrollable feature of the element until the final content is reached

I'm struggling to come up with the right keywords to search for my issue. I've tried using Google, but it seems my search terms are not effective. The problem I'm facing involves two relative div elements with dynamic content. This means th ...

Encountering a React-timestamp problem with the error message "Unable to locate 'react' in the node modules directory."

My journey of creating an App using React was going smoothly until I decided to install react-timestamp via npm for converting unix time (https://www.npmjs.com/package/react-timestamp). However, now I'm encountering a compilation error that states: ...

What's the best way to add margin or padding to an SVG image within a v-app-bar

Seeking assistance with a seemingly simple question that can help enhance my understanding as I dive into using vue/vuetify for a new front end project. I am starting fresh and utilizing v-app-bar. Below is a snippet from my app.vue file: <template> ...

The try/catch block in Node continues to propagate upwards

I'm struggling to capture errors during server-side rendering, especially since they reach the catch block but still cause an uncaught exception leading to a crash. Below is a simplified version of the function causing the issue: export default (htm ...

The JSON.stringify() method is overwriting the file object with a blank one

COMPLEXITY Hey everyone, I could really use some assistance in resolving this intricate problem. I am working on an application using a combination of Spring Boot v2.0.5 and React.js v15.6.2, ReactDom v15.6.2, React Bootstrap v0.32.4. The communication be ...

Ensure the navbar includes the .container class and insert an extra dropdown right next to the container without causing any misalignment

Desired output: Achieve two rows with a .container div, centered in the viewport and aligned with each other. The second row should contain the navbar within a .container. Additionally, add a non-collapsing dropdown at the end of the navbar row, outside th ...

Prioritize the first child in Flexbox by truncating its text before the second

I am working with a flex-box container named .search-bar, which contains two divs with text, labeled as .title. My goal is to make the second child take up any extra space available and only start ellipsizing when there is no more space left for the first ...

Tips for correctly specifying the theme as a prop in the styled() function of Material UI using TypeScript

Currently, I am utilizing Material UI along with its styled function to customize components like so: const MyThemeComponent = styled("div")(({ theme }) => ` color: ${theme.palette.primary.contrastText}; background-color: ${theme.palette.primary.mai ...

What steps do I need to take to extract the date and month from a single datepicker using the code below?

<div class="col-md-3 pull-left" style="padding:9px"> <input type="text" id="datepicker" class="form-control"> </div> The HTML and C ...

Why isn't the favicon functioning properly in Next.js 13?

export const metadata = { title: "Samantha's Portfolio", icons: { icon: "/portfolio-icon.png", }, }; Is there a reason why this code is not functioning as expected? I have tried changing the file extension to .ico but it ...

Testing next-i18next localization with useTranslation in Jest

Exploring testing libraries is always an interesting adventure. In my NextJS project, I'm utilizing next-i18next. We're incorporating the useTranslation hook with namespaces. During testing, a warning pops up: console.warn react-i18next:: You ...

How can you change the alignment of items from vertical to horizontal using Bootstrap?

I'm looking to create a responsive layout similar to the design of duckduckgo.com. This page features a logo, the DuckDuckGo brand name in text form, and a search box. When I reduce the width of the browser window to nearly horizontal bar size, the th ...

What are some ways to utilize the pseudo elements ::before and ::after within the Material-UI framework?

Can someone please explain how to use the pseudo-element method in MUI? I couldn't find clear instructions in the documentation, so I decided to post a question here. The docs mention the following about pseudo elements: The box-sizing property is ...

Experimenting with creating collapsible panels using Bootstrap/CSS can result in various outcomes

, I am in the process of constructing a collapsible sliding left panel using bootstrap 5. The progress has been notable, but I am encountering some hurdles. Upon reviewing the code at this link: https://jsfiddle.net/dizzy0ny/86wjas9L/108/, you will witness ...

Tips for creating a dynamic variable for an object key in jQuery to streamline your code

Utilizing the TweenMax library, I am adding styles to a div while scrolling. However, I am facing a challenge where I need different styles based on varying page resolutions. To address this issue, I implemented an if condition. Is there a way I can use a ...

Error message: It seems that key store is an object, however, it is

I encountered an issue while trying to integrate RNSecureKeystore into my React Native project. The error message I keep receiving is: "TypeError: undefined is not an object (evaluating _reactNativeSecureKeystore.default.set')". This error seems to be ...

What could be causing the constant redirection loop every time I log in?

While developing my application using the latest version of Next JS alongside shadcn/ui and Firebase, I implemented a Sign In feature as part of the authentication process. I utilized the signInWithPopup function to allow users to sign in and then automati ...

The incorporation of zoom disrupts the smooth scrolling capability of the menu

My landing page has a menu that scrolls users to the selected section. However, my client prefers the page at a 90% zoom level. To accommodate this request, I added the following line of code: body { zoom:90%; } Unfortunately, when I click on a menu o ...

Unable to display <IconButton> component from Material UI

Currently utilizing Material UI, I have the code snippet below: import IconButton from 'material-ui/IconButton'; <IconButton iconClassName="muidocs-icon-custom-github" tooltip="bottom-right" tooltipPosition="bottom-right" /> While testing ...