"Is there a way to alter the background color of the second span within a Bootstrap row

I currently have 4 WordPress services in a loop, and I am trying to change the background color of the second service. However, when I try to apply the CSS background property, it ends up affecting all of the services.

Here is my template code:

<div class="row">   
    <?php

        $i=1;
        $default_arg =array('class' => "index_ser_img img-responsive" );
        $total_services = $current_options['service_list'];
        $args = array( 'post_type' => 'rambopro_service','posts_per_page' =>$total_services) ;  
        $service = new WP_Query( $args );
        if( $service->have_posts() )
        {   while ( $service->have_posts() ) : $service->the_post();    
            $link=1;
            $service_icon_image =  get_post_meta( get_the_ID(),'service_icon_image', true ); 
            $meta_service_description =  get_post_meta( get_the_ID(),'meta_service_description', true );
            if(get_post_meta( get_the_ID(),'meta_service_link', true ))
            { $meta_service_link = sanitize_text_field( get_post_meta( get_the_ID(),'meta_service_link', true )) ; }
            else
            {  $link=0; }
            ?>
            <div class="span3 home_service">
                <?php if(has_post_thumbnail()){ ?>
                <span class="icon_align_center">
                  <?php if($link==1) { ?>                   
                <a href="<?php echo $meta_service_link;?>" target="<?php if(get_post_meta( get_the_ID(),'meta_service_target', true )) echo "_blank";  ?>" >
                <?php
                  the_post_thumbnail('',$default_arg);?>
                </a> <?php  } else {  the_post_thumbnail('',$default_arg);  } ?>  </span>
                <?php } else { ?>
                <span class="fa-stack fa-4x icon_align_center"> 
                    <?php if($link==1) { ?>
                <a href="<?php echo $meta_service_link;?>" target="<?php if(get_post_meta( get_the_ID(),'meta_service_target', true )) echo "_blank";  ?>" > <i class="fa <?php echo $service_icon_image; ?> home_media_icon_1x fa-inverse"></i></a>
                <?php } else { ?> <i class="fa <?php echo $service_icon_image; ?> home_media_icon_1x fa-inverse"></i>
                <?php } ?>
                </span>
                <?php } if($link==1) {?>
                <a href="<?php echo $meta_service_link;?>" target="<?php if(get_post_meta( get_the_ID(),'meta_service_target', true )) echo "_blank";  ?>" ><h2><?php echo the_title(); ?></h2></a>
                <?php } else {  echo '<H2>'; echo the_title() ; echo '</h2>' ;   }?> 
                <p><?php echo $meta_service_description; ?></p>
            </div>
            <?php if($i%4==0){  echo "<div class='clearfix'></div>"; } $i++; endwhile;
        } else
        {  for($j=1; $j<=4; $j++) {  ?>
        <div class="span3 home_service">
            <span class="fa-stack fa-4x icon_align_center">                 
                <i class="fa fa-flag home_media_icon_1x fa-inverse"></i>
            </span>
            <h2><?php _e('Html5 & Css3',''); ?></h2>
            <p><?php _e('Mauris rhoncus pretium porttitor. Cras scelerisque commodo odio. Phasellus dolor enim, faucibus egestas scelerisque hendrerit, aliquet sed lorem.','rambo');?></p>
            <!-- <a class="home_service_btn" href="#"><i class="fa fa-angle-double-right"></i><?php _e('Read More','rambo');?></a> -->
        </div>
    <?php } 
    } ?>        
</div>
<!-- /Home Service Section -->  

Link:

Answer №1

There are a couple of ways you can achieve this:

1) Start by defining a CSS class with the desired background color and then apply it to the second service element:

.custom-bg {
background-color: #ececec;
}

<div class="span3 home_service custom-bg">
...
</div>

2) Another method, as suggested by MessyMinds, is to use the nth-child() selector to specifically target and style the second service:

.span3.home_service:nth-child(2) {
background-color: #ececec;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular 2: Implementing a Universal CSS Style Sheet

Is there a way to include a universal CSS file in Angular 2 applications? Currently, I have multiple components that share the same button styling, but each component has its own CSS file containing the styles. This setup makes it difficult to make changes ...

Styling Collapse Text in Bootstrap Framework

I have a question about the "Accordion example" in the Bootstrap Collapse Component. My query is regarding removing the underline from the text "Collapsible Group Item" 1-3 when they are activated. Normally, this text is not underlined, but upon hovering, ...

Aligning text to the left center

I am looking to center left-aligned text in the yellow box (txtbox). The current look is like this: and I want it to appear like this: HTML: <div class="content container small"> <div class="txtbox"> ...

Challenges with the final child element's CSS styling

Hey everyone, I've recently added the following CSS: #tab-navigation ul li:last-child { background-image: url(../images/tabs-end.gif); background-repeat: no-repeat; color: #fff; display: block; border: 1px solid red; backgrou ...

Issue with Ckeditor inside a bootstrap modal

I am encountering an issue while trying to integrate ckeditor into a bootstrap modal. Whenever I attempt to use it, the functionality does not work as expected. Clicking on any icons triggers an error in the console stating Uncaught TypeError: Cannot rea ...

Undefined boolean when passing to AngularJS directive

I developed a custom directive that allows for the addition of a gradient background color in AngularJS: .directive('colouredTile', function () { return { restrict: 'A', controller: 'ColouredTileController&apos ...

Utilize Javascript to conceal images

On a regular basis, I utilize these flashcards. Recently, I have started using images as answers. However, I am facing an issue - I am unable to conceal the pictures. My preference is for the images to be hidden when the webpage loads. function myShowTe ...

Challenge with CSS selectors

Here is the HTML code I am working with: <label for="tx_alterneteducationcatalog_subscriberadd[newSubscriber][gender]" class="error" id="tx_alterneteducationcatalog_subscriberadd[newSubscriber] [gender]-error">This field is required.</label> ...

Guide on converting a date input to a timestamp

I am trying to convert the date retrieved from an HTML5 date input into a timestamp and store it as a variable. HTML: <form> <section class="input"> <input type="date" id="date" name="maintenanace_date"/> </section> <sectio ...

The AngularJS directive seems to be having trouble receiving the data being passed through its scope

Check out this HTML code snippet I created: <div ng-controller="ctrl"> <custom-tag title = "name" body = "content"> </custom-tag> </div> Take a look at the controller and directive implementation below: var mod = angular.mod ...

How to access a PHP include using data retrieved from a MySQL database?

I am currently in the process of developing a website that will be powered by a MySQL database. To simplify the structure of the URLs, I have decided to rewrite them to point to a single file. The file's layout is as follows: <html> <head&g ...

Python Selenium - encountering issues with interactability of elements

Trying to complete the form on () is proving tricky for me. When I utilize Javascript, document.getElementsByName("userName")[0].value = "Hello", I am successful in entering text into a form. However, when I apply the same logic in Sel ...

Having trouble getting my absolute div to center?

Check out this problem on my server at THIS LINK .login-div{ background: #fff none repeat scroll 0 0; height: 500px; left: 50%; width: 500px; right: 0; z-index: 99999; top: 20%; position: ...

Leveraging AJAX to assist in PHP for data parsing

I am currently exploring the world of AJAX and grappling with a unique situation. I am in the process of developing an HTML app that will be integrated into a mobile application using PhoneGap. The main objective of my project is for the HTML page to con ...

A PHP script or regular expression that swaps the positions of characters within a string

(Update1: per suggestion by George Cummins) I need assistance in creating a regex search and replace or a php function to switch positions of characters within a string. (since I couldn't figure it out on my own) For example, if I have the following C ...

Discovering the source of the parent link within html.tpl.php

Is there a way to determine the parent page title within html.tpl.php? I need this information to change the background image based on the selected parent link. Here is the URL for reference: Thank you, Ron I couldn't find a solution to this problem ...

Trouble with Displaying Table Outside of Div in Internet Explorer

As a UI developer, I encountered an issue where the table inside two divs is not displaying correctly in IE8. It works seamlessly in Firefox, but IE is causing me headache. If anyone has any suggestions or advice on how to fix this, please help me out. Yo ...

Laravel Tutorial: Utilizing for and foreach Loops to Showcase Data in Blade Template

I am trying to use a for and foreach loop to display data based on a template table. However, I am running into an issue where the else conditions always display an index of their own. My expectation is as follows: https://i.stack.imgur.com/tqDSn.png Th ...

Gradient Border on CSS Button

I'm trying to create a button with a silver-ish border and gradient like the one in the picture. I've managed everything except for the border, which is giving me some trouble. Here's the current CSS code I'm using for the button. http ...

Is it possible to single out the final element having a specific CSS class in the absence of a parent container?

I have the following elements dynamically rendered in an HTML file. <div class="vehicle"></div> <div class="vehicle"></div> My requirement is to add a style float:right inside CSS class vehicle for the second el ...