How to select first img tag in a div with many img tag?












10















I have this html :



<div class="image">
<img src=... />
<img src= .../>
</div>


And I would to apply css only to the first image of div of class image without having to add a class to my first img (in that case, I would do .image .img1 but is there another way ?



Thanks a lot for your help










share|improve this question



























    10















    I have this html :



    <div class="image">
    <img src=... />
    <img src= .../>
    </div>


    And I would to apply css only to the first image of div of class image without having to add a class to my first img (in that case, I would do .image .img1 but is there another way ?



    Thanks a lot for your help










    share|improve this question

























      10












      10








      10


      5






      I have this html :



      <div class="image">
      <img src=... />
      <img src= .../>
      </div>


      And I would to apply css only to the first image of div of class image without having to add a class to my first img (in that case, I would do .image .img1 but is there another way ?



      Thanks a lot for your help










      share|improve this question














      I have this html :



      <div class="image">
      <img src=... />
      <img src= .../>
      </div>


      And I would to apply css only to the first image of div of class image without having to add a class to my first img (in that case, I would do .image .img1 but is there another way ?



      Thanks a lot for your help







      css css3 css-selectors






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 22 '13 at 14:44









      ReveclairReveclair

      1,36442951




      1,36442951
























          5 Answers
          5






          active

          oldest

          votes


















          24














          You can use the :first-child selector to do this.



          You could also use :nth-child(1) (where the 1 is = to the first child, 2 is equal to the second child etc.)



          Finally, a more proper way would be the use of :first-of-type



          For example:



          div.image img:first-child {}


          Or:



          div.image img:nth-child(1) {}


          Or (if there are other elements, say an <h1> that comes prior to any images:



          div img:first-of-type


          If you have the potential for having a div with class image which has images inside it and also another div which has images in it, like this:



          HTML:



          <div class="image">
          <img src=... />
          <img src= .../>
          <div class="image">
          <img src=... />
          <img src= .../>
          </div>
          </div>


          Then use these selectors:



          For example:



          div.image > img:first-child {}


          Or:



          div.image > img:nth-child(1) {}


          Or:



          div > img:first-of-type


          Documentation on :first-child and on :nth-child() and on :first-of-type and on child combinator.



          Note that :nth-child() and :first-of-type are CSS3 selectors, which carry some limited support when using IE. See Can I use CSS3 Selectors for browser support details.



          Consider something like Selectivizr to help standardize IE.






          share|improve this answer





















          • 1





            In what way is :first-of-type better than :first-child if all the children are going to be img elements?

            – BoltClock
            Mar 22 '13 at 18:08






          • 2





            Edited my answer. In any case, first-of-type would be a better solution overall as it is more specific to the img tag only. If at any point this HTML is modified to include, for example a heading before the images (which wouldn't be an unreasonable idea), :first-child would break, and the solution would be to use :first-of-type to fix. Why not just cut to the chase and use :first-of-type in the first place? No reason that I can see not to.

            – Michael
            Mar 22 '13 at 18:14








          • 3





            "limited support when using IE" would count as one. Also, Modernizr does not polyfill selector support - you do that with Selectivizr instead.

            – BoltClock
            Mar 22 '13 at 18:30



















          6














          You can do it using first-child selector



          .image img:first-child
          {
          height:500px;
          width:200px;
          border:15px solid Red;
          }


          JS Fiddle Example






          share|improve this answer































            4














            The ':first-child' selector seems like it might be what you need.



            http://www.w3schools.com/cssref/sel_firstchild.asp






            share|improve this answer































              2

















              article>img:first-child {
              border:1px solid #f00;
              }

              <article class="image">
              <img src=... />
              <img src= .../>
              <div class="image">
              <img src=... />
              <img src= .../>
              <img src= .../>

              </div>
              <img src= .../>
              <img src= .../>
              </div>








              share|improve this answer

































                1














                If you want the first image inside a DIV (and there might be other elements preceding it) you need first-of-type rather than first-child



                div > img:first-of-type {}


                consider the following html



                <div>
                <p>paragraph, this is :first-child</p>
                <img src="..."><!-- this is img:first-of-type -->
                </div>


                More details can be found at https://developer.mozilla.org/en-US/docs/CSS/:first-of-type






                share|improve this answer

























                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15573159%2fhow-to-select-first-img-tag-in-a-div-with-many-img-tag%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  24














                  You can use the :first-child selector to do this.



                  You could also use :nth-child(1) (where the 1 is = to the first child, 2 is equal to the second child etc.)



                  Finally, a more proper way would be the use of :first-of-type



                  For example:



                  div.image img:first-child {}


                  Or:



                  div.image img:nth-child(1) {}


                  Or (if there are other elements, say an <h1> that comes prior to any images:



                  div img:first-of-type


                  If you have the potential for having a div with class image which has images inside it and also another div which has images in it, like this:



                  HTML:



                  <div class="image">
                  <img src=... />
                  <img src= .../>
                  <div class="image">
                  <img src=... />
                  <img src= .../>
                  </div>
                  </div>


                  Then use these selectors:



                  For example:



                  div.image > img:first-child {}


                  Or:



                  div.image > img:nth-child(1) {}


                  Or:



                  div > img:first-of-type


                  Documentation on :first-child and on :nth-child() and on :first-of-type and on child combinator.



                  Note that :nth-child() and :first-of-type are CSS3 selectors, which carry some limited support when using IE. See Can I use CSS3 Selectors for browser support details.



                  Consider something like Selectivizr to help standardize IE.






                  share|improve this answer





















                  • 1





                    In what way is :first-of-type better than :first-child if all the children are going to be img elements?

                    – BoltClock
                    Mar 22 '13 at 18:08






                  • 2





                    Edited my answer. In any case, first-of-type would be a better solution overall as it is more specific to the img tag only. If at any point this HTML is modified to include, for example a heading before the images (which wouldn't be an unreasonable idea), :first-child would break, and the solution would be to use :first-of-type to fix. Why not just cut to the chase and use :first-of-type in the first place? No reason that I can see not to.

                    – Michael
                    Mar 22 '13 at 18:14








                  • 3





                    "limited support when using IE" would count as one. Also, Modernizr does not polyfill selector support - you do that with Selectivizr instead.

                    – BoltClock
                    Mar 22 '13 at 18:30
















                  24














                  You can use the :first-child selector to do this.



                  You could also use :nth-child(1) (where the 1 is = to the first child, 2 is equal to the second child etc.)



                  Finally, a more proper way would be the use of :first-of-type



                  For example:



                  div.image img:first-child {}


                  Or:



                  div.image img:nth-child(1) {}


                  Or (if there are other elements, say an <h1> that comes prior to any images:



                  div img:first-of-type


                  If you have the potential for having a div with class image which has images inside it and also another div which has images in it, like this:



                  HTML:



                  <div class="image">
                  <img src=... />
                  <img src= .../>
                  <div class="image">
                  <img src=... />
                  <img src= .../>
                  </div>
                  </div>


                  Then use these selectors:



                  For example:



                  div.image > img:first-child {}


                  Or:



                  div.image > img:nth-child(1) {}


                  Or:



                  div > img:first-of-type


                  Documentation on :first-child and on :nth-child() and on :first-of-type and on child combinator.



                  Note that :nth-child() and :first-of-type are CSS3 selectors, which carry some limited support when using IE. See Can I use CSS3 Selectors for browser support details.



                  Consider something like Selectivizr to help standardize IE.






                  share|improve this answer





















                  • 1





                    In what way is :first-of-type better than :first-child if all the children are going to be img elements?

                    – BoltClock
                    Mar 22 '13 at 18:08






                  • 2





                    Edited my answer. In any case, first-of-type would be a better solution overall as it is more specific to the img tag only. If at any point this HTML is modified to include, for example a heading before the images (which wouldn't be an unreasonable idea), :first-child would break, and the solution would be to use :first-of-type to fix. Why not just cut to the chase and use :first-of-type in the first place? No reason that I can see not to.

                    – Michael
                    Mar 22 '13 at 18:14








                  • 3





                    "limited support when using IE" would count as one. Also, Modernizr does not polyfill selector support - you do that with Selectivizr instead.

                    – BoltClock
                    Mar 22 '13 at 18:30














                  24












                  24








                  24







                  You can use the :first-child selector to do this.



                  You could also use :nth-child(1) (where the 1 is = to the first child, 2 is equal to the second child etc.)



                  Finally, a more proper way would be the use of :first-of-type



                  For example:



                  div.image img:first-child {}


                  Or:



                  div.image img:nth-child(1) {}


                  Or (if there are other elements, say an <h1> that comes prior to any images:



                  div img:first-of-type


                  If you have the potential for having a div with class image which has images inside it and also another div which has images in it, like this:



                  HTML:



                  <div class="image">
                  <img src=... />
                  <img src= .../>
                  <div class="image">
                  <img src=... />
                  <img src= .../>
                  </div>
                  </div>


                  Then use these selectors:



                  For example:



                  div.image > img:first-child {}


                  Or:



                  div.image > img:nth-child(1) {}


                  Or:



                  div > img:first-of-type


                  Documentation on :first-child and on :nth-child() and on :first-of-type and on child combinator.



                  Note that :nth-child() and :first-of-type are CSS3 selectors, which carry some limited support when using IE. See Can I use CSS3 Selectors for browser support details.



                  Consider something like Selectivizr to help standardize IE.






                  share|improve this answer















                  You can use the :first-child selector to do this.



                  You could also use :nth-child(1) (where the 1 is = to the first child, 2 is equal to the second child etc.)



                  Finally, a more proper way would be the use of :first-of-type



                  For example:



                  div.image img:first-child {}


                  Or:



                  div.image img:nth-child(1) {}


                  Or (if there are other elements, say an <h1> that comes prior to any images:



                  div img:first-of-type


                  If you have the potential for having a div with class image which has images inside it and also another div which has images in it, like this:



                  HTML:



                  <div class="image">
                  <img src=... />
                  <img src= .../>
                  <div class="image">
                  <img src=... />
                  <img src= .../>
                  </div>
                  </div>


                  Then use these selectors:



                  For example:



                  div.image > img:first-child {}


                  Or:



                  div.image > img:nth-child(1) {}


                  Or:



                  div > img:first-of-type


                  Documentation on :first-child and on :nth-child() and on :first-of-type and on child combinator.



                  Note that :nth-child() and :first-of-type are CSS3 selectors, which carry some limited support when using IE. See Can I use CSS3 Selectors for browser support details.



                  Consider something like Selectivizr to help standardize IE.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 10 '13 at 18:27

























                  answered Mar 22 '13 at 14:50









                  MichaelMichael

                  5,79011937




                  5,79011937








                  • 1





                    In what way is :first-of-type better than :first-child if all the children are going to be img elements?

                    – BoltClock
                    Mar 22 '13 at 18:08






                  • 2





                    Edited my answer. In any case, first-of-type would be a better solution overall as it is more specific to the img tag only. If at any point this HTML is modified to include, for example a heading before the images (which wouldn't be an unreasonable idea), :first-child would break, and the solution would be to use :first-of-type to fix. Why not just cut to the chase and use :first-of-type in the first place? No reason that I can see not to.

                    – Michael
                    Mar 22 '13 at 18:14








                  • 3





                    "limited support when using IE" would count as one. Also, Modernizr does not polyfill selector support - you do that with Selectivizr instead.

                    – BoltClock
                    Mar 22 '13 at 18:30














                  • 1





                    In what way is :first-of-type better than :first-child if all the children are going to be img elements?

                    – BoltClock
                    Mar 22 '13 at 18:08






                  • 2





                    Edited my answer. In any case, first-of-type would be a better solution overall as it is more specific to the img tag only. If at any point this HTML is modified to include, for example a heading before the images (which wouldn't be an unreasonable idea), :first-child would break, and the solution would be to use :first-of-type to fix. Why not just cut to the chase and use :first-of-type in the first place? No reason that I can see not to.

                    – Michael
                    Mar 22 '13 at 18:14








                  • 3





                    "limited support when using IE" would count as one. Also, Modernizr does not polyfill selector support - you do that with Selectivizr instead.

                    – BoltClock
                    Mar 22 '13 at 18:30








                  1




                  1





                  In what way is :first-of-type better than :first-child if all the children are going to be img elements?

                  – BoltClock
                  Mar 22 '13 at 18:08





                  In what way is :first-of-type better than :first-child if all the children are going to be img elements?

                  – BoltClock
                  Mar 22 '13 at 18:08




                  2




                  2





                  Edited my answer. In any case, first-of-type would be a better solution overall as it is more specific to the img tag only. If at any point this HTML is modified to include, for example a heading before the images (which wouldn't be an unreasonable idea), :first-child would break, and the solution would be to use :first-of-type to fix. Why not just cut to the chase and use :first-of-type in the first place? No reason that I can see not to.

                  – Michael
                  Mar 22 '13 at 18:14







                  Edited my answer. In any case, first-of-type would be a better solution overall as it is more specific to the img tag only. If at any point this HTML is modified to include, for example a heading before the images (which wouldn't be an unreasonable idea), :first-child would break, and the solution would be to use :first-of-type to fix. Why not just cut to the chase and use :first-of-type in the first place? No reason that I can see not to.

                  – Michael
                  Mar 22 '13 at 18:14






                  3




                  3





                  "limited support when using IE" would count as one. Also, Modernizr does not polyfill selector support - you do that with Selectivizr instead.

                  – BoltClock
                  Mar 22 '13 at 18:30





                  "limited support when using IE" would count as one. Also, Modernizr does not polyfill selector support - you do that with Selectivizr instead.

                  – BoltClock
                  Mar 22 '13 at 18:30













                  6














                  You can do it using first-child selector



                  .image img:first-child
                  {
                  height:500px;
                  width:200px;
                  border:15px solid Red;
                  }


                  JS Fiddle Example






                  share|improve this answer




























                    6














                    You can do it using first-child selector



                    .image img:first-child
                    {
                    height:500px;
                    width:200px;
                    border:15px solid Red;
                    }


                    JS Fiddle Example






                    share|improve this answer


























                      6












                      6








                      6







                      You can do it using first-child selector



                      .image img:first-child
                      {
                      height:500px;
                      width:200px;
                      border:15px solid Red;
                      }


                      JS Fiddle Example






                      share|improve this answer













                      You can do it using first-child selector



                      .image img:first-child
                      {
                      height:500px;
                      width:200px;
                      border:15px solid Red;
                      }


                      JS Fiddle Example







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 22 '13 at 14:46









                      SachinSachin

                      32.7k67488




                      32.7k67488























                          4














                          The ':first-child' selector seems like it might be what you need.



                          http://www.w3schools.com/cssref/sel_firstchild.asp






                          share|improve this answer




























                            4














                            The ':first-child' selector seems like it might be what you need.



                            http://www.w3schools.com/cssref/sel_firstchild.asp






                            share|improve this answer


























                              4












                              4








                              4







                              The ':first-child' selector seems like it might be what you need.



                              http://www.w3schools.com/cssref/sel_firstchild.asp






                              share|improve this answer













                              The ':first-child' selector seems like it might be what you need.



                              http://www.w3schools.com/cssref/sel_firstchild.asp







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Mar 22 '13 at 14:46









                              Scott StrozScott Stroz

                              7,00111624




                              7,00111624























                                  2

















                                  article>img:first-child {
                                  border:1px solid #f00;
                                  }

                                  <article class="image">
                                  <img src=... />
                                  <img src= .../>
                                  <div class="image">
                                  <img src=... />
                                  <img src= .../>
                                  <img src= .../>

                                  </div>
                                  <img src= .../>
                                  <img src= .../>
                                  </div>








                                  share|improve this answer






























                                    2

















                                    article>img:first-child {
                                    border:1px solid #f00;
                                    }

                                    <article class="image">
                                    <img src=... />
                                    <img src= .../>
                                    <div class="image">
                                    <img src=... />
                                    <img src= .../>
                                    <img src= .../>

                                    </div>
                                    <img src= .../>
                                    <img src= .../>
                                    </div>








                                    share|improve this answer




























                                      2












                                      2








                                      2










                                      article>img:first-child {
                                      border:1px solid #f00;
                                      }

                                      <article class="image">
                                      <img src=... />
                                      <img src= .../>
                                      <div class="image">
                                      <img src=... />
                                      <img src= .../>
                                      <img src= .../>

                                      </div>
                                      <img src= .../>
                                      <img src= .../>
                                      </div>








                                      share|improve this answer


















                                      article>img:first-child {
                                      border:1px solid #f00;
                                      }

                                      <article class="image">
                                      <img src=... />
                                      <img src= .../>
                                      <div class="image">
                                      <img src=... />
                                      <img src= .../>
                                      <img src= .../>

                                      </div>
                                      <img src= .../>
                                      <img src= .../>
                                      </div>








                                      article>img:first-child {
                                      border:1px solid #f00;
                                      }

                                      <article class="image">
                                      <img src=... />
                                      <img src= .../>
                                      <div class="image">
                                      <img src=... />
                                      <img src= .../>
                                      <img src= .../>

                                      </div>
                                      <img src= .../>
                                      <img src= .../>
                                      </div>





                                      article>img:first-child {
                                      border:1px solid #f00;
                                      }

                                      <article class="image">
                                      <img src=... />
                                      <img src= .../>
                                      <div class="image">
                                      <img src=... />
                                      <img src= .../>
                                      <img src= .../>

                                      </div>
                                      <img src= .../>
                                      <img src= .../>
                                      </div>






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Jan 1 at 2:56

























                                      answered Jan 1 at 2:45









                                      Ramakrishnan RRamakrishnan R

                                      213




                                      213























                                          1














                                          If you want the first image inside a DIV (and there might be other elements preceding it) you need first-of-type rather than first-child



                                          div > img:first-of-type {}


                                          consider the following html



                                          <div>
                                          <p>paragraph, this is :first-child</p>
                                          <img src="..."><!-- this is img:first-of-type -->
                                          </div>


                                          More details can be found at https://developer.mozilla.org/en-US/docs/CSS/:first-of-type






                                          share|improve this answer






























                                            1














                                            If you want the first image inside a DIV (and there might be other elements preceding it) you need first-of-type rather than first-child



                                            div > img:first-of-type {}


                                            consider the following html



                                            <div>
                                            <p>paragraph, this is :first-child</p>
                                            <img src="..."><!-- this is img:first-of-type -->
                                            </div>


                                            More details can be found at https://developer.mozilla.org/en-US/docs/CSS/:first-of-type






                                            share|improve this answer




























                                              1












                                              1








                                              1







                                              If you want the first image inside a DIV (and there might be other elements preceding it) you need first-of-type rather than first-child



                                              div > img:first-of-type {}


                                              consider the following html



                                              <div>
                                              <p>paragraph, this is :first-child</p>
                                              <img src="..."><!-- this is img:first-of-type -->
                                              </div>


                                              More details can be found at https://developer.mozilla.org/en-US/docs/CSS/:first-of-type






                                              share|improve this answer















                                              If you want the first image inside a DIV (and there might be other elements preceding it) you need first-of-type rather than first-child



                                              div > img:first-of-type {}


                                              consider the following html



                                              <div>
                                              <p>paragraph, this is :first-child</p>
                                              <img src="..."><!-- this is img:first-of-type -->
                                              </div>


                                              More details can be found at https://developer.mozilla.org/en-US/docs/CSS/:first-of-type







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Mar 22 '13 at 15:06

























                                              answered Mar 22 '13 at 14:59









                                              xecxec

                                              12.9k33247




                                              12.9k33247






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15573159%2fhow-to-select-first-img-tag-in-a-div-with-many-img-tag%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  Monofisismo

                                                  Angular Downloading a file using contenturl with Basic Authentication

                                                  Olmecas