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

Extra space within table in Firefox

Experiencing an issue with excess whitespace in a table on Firefox. Visit the live example at The problem can also be seen on this jsFiddle: http://jsfiddle.net/DVbCC/ In Chrome and Safari, rows are neatly arranged with minimal spacing, but Firefox show ...

Mysterious anomaly observed: Peculiar trajectory detected in canvas image during left

I have a fascinating HTML5 Canvas that showcases the movement of two identical objects to both the right and the left. Although the right side operates flawlessly, I've noticed that the left object leaves behind an intriguing trail with a hint of gree ...

Tips for utilizing javascript document.createElement, document.body, and $(document) in an HTML web resource for Microsoft CRM code reviews

Let me start off by saying that I am not a regular blogger and I am feeling quite confused. If my question is unclear, please provide guidance for improvement. I recently submitted a Microsoft CRM PlugIn to the Microsoft Code Review. I am new to Javascrip ...

Adding style using CSS to a dynamically generated table row in Vue.js

Currently, I am working with a table that dynamically generates rows using v-for: <table> <tr><th class='name'>Name</th><th>Surname</th></tr> <tr v-for='data in datas'><td class=&a ...

The dropdown feature on my theme seems to be malfunctioning on Wordpress

I'm having trouble figuring out what I'm doing wrong. The dropdown navigation works fine with the default Wordpress twentyeleven theme, but when I switch to my custom theme, the dropdown stops appearing. Here is the code I'm using: <div ...

What is the best way to deactivate unclicked href links within a loop?

Looking at the template image, my goal is to disable all links that were not clicked when one of the links from 'number1' to 'number3' is clicked. For example, if 'number2' is clicked, then 'number1' and 'number ...

only a single backdrop filter can be in use at any given moment

Experimenting with backdrop-filter in Chrome 76. I'm working on a menu with 2 divs, each with a backdrop-filter applied. The first div is displaying the filter effect perfectly, but the second one isn't. Interestingly, when I remove the filter fr ...

Display the div only when the radio button has been selected

I have been attempting to tackle this issue for quite some time now, but unfortunately, I haven't had any success. My goal is to display a specific div on the webpage when a particular radio button is selected. While I have managed to achieve this by ...

Angular - Customizing button bindings for various functions

As a newcomer to Angular and TypeScript, I am faced with the task of creating a customizable button that can display text, an icon, or both. For example: button-icon-text-component.html <button> TEST BUTTON </button> app.component.html & ...

Struggling with CSS float problems

I'm currently working with the Pure CSS framework and my code resembles this: <div class="container pure-g"> <header class='pure-u-1'> <h1 class='logo'> <a href="#">TEST</a> </h1> &l ...

Adjust the color of the active link on the page using Angular and CSS

I have a project that I need to modify by adding a sub menu that appears on every page but is only coded once. My goal is to highlight the link for the current page, all within one HTML snippet. Although the list renders correctly, I'm struggling to g ...

Converting Mat-Raised-Button into an HTML link for PDF in Angular 6 using Material Design Library

Greetings, I have a couple of interrelated inquiries: Upon clicking the code snippet below, why does <a mat-raised-button href="../pdfs/test.pdf"></a> change the URL (refer to image 4) instead of opening test.pdf in a new window? If I have a ...

Tips for stopping the navigator from adding content to an HTML input in an Angular application

I am facing an issue with two input fields in my Angular project. These fields are supposed to take values automatically from the browser, and I have tried using the HTML autocomplete option without success. Here is a snippet of my HTML code: <div clas ...

Tips on avoiding blurring when making an autocomplete selection

I am currently working on a project to develop an asset tracker that showcases assets in a table format. One of the features I am implementing is the ability for users to check out assets and have an input field populated with the name of the person author ...

I am looking to enhance a button's appearance by applying CSS styling with a combination of two distinct flat colors

Is there a way to apply CSS styling to a button by incorporating two distinct flat colors? Specifically, I would like the left half of the button to be blue and the right half to be red. ...

The top margin is experiencing issues and is not functioning correctly

I'm struggling to identify the problem with this script: http://jsfiddle.net/AKB3d/ #second { margin-top:50px; ... } My goal is to have the yellow box positioned 50px below the top border of the right box. However, every time I add margin-top to ...

Finding the best way to transfer text between DIV elements?

I have a dilemma involving two DIV elements positioned absolutely on the sides of an HTML page, much like this EXAMPLE: <div class="left"> </div> <div class="right"> </div> These are styled using the following CSS: .left{ pos ...

Modifying JavaScript Code in Inspect Element Editor

When I modify the HTML content using Chrome's Inspect Element editor, any changes made are immediately visible. However, when I make changes to the JavaScript code, the modifications do not take effect. For example, if I have a button that triggers a ...

Repurpose the identical popup window

I am searching for a solution to use the same pop-up window whenever it is called. Currently, I need to submit a form into a pop-up window and it works well, except in Chrome iOS where each form submission opens a new tab/window instead of reusing the prev ...

variable identifier looping through elements

I'm attempting to assign a dynamic ID to my div using ng-repeat. Here's an example: <div id="$index" ng-repeat="repeat in results.myJsonResults"> <div id="$index" ng-click="saveID($index)" ng-repeat="subRepeat in results.myJsonResul ...