How to select a child element inside a div using CSS - html

I want to select a child element inside a div this is my html code:
<div class='body'>
<text>This is a text element</text>
</div>
I want to select the text element instead of ading a class or an id. Sorry I am new to css and html please help

If you want to select a child element inside a div you would use >
This is the code:
.body > text{
color : blue;
}
This is simple CSS

Related

how to target an element child in css

Hello everyone in my case I want to target the 3 element for example, and I have the tags different, the 3 element can be p or div or something else, so I want to just target the 3 element what ever it is, i want to select always the 3 element.
Can I do that with CSS, if any help?
For example :
<div>
<p></p>
<span><span>
<div></div> //3 element here is div
<a></a>
</div>
Or :
<div>
<div></div>
<span><span>
<p></p> //3 element here is p
<a></a>
</div>
If you would always have 4 tags, and want to target first 3,
div > *:not(:last-child){
background-color: red;
}
<div>
<p>AAAAAAAAAAAAAAA</p>
<span>AAAAAAAAAAAAAAA</span>
<div>AAAAAAAAAAAAAAA</div>
<a>AAAAAAAAAAAAAAA</a>
</div>
You could also target first 3 elements like below,
div > *:nth-child(-n+3){
background-color: red;
}
<div>
<p>AAAAAAAAAAAAAAA</p>
<span>AAAAAAAAAAAAAAA</span>
<div>AAAAAAAAAAAAAAA</div>
<a>AAAAAAAAAAAAAAA</a>
</div>
if you only want to select first three child of a parent div(or any other element), you can do something like this,
.parent:nth-child(1),
.parent:nth-child(2),
.parent:nth-child(3){
//Your styles
}
Above code would select 1st, 2nd and 3rd child of the parent div to which i gave a class of parent.
Also, if you want to select all the child of that parent div you could simply do as,
.parent > *{
//Your styles
}
this code would select all the child of the .parent div

change div value without ID or class

How do I change the value of DIV if it has no ID or class? here's a sample code:
<div id=1>
<div id=2>
<div id=3>
<div>
<div id=4>
<div><span>CHANGE HERE</span></div>
</div>
</div>
</div>
</div>
</div>
If you want to change the style of your div you could do the following:
#4>div{
}
this means you will apply the class on the child divs of div with id=4.
But you cannot change its value with the use off css. Use js instead.
Also, please change the id's of your divs so it won't be confusing.
Can be accessed by following the DOM tree
For your code
div's can be access like
body div{ }- select the first div tag
body div div{ }- select the second div taglikewise it goes by the tree structure
For accessing the last div and span-> body div div div div div span{ } can be used
By this way you can select and use the tag without specifying the id or class
Check the css pseudo code for more

Styles will only apply if the selected element is the first child of a specific parent in CSS

I want to find how to select the .if-first-child element that's the first element of a specific parent, which in this case is <div>.
<div class="no-css">
<p class="if-first-child">The style will only take effect here!</p>
<p>No style here..</p>
<p class="if-first-child">No style here..</p>
</div>
<div class="no-css">
<nav>
<p class="if-first-child">No style here..</p>
</nav>
</div>
In other words, e.g. I want to apply background-color: black; in the .if-first-child only if it's the first child of <div>.
Keep note that the div p:first-child selector will still select the .if-first-child element even though it have a <nav> parent.
Unintended, I found how to select the target when I'm exposing in the question that the div p:first-child selector will still select the p:first-child element if it have a <div> grandparent.
div > .if-first-child:first-child {
background-color: black;
}
That will only target the first-child .if-first-child which is a direct child of <div>. It will not target a grandchild .if-first-child:first-child.
In simple, you can try this as well...
.if-first-child{color:red}
div .if-first-child{color:green}
It will target only first child to change the color of the text and rest will apply default text color.

How to apply properties over parent elements using css

I have a div with id thdiv with the help of that id. I need to apply css properties over div with id fdiv
I tried this but not able to get the desired result
<div id="fdiv">
Hello
<div id="sdiv">
Anchor
</div>
<div id="thdiv">
Hii
</div>
</div>
div >div {
color:red;
}
If I am not specifying any id then it should change the color of both the child divs. but I need to change the properties of parent div instead of child div.
It is not possible to address the parent of an element only with CSS selectors. You would need some javascript to find the parent of an element.
you can use jquery
$('#thdiv').parent().css({"color":"red"});

Is there a CSS selector for the first direct child only?

I have the following html
<div class="section">
<div>header</div>
<div>
contents
<div>sub contents 1</div>
<div>sub contents 2</div>
</div>
</div>
And the following style:
DIV.section DIV:first-child
{
...
}
For some reason that I don't understand the style is getting applied to the "sub contents 1" <div> as well as the "header" <div>.
I thought that the selector on the style would only apply to the first direct child of a div with a class called "section". How can I change the selector to get what I want?
What you posted literally means "Find any divs that are inside of section divs and are the first child of their parent." The sub contains one tag that matches that description.
It is unclear to me whether you want both children of the main div or not. If so, use this:
div.section > div
If you only want the header, use this:
div.section > div:first-child
Using the > changes the description to: "Find any divs that are the direct descendents of section divs" which is what you want.
Please note that all major browsers support this method, except IE6. If IE6 support is mission-critical, you will have to add classes to the child divs and use that, instead. Otherwise, it's not worth caring about.
Found this question searching on Google. This will return the first child of a element with class container, regardless as to what type the child is.
.container > *:first-child
{
}
CSS is called Cascading Style Sheets because the rules are inherited. Using the following selector, will select just the direct child of the parent, but its rules will be inherited by that div's children divs:
div.section > div { color: red }
Now, both that div and its children will be red. You need to cancel out whatever you set on the parent if you don't want it to inherit:
div.section > div { color: red }
div.section > div div { color: black }
Now only that single div that is a direct child of div.section will be red, but its children divs will still be black.
The CSS selector for the direct first-child in your case is:
.section > :first-child
The direct selector is > and the first child selector is :first-child
No need for an asterisk before the : as others suggest. You could speed up the DOM searching by modifying this solution by prepending the tag:
div.section > :first-child
Use div.section > div.
Better yet, use an <h1> tag for the heading and div.section h1 in your CSS, so as to support older browsers (that don't know about the >) and keep your markup semantic.
div.section > div
Not exactly the question asked, but maybe useful:
div.section > :first-child:is(div)
This would match only the first child element of .section and only if it was a div.
Match:
<div class="section">
<div>MATCH</div>
<div>NO MATCH</div>
<div>
<div>NO MATCH</div>
</div>
</div>
No match:
<div class="section">
<img ... >
<div>NO MATCH</div>
<div>NO MATCH</div>
<div>
<div>NO MATCH</div>
</div>
</div>
This is how I solved when using TailwindCSS (v3.1) with arbitrary variants.
I only wanted the first column in table to be underlined when hovered, as it is a link.
[&>:first-child]:hover:underline