What methods can be used to alter the background color of the body without impacting the surrounding div elements?

Struggling to find the right CSS selector to change the color of the outer div? I've tried body:not(.wrapper .group) but it's not working.

Any ideas on how to change the body color without causing any issues?

This is what I have so far:

body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #8e44ad;
  color: white;
}

/*body:not(.wrapper .group) {
  background: white;
}*/

.wrapper {
  margin: 50px 20px 50px 20px;
  /* background-color: chartreuse; */
  border: 2px solid #fff;
  border-radius: 5px;
    /* background: #8e44ad;
    color: white; */
}

h1 {
  margin: 0 0 1em;
}

.group {
  display: flex;
  align-items: center;
  margin-bottom: 2em;
}

.newdiv {
  margin-top: 200px;
  display: flex;
  flex-direction: row;
}

...
<!DOCTYPE html>
<html lang="en">
  <head>
    ...
  </head>
  <body>
    ...
  </body>
</html>

Answer №1

Typically, all elements that are descendants of body (as well as most other elements) inherit its background property. To customize the background of the .wrapper element, follow o.v.'s suggestion. Check out the example in the jsFiddle link below.

https://jsfiddle.net/us3fct9w/

Answer №2

The .container div comes with a default transparent background, causing it to show up as purple.

Experiment with different background colors that complement your overall design scheme.

Answer №3

Adjust the margin property by moving it from .wrapper to body, changing it to padding. Next, transfer the background property from body to .wrapper and apply a small margin to .wrapper.

Modifications:

body{
   /* Remove background: #8e44ad;*/
   /* Add padding: 50px 20px 50px 20px;*/
}

.wrapper {
   /* Remove margin: 50px 20px 50px 20px; */
   /* Add background: #8e44ad; */
   /* Add padding: 20px; */
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  color: white;
  padding: 50px 20px 50px 20px;
}

body:not(.wrapper.group) {
 /* background: white;*/
}

.wrapper {
  background: #8e44ad;
  padding: 20px;

/* background-color: chartreuse; */
  border: 2px solid #fff;
  border-radius: 5px;
    /* background: #8e44ad;
    color: white; */
}

h1 {
  margin: 0 0 1em;
}

.group {
  display: flex;
  align-items: center;
  margin-bottom: 2em;
}

.newdiv {
  margin-top: 200px;
  display: flex;
  flex-direction: row;
}

.group1 {
  display: flex;
  flex-direction: column;
}

group3 {
  display: flex;
  flex-direction: column;
}

input[type="checkbox"],
input[type="radio"] {
  position: absolute;
  opacity: 0;
  z-index: -1;
}

label {
  position: relative;
  margin-right: 1em;
  padding-left: 2em;
  padding-right: 1em;
  line-height: 2;
  cursor: pointer;
}
label:before {
  box-sizing: border-box;
  content: " ";
  position: absolute;
  top: 0.3em;
  left: 0;
  display: block;
  width: 1.4em;
  height: 1.4em;
  border: 2px solid white;
  border-radius: 0.25em;
  z-index: -1;
}

input[type="radio"] + label::before {
  border-radius: 1em;
}

/* Checked */
input[type="checkbox"]:checked + label,
input[type="radio"]:checked + label {
  padding-left: 1em;
  color: #8e44ad;
}
input[type="checkbox"]:checked + label:before,
input[type="radio"]:checked + label:before {
  top: 0;
  width: 100%;
  height: 2em;
  background: white;
}

/* Transition */
label,
label::before {
  -webkit-transition: 0.25s all ease;
  -o-transition: 0.25s all ease;
  transition: 0.25s all ease;
}
     <div class="wrapper">
      <h1>Pure CSS Fancy Checkbox/Radio</h1>

      <h2>Radio Buttons</h2>
... (truncated for brevity)
        <label for="q1r">Blood clots</label> <br />
      </div>
      
    </div>

Answer №4

After considering the advice of o.v & Jason Steinhauser about how changing the body could impact elements in the hierarchy, I decided to adjust my design approach.

Big thanks to all of you for your help! This feedback is invaluable, especially during those long coding sessions when my mind gets foggy!

body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #fff;
  color: #15c39a;
  font-family: "Gotham Light", "Futura Bold Italic";
  font-weight: 100;
}

@font-face {
  font-family: "Gotham Light";
  src: url("Fonts/Gotham/Gotham-Light.otf");
  font-style: normal;
  font-weight: 100;
}
/* Rest of the CSS code remains unchanged */
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
   /* HTML markup with radio buttons and checkboxes goes here */
  </body>
</html>

Answer №5

Your z-index is causing an issue that needs to be addressed.

One solution would be to assign a value of z-index: 2; to your label element.

To better illustrate the problem, here is a screenshot showing the discrepancy in results: https://i.sstatic.net/2Cvda.png

body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background:white;
  color: white;
}
/* CSS continues... */
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="wrapper">
      <h1>Pure CSS Fancy Checkbox/Radio</h1>
      /* HTML structure and elements continue... */
    </div>
  </body>
</html>

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

I noticed that my CSS style sheet is only working on three of my pages. What should I do to make sure it affects the fourth page as well?

I am currently facing an issue with my website where I have linked one .css stylesheet to 3 different pages. This is just a temporary template until I finalize the individual look of each page. After performing some div positioning on the site, most pages ...

Is it possible to have multiple validation messages in an html5 form using Symfony2?

Exploring the advantages of utilizing html5 validation in Symfony2 forms has been enlightening. Through my online search, I came across an excellent solution: Enhancing Form Validation messages on Symfony2 However, this method seems to provide only a s ...

Tips on how to target the center of a slider thumb with a pointer using CSS and jQuery

One of the features I have implemented is to display the price range and the most repeated price (Mode). In order to illustrate this, I used the range input type but deactivated it so that users cannot move it. Below you will find my HTML, CSS, and JS cod ...

CSS for dynamic styling of anchor tags

On the example.com website, I have added the following HTML (Very important note). Now, I am attempting to add CSS to a selected link in order to highlight the chosen value. <a class="value" href="http://example.com?filter=value1">Value1 <a class ...

Problem with Bootstrap Dropdown positioning on mobile devices

I have encountered a puzzling issue with our web application. To demonstrate the problem, I have simplified the page to its bare minimum. You can access it here: Everything seems to be working fine on desktop browsers. However, when I test the page on a m ...

What is the reason for the input type number in the <input> field being passed as a string?

I am currently developing a straightforward component that allows the user to input the balance they wish to add. Here is the template used for taking input from the user: <h3>Add Balance:</h3> <input #payment type="number"> < ...

Can you explain how to incorporate a node module script into a React.js project?

I have encountered an issue where the element works perfectly fine when using an external node module, but fails to function properly when using a locally downloaded node module. Unfortunately, I am unable to identify the root cause of this problem. You c ...

What is the process for attaching a right ribbon label onto a card in Fomantic UI components?

I am completely new to Fomantic (and web development in general) and I'm attempting to create a website featuring a card with a ribbon label on the right side. I saw similar designs in the documentation where they used a segment instead. However, when ...

Deliver JavaScript and HTML through an HTTP response using Node.js

My attempts to send both a JavaScript file and an HTML file as responses seem to be failing, as the client is not receiving either. What could be causing the client to not receive the HTML and JavaScript files? I am using Nodejs along with JavaScript and H ...

Extracting data from websites: How to gather information from dynamic HTML elements

On the website I am exploring, there is a dynamic graph with descriptions below it that keep changing. My goal is to extract all these trajectory descriptions. The HTML code snippet related to the description area looks like this: <div class="trajDesc ...

Compatibility of HTML5 websites with Internet Explorer

Following a tutorial on HTML5/CSS3, I meticulously followed each step to create a basic website. While the demo of the site worked perfectly in Internet Explorer 8 during the tutorial, my own version did not display correctly when viewed in IE8. I discove ...

Searching for an element using Selenium can be accomplished by specifying the attribute and value within a div

Utilizing Python with Selenium for web scraping has presented a challenge due to the changing ID's and XPATH's on each page. To work around this, I have decided to access elements based on their attribute-value combinations. I am currently tryin ...

Trigger a JQuery popup by toggling a class with a button

Hey there! I have a modal popup that utilizes an active class. When this class is present, the modal appears, and when it is removed, the modal disappears. I am trying to create a button that, when pressed, will show the modal, and a close button inside th ...

Click to trigger image rotation with JavaScript

img { display: block; margin: 20px; width: 50px; height: 50px; } .crossRotate { -webkit-transition-duration: 1s; -moz-transition-duration: 1s; -o-transition-duration: 1s; transition-duration: 1s; -webkit-transition-property: -webkit-tran ...

Google Chrome seems to have some issues with recognizing CSS rgba

The RGBA option for the input element is functioning correctly in all browsers except Chrome. What could be causing this issue? Additionally, it appears that the entire input field is not working properly - strange, right? Check out this link for more inf ...

Showing a solo item from an array in HTML using Angular

How can I display the account name instead of the accountId property value from my list of transaction items? I have a list of transactionitems with an accountId property, and all accounts are loaded in the accounts$ array. However, I am having trouble us ...

What is the best way to share an HTML element across multiple pages in Next.js?

I am in the process of developing a website that features a table of contents with links on the left side, content sections in the middle, and an overview on the right. For a visual representation of the general concept, please visit: https://i.sstatic.net ...

Application with Single Page design has a Large DOM, leading to sluggish performance

I've been working on a single page application that heavily relies on widgets like grids and tabs from the jqWidgets library. These widgets are all loaded when the page is first opened. However, I've noticed that after interacting with the site f ...

Adjust the color of the font based on its value

Is it possible to change the color of text on a page based on a specific value? <div [ng-Style]="color":colorFont({{item.temp.day}})> {{item.temp.day}} {{vm.symbal}}</div><br> The data {{item.temp.day}} is a numerical value that determi ...

Unable to toggle navigation menu visibility on responsive design

I have provided the following code for responsive design. However, when I click the logo, it does not hide or show as expected. .toggle-nav { display: none; } .menu { float: right; } // Additional CSS code here <nav class="menu"> <ul c ...