CSS structure design - css

I have more than twenty divs.
The divs have id's of #div_1,#div_2,#div_3...
Each div has a button and an image. and they are named the same in each div: #button_1, #image_1.
Is it possible to construct my css like this:
#div1 {
#button_1{ };
#image_1{};
}
#div2 {
#button_1{};
#image_1{};
}

First things first, you should never use the same ID more than once in a single page. The whole point of an ID is that it is unique.
Therefore, having a button called id='button_1' inside each of your divs is wrong.
If you want to use the same reference multiple times, use a class instead of ID. So your buttons, for example, should be class='button_1' instead.
Secondly, the CSS syntax you're asking for is not correct. You can't nest CSS in this way. However, what you're asking for can be achieved quite easily; simply chain the selectors together like so:
#div1 .button_1 { ... }
#div1 .image_1 { ... }
#div2 .button_1 { ... }
#div2 .image_1 { ... }
etc.
However, if the images and buttons are similar between, you don't need to specify the styles twice, just create a CSS block for the two classes that contain the shared styles:
.button_1 { ... }
.image_1 { ... }
Any styles that are different between them you can still declare in the same way as discussed before, so your whole CSS could look like this:
/* shared styles for all the buttons */
.button_1 { ... }
/* shared styles for all the images */
.image_1 { ... }
/* extra styles only applying to the ones in div1 */
#div1 .button_1 { ... }
#div1 .image_1 { ... }
/* extra styles only applying to the ones in div2 */
#div2 .button_1 { ... }
#div2 .image_1 { ... }
...etc.
Hope that helps.

IDs are unique, you should not repeat them in a page. You can't have multiple #button_1's and #image_1's
What you really should be doing is:
HTML
<div id="div-1" class="foo">
<button id="button-1">Hello</button>
<img src="blah.gif" id="img-1" />
</div>
<div id="div-2" class="foo">
<button id="button-2">Hello</button>
<img src="blah.gif" id="img-2" />
</div>
<div id="div-3" class="foo">
<button id="button-3">Hello</button>
<img src="blah.gif" id="img-3" />
</div>
CSS
.foo { ... }
.foo button { ... }
.foo img { ... }
Note that to style the buttons and images inside the divs you don't actually need to give them IDs

No, you can't write your CSS like that. In any case, if you find yourself styling 20 individual DIVs in a series then you're doing something wrong. It would be simpler for you to write styles for the classes rather than writing them for IDs

What you can do is write something like :
#div2 #button_1{
}
#div2 #image_1{
}
if you are trying to make the CSS rules only available to the objects inside a certain div (in the example above they only apply to divs inside div2.

Take a look at http://sass-lang.com/ you want to use a mixin

Related

Apply class by name to CSS elements with selector without JS

Is it possible to apply the class with CSS to subelement as in the following example, without adding <el class="classname"> to each element?
HTML
<div id="container">
<p>TEXT</p>
<div>text</div>
<p>TEXT</p>
<div>text</div>
</div>
CSS
#container {
background-color:#000;
color:#FFF;
}
#container > p {
color:#F0F0F0;
/* APPLY ALSO CLASS TITLE TO ALL #container > p */
}
#container > div {
font-size:125%;
/* APPLY ALSO CLASS WRITE TO ALL #container > div */
}
.titles {
font-family:....;
font-style:....;
/* and so on */
}
.write {
/* some stuff */
}
No. There is no way in CSS to say that you want #container > p to also include all the styles you've written for .titles.
However, take a look at Sass. Writing Sass is a lot like writing CSS, but it lets you do exactly what you've described here. You could write:
.titles {
...
}
#container > p {
#extend .titles;
...
}
This is not something the browser knows how to do, but the browser never sees it. Sass turns your special code into real CSS, and you use that on your site.
Using pure CSS, you'd have to do something like:
.titles, #container > p, #container >div {
...
}
No, you can't apply classes to elements from within CSS. What you would do is copy the code from the .tiles & .write classes and apply it to the the elements you want styled that way.
Something that can make this relationship easier to manage is a CSS pre-processor like Less or Sass
This would work but it's not very elegant. Unfortunately all the styles would be used on the parent element which would cause problems most of the time. What you need is SASS or LESS like already mentioned so you can create reusable functions that allow flexibilty.
.titles,
.titles > p {
font-family:....;
font-style:....;
/* and so on */
}
.write,
.write > div {
/* some stuff */
}
div id="container" class="titles write">
<p>TEXT</p>
<div>text</div>
<p>TEXT</p>
<div>text</div>
</div>

CSS Shorthand available?

Let's say I have the following markup.
<div class="parent1">
<div class="inner1"></div>
<div class="inner2"></div>
</div>
<div class="parent2">
<div class="inner1"></div>
<div class="inner2"></div>
</div>
If I only want to style inner1 of parent1 then I can do something as follows.
.parent1 .inner1{}
However if I want to specify different styles for each of the inner containers then I have to write .parentx in each statement. So my question is can I nest my css statements? The logic would resemble the following:
.parent1{
.inner1{}
.inner2{}
}
.parent2{
.inner1{}
.inner2{}
}
CSS itself does not allow nesting. However, clever guys these days came up with a concept of pre-compiled CSS, such as SASS, LESS etc.
http://lesscss.org/
For example, in LESS something like this is allowed:
#header {
h1 {
font-size: 26px;
font-weight: bold;
}
p { font-size: 12px;
a { text-decoration: none;
&:hover { border-width: 1px }
}
}
}
But if you are stuck with raw CSS, then what #Brian suggested in his answer would be the best option:
.parent1 .inner1,
.parent1 .inner2 {
/*styles*/
}
.parent2 .inner1,
.parent2 .inner2 {
/*styles*/
}
What you want is:
.parent1 .inner1,
.parent1 .inner2 {
/*styles*/
}
.parent2 .inner1,
.parent2 .inner2 {
/*styles*/
}
This will apply the styles to the 2 lots accordingly.
CSS doesn't give us much in the way of reducing keystrokes. This is why projects like Less and Sass were created.
The shorthand you've described there would be nice, but sadly is not a feature of CSS. The selectors are always read right to left, so .parent1 .inner1 instructs the browser to "find all items with an inner1 class, then loops through those to find the one(s) that are also a parent1 class.
In your specific example, you might consider one of the following options:
If your styles are specific to a single item (the first div within parent1), consider giving that item an id="foo" and then refer to it as #foo in your CSS.
If you don't want to do that, use the > (child) operator to define rules like this...
.inner1 > .parent1
This applies rules for the .inner1 divs that are directly descended from a .parent1 block.
Hope that helps.

Make a certain image unaffected by img css

I have this CSS on my page.
img {
opacity:0.4;
filter:alpha(opacity=40);
}
img:hover {
opacity:1;
filter:alpha(opacity=100);
}
I want to have a few images that aren't affected by this opacity. How would I go about accomplishing this?
The best way to do it would be to give it a unique ID and target it separately:
img#theID {
// CSS for that particular image here
}
Method 1
The most popular method to accomplish something like this suggests the usage of specificity. Basically, the more specific the selector is, the higher precedence. Consider this markup
<section>
<div class="container">
<img src="blahblah.jpg" />
<div class="wrap">
<img src="blahblahblah.jpg">
</div>
</div>
<section>
<img>
<img>
If you use
img {
/* Styles */
}
Those styles will be applied to all. But if you used something like this
section .container .wrap img {
/* Different Styles */
}
Those styles will take precedence for that image, because CSS likes the most specific answer you can give.
Method 2
In addition to this answer given by user125697, which suggests using id's as such
/* img#ID - ID can be whatever you want*/
img#hover {
opacity:1;
filter:alpha(opacity=100);
}
And you would impliment it as such
<img id="hover" src="blahblah.jpg" />
Chris Coyier has published a pen about this. To test the results, just remove one of the classes from the box, and you will see that it changes colors. So basically classes are always overridden by IDs
Method 3
Not Encouraged
The final method I know of is to use !important, which overrides every style on the page. No matter what. This is highly disapproved of, because it can create a lot of problems down the road.

LESS CSS - accessing classes further up the dom tree from within a nested class

I want to be able to access classes further up the dom tree from within a nested class using LESS CSS, see example:
HTML:
<html class="svg">
<body>
<div class="content">
<div class="container">
<div class="logo"></div>
</div>
</body>
</html>
LESS:
.container {
.logo {
background:url(/images/ws-logo.gif);
}
}
I want to target the .svg class on the html tag from within the .logo nested rule, to keep things tidy instead of writing another rule like this:
.svg {
.container {
.logo {
background:url(/images/logo.svg);
}
}
}
So, ideally something like this:
.container {
.logo {
background:url(/images/logo.gif);
(some-symbol).svg {
background:url(/images/svg-logo.svg);
}
}
}
I'm using modernizr to detect svg support.
Anyone know if this is possible? Or have any recommendations?
Yes! (an update)
When I tested this here, it worked!
.container {
.logo {
background:url(/images/logo.gif);
.svg & {
background:url(/images/svg-logo.svg);
}
}
}
This is not possible because you can't "step back" in the path to add another class to a parent. Instead, just write another rule:
.svg .container .logo,
/* or perhaps even simpler, however be aware of rule specificity*/
.svg .logo{
background:url(/images/logo.svg);
}
It's not much of a deal, is it?
For the sake of completeness: You can reference to the actual element via the &-symbol. THis makes sense if you want to target pseudo-classes/elements or additional classes of the current element:
.container {
.logo {
/* styles for ".container .logo" */
}
&:hover .logo{
/* styles for ".container .logo"
The hover however is bound to the .container element
equals the following selector: .container:hover .logo */
}
}

Confused about CSS inheritance

I've been reading about CSS and I am really confused about how the inheritance works (I think thats the right term for the following). In CSS I can declare a class:
#mytext {
}
then I see some people do:
p.mytext {
}
But why do that? Why can't they just do:
<p class="mytext">
Without declaring p.mytext? Does it make sense what I am asking?
and sometimes i see:
p#mytext ... Why is it different? I'll keep searching tutorials but thanks for any advise.
The pound sign (#) refers to an ID which needs to be unique for the page. The period (.) refers to a class which can be used many times. People would use p#mytext if they wanted a unique styling for one (just one) paragraph tag.
You can read up about it here.
Wanted to add that some web developers seem to gravitate towards declaring everything as classes. If you use a layout generator of any kind more often than not every element will be a class.
#mytext references <p id="mytext"/> (doesn't need to be a p element, #mytext just refers to that ID)
Whereas .mytext references <p class="mytext"/> (doesn't need to be p element, .mytext just refers to anything with that classname)
By adding other things such as p.mytext you create a stronger bind to your rule, for instance:
p.mytext { color:white; } .mytext { color:black; }
may at first seem like the color would be black, however as you have created a stronger bind (by being more specific earlier) the actual color will be white.
First check this question here.
In short # represents an ID in css, and . represents a class. if you say p#myText in your css it means you have a <p id="myText"></p> in your html, and p.myText is for <p class="myText"></p>.
Furthermore you declare an ID if you have an unique item in your html, and if you have multiple elements with same styles you declare a class for them.
CSS 101 - the basics
CSS - all elements
* { ... }
HTML - basic element
<p></p>
CSS
p { ... }
HTML - element with id
<p id="someid"></p>
CSS - element with id
p#someid { ... }
CSS - all id's
#someid { ... }
HTML - element with class
<p class="someclass"></p>
CSS - element with class
p.someclass { ... }
CSS - all elements with class
.someclass { ... }
CSS - is equal to
*.someclass { ... }
HTML - element with both id and class
<p id="someid" class="someclass"></p>
CSS
p#someid.someclass { ... }
HTML - nested element
<p><span></span></p>
CSS
p span { ... }
HTML - nested element with id
<p><span id="someid"></span></p>
CSS
p span#someid { ... }
HTML - nested element with class
<p><span class="someclass"></span></p>
CSS
p span.someclass { ... }
HTML - nested element with id in element with class
<p class="someclass"><span id="someid"></span></p>
CSS
p.someclass span#someid { ... }
now you can mix and match all those things up to make really complicated selectors
if you want multiple selectors with the same properties you can seperate them with a comma
p.someclass, span#someid { ... }
A hash (#) is a unique ID definition.
#foo { color: blue; }
<div id="foo">
A dot (.) is a class definition.
.bar { color: red; }
<div class="bar">
But you can also refer to tags with certain classes and ID's:
div.baz { color: green; }
span#qux { color: yellow; }
<div class="baz">
<span id="qux">
+1 for the interesting question.
First, you have it backwards, . (period) is class and # is ID. You probably already know this, but an element can only have one ID and you should only have that ID defined once on your page.
As for the second part of your question, some people like to append the element name to their classes and IDs. It's just more specific that not having it defined.
img.large { width 200px /* Only applies to img with large class */ }
textarea.large { width: 300px /* Only applies to textareas with large class */ }
p#large { font-size: 1.5em; /* Only applies to p with ID of large */ }
.large { font-size: 2em; /* Applies to any element with class of large */ }
Personally, I like to append the element name in my styles so that I don't forget which elements it is affecting.
lets say you have the following HTML:
<div id="main">
<p class="para">content</p>
<p class="para">content</p>
</div>
then:
div#main { }
references divs with the id of "main"
#main { }
references all elements that have the id of "main"
p.para { }
references all p elements with the class of "para"
.para { }
references ALL elements with the class "para"
NB. An ID must be unique on the page whereas a class can be used multiple times

Resources