0

I realise this question has been asked before but none of the solutions i have read through have given me a fix.

Basically i have 4 responsives boxes within a div and as i move down the screen sizes the css first-child and nth-child selectors wont work EVEN though i have used the exact same code for another set of boxes below this set and this works perfectly??

As you will see the bottom 2 boxes act as normal but the top two boxes are stuck together even though the css for the margins have been set to prevent this. It's possibly an easy fix but after banging my head off this all last night I feel like giving up.

Any help would really be appreciated, thanks!

HERE is a fiddle showing exactly what is going on: http://jsfiddle.net/f284xyjh/

Here is the HTML code:

<section id="content"> 

    <div class="container">

        <div id="about">
            <h1><a name="about">WE'RE GOOD... JUST ASK OUR CLIENTS!</a></h1>
            <div id="about-text">
            <p>Above The Fold is a Dublin based creative, digital marketing agency that provides a personal, professional and passionate service tailored specifically to each of our clients no matter the size. We specialise in creative digital solutions centred around the user and aim to provide a rich user experience for people interacting with your brand online. Blending creativity with technical know-how and research we can brand, plan, design and build your websites and online campaigns<p>

            </div><!-- end of about-text -->
        </div><!-- End of about -->

        <div id="services">
        <a name="services"></a>
            <div class="service">
                <div class="service-info">
                    <img src="images/creative-design.png" alt="Creative Design icon" />
                    <h3>Creative Design</h3>
                    <p>Creativity is at the heart of what we do. Combining creative thinking with technology to deliver beautiful websites that stand out and engage your target audience.</p>
                </div>
                <a href="#"><div class="find-out-more">FIND OUT MORE</div></a>
            </div>
            <div class="service">
                <div class="service-info">
                    <img src="images/branding-icon.png" alt="Branding icon" />
                    <h3>Branding</h3>
                    <p>Much more then simply the design of a website. Through strategic planning and research we create dynamic brand icons (logos) that define who you are.</p>
                </div>
                <a href="#"><div class="find-out-more">FIND OUT MORE</div></a>
            </div>
            <div class="service">
                <div class="service-info">
                    <img src="images/strategy.png" alt="Social Media Marketing icon" />
                    <h3>Social Media Strategy</h3>
                    <p>Social media marketing gives you an opportunity to expose your brand to a wider audience. All our campaigns are strategically  planned to maximise your reach. </p>
                </div>
                <a href="#"><div class="find-out-more">FIND OUT MORE</div></a>
            </div>
            <div class="service">
                <div class="service-info">
                    <a href="#"><img src="images/digital-marketing.png" alt="Digital Marketing icon" />
                    <h3>Digital Marketing</h3></a>
                    <p>Your business is unique so you need an online strategy that is designed for you. We deliver research driven results that create awarenss of your brand or services</p>
                </div>
                <a href="#"><div class="find-out-more">FIND OUT MORE</div></a>
            </div>

        </div><!-- end of services -->



    </div><!-- End of content container -->

</section><!-- end of top-content section -->

And here's the CSS code:

.container {
width: 1160px;
margin: 0 auto;
overflow: hidden;
}

#content {
height: 605px;
background: #F3F3F2;
margin-top: 1px;
padding-top: 25px;
}

#about {
width: 100%;
display: inline-block;
text-align: center;
}

#about p {
font-family: "Proxima Light",Arial,helvetica,garuda,sans-serif;
font-size: 24px;
line-height: 28px;
margin-bottom: 10px;
}

#about-img {
background: url(../img/about-img.png) no-repeat;
width: 370px;
height: 154px;
float: left;
margin-right: 20px
}

#about-text {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #333;
line-height: 23px;
}

#services {
width: 100%;
display: inline-block;
}

#services h3 {
font-size: 24px;
font-weight: bold;
margin-bottom: 15px;
color: #FFF;
}

#services p {
font-size: 16px;
line-height: 20px;
color: #FFF;
}

#services a {
text-decoration: none;
}

.service {
background: #3498db; url(../images/service-find-out-triangle.png) no-repeat center   256px;
width: 275px;
height: 295px;
float: left;
margin: 20px 20px 20px 0;
text-align: center;
position: relative;

}

.service:last-child {
margin-right: 0;
}

.service-info {
background: #3498db;
width: 255px;
height: 245px;
padding: 20px 10px 0;
}

.service img {
margin-bottom: 10px;
}

@media only screen and (max-width : 1224px) {

.container {
width: 1000px;
margin: 0 auto;
overflow: hidden;
}

#content { height: auto;}

.service { width: 490px; margin: 20px 20px 20px 0;}
.service:first-child { margin-right: 0!important;}
.service:nth-child(2) { margin-right: 0!important;}
.service-info { width: 465px; }

}

@media only screen and (max-width : 1024px) {

.container { width: 800px;}

.service { width: 390px;}
.service-info { width: 370px; }

}

@media only screen and (max-width : 824px) {

.container { width: 680px;}

.service { width: 330px;}
.service-info { width: 310px;}

}

@media only screen and (max-width : 768px) {

.container { width: 95%;}

.service { width: 100%; margin: 20px 0;}
.service-info { width: 94%; }

}


}
DannyW86
  • 201
  • 3
  • 8
  • 20
  • FYI: "Much more then simply the design of a website." should be "Much more than simply...". – i alarmed alien Oct 17 '14 at 09:25
  • In which browser you are facing this issue? – Prateek Oct 17 '14 at 09:27
  • can't seem to find your problem, but i'm going to take a wild guess and say that your (first-child) selector doesn't work because it isn't the first element in the container. Have you tried first-of-type instead of first-child ? – Gho Oct 17 '14 at 09:44

1 Answers1

4

This selector: .service:nth-child(2) { margin-right: 0!important;} isn't working because the element you are trying to select is not the 2nd child.

Also...by the way, neither is this one: .service:first-child { margin-right: 0!important;}.

The first-child is the anchor tag

<a name="services"></a>

Accordingly, you should use

.service:nth-child(3) { margin-right: 0!important;}

instead...in this specific instance.

JSfiddle Demo

Paulie_D
  • 107,962
  • 13
  • 142
  • 161
  • Yes another user pointed this out but has since deleted his answer so i'll credit you for it all the same! Thanks a lot. Can you please give me an explanation of this though? I don't understand why "serviceS" has anything to do "service" with no S at the end? Is it simply a case that any element at all inside one div becomes the first child no matter if the id/class etc is the same as the next? I had always taken it that it was only for the first of a group, i.e I have four "service" divs so neglected to think the anchor tag would affect things. – DannyW86 Oct 17 '14 at 09:56
  • 1
    the `nth` selectors only work on **elements** and, in general ignore classes. CSS selectors work right to left if we take `.service:first-child` it looks for the `first-child` and **if** it has a class of `service` then it applies. If it doesn't then the selector fails. - See here for more - http://stackoverflow.com/questions/2717480/css-selector-for-first-element-with-class/8539107#8539107 – Paulie_D Oct 17 '14 at 09:59