AngularJS: Creating a dual-column layout with alternating styles

Struggling to create a two-column layout that repeats in AngularJS using a JSON object of image data. I'm aiming for a display of images in a side-by-side format, but my odd/even logic seems to be off no matter what adjustments I make. Can anyone point out where I might be going wrong?

.left {
    float: left !important;
    width: 50% !important;
}
.right {
    float: right !important;
    width: 50% !important;
}
.group:after {
    content:"";
    display: table;
    clear: both;
}
img {
    max-width: 100%;
    height: auto;
}
@media screen and (max-width: 480px) {
    .left,
    .right {
        float: none;
        width: auto;
    }
}
<div ng-repeat="issue in issues">
  <div ng-if="$even" class="group">
    <div class="left" ng-if="$even">
      <img src="{{ issue.image }}" ng-src="{{ issue.image }}">
    </div>
    <div class="right" ng-if="$odd">
      <img src="{{ issue.image }}" ng-src="{{ issue.image }}">
    </div>
  </div>
</div>

Answer №1

The problem with the code is that you have enclosed your logic inside

<div ng-if="$even" class="group">

This div is limiting the display of odd logic divs.

Instead of using two separate divs, I would suggest using the ngClassEven and ngClassOdd directives. Also, remove the wrapper div with the ng-if="$even" condition.

<div ng-repeat="issue in issues">
    <div ng-class-even="'left'" ng-class-odd="'right'">
        <img ng-src="{{ issue.image }}">
    </div>
</div>

Answer №2

It seems like you already have the answer you need, but here are some other options that might come in handy:

  1. You could consider using ng-class for more flexibility in different situations. For example:

    <div ng-repeat="issue in issues" ng-class="{left: $even, right: $odd}">
      <img ng-src="{{ issue.image }}">
    </div>
    

    or

    <div ng-repeat="issue in issues" ng-class="$even ? 'left' : 'right'">
      <img ng-src="{{ issue.image }}">
    </div>
    

    Keep in mind that ng-class can work alongside the class property, so you can also include something like class="item".

  2. If it's a styling issue, you might want to consider solving it with CSS. If you're comfortable not supporting IE 6-8, you can use the nth-child selector:

    :nth-child(odd) { ... }
    :nth-child(event) { ... }
    

Since both my and Pankaj's solutions removed your group class, here is a simpler CSS alternative:

.item {
    float: left;
    width: 50%;
}
.left {
    clear: left;
}
@media screen and (max-width: 480px) {
    .item {
        float: none;
        width: auto;
    }
}

Alternatively, if you're open to modern solutions and don't need JavaScript at all, you could try using flexbox:

.parent {
    display: flex;
    flex-wrap: wrap;
}
.item {
    flex: 0 0 50%;
}
@media screen and (max-width: 480px) {
    .item {
        flex-basis: 100%;
    }
}

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

The white background of the div conceals the shadow of the Bootstrap navbar

I am using Bootstrap's navbar with .shadow-sm. I have two divs beneath it, one without background and the other one with a white background. The shadow appears as expected in the div with no background specified, but it is being obscured by the white ...

Is it the right time to dive into HTML5 & CSS3?

The excitement around HTML5 and CSS3 is hard to ignore. But when is the right time to start incorporating them into our projects? How close are we to fully utilizing their capabilities? Update: I'm not interested in following the principles of: Grac ...

The functionality of the AngularJS dropdown becomes unresponsive when placed within a dialog box

When utilizing an AngularJS dropdown list, everything functions smoothly. However, I encounter an issue where the results are not displayed when attempting to open it within a dialog box. Any insight into what might be causing this inconvenience? ...

The use of the display property with inline-block is not functioning correctly on mobile devices

Can anyone help me figure out why my divs are displaying differently on PC versus phone? They line up well on the computer, but on the phone, the last div is on a new line. Here is a snippet of my code and screenshots for reference. Thank you in advance fo ...

hiding an "input type="file" element by using a button to conceal it

Is it possible to create a button that triggers file upload functionality? I couldn't find any solutions on Google. I've heard it can be done with labels, but I specifically need to use a button for better UX/UI. <button style="position:fixe ...

When Node.js meets Angular, it results in the error message: "Uncaught ReferenceError: require is

Working on setting up an Express.js API hosted on a Node.js server. The purpose of the API is to retrieve data stored on the server and keep track of API access in a database. Currently, I'm looking to develop an admin section that will utilize Angul ...

What function does the sx prop serve in Material UI?

<Container style={{ margin: "10px" }}> <Article post={post} setCurrentId={setCurrentId} /> </Container> <Container sx={{ margin: "10px" }}> <Article post={post} setCurrentId={setCurrentId} /> </Cont ...

Is polymer routing the best choice for large-scale websites?

I am currently working on a large project that consists of various layouts and structures, totaling around 100 different pages. Despite the variations in content, the core elements such as headers and menus remain consistent throughout. To streamline my ...

How can I encode and decode a base64 string using AngularJS1 and TypeScript?

I am currently working with Angular1 using TypeScript and I have a question that needs some clarification. Within the environment that I am operating in, is there a method available to encode and decode a string in base64? Despite conducting extensive re ...

Expanding across the entire horizontal space, excluding any space allocated for

I've been on a quest to find a solution for my unique problem, but all my efforts have been in vain so far. Currently, I'm working on developing a mobile website and facing a challenge with the input boxes. Despite wanting them to take up the en ...

When a user scrolls over an element with a data-attribute,

Looking to create a dynamic header effect on scroll? I have two headers, menu1 by default and menu2 hidden with display:none. In specific sections of my website, I've added a special attribute (data-ix="change-header") to trigger the header change. T ...

"Enhance your web application with the powerful combination of Webix and Angular

I'm facing an issue where the datatable is empty and the data isn't binding properly. Here is the HTML: <div id="usersFormDiv" ng-controller="adminController"> <div webix-ui="usersGrid"></div> </div> Controller: $ ...

Retrieving data from MongoDB and saving it to your computer's hard drive

I've asked a variety of questions and received only limited answers, but it has brought me this far: Mongoose code : app.get('/Download/:file(*)', function (req, res) { grid.mongo = mongoose.mongo; var gfs = grid(conn.db); var fi ...

Troubles with caching on Amazon S3

Just starting out in web development and I'm working on updating HTML and CSS files stored on Amazon S3. Our website's backend is hosted on Heroku, while the front-end is on Amazon S3. Whenever I make changes to a CSS file, I can immediately see ...

What is the best way to incorporate additional list items into a field input?

I have been working on developing a simple to-do app and I am encountering an issue. After the user clicks enter in the input box, nothing happens as expected. Despite trying various methods, the problem persists. Below is the recent code that I have been ...

Issues with JQuery OnClick Function Detected

I am struggling with an issue related to the scripts in the <head> section of my website: <script src="scripts/jquery-1.11.0.min.js" type="text/javascript"></script> <script> $('#submit').click(function() { $('#submi ...

CSS: When using the float: right property, the container does not grow to

I'm having trouble with expanding the comment body that is floated to the right, as my container doesn't expand along with it. Any tips on how I can resolve this issue? For a clearer understanding, please refer to this link: http://jsfiddle.net/ ...

html distinguishing between various fields within a form

As a newcomer to website development, I am trying to enhance the readability of my form with fields. Currently, all the fields are displayed one after another without any separation. Is there a specific HTML function that can help group these fields toge ...

Kendo UI NumericTextBox does not properly handle displaying zero values

Issue with displaying initial value of variable "0" in NumericTextBox field. Sample Code: <div ng-app="app"> <div ng-controller="MyCtrl"> <input type="text" ng-model="value" kendo-numeric-text-box> <br> ...

Let us know when the information on a different tab is updated

I have multiple tabs and I want to indicate when a change occurs on another tab that the user hasn't clicked. For example, if the user hits the run button while on the Data pane, I would like the Errors tab to change to red to show that there was a ch ...