Multiple ids with div not working in css - css

I have the following CSS:
#form1,#form2,#form3,#form5,#form6,#form7,#form8 div{
padding:10px;
border:1px solid blue;
background-color: grey;
font-family:"lucida grande",tahoma,sans-serif;
}
For some reason, the last id does not get the style. (ie #form8 does not get the style).
If I switch the css like this (Without changing any html code):
#form1,#form2,#form3,#form5,#form8,#form6,#form7 div{
Now #form7 does not have the style.
Did I code the structure wrongly please? Its very strange

It's probably an HTML markup issue. Can you provide it?
A wild guess is that your code looks like:
<div id="form8">
...
</div>
And the last part of your CSS selector (#form8 div) actually targets a markup like:
<div id="form8">
<div>
...
</div>
</div>
Here's a meta advice: if your selectors list is so long and apparently targets the same type of element (a form), use a class!
.form{
padding:10px;
border:1px solid blue;
background-color: grey;
font-family:"lucida grande",tahoma,sans-serif;
}

Seems you are targeting div#form1, div#form2 ... and so on... You can skip writing div for the selector. Try this
#form1, #form2, #form3, #form5, #form6, #form7, #form8 {
padding:10px;
border:1px solid blue;
background-color: grey;
font-family:"lucida grande",tahoma,sans-serif;
}
Or even better ... give all of them a class name like <form class="myform" id="whatever"></form> and use:
.myform {
padding:10px;
border:1px solid blue;
background-color: grey;
font-family:"lucida grande",tahoma,sans-serif;
}

You should just use #form1,#form2,#form3,#form5,#form6,#form7,#form8
#foem8 div refers to the all child divs of the element with this is #foem8

Related

How to i make to boxes inline with each other. One is under the other?

The code is exactly the same but it just isnt inline with the box above it.
Thanks in advance.
CSS
#menu {
margin-top:75px;
min-width:19px;
max-width:1920px;
height:40px;
background-color:#0F0;
border:4px groove #F00;
}
#header {
margin-top:50px;
max-width:1920px;
height:70px;
background-color:#000;
border:4px groove #F00;
If those are div or other block elements, you could use the CSS-attribute float.
I think you have part of the answer you are looking for already in your question.
If you have a block-element (or any other non-inline-element) and want it to behave like it is inline you can do so by setting the display-property:
display: inline;

CSS rules priority

I have this simple CSS:
.cont div {
margin:10px;
border:1px solid;
}
.mark { /* This get ignored? */
margin:30px;
}
With this markup:
<div class="cont">
<div>a</div>
<div class="mark">b</div>
</div>
I except the div.mark having margin:30px; but at least in Chrome this isn't true because the generic rule .cont div seems to have a higher priority.
Consider I don't want to use !important are there any other way to solve this?
http://jsfiddle.net/xNVRm/
Just make your selector more specific by adding the tag name:
div.mark {
margin:30px;
}
Demo: http://jsfiddle.net/xNVRm/1/
You could also use .cont .mark if you want to avoid using the tag name.
In order to avoid to use the important you need to make your css selector more specific. You can use .cont div.mark. It is more specific than div.mark.
The ".cont div" declaration overrides the ".mark" declaration because it's actually more specific. CSS uses a kind of point system to figure out which rules apply. In your case, ".cont div" specifies both a class and an element inside it, whereas ".mark" only specifies a class.
For the exact rules that should be used by all conforming browsers, see this link: http://www.w3.org/TR/CSS21/cascade.html#specificity
In your case you could fix this by using ".cont .mark" in the second declaration.
Specificity is key to how CSS rules are given a pecking order. Try looking at this article from HTML Dog:
http://www.htmldog.com/guides/cssadvanced/specificity/
You could use div.mark instead, which means any div that has the class of mark, do this.
Looking over this again, I see I wasn't understanding what you were trying to do. I think I see now.
You are is saying - ANY div inside of anything with class .cont will have 10px margin. It's more specific then .mark. .mark is 30px - BUT it's a div that is inside of .cont - so it's 10px. It reads right to left - that is a good way to think about it and check specificity.
I have come to think of things with a more object oriented approach. What do you think about this approach?
HTML
<div class="container section01">
<div class="block a">a</div>
<div class="block b">b</div>
</div>
CSS
.container {
width: 100%;
float: left;
border: 1px solid red;
}
.container .block {
/* you can style these site wide */
}
.section01 .block {
border:1px solid black;
padding:10px;
margin-bottom: 1em;
}
.section01 .block:last-of-type {
margin-bottom: 0;
}
.section01 .a {
background-color: red;
}
.section01 .b {
background-color: lightblue;
}
SASS would make this much easier.
a jsFiddle of this example
a CODEPEN of this on a larger scale

Align text at the end and the beginning of a div

Currently I'm having a solution, but I'm almost certain that there's a better solution out there. Basically I'm having a block-element and want to align some of the text at the beginning of my block and some at the end.
Here's a little jsfiddle example
What I'm doing is using float and 2 more block-elements inside to align it:
<div id="block">
<div id="start">1</div>
-
<div id="end">12</div>
</div>
#block {
text-align:center;
background: #000;
color: white;
width:150px;
}
#start {
float:left;
}
#end {
float:right;
}
I have many of those little objects, so my code is bloated with div's. Is there no more lightweight solution for this out there ?
I fiddled a possible answer based on the answer to this question.
http://jsfiddle.net/ScHdJ/2/
Works in all browsers, as far as I can see...
May be you can use CSS :after & :before pseudo classes like this:
HTML:
<div id="block">
hello
**</div>
CSS:**
#block {
text-align:center;
background: #000;
color: white;
width:150px;
overflow:hidden;
}
#block:before{
content:"1";
float:left;
}
#block:after{
content:"12";
float:right;
}
http://jsfiddle.net/ScHdJ/3/
But is not work in IE7 & below.

css nesting problem

I want the links inside of the second, nested div to have red text.
Dulled down CSS:
#outerdiv{ padding:10px; background-color: #ddd;}
#outerdiv a:link{ color:blue; }
.innerdiv{ padding:10px; background-color: #aaa;}
.innerdiv a:link{ color: red; background-color:White;}
Dulled down HTML:
<div id="outerdiv">
OUTERDIV link
<div class="innerdiv">
INNER DIV link
</div>
</div>
JSFiddle: http://jsfiddle.net/5S6ez/1/
How can I make my innerdiv links have red font?
My link keeps as much of its grandparents' styles as possible even though it has new styles applied to it that occur later in the CSS file. Why?
The problem is that the id based selector is more specific than the class-name based selector, to change that, use:
#outerdiv .innerdiv a:link{ color: red; background-color:White;}
Try making the outerdiv classes instead of ids. Like this:
.outerdiv{ padding:10px; background-color: #ddd;}
.outerdiv a:link{ color:blue; }
.innerdiv{ padding:10px; background-color: #aaa;}
.innerdiv a:link{ color: red; background-color:White;}
If that is not an option (outer div must be an id), then you can try to make the innderdiv rules more specific to the outerdiv, like this:
#outerdiv .innerdiv{ padding:10px; background-color: #aaa;}
#outerdiv .innerdiv a:link{ color: red; background-color:White;}
Also, I was recently introduced to this article, and it really has helped me a lot with CSS in general:
http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade/
Use a, not a:link.
:link is a pseudo-class for unvisited links.
Also, just for a heads up that may help you with other things, keep in mind that a tags are also inline elements and not to style them with padding, etc unless you set "display: inline-block" or "display: block". Yeah, a bit more than you asked but still can be helpful.

How can I select this element?

I have code blindness, it's like snowblindness, just wth far too much code.
I have a div class dynamically generated,
<div class="even last">
How can I select that with CSS?
div.even last {
background-color:#ffffff;
height:100%;
border-top:1px solid #F5F5F5;
padding:2px;
margin-top:35px;
}
Doesn't seem to work, and I just can't think more..
Thanks :)
When multiple classes are specified with a space, it applies both of those classes to the element.
Therefore if you specify div.even AND/OR div.last it will use them.
You cannot have spaces in a css class name. This should work:
<div class="even_last">
div.even_last {
background-color:#ffffff;
height:100%;
border-top:1px solid #F5F5F5;
padding:2px;
margin-top:35px;
}
Spaces in css mean: the next element contained in the previous one, for example:
<div class="even_last">
<div>
Hello
</div>
World
</div>
div.even_last div {
font-weight:bold;
}
Hello will be bold, while World will not.
Replace the space with a dot and you're right:
div.even.last {
background-color:#ffffff;
height:100%;
border-top:1px solid #F5F5F5;
padding:2px;
margin-top:35px;
}
Name your class like evenlast:
div.evenlast
{
/* styles here */
}

Resources