Creating Custom Styled Divs Dynamically

I am currently trying to customize the appearance of divs that are displayed on the front page. https://i.sstatic.net/BsEaM.png

My platform is WordPress, and my goal is to have each new post align alternately left and right starting with the first post.

This was my initial attempt:

            echo '<div class="popImage">';
            the_post_thumbnail();
            echo '</div>';

I tried using :nth-child(odd) and :nth-child(even) to style them. However, after adding a third post, the layout broke and the box appeared next to the first post.

I'm unsure if I'm approaching this correctly or if there's a better way to achieve what I want. Any advice would be greatly appreciated.

Here is the CSS code that didn't work as intended:

.popImage {
width: 15rem;
height: 15rem;
background-color: #3700ff;
display: inline-block;

}

.popImage:nth-child(even) {
float: left;
}

.popImage:nth-child(odd) {
float: right;
background-color: yellow;
}

Answer №1

I couldn't quite grasp the issue you're facing, but it seems like you want to create a 2-column grid where each column features a new post next to the previous one. Here's a visual representation of what the code snippet I'll provide will achieve:

~~~~~

| [p] [p] |

| [p] [p] |

| [p] [p] |

~~~~~

I hope this clarifies things. Now, here's the code snippet to help you set up this grid (I'll use float in this example, but I recommend using flexbox instead):

.popImage { 
margin: 1rem 0; 
width: 49%; 
height: 15rem; 
background-color: #3700ff; 
display: block; /* must be block for correct functionality */ 
float: left; 
} 
.popImage:nth-child(even) { 
margin-left: 1%; 
} 
.popImage:nth-child(odd) { 
background-color: yellow; 
margin-right: 1%; 
} 
.popImage img { 
height: 100%; 
width: 100%; 
} 

P.S: I suggest wrapping all the .popImage div elements within a container for the posts and then utilizing its ::after pseudo-element to clear the float. Here's an example:

.popImageWrapper:after { 
content: ""; 
display: table; 
clear: both; 
/* using single colon before 'after' to support older browsers */ 
} 

I hope this guidance propels you forward.

Answer №2

To achieve the desired effect, consider removing the float properties. If you want to maintain the offset spacing, try adding position:relative; to .popImage and adjusting the values with top or bottom.

In order to balance the left/right alignment, limit the .popImage container to hold only two images.

If I were in your shoes, I would opt for using flexbox for a cleaner solution :)

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 add selected values from a multi-select dropdown into an array?

Attempting to populate an array from a multiple select dropdown, I've encountered an issue. Despite using splice to set the order of values being pushed into the array, they end up in a different order based on the selection in the dropdown. For insta ...

Guide to importing a single icon from an icon library image

I'm not very experienced with web front-end development. The web designer provided me with a JPG file that contains a bar full of icons. https://i.sstatic.net/3fmAx.png However, I need to use these icons individually. I'm unsure if there is a ...

Why won't my jQuery datepicker show up?

I am encountering these issues with IE7: Line 22: object does not support this property or method line 142: invalid argument I am attempting to implement a datepicker on a textbox: <script> jQuery(function($) { $("#<%= re ...

Unique twist on the Bootstrap grid: perfectly centered

I'd like to design a 12-column Bootstrap grid with specific colors. The header should be light blue, the body in green, and the footer orange against a grey background. I want the grid to be centered on the screen with horizontal light blue stripes m ...

Challenges with the Task List Board

I'm facing a bit of a challenge with this task. As a newcomer to JavaScript, I've set out on a quest to create a task list board where users can input tasks along with dates and times. The goal is for these entries to appear inside a photo using ...

What could be causing my dropdown buttons to malfunction when paired with an embedded Google Maps feature?

Whenever I integrate my google map into the page, the dropdown menu in the navbar and navbar hamburger stop working. Clicking on them does nothing at all. This is how my page looks with the embedded google map: <body> <nav> <nav class= ...

Discover the status of appcache on a webpage even without a manifest file by utilizing an iframe

Looking to cache two important pages of my web application using Application Cache. Successfully implemented with the iframe technique to specifically cache those two pages without caching the manifest-calling page. However, encountering an issue where I ...

Tips for concealing labels until the submit button is activated

I am currently handling a project involving a Spring Controller and JSP. Within a JSP page, I have included a button labeled Process. Upon clicking this button, two radio buttons are displayed below it along with a Submit button. After tapping the Submit ...

mobile-exclusive wordpress text area problem

There seems to be a padding issue that is only affecting mobile devices. https://i.stack.imgur.com/2Tbuc.png The cause of this problem has been somewhat identified, but the solution isn't clear without impacting the preview on computers. Here is th ...

The images fail to load on Mozilla browser and appear as blank spaces

My images are only displaying as white in Mozilla but fine on other browsers. I've tried using -moz without success. It's a simple fix that I can't seem to locate. Any help would be appreciated. Just a quick note, the images appear blank wh ...

Card carousel rotation

Apologies for the vague title, I'm unsure how to properly label my issue. I have a section set up with cards, but I'm aiming to create something resembling a menu that functions like a carousel. Essentially, I want to include right and left arro ...

Clicking on a link to submit a form and passing a POST variable

Hey there, I'm trying to figure out how to use an anchor tag to submit a form. My form originally had an input button that passed a post variable for me to check in my php script. How can I replicate this functionality with an anchor tag? Here's ...

Is there a way to resolve the warning message "The shadow-piercing descendant combinator (>>>) is deprecated" that appears when running ng build for production?

I keep receiving warnings when I run ng-build -c production for all my styles that contain the '>>>' notation. Warning: ▲ [WARNING] Unexpected ">" /Users/mike/project2022/client/src/app/bank/bank-new/bank-new.component.ts-angular- ...

Is it possible to conceal an error message while another error message is being shown?

<form name="form"> <input type="password" placeholder="Password" ng-model="password" name="password" minlength="6" maxlength="15" ng-pattern="/^\S*$/" required> <span ng-show="form.password.$dirty && form.password.$in ...

How to collapse one section while expanding another in Bootstrap 4?

const cur_url = window.location.href.split('#')[0].split('?')[0], body = $('body'), menu_toggle = $('.menu-toggle'), sidebar_menu = $('#sid ...

Iterating through styles in a selector's attribute

I am creating a site map layout. Looking for a solution similar to this: ul.site-map li[data-level="1"] { margin-left: 50px; } ul.site-map li[data-level="2"] { margin-left: 100px; } ul.site-map li[data-level="3"] { margin-left: 150px; } This code ...

Exploring the Functions of Bootstrap's Top Navbar and Sidebar

I attempted to utilize the top navigation and sidebar components in Bootstrap 4, but encountered issues. My header.jsp file serves as the top navigation bar, which is connected to the sidebar page using an action tag like this: <header> <jsp ...

Flex containers positioned next to each other without overlapping and adjusting in relation to one another

I would like to prevent the lower div (which includes an image and a button) from overlapping with the division above it that contains text. So, when the button encounters this upper division, I want it to move down in a way that pushes the image beneath i ...

Using the <li dir="..."> tag can cause list indicators to break

Is there a method to utilize dir tags for <li> elements without disrupting the list indicators? Logically, they should all align on the same side and RTL <li> elements in an otherwise LTR document should either be left-aligned if it's only ...

Having trouble passing parameters from an HTML/JS form to a URL

As someone new to the realm of front-end development, I find myself tackling what appears to be a straightforward task. The challenge at hand involves taking a value entered by a user in an HTML/JS form and then adding that value to the end of a public se ...