Accessing the first <section> within unknown number of divs - html

Given the following markup:
<div className="row">
<noscript></noscript>
</div>
<div className="row">
<noscript></noscript>
</div>
<div className="row">
<section></section>
</div>
<div className="row">
<section></section>
</div>
<div className="row">
<section></section>
</div>
...
There may be any number of divs at the top that contain noscript.
How can I access only the first section in the rows?

It won't be possible to achieve this using CSS only, you need to use Javascript.
Explanation:
Because :nth-of-type(1), nth-child(), :first-of-type and :first-child will always give you all the sections as they are the first child and the first of type section in their parents div.
All these selectors will only work, if you are putting all the sections inside one parent div with class="row".
JavaScript solution:
You can use document.querySelector(".row section") to get the first section in a div with class="row".
But with CSS only this won't be possible:
document.querySelector(".row section").innerHTML = "I am the first section !";
.row {
width: 200px;
height: 50px;
border: 1px solid black;
}
<div class="row">
<noscript></noscript>
</div>
<div class="row">
<noscript></noscript>
</div>
<div class="row">
<section></section>
</div>
<div class="row">
<section></section>
</div>
<div class="row">
<section></section>
</div>
Note:
Also in HTML it's class="row" and not className="row", in fact className is used in Javascript.

Alternatively to #chsdk, you could use the jQuery method of .first()
HTML:
<div class="row">
<noscript></noscript>
</div>
<div class="row">
<noscript></noscript>
</div>
<div class="row">
<section>1</section>
</div>
<div class="row">
<section>2</section>
</div>
<div class="row">
<section>3</section>
</div>
CSS
.row {
display:block;
height:50px;
width:50px;
}
section {
display:block;
height:20px;
width:20px;
}
jQuery
$('.row section').first().css('background','#111');
https://jsfiddle.net/ojuun4uh/

Using JavaScript, the getElementsByTagName method can help you. Try this:
var section = document.getElementsByTagName('section').item(0);

Related

How can i add CSS on that DIV

I have a below div structure and I want to add css on first .column element, not its sibling
<div class="row" id="team">
<div class="column">
<div class="row">
<div class="column">
A
</div>
<div class="column">
B
</div>
<div class="column">
C
</div>
</div>
</div>
</div>
I want to add CSS only first .column that comes just after #team div. So how can I select a class for that .column not for the inner .column?
You would use the direct descendant / child combinator ">" which in effect says - target the .column class that DIRECTLY descends from the #team parent div.
In the following - I am placing a border around the targetted .column div and not around the nested children .column divs.
and if there are other divs that are siblings of that particvular div - then you could use the :first-child pseudo selector as well..
#team > .column:first-child {...}
which says - target the .column div that is a direct descendant AND the first child of the #team div.
#team > .column {
border: solid 1px red;
}
<div class="row" id="team">
<div class="column">
<div class="row">
<div class="column">
A
</div>
<div class="column">
B
</div>
<div class="column">
C
</div>
</div>
</div>
</div>
The most specific selector in this case is #team>.column, with > between parent and child to make sure the nested divs which also have the .column class are not affected.
#team .column would not work in this case, since it also selects the .column divs which are nested in lower instances.
BTW: You mention "siblings", which is a bit confusing, since there are not any siblings to that element...
#team>.column {
background: yellow;
}
<div class="row" id="team">
<div class="column">
<div class="row">
<div class="column">
A
</div>
<div class="column">
B
</div>
<div class="column">
C
</div>
</div>
</div>
</div>
Ok, so I think you may have confused your HTML 'parent/child' structure.
You could use
#team > .column:first-child {
}
However, I don't know if you are aware that you can add any number of classes to HTML elements. You could have many classes to easily distinguish between your components and to be able to grab hold of them with CSS or JS.
For the sake of ease, you could just add another class to the element you want to add another separate class style, as I have below.
Then you could just add CSS styling for that class.
<div class="row" id="team"> //this is parent
<div class="column main"> // a child that I've added the
// class of .main to
<div class="row"> // a grandchild
<div class="column"> // then great grandchildren
A //these are siblings
</div>
<div class="column"> //these are siblings
B
</div>
<div class="column"> //these are siblings
C
</div>
</div>
</div>
</div>
/*Then you would just add stylings for*/
.main {
}

How to avoid CSS impact to dynamically added classes

What actually happens: I have a div in a file there is HTML code, which added Dynamically from BackEnd, CSS Classes(which included from Dynamic code) uses CSS from my style.css
What I need: My own CSS should not impact on dynamically added code.
Similar Solution: iframe is a solution but I want to do same using div tag.
I think this is:
<div class="row" style="all:initial"> do some thing</div>
Better you can write you css with parent-child combination to avoid such issues as like below snippet. Here newly added <p> tag has same class .content but parent has different class name as .dynamic from .static.
jQuery(document).ready(function() {
jQuery('.dynamic').html(jQuery(".static").html());
});
.static .content {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="static">
<p class="content">Some content goes here</p>
</div>
<div class="dynamic">
</div>
Three options from my perspective:
1. Use unique classnames
2. Unique ID
If only classes are added, give the parent div an ID and overwrite the css by using the ID.
<div id="uniqueStyle" class="dynamicDiv">
<div class="row clearfix"></div>
<div class="row clearfix"></div>
<div class="row clearfix"></div>
<div class="row clearfix"></div>
</div>
#uniqueStyle{
background: red; //overwrite
}
#uniqueStyle .row{
background: blue; //overwrite
}
3. Using :not
Give the div an unique ID and use the :not selector to exclude it from styling.
<div id="noStyling" class="dynamicDiv">
<div class="row clearfix"></div>
<div class="row clearfix"></div>
<div class="row clearfix"></div>
<div class="row clearfix"></div>
</div>
:not(#noStyling){
background: blue; //set styling of all elements except #noStyling
}
More info about :not selector: https://www.w3schools.com/cssref/sel_not.asp

How to select children elements until element found

I have content like this:
<div class="content">
<div class="row">
<h1>foo1</h1>
</div>
<div class="row">
foo11
</div>
<div class="row">
foo12
</div>
<div class="row">
<h1>foo2</h1>
</div>
<div class="row">
foo21
</div>
</div>
How do I select the links between h1 tags foo1 and foo2?
I have tried this:
.content .row > :not(h1) a
but this selects on:
foo11
foo12
foo21
and what I want is:
foo11
foo12
Also, the number of div.row after the rows containing h1 is variable.
You essentially have a hierarchy which is not represented in hierarchical form in your HTML. The best solution is to add another level to your HTML which represents the hierarchy.
If you can't do that, and are stuck with this HTML, then you can try with sibling combinators, but in any case, you will need some way to address the foo1 and foo2 elements. That could be a class, or nth-child if you know the order, or data attribute, or anything else. This cannot be something on the <h1> element, since CSS provides no way to go "up and over". It must be a way to address the higher-level row elements containing the h1. In the below, I'll assume you have a class available. In that case:
/* Make everything after `foo1` red. */
.foo1 ~ .row a { color: red; }
/* But make `foo2` and everything after it the original color. */
.foo2.row a, .foo2 ~ .row a { color: inherit; }
<div class="content">
<div class="row foo1">
<h1>foo1</h1>
</div>
<div class="row">
foo11
</div>
<div class="row">
foo12
</div>
<div class="row foo2">
<h1>foo2</h1>
</div>
<div class="row">
foo21
</div>
</div>
The following should work:
.content .row:not(:last-child) a {
background-color: red;
}
<div class="content">
<div class="row">
<h1>foo1</h1>
</div>
<div class="row">
foo11
</div>
<div class="row">
foo12
</div>
<div class="row">
<h1>foo2</h1>
</div>
<div class="row">
foo21
</div>
</div>
Is this kinda what you are looking for jsfiddle
?
for (var i = 0; i < document.getElementsByTagName('a').length; i++) {
var x=document.getElementsByTagName('a')[i];
var t=x.innerHTML;
if (t=='foo11'||t=='foo12') {
x.style.backgroundColor="red"; }
}
You can do it like this with variable div's:
.content > .first ~ .row > a {color: red}
.content > .second ~ .row > a {color: initial}
<div class="content">
<div class="row first">
<h1>foo1</h1>
</div>
<div class="row">
foo11
</div>
<div class="row">
foo12
</div>
<div class="row">
foo13
</div>
<div class="row second">
<h1>foo2</h1>
</div>
<div class="row">
foo21
</div>
<div class="row">
foo22
</div>
<div class="row">
foo23
</div>
</div>
Here the initial keyword sets the color property to its default value and without defining additional two classes for .row's with the h1 tag inside, this can't be done in any other way with pure CSS.

Two divs, each within two divs - how can I change the stacking order using CSS (for responsive)?

Preface: I cannot change the HTML.
To better explain my question, I have provided an illustration below. Essentially, I have two rows of divs - the first row has content, and the second row would have a button beneath the content.
I want to make the page responsive, so that the div with a button always is below its corresponding div with content, but the extra container divs are proving a challenge. Any ideas?
What I have (above) and what I want (below).
Here's the HTML code:
<div class="choice-section">
<div id="choice_1" class="choice odd" style="margin-top: 242px;">
<div class="content">asdf</div>
</div>
<div id="choice_2" class="choice even" style="margin-top: 322px;">
<div class="content">asdf</div>
</div>
</div>
<div>
<div class="vote-choice odd">
<a class="vote btn" href="javascript:void(null)" choice="1">Vote</a>
</div>
<div class="vote-choice even">
<a class="vote btn" href="javascript:void(null)" choice="2">Vote</a>
</div>
</div>
I would strongly reccomend looking at Bootstrap's grid and column system with which this can easily be achieved. This framework will make responsive design a breeze for you.
You can see an example of something pretty similar to what you are trying to achieve in a Plunker I very quickly put together
CSS:
.blue {
background-color:blue;
height:200px;
}
.red {
background-color:red;
height:200px;
}
.purple {
background-color:purple;
height:200px;
}
.green {
background-color:green;
height:200px;
} here
HTML:
<div class="container">
<div class="row">
<div class="col-sm-3">
<div class="red">
</div>
</div>
<div class="col-sm-3">
<div class="blue">
</div>
</div>
<div class="col-sm-3">
<div class="purple">
</div>
</div>
<div class="col-sm-3">
<div class="green">
</div>
</div>
</div>

First-child and last-child in separate containers

Can I apply the first-child and last-child pseudo elements to the divs with the same class that are in different sub-containers? In the example below, div with the classes "box selected" are all stored under "1st-container", but there are 2 additional containers on the way:
<div class="1st-container">
<div class="2nd-container">
<div class="3rd-container">
<div class="box selected"> // this div should have first-child pseudo elements
<div class="box">
</div>
</div>
<div class="2nd-container">
<div class="3rd-container">
<div class="box selected">
<div class="box">
<div class="box">
<div class="box">
</div>
</div>
<div class="2nd-container">
<div class="3rd-container">
<div class="box selected"> // this div should have last-child pseudo
<div class="box">
</div>
</div>
</div>
Is that doable with pure css?
You can't do what you've described, because in both cases, "box selected" is the first child of its parent. You could take a different approach, though. For example, you could target those two divs like so:
.2nd-container:first-child .selected,
.2nd-container:last-child .selected
{}
As pointed out, replace the 2 with a non-digit, as that won't work. I was distracted by the main question. :-)
Firstly, you cannot use numbers to start classes or id's.
Secondly, you just need to do a find on the first and last child of the second element in and you should be able to find the elements you need.
.box {
height: 20px;
width: 400px;
background: blue;
}
.box.selected {
background: red;
}
.two-container:first-child .box.selected,
.two-container:last-child .box.selected {
background: green;
}
<div class="one-container">
<div class="two-container">
<div class="three-container">
<div class="box selected">// this div should have first-child pseudo elements</div>
<div class="box"></div>
</div>
</div>
<div class="two-container">
<div class="three-container">
<div class="box selected"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
</div>
<div class="two-container">
<div class="three-container">
<div class="box selected">// this div should have last-child pseudo</div>
<div class="box"></div>
</div>
</div>
</div>
with JS it looks easier than CSS:
var selected = document.querySelectorAll('.one-container .box.selected');
selected[0].style.background = selected[selected.length-1].style.background='green';
FIDDLE