CSS - Nesting classes within one another

I'm currently working on customizing a Tumblr theme. Users have the option to choose between a one-column or two-column layout style, and I'm facing some challenges with implementing this in the CSS code. I attempted to add a class based on the user's selection to the content, but it doesn't seem to be functioning properly. I am unsure if having a class inside another class is the correct approach.

In essence, my objective is for the Content div class to determine the Container div class.

<meta name="select:Layout" content="ClassOne" title="One Column">
<meta name="select:Layout" content="ClassTwo" title="Two Columns">

CSS code snippets:

.ClassOne{ 
  @media (max-width:480px){
    .container{
      width: 250px;
      float: left;
    }
  }
  @media (min-width: 480px){
    .container{
      width: 300px;
      float: left;
    }
  }
}

.ClassTwo{ 
  @media (max-width:480px){
    .container{
      width: 500px;
      float: left;
    }
  }
  @media (min-width: 480px){
    .container, .container .photo img, .photoset img, .link, .quote, .text, .chat, .audio, .video, .video iframe, .answer{
      width: 600px;
      float: left;
    }
  }
}

HTML structure:

<div id="content" class="masthead {select:Layout}">
  <div class="container">
  </div>
</div>

Answer №1

Unfortunately, achieving that in plain CSS is not possible. Your CSS code should resemble the following:

@media (max-width:480px){
    .ClassTwo .container{
      width: 500px;
      float: left;
    }
  }
  @media (min-width: 480px){
    .ClassTwo .container, .ClassTwo .container .ClassTwo .photo img, 
    .ClassTwo .photoset img, .ClassTwo .link, .ClassTwo .quote, 
    .ClassTwo .text, .ClassTwo .chat, .ClassTwo .ClassTwo .audio, 
    .ClassTwo .video, .ClassTwo .video iframe, .ClassTwo .answer{
      width: 600px;
      float: left;
    }
  }

However, nesting can be utilized in SCSS with an SCSS compiler to convert it into flattened CSS. SCSS serves as a method for writing organized CSS files, but direct usage by browsers is not supported.

  • Refer to http://sass-lang.com/ for more information on SASS and SCSS. Both are similar, but SCSS features a syntax closer to regular CSS.
  • Explore for a reliable PHP-based SCSS compiler. I've personally used this tool and found it to work efficiently.
  • Avoid JavaScript SCSS compilers as they may significantly slow down webpage loading due to browser compilation requirement on each view.

Answer №2

Is it your intention for the user to select class2 or theme2 and have that class name added to the content?

You have the option of achieving this with Jquery. I quickly put together an example on JS Bin which might assist you. Check it out here: http://jsbin.com/cawuvapicage

Below is the Jquery code used, along with simple html. You'll find that theme1 and 2 are clickable links. Upon clicking one of them, the corresponding class name will be added to the container div.

 $(function() {
     $( ".theme1" ).click(function() {
        $( ".container" ).toggleClass("style1");
        $( ".container" ).removeClass("style2");
      });
    $( ".theme2" ).click(function() {
        $( ".container" ).toggleClass("style2");
        $( ".container" ).removeClass("style1");
});

});

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

When incorporating conditional statements into your code, make sure to utilize the tags <script> along with

I have some script tags as shown below: <script src="cordova-2.5.0.js"></script> <script src="js/jquery-1.7.2.min.js"></script> <script src="js/jquery.mobile-1.1.1.min.js"></script> <script src="js/jquery.xdomainajax ...

What could be causing my Time Picker MUI to malfunction in my React project?

I am currently working on a React and Rails project where I am attempting to style my React components. However, I have encountered an issue with the MUI component Time and Date picker. Whenever I try to implement the time picker, I get a strange error reg ...

Ways to eliminate the white overlay that occurs on the page after clicking the scroll top button

Hi there! I've been busy with updating my site at , and noticed a small glitch. Every time I hit the Back to Top button at the bottom of the page, the screen goes white. Any ideas on how to fix this issue? Thanks in advance! ...

Can CSS Grid layout be used to generate a grid consisting of squares?

I'm trying to set up a grid with equal square dimensions, but I'm having trouble with it. I've explored CSS Grid as a possibility since I thought it allowed for the creation of squares of equal height and width within a grid layout. However, ...

What is your approach to setting up this navigation system with HTML and CSS?

Navigation Nav w/ Hover State Can you create a navigation menu using HTML and CSS? The white box serves as a placeholder for a logo. Note: The "Products" link has a drop-down menu feature. See my current implementation below, although it's not ye ...

The automatic redirection feature on a webpage is not functioning correctly on mobile devices

There's a peculiar issue on our website that I'll do my best to explain, and I'll provide photo links for more clarity. In our WooCommerce site, the process of paying for the cart goes as follows: Click "place order" button pic: Page for o ...

Alignment and spacing in a flexible direction

Hello, I am developing a bar app. <AppBar position="sticky" className={classes.appBar} > <Container maxWidth="lg"> <Toolbar className={classes.root}> <Typography noWrap> <img src={require("./node ...

Using Jquery and CSS to add a class with a 1-second delay when the mouse enters

I have successfully implemented the addClass function in my code, but I'm encountering issues when attempting to use delay or setTimeout functions. It's important to note that I'm avoiding the use of webkit attributes in CSS. If anyone has ...

Define the term "Parse Error" as it pertains to CSS

i'm encountering an issue while attempting to validate my CSS. Despite trying to validate using direct input (copy and paste), the error persists. In my pursuit to find a solution, I researched on Google only to discover that it could be related to sp ...

Does the height of a block element change based on the font size?

Does the font size of content affect the height of a block element? Let me demonstrate what I'm talking about, take a look at this example fiddle If you enlarge the font size of the class .p within the div, you'll notice that the div's hei ...

Attaching a buoyant div to the precise location of a different element

I have a unordered list (ul) with individual list items (li) that are displayed within a scrollable container. This means that only 8 list items are visible at a time, but you can scroll through them to see the others. Each list item (li) has an "edit" b ...

Developing a division with an image source based on the selection of a checkbox

My goal is to develop a "change function" that generates a new div for each checked checkbox selection and removes the div when the checkbox is unchecked. These new divs should also display the img src of the selected checkbox. Currently, my JavaScript c ...

Tips for retrieving information from a dynamically created form using VUE?

Welcome Community I am working on a parent component that includes a child component. The child component dynamically renders a form with various controls from a JSON object retrieved via a Get request using Axios. My goal is to be able to read and loop ...

How can we add styles to text entered into a form input field in a targeted way?

Is there a way to apply styling to the second word entered into a form input text field after the user hits "enter" and the data is output? In this scenario, the text always follows the format of 3 numbers, followed by a space, and then a name. For examp ...

css - flexible div height based on content

Issue encountered: In my website, I have a dynamically generated content inside a specific div. On the left side of the provided URL, you can see the current appearance of the div. It is worth noticing that the height of the container div (mainContent_mid ...

Filtering a section of the HTML using Ajax and jQuery

Dealing with an Ajax call in jQuery that's delivering a lot of unexpected HTML content. I'm attempting to extract the specific part I need, but the console is showing an empty string. Can anyone provide guidance on resolving this issue? $.ajax({ ...

Looking to distinguish selected options in a multiple select in AngularJS by making them bold?

When working with Angular, I have a multiple select drop down with options such as Select fruits, Apple, Orange, Banana. How can I make the selected options, like Banana and Apple, appear in bold and change background colors? ...

Flexbox keeps elements on the same line without breaking

I'm currently delving into flexbox and aiming to create a layout similar to the one shown below: https://i.sstatic.net/epnkU.png Here is the code I have come up with: .container { display: flex; gap: 26px; } .flex50 { flex: 50%; ...

How can I shift the button's location within a pop-up in Ionic 1?

enter image description here I am struggling to make the button green underneath the three other buttons. Can someone please assist me with this? Here is my code: $scope.showIntroductionPage = function(childs){ if(childs == 0){ var myPopup = $io ...

Troubleshooting Cufon Compatibility with Internet Explorer - Investigating a CSS Problem

Currently facing an unusual issue. I am creating a Wordpress theme with Cufon and it works flawlessly in Chrome and Firefox, but refuses to render in IE. I decided to put this problem on hold and focus on it later as I am still in the development phase. Y ...