How can I select an nth element without knowing the element in CSS?


Tags: css,css-selectors

Problem :

I want to select the first child inside the div with CSS. But I don't know what the element is:

<div>
   <youdontknowwhat>One</youdontknowwhat>
   <input>
</div>

Something like:

div>x:first-child{  }

But 'x' is not usable... Any ideas?

A little different from: How can I select an nth element without knowing the parent element?



Solution :

If you want to select the first child of the <div> you can use the following solution:

div > :first-child {
  color:red;
}
<div>
  <p>Test #1</p>
  <div>Test #2</div>
</div>


    CSS Howto..

    How do I center the body text div?

    How to have multiple borders on div using css

    How to make an external HTML file not appear in the home page?

    How to completely change the css position of an element on the page to appear in another part of the document flow?

    IE11: How to prevent sprite animating to correct co-ords

    How to debug web page rendering compatibility issues across browsers?

    How can I center the nav menu items using bootstrap 3?

    how to select image using css

    How can I vertically and horizontally center a DIV?

    How to add 10 pixels to a dynamic top position in CSS using SASS

    How can I set the System.Web.Optimization bundler to correctly transform ISO unicode

    How to Create CSS Selector Element including index

    How can i go back, forward and print a (local) WebBrowser1.DocumentText html page that's sourced from my.resorces - vb.net

    How do I offset where my fixed nav bar takes me?

    How can I make the first letter push itself into the paragraph like a newspaper style

    How do I select all div-elements where first-child is not a table

    How do I keep my footer at the bottom of the page without it coming up automatically to fill space if there is little content?

    How do I show a background image in the first part of a page and make it cover 100% of the visible part of the screen?

    How to check css in unit test

    how do you add footer? like all right's reserved for a CSS design?

    How to solve this (seemingly simple) formatting problem in CSS?

    How to make sure that different severity Toasters are shown differently?

    CSS+HTML: How to draw table/cell border

    How to bottom align a title of variable length

    How to prevent css keyframe animation to run on page load?

    Handle text as elements, how to hide with CSS?

    How to render a template with dynamic CSS?

    How to get rid of “margin”

    How can I control the height of an Option element in Webkit?

    How can I get li items to slide and/or fade into view on this jQuery list?