Switch up the placement of articles, going back and forth between left

On the page, I want to display articles with alternating positions - one on the left, the next on the right.

In this example, all the articles are currently being displayed twice, both on the left and the right side. How can I ensure that only one instance of each article is shown in the correct position?

To achieve the desired alternation without displaying unnecessary duplicates, we need to adjust the structure as follows:

<div class="container">
    <div class="article-list">
        <div class="article article--left">
            <h2 class="article_title">Article 1 (l)</h2>
        </div>

        <div class="article article--right">
            <h2 class="article_title">Article 1 (r)</h2>
        </div>
        
        <div class="article article--left">
            <h2 class="article_title">Article 2 (l)</h2>
        </div>

        <div class="article article--right">
            <h2 class="article_title">Article 2 (r)</h2> 
        </div>
        
        <div class="article article--left">
            <h2 class="article_title">Article 3 (l)</h2> 
        </div>

        <div class="article article--right">
            <h2 class="article_title">Article 3 (r)</h2>
        </div>
        
        <div class="article article--left">
            <h2 class="article_title">Article 4 (l)</h2>
        </div>

        <div class="article article--right">
            <h2 class="article_title">Article 4 (r)</h2>
        </div>
    </div>
</div>

Answer №1

Utilizing the nth-child selector, you have the ability to pinpoint specific elements and conceal them from view.

.article:nth-child(2) {display: none;}
.article:nth-child(3) {display: none;}
.article:nth-child(6) {display: none;}
.article:nth-child(7) {display: none;}
<div class="container">
    <div class="article-list">
        <div class="article article--left">
            <h2 class="article_title">Article 1 (l)</h2>
        </div>

        <div class="article article--right">
            <h2 class="article_title">Article 1 (r)</h2>
        </div>
        
        <div class="article article--left">
            <h2 class="article_title">Article 2 (l)</h2>
        </div>

        <div class="article article--right">
            <h2 class="article_title">Article 2 (r)</h2> 
        </div>
        
        <div class="article article--left">
            <h2 class="article_title">Article 3 (l)</h2> 
        </div>

        <div class="article article--right">
            <h2 class="article_title">Article 3 (r)</h2>
        </div>
        
        <div class="article article--left">
            <h2 class="article_title">Article 4 (l)</h2>
        </div>

        <div class="article article--right">
            <h2 class="article_title">Article 4 (r)</h2>
        </div>
    </div>
</div>

If PHP is your language of choice, consider implementing this method

<?php foreach ($articles as $index => $article): ?>
    <div class="article-list">
        <?php  if ($index % 2 === 1): ?>
            <div class="article article--left" >
                <h2 class="article_title">{{ $article->title }}</h2>
            </div>
            
        <?php else: ?>

            <div class="article article--right">
                <h2 class="article_title">{{ $article->title }}</h2>
            </div>
        <?php endif; ?>
    </div>
<?php endforeach; ?>

Answer №2

If you're looking to dip your toes into CSS, here's a simple snippet to help get you started:

<style>
.column {
  float: left;
  width: 50%;
}

/* Don't forget to clear the floats */
.row:after {
  content: "";
  display: table;
  clear: both;
}
</style>
<div class="row">
  <div class="column">Article 1 (left)</div>
  <div class="column">Article 1 (right)</div>
</div>
<div class="row">
  <div class="column">Article 2 (left)</div>
  <div class="column">Article 2 (right)</div>

Take special note of the display property:

https://developer.mozilla.org/en-US/docs/Web/CSS/display

table

This attribute makes elements act like HTML <table> elements, creating a block-level box.

TIP:

For an easier approach to formatting and layout, consider using a framework like Bootstrap.

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

What is the best way to insert a tagline above a form?

I'm struggling to figure out where to place a tagline above my form. I want it to be neat and clean, but haven't been successful in finding the right spot. I attempted to insert an <h2> inside the form, but it doesn't look good at al ...

Mastering the art of flawlessly executing kerning-generated code

I am facing a problem while implementing logotext with kerning technique in the <header> element. I found a useful tool made by Mr. Andrew which can be accessed through this link. Before making any modifications, I had the following code in the heade ...

When updating the innerHTML attribute with a new value, what type of performance enhancements are implemented?

Looking to optimize updating the content of a DOM element called #mywriting, which contains a large HTML subtree with multiple paragraph elements. The goal is to update only small portions of the content regularly, while leaving the majority unchanged. Co ...

Gauging Screen Size: A Comparison between Media Queries and JavaScript for Adjusting Div Position

I am currently facing an issue with the banner on my website. It contains a slider and has a position set to absolute. The problem arises when viewing it on smaller screens, as only the left side of the wide banner is visible. Initially, I tried using med ...

Utilizing Vuejs to initiate a GET request using a method

Currently, I am working on enhancing the functionality of a GET request in Vue. My goal is to attach the request sending action to the form's submit button and also include a parameter from a text field within the form. HTML <html> <head> ...

Implementing timeAgo with jQuery (or a similar tool) to display the elapsed time since a user (client) accesses or updates a webpage

Currently, the timer displayed always shows the actual time instead of phrases like "about a minute ago" or "5 minutes ago". I have tried different solutions without success. //timeago by jQuery (function timeAgo(selector) { var templates = { pref ...

Having trouble using jQuery to target the element "(this)"?

Currently, I am delving into jQuery and have stumbled upon a section that perplexes me. Due to my lack of familiarity with the terminology, conducting an effective Google search has proven difficult. Therefore, I decided to craft this question to elucidate ...

What is the process for excluding or removing an input once the PHP code has been run?

I am working on a project with a post input field where I submit simple data. <form method="post" action="result.php"> <input type="url" name="url" class="form-control" placeholder="http://example.com/"> <input type="submit" name="s ...

Manipulating the visibility of a template with ng-show and ng-hide

Is it possible to use ng-show and ng-hide in order to display or hide an HTML template, particularly if that template is a separate HTML file? I am attempting to divide my HTML file into two sections and toggle their visibility based on an event. ...

How can I retrieve text within a p tag using xpath and xpath axes?

<div class="details"> <p><b>Compatibility:</b> All versions</p> <p><b>Category:</b> Entertainment</p> <p><b>Updated:</b> Apr 2, 2014</p> <p><b>Version ...

Retrieve all elements contained within the body tag using XPath

In my search query, I have been using the following code: //*[self::div or self::p or self::span]/text()[normalize-space()][string-length() > 140] However, this code is fetching content from non-required elements, such as <head>, which is not wh ...

Issue with Vuetify carousel not resizing to fit the entire width and height

I am looking to create a carousel that spans the entire width and height of a viewport in my Vue.js 2 project using Vuetify. Below is the code I have so far: <head> <link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500, ...

CSS: concealing certain portions of an element's content

I don't have control over the following markup: <p class="filename"> <img src="http://domain.com/uploads/image_gallery/_thumbs/Chrysanthemum.jpg" alt="Chrysanthemum.jpg"> <br> Chrysanthemum.jpg </p> Is it possible ...

Align the final row to the left in a flexbox that is centered

My issue involves a flexbox containing NxN squares. I need the container to adjust to fit as many squares as possible within the display width, while also center-aligning the flexbox on the page. The problem arises when I set the CSS property: justify-con ...

Displaying JavaScript Countdown in PHP Table

I have a database table with multiple fields and I am looking to create a countdown using the integer value from one of the fields (in minutes). How can I loop through and display the countdown for each row in a PHP table utilizing these values, with the a ...

Changing the format of PHP SQL results from vertical to horizontal

Is there a way to convert the vertical table generated by the following code into a horizontal one with stylish formatting? Additionally, is it possible to display certain columns in a popup window based on query parameters using jQuery or CSS? <html&g ...

Maximizing values entered into form fields

Looking for a way to extract the highest number from a set of input fields in an HTML form using JavaScript? Take this example: <input id="temp_<strong>0</strong>__Amount" name="temp[<strong>0</strong>].Amount" type="text" valu ...

Enabling auto-expansion for textareas with every keystroke

Currently, I have implemented the following script: jQuery.each(jQuery('textarea[data-autoresize]'), function() { var offset = this.offsetHeight - this.clientHeight; var resizeTextarea = function(el) { jQuery(el).css('h ...

Tips on creating a horizontal scrolling effect using the mouse

Is there a way to enable horizontal scrolling by holding down the mouse click, instead of relying on the horizontal scroll bar? And if possible, can the scroll bar be hidden? In essence, I am looking to replicate the functionality of the horizontal scroll ...

What prevents Django websites from being embedded within another HTML (iframe)?

I attempted to include a Django form within another HTML page, but it's not functioning correctly. I've tried on several of my other Django sites with no success. I even tested it on other websites as well. Is there a restriction on using Django ...