The child's styling is conflicting with the parent's styling

Issue with Parent and Child Component Margins

import "../src/App.css";
import CardComponent from "./components/CardComponent";

function App() {
  return (
    <div className="App">
    <div className="card"></div>
      <CardComponent />
    </div>
  );
}

export default App;

function CardComponent() {
  return (
    <div className="card-component-wraper">
      <div> FAQs </div>
      <div>
        <div> What is Frontend Mentor, and how will it help me? </div>
        Frontend Mentor offers realistic coding challenges to help developers
        improve their frontend coding skills with projects in HTML, CSS, and
        JavaScript. It's suitable for all levels and ideal for portfolio
        building.
      </div>
    </div>
  );
}

export default CardComponent;

* {
  margin: 0;
  padding: 0;
}

.App {
  background-color: hsl(275, 100%, 97%);
  background-image: url("../assets/images/background-pattern-desktop.svg");
  background-size: 100%;
  background-repeat:repeat-x;
  width: 100%;
  height: 100vh;
}

.card-component-wraper {
    background-color: hsl(0, 0%, 100%);
    width: 20%;
    height: auto;
    border-radius: 10px;
    margin-left: 40%;
    margin-top: 10%;
}

Answer №1

To make sure that the margin only affects the CardComponent itself, you can use absolute units such as pixels or try a different method to apply margin solely to the child component without affecting the parent. Here's how you can do it:

.card-component-wraper {
/* Other styles */
position: relative; /* Required for absolute positioning */
width: 20%;
height: auto;
border-radius: 10px;
margin-left: 40%;

}

.card-component-wraper > div:first-child {
/* Add margin to the first child div within CardComponent */
margin-top: 20px; /* Edit this as necessary */

}

In this adjusted CSS code, the margin-top is specifically applied to the first child div inside CardComponent. Feel free to adjust the margin value (20px in this example) according to your layout requirements.

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

Using ReactJS to dynamically load data based on the previously loaded data

On my ReactJS page, I am using axios to fetch a list of IDs and image URLs from an API, storing this data in the component's state. For example: [ {id: 21321321, img_url: }, {id: 97777798, img_url: } .... ] Once the data is retrieved, I display the ...

Exploring the method of extracting multiple checkbox values from an HTML form

While I'm aware that jQuery can be used to retrieve checkbox values when there are multiple checkboxes, the solutions available online don't seem to work for me. This is because my checkbox inputs are embedded within an HTML form, and none of the ...

Defining JSON Schema for an array containing tuples

Any assistance is greatly appreciated. I'm a newcomer to JSON and JSON schema. I attempted to create a JSON schema for an array of tuples but it's not validating multiple records like a loop for all similar types of tuples. Below is a JSON sampl ...

Mobile browser not resizing window width in fluid layout on WordPress site

Currently, I am working on developing a basic web layout that can adapt to mobile view. I have made some progress which you can check out here. The layout successfully adjusts when the window is resized in Firefox or Chrome. However, when I tried to acce ...

Enhancing Material-UI component [TreeView] with drag and drop functionality using react-dnd

I have encountered an issue with my TreeView implementation. Whenever I try to drag and drop a TreeItem, all of its child TreeItems move along with it. Is there a way to enable drag'n'drop functionality for just one TreeItem without affecting its ...

What is the starting point of a Next.js application?

When deploying a nextjs app on c-panel hosting, it requires the entry point of the application which is typically set as app.js by default. In a regular React application, you have full control over this aspect, but with nextjs, it's not always clear ...

I'm unsure of the most efficient way to condense this statement

$(document).ready(function(){ if ($(window).width() <961){ $('.item').on('click',function(){ /*---do something---*/ }) }else{ $('.item').on('click',function(){ ...

Setting environment variables for a ReactJS app that has been deployed on a server - a step-by-step guide

I have a unique query that hasn't been addressed in previous discussions about environment variables. I am well-versed in setting environment variables during the ReactJs app development process. After the app is compiled, I receive the static files a ...

Looking for a discreet JavaScript-based text editor with advanced features?

Our CMS has been using the now-unsupported RichTextBox control for a while, and we want to find a lighter-weight alternative with better cross-browser support. Initially, we were considering different ASP.NET components, but I am starting to think that an ...

What is the most effective method to implement a toggle function using only JavaScript, without any reliance on jQuery?

Recently, I had a navigation bar in the form of an unordered list (ul) with multiple list items (li), and the last li element was labeled 'more' which had its own sub-menu in the form of another ul. To toggle this sub-menu between visible and hid ...

The foundation gem is encountering an issue with incompatible units: 'rem' and 'px'

After installing the foundation gem version 5.5.2.1, I encountered an error when starting the application: Showing /home/user/Scrivania/sites/store/app/views/layouts/application.html.erb where line #9 raised: Incompatible units: 'rem' and &apos ...

Adjust the height of a div according to the width using CSS in a progressive manner

I'm looking to create a fluid container that adjusts its height gradually based on the width of the window (as the div's width is set to 100%). Similar to the behavior achieved with the aspect-ratio CSS rule, I want the height to smoothly increas ...

The <img> tag needs to dynamically adjust its size based on the width of its parent element, while ensuring it does not exceed its original dimensions

I spent hours working on my tribute page project on codepen and managed to pass 9 test cases. However, no matter what I try, I just can't seem to pass the last test case. My CSS code: #img-div { display: block; filter: grayscale(100%); width: 1 ...

The issue with Extjs store.proxy.extraParams being undefined appears to only occur in Internet Explorer

I currently have an ExtJs store set up with specific configurations. var fieldsStore = new Ext.create('Ext.data.Store', { model : 'FieldsModel', proxy : { type : 'ajax', url : 'queryBuilder_getQueryDetails', ...

Unable to Change Navbar Color

Presented here is the code snippet for the navbar located in app/assets/views/elements: <nav class="navbar navbar-default" role="navigation"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> ...

Tips for capturing text input from a Quill rich text editor div

My attempt to retrieve the content entered in Quill editor's editor div using jQuery codes is not proving successful. Although it works for other text inputs, it fails to do so for the editor div. Below is a demonstration of the issue: $(function() ...

Exploring the combination of Tailwind CSS variants with Framer Motion capabilities

Is it possible to integrate tailwind css classes with framer motion objects effectively? const variant = { hidden: { 'w-0' }, visible: { width: 400, transition: { type: 'spring', stiffness: ...

Discovering the art of incorporating various color palettes within a single stylesheet using HTML

I'm curious about incorporating multiple color schemes into a single stylesheet that can be activated by clicking, similar to the functionality found in platforms like WordPress and Magento. These platforms offer themes with various color options avai ...

Troubleshooting: CSS Selectors Hover Functionality Not Functioning as

I am having an issue where the styling does not change the display to block when hovering. Any insights or feedback would be greatly appreciated. <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="t ...

Unable to display Bootstrap 5 modal

I am currently in the process of constructing a basic webpage that includes a navigation bar and a table. The table rows are being dynamically generated through a simple JavaScript script that fetches data asynchronously from a database. I opted to utilize ...