Choose elements with an even or odd class

I am seeking a selector that can target an element with the parent element of 'elementWrapper'. Depending on whether the parent element is even or odd, I want the child element to float right or left respectively.

Here is the expected result:

    <body>
        <style>
            .elementWrapper
            {
                width:100%;
                height:100px;
            }

            header, nav, div div, footer
            {
                width:50%;
                height:100px;
                background:#000;
            }
        </style>
        <div class="elementWrapper">
            <header style="float:left;"></header>
        </div>
        <div class="elementWrapper">
            <nav style="float:right;"></nav>
        </div>
        <div class="elementWrapper">
            <div style="float:left;"></div>
        </div>
        <div class="elementWrapper">
            <footer style="float:right;"></footer>
        </div>
    </body>

Can this be achieved using CSS selectors alone, or would JavaScript be necessary? JSFiddle

Answer №1

To alternate the styling, you can utilize nth-child(odd) or nth-child(even).
View the live example here

The following css snippet should achieve the desired effect:

 .elementWrapper:nth-child(odd) {
       float:left;
    }
    .elementWrapper:nth-child(even) {
        float:right;
    }

Answer №2

Consider giving this approach a try,FIDDLE

.elementWrapper {
    width:100%;
    height:100px;
}
.elementWrapper:nth-child(even) * {
    float:left;
}
.elementWrapper:nth-child(odd) *{
   float:right;
}

Be more specific for sub elements

    .elementWrapper:nth-child(odd) > header,
    .elementWrapper:nth-child(odd) > nav,
    .elementWrapper:nth-child(odd) > footer,
    .elementWrapper:nth-child(odd) > div{
       float:right;
    }

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

Unable to customize the appearance in SharePoint Online

I've been experimenting with various approaches to implement custom CSS on our SharePoint Online site, but unfortunately, none have been successful. Here is a summary of what I've attempted: Created a custom master page using the Seattle templa ...

What is the best way to embed sections of one HTML page into another page?

Is there a method I can use to embed sections of one webpage within another webpage? The dilemma arises from the fact that the second page has a distinct style compared to my main page. Is it feasible to apply the alternate style solely to specific content ...

Utilize JavaScript and CSS to customize the appearance of Qualtrics

Currently utilizing Qualtrics and hoping to enhance the appearance of certain survey screens by incorporating JavaScript or HTML. Have you discovered a method for achieving this? I'm facing some difficulties adding custom HTML at the question level, ...

What could be causing me to have to click the button twice in order to view my dropdown list? Any suggestions on how to

I am facing an issue where I have to click twice on the "Action" button in order to see my dropdown list. How can I resolve this? Is there a way to fix it? $(document).ready(function() { $(".actionButton").click(function() { $dropdown = $("#cont ...

Attempting to locate an element using Selenium IDE proves to be challenging unless each command is executed individually

Currently, I am utilizing selenium ide for automating my tests. Once I click on a link, a popup window appears with a div containing text. Strangely, I am unable to retrieve the text within the div tag without either double-clicking on it or executing the ...

jQuery did not display the alert message

A JQuery weather plugin has been successfully implemented, but there is an issue with some code not displaying the data. The problem lies in the last section of the JQuery code, where a message should be displayed to the user when the temperature exceeds a ...

Unable to post login form using HTML in Node.js

I've encountered a similar issue on Stack Overflow several times, but haven't found a solution that works for me. I'm working on a registration form with a submit button that should send data to MySQL and then redirect me to the login page u ...

Generate a variety of HTML ordered lists

Can anyone assist me in determining the type of ordered list shown below? What is the method to achieve an ordered list like this: (a) (b) (c) in HTML? ...

What is the best way to display properly formatted code to users in a React JS application?

The backend is sending code to me in plain text format. Here's an example: def countNegatives(matrix): count = 0 for row in matrix: for num in row: if num < 0: count += 1 return count It's just basic text, but I need to show this to the user ...

The script functions smoothly on XAMPP, however, it encounters issues when deployed on

I've encountered an issue with a script that is designed to display posts and images from a user. While it works perfectly fine on Xampp, I'm facing an issue on the host server where only the posts are visible but not the images that the user has ...

Positioning an Element on a Web Page in Real-Time

Trying to implement an Emoji picker in my React application, I have two toggle buttons on the page to show the picker. I want it to appear where the Toggle button is clicked - whether at the top or bottom of the page. The challenge is to ensure that the pi ...

What is the process for taking a website project running on localhost and converting it into an Android web application using HTML, CSS, and JavaScript

Looking for recommendations on how to create an Android web application using HTML, CSS, and JavaScript. Any suggestions? ...

Having difficulty finding the close button in a pop-up window while using the Selenium library with Python

While using chromedriver to open the website mentioned below in my extract_data() function, I encountered a problem with dismissing a pop-up message by clicking the 'x' button. Surprisingly, it seems to click the wrong button instead. What' ...

Steps to Create Javascript Image Zoom Effect on Mouse Hover

Looking to implement a feature on my website located at www.thetotempole.ca/javas2.html/. Basically, I have a table with images and I want them to enlarge when a user hovers over them and return to normal when the cursor is moved away. Is there a way to ac ...

Utilizing cropperjs to upload and customize crop images and forms within the Django framework

I am utilizing the cropper function from https://github.com/fengyuanchen/cropper/blob/master/README.md. However, my goal is to submit field objects (such as the title) along with the cropped image. Yet, I encountered an error on the admin side. I have alre ...

Unable to open certain HTML element using Force option in Firefox browser

Take a look at my progress here: http://jsfiddle.net/cYHae/4/ Below is the code I am using: // <select> element displays its options on mousedown, not click. showDropdown = function (element) { var event; event = document.createEvent(' ...

Picture not showing up when loading iPhone video

My website features a video that is displayed using the following code: <div class="gl-bot-left"> <video controls=""> <source src="https://www.sustainablewestonma.org/wp-content/uploads/2019/09/video.fixgasleaks.mp4" ...

View complex response objects in Postman as easily digestible tables

I am interested in displaying the data provided below as a table using Postman Tests. The table should have columns for product, price, and quantity, with Items listed in rows. It's important to note that there may be multiple shippingGroups within th ...

Using various span elements with distinct alignment within an h1 tag in Bootstrap 5

How can I nest three span tags inside an h1 tag, aligning them differently? The first on the left, the second in the center, and the third on the right. Can this be achieved using Bootstrap or does it require custom CSS? ...

"Troubleshooting AmCharts: How to Resolve Missing Data on Graphs/Implementing AJAX for AmCharts Data Visualization/Querying a Database to Dynamically

After writing some HTML and JavaScript code, I'm trying to set up an amcharts plot. To do this, I fetch data from a database using PHP, convert it into JSON format, and then pass it to the graph for display. The issue arises when I try to assign the ...