So I have a question about the css selectors in reference to my design.
I'm trying to fiddle around with how you can manipulate check-boxes through css by using multiple selectors such as "+", "~", etc to perform an event when the checkbox is either checked or unchecked.
I'm confused as to why this doesn't work, or by using "~" instead since from my understanding "+" selects elements matching it directly after the first element while "~" selects every element that comes after the prior element. This is my first time playing around with it so I'm not the best...but I'm trying to wrap my head around it. Does "element":checked not count as a reference point when stringing multiple selectors? I left two tried on the jsfiddle.
While this is intended to be a navigation menu later, I just, for the time being, left it like this to get a hang of selectors first.
HTML
<div id="container">
<header>
<input type="checkbox" id="navigation_drawer">
<label for="navigation_drawer" id="drawer_open"></label>
<label for="navigation_drawer" id="drawer_close"></label>
<nav id="navigation">
<ul>
<li id="home_page">Home</li>
<li id="about_page">About</li>
</ul>
</nav>
</header>
</div>
CSS
label#drawer_open{
position: absolute;
top: 2;
right: 0;
margin-right: 1rem;
background-image: url('http://i.imgur.com/Nxafddw.png');
background-image: no-repeat;
width: 35px;
height: 23px;
}
#navigation{
background-color:blue;
}
#navigation_drawer:checked + #container #navigation{
background-color:red;
}
#navigation_drawer:checked + container #navigation{
background-color:red;
}
Thanks!
Edit: Man you guys are harsh xD Yes, I did make some mistakes copying and pasting this into here and in my code itself when I was changing it around. My fault. Here is an updated fiddle of the original problem for reference. I updated the HTML/CSS here as well. Thank you Pete for the updated fiddle. I would give it to you but since it's a comment instead of a reply I'll accept the other person's since it's another way as well. Thanks :D
https://jsfiddle.net/y9x8accc/6/
Your code doesn't work because the + (Adjacent Sibling Selector) selects the element that immediately follows it. In this case, the immediate element after your checkbox is your #drawer_open label, not the nav.
Adjacent Sibling Selector This is referred to as an adjacent selector or next-sibling selector. It will select only the specified element that immediately follows the former specified element.
You seem to have incorrect indentation for your labels, so perhaps that made you think the nav is the direct sibling. In any event, you'd need to use the General Sibling Selector (~).
The correct code to achieve what you want would be:
#navigation_drawer:checked ~ #navigation{
background-color: red;
}
Fixed fiddle
Related
I have a webpage with elements, styles (imported and inline)
I want to reset the style for a specific element.
Example:
HTML:
<div class="parent">
This is the parent div, it colors the <strong>strong in red</strong>
makes a <small>small underlined</small>
<h4>sets a margin-left 10px for a H4</h4>
and many other stuff<br><br>
<div class="child">
this is the child element<br>
here a <strong>strong should not be red</strong><br>
<small>small should not be underlined</small>
<h4>H4 should not have a margin-left</h4>
and so on...
</div>
</div>
CSS:
.parent strong{
color:red;
}
.parent small{
text-decoration: underline;
}
.parent h4{
margin-left: 10px;
}
I want the child div to ignore the styles coming from his parents, including the html element
Here is an illustration of my example
The styles I gave here are just examples, there are much more
I cannot modify the parent CSS, is being dynamically generated
My child div is injected in the page, I can also inject any CSS I want
I cannot know in advance the content of the parent CSS
The only solution I found so far is including the child element in an Iframe, but is really really ugly!!
Any one can help how to achieve this? A JS solution is also acceptable.
.child strong{
color:pink !important;
}
1.You adjust the injecting code css via !important.
2.Even though you can't predict the css of the parents you can only have some basic CSS thing for your injected code.
Example
You can use css immediate child selector '>'
in your example
.parent>h4{
margin-left: 10px;
}
.parent>strong{
color:red;
}
check the updated demo
http://jsfiddle.net/WRDft/11/
Refer: http://msdn.microsoft.com/en-in/library/ie/aa358819(v=vs.85).aspx
CSS '>' selector; what is it?
This question has already been asked and discussed.
There is no way to blanket clear styles but there are work arounds.
Reset/remove CSS styles for element only
If I am understanding you correctly and if you know what content is being injected into your child div then the JQuery solution is very simple:
$(".child strong").css({"color":"black"});
$(".child small").css({"text-decoration":"none"});
$(".child h4").css({"margin-left":"0"});
The JQuery code can then be wrapped in any sort of function you desire.
Here is your fiddle with the JQuery added. Hope that helps.
Note: the JQuery selector - for example: $(".child strong") - can be as specific or as general as you like and you can add as many css rules as you like by using a comma separated list like this:
$(".child strong").css({"color":"black", "font-weight":"bold", "text-decoration":"underline", etc, etc});
Thank you all for your thoughts guys, unfortunately, the best way I managed to achieve this is by wrapping my content inside an IFrame
Advantage: Immediate and easy reset
Disadvantage: I cannot manipulate the elements outside of the IFrame
Can any body tell me how I use last-child selector to style my last div of subs?
This is my HTML -
<div class="main">
<div class="subs"></div>
<div class="subs"></div>
<div class="subs"></div>
<div class="subs"></div>
<div class="paginate"></div>
</div>
I tried it something like this in my CSS -
div.main div.subs:last-child {
border: none;
}
But its not working. If I remove paginate div, then it is working. So can I know how can I style last subs div without any extra id or class declaration.
Thank you.
Assuming there is only ever 1 element succeeding your .subs (.paginate), you can use this:
div.main div:nth-last-child(2) {
border:none;
}
See this JSFiddle
This can be seen as a little hacky, and if your paginate element is ever absent, then the wrong sub element will be targeted. Your only other option is to give the .subs their own container and then use :last-child:
Another JSFiddle
P.S: To understand why :last-child isn't working the way you want it to, I really recommend also reading Spudley's answer.
The problem you have is because of :last-child doesn't work the way you think it does.
The :last-child selector will select an element only if it is the last child of its parent.
In the case of your .main element, the last child inside it is the .pagination div. This means that .main>*:last-child can only select the pagination div. It doesn't matter if you filter it down by specifying .subs; you can't select anything else using :last-child because none of the other elements are the last child of .main. If the actual last child element isn't in the filtered selection, it will select nothing rather than selecting something that isn't the last child.
The best way to work around this is to wrap your subs elements inside an additional layer of markup, so that the last one then does become the last child of that container element. Either that, or move the pagination element outside of the main element; whatever works best for your layout.
The other selector you might have tried, :last-of-type works in a similar way. For the time being, there isn't a CSS selector you can use instead to pick the last .subs element, using your current markup. (unless you're happy to go with :nth-last-child(2) which will pick the second-last child, on the assumption that the pagination div will always be present).
In the new selectors being designed for CSS4, there is a set of 'match' selectors that would do exactly what you want to do. You would use :nth-last-match(1) to get the last matching element. This is the selector you need. Unfortunately, it isn't available in current browsers, and there's no real hint yet as to when (or even whether) it will be available in the future. For the time being, you can read about it here, but not use it. You might be able to use it or something similar via a JS library like jQuery.
Hope that helps explain things to you.
I would suggest that you add an extra class name to the last element. http://jsfiddle.net/5FQck/
div.main div {
border: #000 thin solid;
}
div.main div.subs.last {
border: none;
}
<div class="main">
<div class="subs">subs</div>
<div class="subs">subs</div>
<div class="subs">subs</div>
<div class="subs last">subs</div>
<div class="paginate">pagination</div>
</div>
None of the following selectors work in IE 8 and below, primarily because they are all CSS3 selectors.
:nth-child(N)
:nth-last-child(N)
:nth-of-type(N)
:nth-last-of-type(N)
You could also add that new class to the last element using JQuery: http://jsfiddle.net/5FQck/1/
$('div.main div.subs:last').addClass('last');
If I understand you correctly, I would do it like this.
.main .subs:nth-child(4)
{
border:none;
}
I have the following HTML markup:
<h1>
<div class="sponsor">
<span>Hello</span>
</div>
World
</h1>
When I use the CSS selector h1 I get Hello World.
I can't unfortunately change the markup and I have to use only CSS selectors because I work with the system that aggregates RSS feeds.
Is there any CSS selector which I can take only the text node? Specifically the World in this example?
The current state of CSS can't do this, check this link: W3C
The problem here is that the content you write to the screen doesn't show up in the DOM :P.
Also ::outside doesn't seem to work yet (at least for me in Safari 6.0.3) or it simply doesn't generate the desired result yet.
Check my fiddle and then check the DOM source: JSfiddle
Finally there are attribute selectors a { content: attr(href);}, making CSS able to read DOM-node attributes. There doesn't seem to be a innerHTML equivalent of this yet. It would be great tho if that was possible, whereas you might be able to manipulate the inner markup of a tag.
Bit of a workaround:
h1 {
color: red;
}
h1 * {
color: lime;
}
<h1>
<div class="sponsor">
<span>Hello</span>
</div>
World
</h1>
This is almost the opposite of a question I asked last week: Is it possible to select the very first element within a container that's otherwise pure text without using classes or identifiers in pure CSS?
The short answer is no. "World" in this example isn't an element of its own - therefore there isn't a way to select it.
What you would have to do here is style the h1 then override that styling with div.sponsor. For instance, if you wanted "World" here to have a black background with white text you woud use something similar to:
h1 {
background:black;
color:white;
}
h1 div.sponsor {
background:white;
color:black;
}
Unfortunately, however, this wouldn't work if you were only wanting the word "World" styled and your markup had more than just that within <div>Hello</div> World Foo, for instance.
I don't believe it would be possible with pure CSS to style just "World" in this situation.
I also met same problem, where I can't touch the markup and have no control with js.
I needed to hide a text nodes in a div element, but the element to remain visible.
So here is my solution:
markup:
<div id="settings_signout_and_help">
<a id="ctl00_btnHelpDocs" class="ico icoHelp" href="http://" Help Guide</a>
Signed in as: <a id="ctl00_lUsr" href="Profile.aspx">some</a>
Home
Sign out
</div>
css:
#settings_signout_and_help {
font-size: 1px !important;
}
#settings_signout_and_help a {
font-size: 13px !important;
}
Hope this helps guys!
I had a similar problem where I had to remove the "World" text from html generated by a C# function.
I set the font-size to 0 on the 'h1' element and then applied my css to div class. Basically hiding the extra text, but keeping content in the div.
I don't know how to do it with just CSS, but...
Using JQuery, you could select all the elements inside except the stuff inside its child element
$("h1:not(h1 > div)").css()
and put whatever CSS effect you want inside there.
My drop down menu works perfect in XML and not in HTML. Please view this fiddle for a visual presentation: http://jsfiddle.net/H8FVE/9/
If you hover your mouse over the MORE button, you will see that the drop down aligns fine. The code I use for the drop down bit is:
<moretopbar>
<ul>
<li class="mgames">Games</li>
<li class="mliterature">Literature</li>
<li class="marts">Arts</li>
<li class="mcontact" style="background:none;">Contact</li>
</ul>
</moretopbar>
And some CSS:
#mega div moretopbar {
clear: both;
float: left;
position: relative;
margin-left:1px;
margin-right:1px;
width: 495px;
height: 74px;
background-image: url(images/morebgwide.png);
background-size:495px 74px;
background-repeat:no-repeat;
}
I tried to change the <moretopbar> to <div id="moretopbar"> but it messed up the drop down completely.
Why is that? And how should I fix it so that I use HTML only? Feel free to update the fiddle if you choose to answer: http://jsfiddle.net/H8FVE/9/
Before you choose to answer I should note two things. Firstly, I am not familiar with XML, the above is a coding mistake that randomly worked for me (and someone pointed out it was XML), and secondly, is there a reason of why I shouldn't use it this way? For instance compatibility issues...
Your HTML is totally invalid. You are opening a lot of divs and not closing them all. That is why your design breaks when you introduce one more div.
Please fix your HTML. You can use W3c's Online Validator to see problems with your markup. Make writing valid HTML your habit, otherwise expect to get "strange" errors like this.
In this fiddle, I made the following changes:
Moved the ID to your <ul> and got rid of <moretopbar>:
<ul id="moretopbar">
Changed the selector to: #mega div #moretopbar.
It "works" because the ID is now on an ul, not a div - as I already mentioned, the browser cannot really identify which div is which because of the lack of closing tags. Unless you fix this problem you are very very likely to see other strange bugs with your current divs.
Edit: Also the following CSS rules need to be more specific than simply saying div:
#mega div {...}
#mega li.dif:hover div {...}
For example you can use a specialdiv class on the div you mean these rules for, and use .specialdiv instead of div in the rules.
Working jsFiddle Demo
You forgot to declare moretopbar as an id aka #moretopbar
I have a grid of news articles and I wanting it so the last two articles in the grid do not have a bottom border, however my css last-child selector does not seem to be working, the last article with the class right has the border taken off, however the last article with the class left does not, is there a reason for this?
Here is a fiddle of my code and problem.
http://jsfiddle.net/Udders/HJE5h/
As mentioned by #BoltClock above, swap the broder-bottom for border-top and target the first-child instead. SOme older browsers do not support last-child:
JSFiddle: http://jsfiddle.net/HJE5h/2/
Edit
Ok, as #BoltClock mentions in the comment below, the problem is not entirely with the last-child issue. However, if you do use border-top as suggested above and then target the next select element that directly follows the first-child, you can remove border-top from the first two articles.
section:first-child .snippet, section:first-child + section .snippet {
background:none;
border-top:none;
}
JSFiddle: http://jsfiddle.net/HJE5h/5/
You can achieve that by using nth-last-child(n) pseudo class. It begins at the end of the collection and this way you can specify the last two elements without knowing the size of the collection. Please try this selector in your css code:
.grid_9:nth-last-child(1) .snippet, .grid_9:nth-last-child(2) .snippet {
background: none;
border-bottom: none;
}
This is a good reference for useful css selectors http://net.tutsplus.com/tutorials/html-css-techniques/the-30-css-selectors-you-must-memorize/