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.
Related
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 {
}
I'm working with owl-caroussel plugin, and I need to apply a border-right style to odd divs, when I apply this selector it just adds the border to all divs of class "info", it doesn't matter if it's even or odd:
section#presentation section#last div.info:nth-of-type(odd){
border-right: 1px solid #ede1d9;
}
I must be doing something wrong, this is the code in the razor view:
<section id="last">
<div class="row">
<div class="col-lg-9 col-md-8 col-sm-12 col-xs-12 newswidth">
<div id="news-wrap">
<div id="owl-example" class="owl-carousel owl-theme">
<!--item-->
#{ var counter = 0;}
#foreach (var itemBlogList in Model.BlogList)
{
if (counter == 4)
{
break;
}
<div class="info">
#itemBlogList.Title
<div class="credentials">
<div class="author">PETER SCHULTZ</div>
<div class="date">#itemBlogList.PublishDate</div>
</div>
<div class="cut">
#Html.Raw(#itemBlogList.Text)
</div>
</div>
counter++;
}
</div>
</div>
</div>
</div>
</section>
How can I add that border to only odd divs of class "info"?
Take a look at this: https://www.w3.org/Style/Examples/007/evenodd.en.html
.test:nth-child(odd) {
background: #ddd;
}
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
Try to use this selector instead:
section#presentation section#last .owl-carousel > div:nth-child(odd){
border-right: 1px solid #ede1d9;
}
Yours will count all DIVs, (nth-of-type, i.e. DIV), not just the ones with class .info
To get the odd ones use this pseudo class
.info:nth-child(2n-1) {
your styles
}
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);
I need to span a col-md-6 div over 2 rows dynamically, i.e. only if a certain angular var is true.
If this var is false this div wont span over 2 rows.
All the questions I found about this topic didn't addressed the dynamic aspect.
Here are the two options, the div marked with X needs to change dynamically.
Option 1 when var is true
Option 2 when var is false
The html I have right now is this:
<div class="container">
<div class="col-md-7 col-lg-7">
<div class="row">
<p>text</p>
</div>
<div class="row">
<p>text </p>
</div>
</div>
<div class="col-md-5 col-lg-5">
<div class="row">
<div ng-class="show ? ['col-lg-6','col-md-6'] : ['col-lg-12','col-md-12']">
<p>text</p>
</div>
<div ng-if="show" class="col-md-6 col-lg-6">
<p>text</p>
</div>
</div>
<div class="row">
<div class="col-md-6 col-lg-6">
<p> text</p>
</div>
<div class="col-md-6 col-lg-6">
<p> text</p>
</div>
</div>
<div class="row">
<div class="col-md-6 col-lg-6">
<p>text</p>
</div>
<div ng-if="show">
<p>text</p>
</div>
</div>
</div>
</div>
You can use bootstrap to dynamically adjust the width of any div by using .container-fluid or .row-fluid classes. It will automatically take the width of the container whenever the viewport or contents within it's parent change. But to dynamically adjust the height bootstrap won't help, you will have to write your own custom css.
The approach i have taken is to use flexbox css layout model. You can use this link to learn more about it.
Set the display property of the parent div to display:flex and flex-direction:column. This will display the child elements vertically. Now give flex property to each of the child divs in the ratio that you would like them to be displayed. For example i have used flex:1 to both childs. This will display them with equal heights.
Now apply the ng-if directive . When the bottom div gets removed from the DOM, the top div automatically fills up the parent container.
html:
<div ng-controller="MyCtrl">
<label>Show Y</label> <input type="checkbox" ng-model="checked" ng-init="checked=true">
<div class="container">
<div class="row-fluid">
<div class="col-xs-6">Hello</div>
<div class="col-xs-6 wrap">
<div class="top">X</div>
<div class="bottom" ng-if="checked">Y</div>
</div>
</div>
</div>
css:
.wrap{
background-color:#e3e;
height:40px;
display: -webkit-flex;
display: flex;
flex-direction: column;
}
.top{
background-color:#ccc;
flex:1;
}
.bottom{
background-color:#afc;
flex:1;
}
Here is a working fiddle.
My HTML code:
<div class="span4">1</div>
<div class="span4">2</div>
<div class="span4">3</div>
<div class="span12">banner</div>
<div class="span4">4</div>
<div class="span4">5</div>
<div class="span4">6</div>
<div class="span4">7</div>
<div class="span4">8</div>
<div class="span4">9</div>
Css:
.span4:nth-child(3n+1){
color: red;
}
In result there must be 1, 4, 7 red colored. But it's not.
Example: http://jsfiddle.net/473UR/
How to solve that problem?
.nth-child works with the consecutive element. If there is any other elements comes in between it starts calculating from the first.
<div class="span4">1</div> <-- this is first-child -->
<div class="span4">2</div>
<div class="span4">3</div>
<div class="span12">banner</div>
<div class="span4">4</div> <-- sequence reset.
<div class="span4">5</div>
<div class="span4">6</div>
<div class="span4">7</div>
<div class="span4">8</div>
<div class="span4">9</div>
That's because .span12 counts as a child too. If you remove it, it will work
You need to move your banner div and it will work
EXAMPLE
<div class="span4">1</div>
<div class="span4">2</div>
<div class="span4">3</div>
<div class="span4">4</div>
<div class="span4">5</div>
<div class="span4">6</div>
<div class="span4">7</div>
<div class="span4">8</div>
<div class="span4">9</div>
<div class="span12">banner</div>