Tips for managing the margin on the right side within a container that has padding applied on the

I am facing an issue with my typical setup, where a div contains content with padding and uses the padding-right property to create separation. However, when items near the right edge, both the padding-right and overall padding (essentially padding-left) from the parent div are being applied. How can I solve this problem?

#container {
    padding: 20px;
}

#container div {
    margin: 0 5px 5px 0;
    width: 100px;
    display: inline-block;
}

<div id="container">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</div>

Answer №1

If you're looking for a temporary solution in your situation, try including the following snippet:

#wrapper:last-child {
    padding: 0;
}

Answer №2

There is no need to include margin-right in the CSS code as it will be applied by default when there is no float property set

#container div {
    margin: 0 0 5px 0;
    width: 100px;
    display: inline-block; border:green solid 1px
}

View DEMO here

Answer №3

#content div {
    padding: 5px;
    width: 200px;
    display: block;
}

#content div:last-child {
    margin-right: 0;
}

:last-child is compatible with IE8, but :first-child is not. In order to ensure proper alignment, I have adjusted the right margin instead.

Answer №4

In terms of compatibility with web browsers,

#container > div:last-of-type {
    margin-right: 0;
}

This solution appears to be quite successful.

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

React.js combined with Webpack may not properly interpret @media queries

Currently, I am working on a website built with React.js (version 15.6.1) and Webpack 1. I have implemented a SASS-compatible version of the Flexbox Grid following this tutorial. However, I have noticed that the @media expressions are not being compiled as ...

Guide to Aligning Divs at the Center in Bootstrap 4

I've been attempting to center the div on the page using Bootstrap 4, however, it's not cooperating. I've tried using the margin:0 auto; float:none property as well as the d-block mx-auto class, but neither are working. Below is my HTML code ...

The webpage transforms with the holiday spirit

I have created multiple backgrounds for my website (available here), and I am looking for a way to have them automatically change based on specific occasions like Christmas, Halloween, etc. Manually changing them is an option, but where's the fun in t ...

Forced Landscape Viewing on Mobile Site with No Auto-Rotation

My website is equipped with a mobile stylesheet: <link rel="stylesheet" href="css/mobile.css" media="handheld"> In addition to that, I am utilizing jQuery to customize functionality for mobile devices. I am wondering if there is a method to enforc ...

Modify the td attributes while retaining the extracted data values from the website

I'm currently utilizing this code to extract data from another website: $searchURL = "http://www.anotherwebsite.com"; $html = file_get_contents($searchURL); $patternform = '/(<tbody.*<\/tbody>)/sm'; preg_match_all($patternfor ...

Can combinators be applied to select elements containing options?

How can I use a select dropdown menu to toggle the appearance of a text input field? Specifically, when the option with a value of '4' is chosen, the text input should become visible. Here is the code snippet: HTML <div class="paren ...

Can anyone provide a method for identifying the location of the page fold on a specific viewport?

Many innovative designs have successfully managed to position their content above the page fold in a way that it always remains visible within the viewport. Currently, I am using JavaScript to calculate the height, but this approach has its drawbacks. Her ...

JavaScript causing issues with CSS transform translate functionality

const openBtn = document.querySelector('.open') const closeBtn = document.querySelector('.close') const navContainer = document.querySelector('.nav-container') openBtn.addEventListener('click', () => { openBt ...

Is assigning an ID to multiple objects a poor decision?

While working on my Django project, the template quickly became cluttered with elements all having unique IDs. I could reduce the number of IDs by assigning them to their parent elements, but this would require longer jQuery code. So now I'm faced wit ...

Executing a function on a dynamically generated JavaScript button

Is there a way to create a button that triggers an onClick event calling a function with the signature deleteEntry(entry)? I'm encountering an error stating that entry is undefined even though I have used it in the same function. Adding escape charact ...

Issue with jquery .click function not triggering after CSS adjustment

In the process of creating a fun game where two players take turns drawing on a grid by clicking <td> elements. Currently, I have implemented the functionality to toggle the background color of a <td> element from black to white and vice versa ...

Why does the DropDownList consistently remove the initial value?

I am currently in the process of developing a recipe website focused on meals. I have created an admin panel where I can manage the meals added to the site. One issue I am facing is with deleting a meal that was previously added. The menu displays the numb ...

Change when the mouse departs

I am currently working on implementing a transition effect. My block consists of 3 main parts: Base block Hover image (coming from the top) Hover title (coming from the bottom) When hovering the mouse, there is also a background-color transition that o ...

Step-by-step guide to adding a skew overlay to your video

I am experimenting with creating a skewed overlay on a video playing in the background at full width. Currently, the skew overlay is functioning perfectly. What if I want it to appear in the bottom-right corner instead of the top-left corner? Would I need ...

Accessing JavaScript cookie within .htaccess file to establish redirection parameters according to cookie content

Is there a way to modify the rules within my .htaccess file so that it can properly read a user-side cookie and redirect based on its content? The scenario is this: I have a cookie called userstate which contains an abbreviation of US states, such as AL ( ...

"Unexpected behavior: PHP+JS getElementById() function is returning an integer value instead of a

EDIT: Apologies for the PHP section not displaying correctly. I am attempting to extract a decimal value from PHP/HTML in JavaScript using getElementByID().value. However, despite the PHP value being decimal, it is represented as an integer in JavaScript ...

Is there a proper method for populating an HTML text with values from a form?

As a newcomer, I could really use some guidance here :) I'm trying to populate text with various words, numbers, and calculation results from a form. This is my initial attempt for just one field/word, and it appears to be functioning correctly. Do y ...

Is it possible to resize the background-image style?

When using the regular img tag in HTML, you can specify the width and height, and the browser will scale the image. If the dimensions are not too different from the original, the result is usually decent. For example, you can scale the avatar image to 32x ...

Styling Your Navigation Bar with CSS and Active States

Creating an interactive navigation element for a menu can be challenging, but here's a helpful example. http://jsfiddle.net/6nEB6/38/ <ul> <li><a href="" title="Home">Home</a></li> <li class="activ ...

How can I maintain code reusability while customizing buttons with pseudo-elements to display unique content on each button?

I have a ul in my HTML with lis that contain navigation options such as home, contact, about, etc. I am trying to figure out the best way to add a unique effect to each button since they all have different content and require different icons. Essentially, ...