-1

html...

<div id="main">
<div class="news"></div>
</div>

How could I assign css for #main id which has .news class with pure css ?


Is there something like #main:only(.news){...}?

Bhojendra Rauniyar
  • 73,156
  • 29
  • 131
  • 187

4 Answers4

2

You could keep using the #main id and then apply a class to the same element with it's own styles that could override the default #main styles, something like

<div id="main" class="news">

</div>

Then you could write a css rule like this

#main.news {
 /* your css rules go here */
}
jono
  • 1,584
  • 14
  • 16
  • Sorry I was thinking it's a mistake. The index page generate all #main div with class .news then how could I assign the main for .news #main – Bhojendra Rauniyar Sep 09 '13 at 04:08
  • Sorry, I don't understand your question? Using my answer above, you could retain all styles of #main on all other pages, then on the news page you could add the .news class to the #main div to allow you to style it differently. – jono Sep 09 '13 at 04:12
  • all page will have #main and when assign .news in #main then it also will be in all page as I'm using joomla template. – Bhojendra Rauniyar Sep 09 '13 at 04:18
1

I often use a body class to allow me to distinguish a common id from one page to another

<body class="news-page">
  <div id="main">
    <div class="news">

then your css can do thus

.news-page #main {
  background: blue;
{

.another-page #main {
  background: green;
}


.news {
  background: red;
}
rolfsf
  • 1,763
  • 3
  • 22
  • 32
0

How about something like this? You can target id="main" with #main and class="news" with .news.

<!DOCTYPE html>
<html>
    <head>
        <title>Page Title</title>    
        <style type="text/css"
            #main { color: red; }
            .news { color: blue; }
        </style>
    </head>
    <body>
        <div id="main">
            This is #main
            <div class="news">
                This is .news
            </div>
        </div>
    </body>
</html>
jbiz
  • 376
  • 1
  • 5
0

try a parent child relationship something like

#main > div { 
   color:pink;
} 
Kara
  • 5,650
  • 15
  • 48
  • 55
spicy
  • 1
  • 1