1

I am trying to give a border color for second div, but I cannot add id or class names to the second div.

Providing the code below, is there any special selector in CSS to achieve it?

<div>
    <div>
        <div id="d3">
            <div>
                <div>
                </div>
            </div>
        </div>
    </div>
</div>
Fred Gandt
  • 3,691
  • 1
  • 29
  • 37

2 Answers2

1

Depending on other considerations

We can use the > selector to specify the immediate child.

div {
    padding: 10px 25px;
    border: 1px solid black;
}

body > div > div {
    border: 2px solid red;
}
<div>
    <div>
        <div id="d3">
            <div>
                <div>
                </div>
            </div>
        </div>
    </div>
</div>
Fred Gandt
  • 3,691
  • 1
  • 29
  • 37
0

You can get the parent of the div element with id d3.

document.getElementById('d3').parentElement.style.border = "solid #FF0000";
<div>
    <div>
        <div id="d3">sss
            <div>
                <div>
                </div>
            </div>
        </div>
    </div>
</div>
LF00
  • 22,077
  • 20
  • 117
  • 225
  • hey I should use only css not js –  May 29 '17 at 01:23
  • @texirv if you just want to use css, you can use [Fred Gandt's way](https://stackoverflow.com/a/44212611/6521116). You also can add `id` for the first `div` , then use `#id > div` to select the second `div`. For not there is now parent selector for css. Refer to [is there a CSS parent selector](https://stackoverflow.com/q/1014861/6521116) – LF00 May 29 '17 at 01:43