Tips on preventing gaps between the initial div elements in each row

I have DIVs that are positioned next to each other, but only the fourth DIV is in the first row, while the others are shifted to the next row.

I want each first div in a row to not take up space from the left side, but this is not happening. Here is my code:

http://jsfiddle.net/25pwG/

I know I can achieve this by manually assigning a class to the new row DIV, but I prefer a solution with minimal CSS and without altering my markup.

NOTE: I need it to work on IE8 as well.

Thanks in advance :)

UPDATED:

http://jsfiddle.net/25pwG/8/

Firstly, the .parent div has a common class used in other sections as well. Secondly, according to the design, the parent DIVs should reach the last div of the row, leaving no space on the right side.

Answer №1

Recommendation A:

Try adding margin-left:-16px to the parent element

See it in action: http://jsfiddle.net/25pwG/1/

Recommendation B:

Consider using margin-right: 16px on the inner divs instead of margin-left

See it in action: http://jsfiddle.net/25pwG/4/

Recommendation C:

If the width of the parent div is fixed, you may remove margin from every 4th child like this:

.feature:nth-child(4n){
    margin-right:0px;
}

See it in action: http://jsfiddle.net/25pwG/15/

Recommendation D:

Enclose your inner divs in a wrapper and make it wider than the parent div

See it in action: http://jsfiddle.net/25pwG/17/

Answer №2

Avoid using negative margin and padding. It's better to utilize the margin-right:16px rule in the feature class and eliminate the .feature + .feature class.

Therefore, your css should look like this:

.parent{
    width:480px;
}
.feature{
    width:100px;
    height:100px;
    background:red;
    display:inline-block;  
    margin-bottom:10px;
    margin-right:16px;
}

Check out the example here: http://jsfiddle.net/25pwG/7/

Answer №3

.container{
    width:480px;
    text-align: center;
}
.box{
    width:100px;
    height:100px;
    background:red;
    display:inline-block;  
    margin:0 6px 10px;
}

Consider this alternative approach. Negative values could potentially lead to complications down the line. It's best to steer clear of them if possible.

Answer №4

In order to make the adjustment, all you need to do is switch from

.feature + .feature { margin-left:16px; } /*This line can be removed*/

to

.feature { margin-right:16px; }

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

When using JQuery Validation on a small screen, error messages can cause the button to shift downwards

After successfully creating an email form with jquery validation error style that appears when there's an error, I encountered a problem. When the screen width is reduced or viewed on a small screen, the error message pushes down the button. I&apos ...

Ways to manipulate the content of a div

HTML <span class = "Box"> <div class = "active"> <a href ="#-night"> <span class ="list">4</span> <span class="locations"><?php echo $location; ?></span> <span ...

Customizing the background color in Bootstrap 4 without impacting the container

Hey there, I'm currently working on a login page for my website. However, I've run into an issue where changing the background also changes the color of my div container. How can I resolve this problem so that only the background changes? Below ...

``When the screen size is reduced, the Bootstrap columns will resize and appear next to

Struggling to create a responsive web design similar to: https://i.sstatic.net/scRyw.png When resizing, I want the skills section to appear side by side under the introduction: https://i.sstatic.net/SLzsx.png The issue lies with the layout of the introdu ...

The footer of the website remains separate from the overlay of the modal Popup Form

I recently implemented a modal popup form on my website that covers the entire viewport with its background. However, I am facing an issue where the footer of the website is still displayed on top of the modal background, even though everything else seems ...

What's the best way to make sure my table fits perfectly within its container's width?

I've encountered an issue with a table: If you want to see how the table was created, check it out on jsFiddle I attempted to modify the CSS like this : table{ width:100%; } td span { float:right; width:20%; text-align:center ...

Generate a spider's web beneath an hour element using CSS

I am attempting to create a spider's web effect under an <hr /> element, but I am encountering difficulties with the circular parts. Instead of using SVG, I want to style the hr element so that the web appears under all instances of it, without ...

When the user clicks the back button in AngularJS

After searching extensively, I have yet to find a straightforward solution to my issue. The problem lies in a search/filter field that filters the page based on user input. While this filter works efficiently, it clears whenever a navigation item is clicke ...

When using React, the page loads and triggers all onClick events simultaneously, but when clicking on a button, no action is taken

I have a strong foundation in HTML and CSS/SASS but I'm just getting started with React. Recently, I encountered an issue that has me stumped. I am trying to highlight a button on the navigation bar based on the current page the user is on. I attemp ...

What is the method of aligning content to the left side in a slick slider?

My slider is set up to display three elements, but I'm having trouble aligning one or two elements to the left instead of centering them. jQuery(function () { jQuery('.slider-blog').slick({ arrows: false, dots: true, ...

Swapping data between two HTML files and PHP

As I work on setting up a signup form for a MikroTik hotspot, I've encountered limitations with the device not supporting PHP. In order to pass the necessary variables from the device to an external PHP page where customer data will be saved and trial ...

Content is not properly fitting in the Bootstrap modal window

I am experiencing difficulties with the appearance of a bootstrap modal on my website. Specifically, the modal div seems to be too tall and too narrow in comparison to the modal-dialog div. Here is where I am triggering the modal. <footer> < ...

Displaying information from a database in HTML dropdown menus and tables

Recently, I started learning PHP and I am facing a challenge with my database that has 5 columns: "id", "fldName", "fldPrice", "fldSupplier", and "fldPackage". In my HTML table, I have select options where I display "fldName" based on a specific value of ...

Angular causing WKWebview to freeze

Situation Our Angular+Bootstrap app is functioning smoothly in Desktop on all operating systems, as well as Android and iPhone devices. There are no visible JavaScript errors or CSS warnings. Dilemma However, an issue arises intermittently while using t ...

Achieve vertical centering of items without relying on absolute positioning or flexbox

I am struggling to align three divs vertically without using position:absolute or flexbox, as they will affect other elements on the page that I cannot control. Here is the current code snippet: <div class="search-wrapper text-center " ...

A guide on retrieving values from programmatically created input elements in Java

Here's the HTML code that I am currently working with: <form method="get" action="#" id="startForm"> <input type="text" name="period" id="period" placeholder="The number of days"/> <input type="submit" name="submit" value="subm ...

How to make an image expand to fit the screen when clicked using HTML and CSS

Is there a way to make the images on this page grow to screen size when clicked, rather than extending past the boundaries of the screen? Currently, the images enlarge to twice their original size on click. How can I ensure that they expand responsively to ...

How come my ejs files are all affected by the same stylesheet?

Currently, I am in the process of developing a nodeJS application utilizing Express, MongoDB, and EJS template view engine. The server.js file has been successfully created to establish the server. In addition, a separate header.ejs file has been implement ...

Tips for inserting text into the description field on Shopify using Python Selenium

I am looking to build a bot using Python Selenium that can input the values I provide. I understand how to work with headings and select items, but I am unsure of how to paste text in a description field. For example: How can I paste text? I am unable to ...

Expanding an element to cover all columns in a grid with automatically generated columns

I am working on a CSS grid layout with automatic columns and 2 rows. All elements are normally placed in the first row except for one special element that should span both rows while filling all available column space. My current code looks like this: ...