-1

Given the following html (I only included three divs below, but let's imagine that there are many more divs )

<div id="1">
    <p>hi</p><p>Peter</p>
</div>
<div id="2">
    <p> hola</p><p>Peter</p>
</div>
<div id="3">
    <p>kaixo</p><p>Peter</p>
</div>

I would like to have a selector that selects all <p> for a given <div id="x">

does CSS3 allow you to design such selector?

Thanks!

koldobika
  • 1
  • 2
  • Yes, I guess; however, I could not find any example online; is it semantic and operationally correct? – koldobika Oct 12 '14 at 06:07
  • Yes, you can read more about the id selector and other selectors [here](http://www.w3schools.com/css/css_selectors.asp). It's worth noting you probably don't want to start your id names with numbers. You can read about why in [this](http://stackoverflow.com/a/5672936/3781639) answer. – Michael Harvey Oct 12 '14 at 06:12
  • Suggest reading a real basic CSS tutorial. You could do worse than starting at https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_Started/Selectors#Information.3A_Selectors_based_on_relationships. –  Oct 12 '14 at 06:34
  • I did ... and several other places; I could not find an example like mine. Thank you for your interest. – koldobika Oct 12 '14 at 06:37

2 Answers2

0

To select all <p>s within a given <div id="x">, you would use

#x p {
    // styling
}

#x selects the element with id="x" and #x p selects all ps that are a descendent of #x

Michael Harvey
  • 228
  • 3
  • 10
0

Considering all ids should be unique, wouldn't it be better to assign a class to the elements you wanted to single out? Then, rather than selecting all ids (in theory would just be one, you would get the elements that you wanted.

<div id="1" class="blue">
    <p>hi</p><p>Peter</p>
</div>
<div id="2" class="blue">
    <p> hola</p><p>Peter</p>
</div>
<div id="3" class="green">
    <p>kaixo</p><p>Peter</p>
</div>

.blue p {
    //styling
}
Ryan Moss
  • 13
  • 4
  • Here it is my global issue: I want to dynamically load a html-string and a CSS-file into one container, and apply these styles from the css-file to the elements of this container. – koldobika Oct 12 '14 at 06:28