How to properly use nth-child css selector [duplicate] - html

This question already has answers here:
Why doesn't nth-of-type/nth-child work on nested elements?
(1 answer)
nth-of-type vs nth-child
(7 answers)
Closed 10 months ago.
I am trying to implement some css with nth child selector and unfortunately it's not working.
div.abc input:nth-child(3)
{
background: red;
color: red;
border: 1px solid red;
}
<div class="abc">
<p>This is some text.</p>
<div class="bc">
<input type="text"/>
</div>
<div class="bc">
<input type="text"/>
</div>
<div class="bc">
<input type="text"/>
</div>
<div class="bc">
<input type="text"/>
</div>
</div>

If you want the third input:
.abc div:nth-child(4) input
/* OR */
.abc div:nth-of-type(3) input
nth-child will seek all direct descendant tags so that would be:
<p> -> <div> -> <div> -> <div>
nth-of-type will seek all direct descendants of a specific type:
<div> -> <div> -> <div>
.abc div:nth-child(4) input {
color: gold;
}
.abc div:nth-of-type(3) input {
background: red;
border: 1px solid red;
}
<div class="abc">
<p>This is some text.</p>
<div class="bc">
<input type="text" />
</div>
<div class="bc">
<input type="text" />
</div>
<div class="bc">
<input type="text" value='TEST'/>
</div>
<div class="bc">
<input type="text" />
</div>
</div>

Your input tags are not siblings. They are wrapped in divs, so you need to select the nth-child of a div element and get the input inside that div.
.abc > div:nth-child(4) > input{
background: red;
color: yellow;
border: 1px solid red;
}
<div class="abc">
<p>This is some text.</p>
<div class="bc">
<input type="text" />
</div>
<div class="bc">
<input type="text" />
</div>
<div class="bc">
<input type="text" />
</div>
<div class="bc">
<input type="text" />
</div>
</div>

What your code is doing is not valid. nth-of-child is a selector for direct children of the parent container - in this case abc - and this parent element does not have any direct input-elements as children.
Instead, you need to target the bc-elements, as they are direct children of the the parent element abc, and then target the input.
In this example, I used nth-of-type. They are similiar, but nth-of-type targets DOM-elements directly, which are divs in this example.
div.abc .bc:nth-of-type(3) input {
background: red;
color: red;
border: 1px solid red;
}
<div class="abc">
<p>This is some text.</p>
<div class="bc">
<input type="text" />
</div>
<div class="bc">
<input type="text" />
</div>
<div class="bc">
<input type="text" />
</div>
<div class="bc">
<input type="text" />
</div>
</div>

Related

How do I target div class within a form?

I am trying to target what and where, as well as
search.
Here is my html:
<div class="entry-content">
<p> </p>
<div class="entry-content">
<div class="job_listings" data-location="" data-keywords="" data-show_filters="true" data-show_pagination="false" data-per_page="10" data-orderby="featured" data-order="DESC" data-categories="">
<form class="job_filters">
<div class="search_jobs">
<div class="search_keywords">
<label for="search_keywords">What?</label><br />
<input id="search_keywords" name="search_keywords" type="text" value="" placeholder="Chef, Cleaner, Manager" />
</div>
<div class="search_location">
<label for="search_location">Where?</label><br />
<input id="search_location" name="search_location" type="text" value="" placeholder="London, Berlin, Bristol" />
<input name="filter_job_type[]" type="submit" value="search" />
</div>
</div>
<div class="showing_jobs"></div>
<div></div>
<div>
Post A Job
</div>
</form>
</div>
</div>
<p><a class="load_more_jobs" style="display: none;" href="#"><strong>Load more listings</strong></a></p>
</div>
Tried everything including.
.entry-content {color: green;}
Interestingly,
. entry-content {background-color: green;}
works, but
.entry-content {color: green! important;}
does nothing.
you can use the following style to target all the labels inside entry-content. This works unless the style is not overrided by other styles
.entry-content label{
color:green;
}
For specific labels, you can use,
.search_keywords label{
color:green;
}
.search_location label{
color:green;
}
UPDATE
For input fields like chef,manager etc you can use
.search_keywords input{
//Your style here
}
or since you have id for the input field, use
#search_keywords{
//Style here
}
and for the submit button search, you can use the following,
.search_location input[type=submit]{
//style here
}
You can target by attribute and value:
label[for="search_keywords"]{ /* what label*/
background-color: red;
}
label[for="search_location"]{ /* where label */
background-color: blue;
}
input[type="submit"]{ /* search button */
background-color: green;
}
The color attribute refers to the text color, not the background colour.
Try attribute selectors
[for="search_keywords"],
[for="search_location"],
[name="filter_job_type[]"] {
color: green;
}
<div class="entry-content">
<p> </p>
<div class="entry-content">
<div class="job_listings" data-location="" data-keywords="" data-show_filters="true" data-show_pagination="false" data-per_page="10" data-orderby="featured" data-order="DESC" data-categories="">
<form class="job_filters">
<div class="search_jobs">
<div class="search_keywords">
<label for="search_keywords">What?</label><br />
<input id="search_keywords" name="search_keywords" type="text" value="" placeholder="Chef, Cleaner, Manager" />
</div>
<div class="search_location">
<label for="search_location">Where?</label><br />
<input id="search_location" name="search_location" type="text" value="" placeholder="London, Berlin, Bristol" />
<input name="filter_job_type[]" type="submit" value="search" />
</div>
</div>
<div class="showing_jobs"></div>
<div></div>
<div>
Post A Job
</div>
</form>
</div>
</div>
<p><a class="load_more_jobs" style="display: none;" href="#"><strong>Load more listings</strong></a></p>
</div>

popup at different position where the cursor clicks css

Here is my code:
<div class="checkbox">
<input id="230am" type="checkbox" onclick="openPopup()"> <label for="230am"></label>
<div id="popupBk">
<div id="title">Reminder</div>
<div id="timeSelect">
Start time: <input id="field1" /><br /><br />
End time: <input id="field2" /><br /><br />
</div>
<button onclick="processTime('field1','field2')" name="submit" id="submitButton"/>Create</button>
<div id="close_popup" title="Close this menu" onclick="closePopup()">
<p>X</p>
</div>
</div>
</div>
<div class="checkbox">
<input id="3am" type="checkbox"> <label for="3am"></label>
<div id="popupBk">
<div id="title">Reminder</div>
<div id="timeSelect">
Start time: <input id="field1" /><br /><br />
End time: <input id="field2" /><br /><br />
</div>
<button onclick="processTime('field1','field2')" name="submit" id="submitButton"/>Create</button>
<div id="close_popup" title="Close this menu" onclick="closePopup()">
<p>X</p>
</div>
</div>
</div>
Below are my css:
#popupBk{
position: absolute;
width: 25%;
height: 20%;
border: 2px solid grey;
border-radius: 2px;
background-color: white;
margin-left: 3%;
box-shadow: 2px 2px 10px;
display: none;
}
So I am trying to do a calendar app. I put two popup in two checkboxes just want to do that different popup will display under each checkbox once I click. Any ideas how to do this?
i think there is a way to make each class name differently and then create its own css, but if that it might needs a huge work. Or is there any other way?
Thanks.
You can use the :checked state of checkboxes in CSS, coupled with the general sibling selector (~) to do this.
Read about sibling selectors here: https://css-tricks.com/child-and-sibling-selectors/
Here's a live example:
.wrapper {
float: left;
}
.hidden {
border: 1px solid #000;
display: none;
}
input[type="checkbox"]:checked ~ .hidden {
display: block;
}
<div class="wrapper">
<input type="checkbox" id="box1" /> <label for="box1">Show Box 1</label>
<div class="hidden">Box 1</div>
</div>
<div class="wrapper">
<input type="checkbox" id="box2" /> <label for="box2">Show Box 2</label>
<div class="hidden">Box 2</div>
</div>

combination selector - Plus selector in css not working

May be i don't understand fully plus selector,
What i want, when user click on radio button home, div one should get displayed,
and when user click on radio button about, div two should get displayed, it did not work,
So i strip down the code, where is the problem, with this code i accepted div one to get displayed as home is by default checked. But it did not happened, so i know where is the problem but i dont know why,
Please read the comment, in the code, as i said which line is giving the problem hint it's css last section,
HTML CODE
<div class="container">
<input type="radio" name="option" id="home" checked />
<input type="radio" name="option" id="about" />
<div class="navigation">
<label for="home" class="link">Home</label>
<label for="about" class="link">About Us</label>
</div>
<div class="display">
<div class="one">
<h3>This is first</h3>
</div>
<div class="two">
<h3>This is second</h3>
</div>
</div>
</div>
CSS CODE
.navigation {margin-top:20px;}
.link{cursor:pointer;}
/*making div display*/
.one,.two{
display:none;
}
/*
###This line is not working## want to display div, if user click on radio
button
*/
#home:checked +.container > .one{
display:block;
}
if you want to run the code here is the code pen link https://codepen.io/arif_suhail_123/pen/KvdWey
.container is not a sibling of #home.
To select the element in question, when #home is checked, you can use the ~, which is the general sibling selector:
#home:checked ~ .display > .one
.navigation {margin-top:20px;}
.link {cursor:pointer;}
.one, .two {
display:none;
}
#home:checked ~ .display > .one {
display:block;
}
#about:checked ~ .display > .two {
display: block;
}
<div class="container">
<input type="radio" name="option" id="home" checked />
<input type="radio" name="option" id="about" />
<div class="navigation">
<label for="home" class="link">Home</label>
<label for="about" class="link">About Us</label>
</div>
<div class="display">
<div class="one">
<h3>This is first</h3>
</div>
<div class="two">
<h3>This is second</h3>
</div>
</div>
</div>
The + is the adjacent sibling combinator. Which requires:
The elements to be siblings
The selector on the left of + is the first positioned element
The selector on the right of + is the selector that follows.
There must be no other elements between them.
In the following demo:
Each radio was moved in front of the div it's associated with.
Each radio is display:none since there's no need to show them because the interface are the labels.
Demo
input[name='option'],
.one,
.two {
display: none
}
#home:checked+.one {
display: block;
}
#about:checked+.two {
display: block;
}
<div class="container">
<div class="navigation">
<label for="home" class="link">Home</label>
<label for="about" class="link">About Us</label>
</div>
<div class="display">
<input type="radio" name="option" id="home" checked />
<div class="one">
<h3>This is first</h3>
</div>
<input type="radio" name="option" id="about" />
<div class="two">
<h3>This is second</h3>
</div>
</div>
</div>
I believe for the plus operator to work the element has to be the immediate next sibling - So in this case the .one div would have to immediately follow the #home label, and the css would have to be:
#home:checked + .one{
display:block;
}
The html:
<div class="container">
<input type="radio" name="option" id="home" checked />
<div class="one">
<h3>This is first</h3>
</div>
<input type="radio" name="option" id="about" />
...
+ Selector : The element+element selector is used to select elements that is placed immediately after (not inside) the first specified element.
~ Selector : The element1~element2 selector matches occurrences of element2 that are preceded by element1.
So,you must use ~ instead of +.
.navigation {
margin-top:20px;
}
.link{
cursor:pointer;
}
.one,.two{
display:none;
}
#home:checked ~ .display > .one{
display:block;
}
#about:checked ~ .display > .two{
display:block;
}
<div class="container">
Home: <input type="radio" name="option" id="home" checked />
About: <input type="radio" name="option" id="about" />
<div class="navigation">
<label for="home" class="link">Home</label>
<label for="about" class="link">About Us</label>
</div>
<div class="display">
<div class="one">
<h3>This is first</h3>
</div>
<div class="two">
<h3>This is second</h3>
</div>
</div>
</div>
The order of elements is important when using this selector.
So to use the ~ operator the element should be after the first part.
Ex.
input[type=radio]:checked ~ label {
display: none;
}
The Html should be:
<div class="radio-groupe">
<input type="radio" name="water" id="choice-2" value="more-than-8-cups-a-day">
<label for="choice-2">More</label>
</div>
and not:
<div class="radio-groupe">
<label for="choice-2">More</label>
<input type="radio" name="water" id="choice-2" value="more-than-8-cups-a-day">
</div>

CSS not selector - missing a comma in function argument

I have a css:
.form-group input[type=checkbox]:not(.form-group div.checkbox input[type=checkbox]) {
margin-top: 10px;
}
Which aims to have a margin-top for those checkbox without div.checkbox
but I want to use default css for those checkboxes inside div.checkbox
However, I got this warning when I check the css, what is the proper way to do this? thanks.
The below won't work because the CSS :not() selector takes only simple selectors as argument and as per the W3C Specs, a simple selector is either a type selector, universal selector, attribute selector, class selector, ID selector, or pseudo-class.
.form-group input[type=checkbox]:not(.form-group div.checkbox input[type=checkbox]) {
margin-top: 10px;
}
The argument that is used in the above selector is a sequence or chain of simple selectors.
The negation pseudo-class selector is in my opinion the most complex CSS selector to employ and it gets extremely messy if even one element which doesn't respect the rule is introduced somewhere in the middle. For example, a selector div:not(.checkbox) input[type=checkbox] will exclude only the second checkbox in the below structure.
div:not(.checkbox) input[type=checkbox] {
margin-top: 10px;
outline: 2px solid red;
}
<div class='something-else'>
<input type='checkbox' />
</div>
<div class='checkbox'>
<input type='checkbox' />
</div>
<div class='checkbox'>
<div class='something-else'>
<input type='checkbox' />
</div>
</div>
<div class='something-else'>
<div class='checkbox'>
<input type='checkbox' />
</div>
</div>
You may have expected the last two checkboxes to also have been excluded because they have one div.checkbox ancestor above them but that doesn't happen because they also happen to have 1 non div.checkbox ancestor which is making the element get matched. This is why it becomes complex to use this selector.
If your HTML structure is something like the one in the below snippet then you could use combination of direct-child selectors along with the negation pseudo-class to style only checkboxes that aren't part of a div.checkbox ancestor.
.form-group > div:not(.checkbox) input[type=checkbox],
.form-group > input[type=checkbox] {
margin-top: 10px;
outline: 2px solid red;
}
<form class='form-group'>
<input type='checkbox' />
<div class='checkbox'>
<input type='checkbox' />
</div>
<div class='not-checkbox'>
<input type='checkbox' />
</div>
<div class='checkbox'>
<div class='something-else'>
<input type='checkbox' />
</div>
</div>
<div class='not-checkbox'>
<div class='something-else'>
<input type='checkbox' />
</div>
</div>
</form>
Or else, you could write the rules for all checkboxes generically and then override for those which are present within the div.checkbox.
.form-group input[type=checkbox] {
margin-top: 10px;
outline: 2px solid red;
}
.form-group div.checkbox input[type=checkbox] {
margin-top: 0px;
outline: none;
}
<form class='form-group'>
<input type='checkbox' />
<div class='checkbox'>
<input type='checkbox' />
</div>
<div class='not-checkbox'>
<input type='checkbox' />
</div>
<div class='checkbox'>
<div class='something-else'>
<input type='checkbox' />
</div>
</div>
<div class='not-checkbox'>
<div class='something-else'>
<input type='checkbox' />
</div>
</div>
</form>

how to display 3 input box with text & submit button in a row

Following is the code :
in this code the 1st box is not coming in a row else other 2 are having no problem
<div class="foo">
<div class="bar">1<input id="J" type="textt" style="width:40% height:20%" autocomplete="off" autofocus class="textboxx"/>
</div>
<div class="bar"><input type="submit"></div>
</div>
</br>
<div class="bar">2<input id="J" type="textt" style="width:40% height:20%" autocomplete="off" autofocus class="textboxx"/>
</div>
<div class="bar"><input type="submit"></div>
</div>
</br>
<div class="bar">3<input id="J" type="textt" style="width:40% height:20%" autocomplete="off" autofocus class="textboxx"/>
</div>
<div class="bar"><input type="submit"></div>
</div>
& the CSS
.foo {
display: table;
width: 100%;
}
.bar {
display: table-cell;
}
.bar:first-child, input[type="text"] {
width: 20%;
}
input {
box-sizing: border-box;
}
Help regarding this asap.
Check this Fiddle
CSS:
.foo {
display: block;
width: 100%;
}
.row
{
display:block;
}
.bar {
display: inline-block;
}
input {
box-sizing: border-box;
}
HTML:
<div class="foo">
<div class = "row">
<div class="bar"> 1
<input id="J" type="text" style="width:40% height:20%" autocomplete="off" autofocus class="textboxx"/>
</div>
<div class="bar"><input type="submit"/></div>
</div>
<div class = "row">
<div class="bar"> 2
<input id="J" type="text" style="width:40% height:20%" autocomplete="off" autofocus class="textboxx"/>
</div>
<div class="bar"><input type="submit"/></div>
</div>
<div class = "row">
<div class="bar"> 3
<input id="J" type="text" style="width:40% height:20%" autocomplete="off" autofocus class="textboxx"/>
</div>
<div class="bar"><input type="submit"/></div>
</div>
just change the width from 20px to 29.5%
.bar:first-child, input[type="text"] {
width: 29.5%;
}
DEMO:http://jsfiddle.net/LrshX/
use table instead of div. give every input in a
<td> <input type ="text"\> <\td>
tag
Change foo class from display:table to display:inline
I think you are overcomplicating things for no reason. And you have messed up (somewhat) your HTML tags, your CSS styles etc.
What (I believe) you are after, could be achieved (among many ways) like this:
HTML
<div class="foo">
<div class="bar">
1
<input type="text" id="J1" class="textbox"
autocomplete="off" autofocus />
</div>
<div class="bar"><input type="submit" /></div>
</div>
<br />
...
CSS
.foo {
display: table;
width: 100%;
}
.bar { display: table-cell; }
.bar:first-child { width: 20%; }
input[type="text"] { width: 80%; }
See, also, this short demo.
Some additional remarks:
Do not use the same id on multiple elements in your DOM, or you're very likely to experience unexpected behaviour.
If you use autofocus on all text-fields, the ladt one will receive the focus.
If you want your submit buttons to actually do something, don't forget to include them in form or bind them using JavaScript.