Managing Margins and Padding in Multiple Lines of Divs in CSS

Trying to understand the spacing issues that arise when tiling a series of divs within a parent div. Here is a link to the fiddle for reference: http://jsfiddle.net/SNSvU/4/. Below is the code snippet:

<div id="prioritiesWrapper">
   <div class="priority"><div class="img">Img here</div><div class="title">This is my priority title</div></div>
   <div class="priority"><div class="img">Img here</div><div class="title">This is my priority title; it's longer than the others and wraps</div></div>
   <div class="priority"><div class="img">Img here</div><div class="title">This is my priority title</div></div>
   <div class="priority"><div class="img">Img here</div><div class="title">This is my priority title</div></div>
   <div class="priority"><div class="img">Img here</div><div class="title">This is my priority title; it's longer than the others and wraps</div></div>
   <div class="priority"><div class="img">Img here</div><div class="title">This is my priority title; it's longer than the others and wraps</div></div>
   <div class="priority"><div class="img">Img here</div><div class="title">This is my priority title</div></div>
   <div class="priority"><div class="img">Img here</div><div class="title">This is my priority title</div></div>
   <div class="priority"><div class="img">Img here</div><div class="title">This is my priority title</div></div>
   <div class="priority"><div class="img">Img here</div><div class="title">This is my priority title</div></div>

CSS

#prioritiesWrapper {
    color: white;
    background-color: blue;
    margin: 0 auto;
}

#prioritiesWrapper .priority .img {
    float: left;
    height: 50px;
    margin: 0 15px 0 0;
}

#prioritiesWrapper .priority .title {
    margin: auto 0;
}

#prioritiesWrapper div.priority {
    width: 350px;
    height: 80px;
    display: inline-block;
    margin: 10px 30px;
    padding: 10px;
    background-color: black;
    text-align: left;
    font-weight: bold;
    line-height: 2em;
}

The spacing between divs seems consistent only if the priority titles on a row have similar lengths (one or two lines), causing alignment issues when they differ in length. How can I achieve uniform spacing regardless of the content length?

Answer №1

Would you prefer it like this? http://jsfiddle.net/SNSvU/5/

#prioritiesWrapper div.priority
{
width: 600px;
height: 80px;
display: inline-block;
margin: 10px 30px;
padding: 10px;
background-color: black;
text-align: left;
font-weight: bold;
line-height: 2em;
}

Alternatively, would a more dynamic approach be better, such as this? http://jsfiddle.net/SNSvU/6/

#prioritiesWrapper div.priority
{
width: auto;
height: 80px;
display: block;
margin: 10px 30px;
padding: 10px;
background-color: black;
text-align: left;
font-weight: bold;
line-height: 2em;
}

Answer №2

Check out the demonstration of this CSS in action here

#prioritiesWrapper div.priority {
width: 350px;
height: 80px;
display: inline-block;
margin: 10px 30px;
padding: 10px;
background-color: black;
text-align: left;
font-weight: bold;
line-height: 2em;
vertical-align: middle;/* ADDED */
}

Answer №3

For those unconcerned with the height of divs, simply adjust the height property to auto as shown below:

#prioritiesWrapper div.priority
{
width: 350px;
height: auto;
display: inline-block;
margin: 10px 30px;
padding: 10px;
background-color: black;
text-align: left;
font-weight: bold;
line-height: 2em;
}  

Check out the fiddle here

Answer №4

Check out this fiddle link http://jsfiddle.net/SNSvU/11/. Apply the CSS properties

#prioritiesWrapper { overflow: hidden; }
and
#prioritiesWrapper div.priority { float: left; }
to achieve the desired effect. That should do the trick!

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

Updating the content within a div following the submission of a contact form 7

I'm struggling with my contact form. I want the content of my contact form div to change after clicking submit on the form. I've been searching for a solution, but so far, no luck. Here is what I have: Form (div1) with input fields, acceptance c ...

Tips for dynamically hiding/showing a button depending on the focus of two different HTML inputs

I'm working on a login section and looking to add a unique feature: When a user selects the email or password input field, I want the button text to change to 'LOGIN'. If neither input field is selected, the text should display 'NOT A ...

"Applying CSS styles to HTML elements through PHP code

Hey there, I need help with some CSS styling for my PHP code. The code below is used to display a name: while ($db_field = mysql_fetch_assoc($result)) { print $db_field['ship_name'] . "<BR>"; I really want to jazz it up with some ...

implementing a fixed header in HTML

I am facing a challenge where I have a header with dynamic height and fixed position. My goal is to position the container div right below the header. However, since the header height varies, using a fixed value for margin-top is not feasible. Any sugges ...

There is zero gutter in Bootstrap

Is it possible to use the bootstrap grid system without any gutters? I want to have 3 columns in one row like this: http://prntscr.com/6gpmhm. I have shared the markup for the CSS I am using, which is the default CSS of the framework. <section class ...

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: ...

Displaying JSON data with dynamic color changes in an HTML table: A comprehensive guide

Scenario: I am currently working on a project where I need to fetch data from an external json file, parse it, and then dynamically add it to an HTML table using the footable jQuery plugin. Question: I have a query regarding how to use JavaScript to parse ...

jQuery's slide toggle function is limited to toggling the visibility of just one section at

Welcome to my first post! As a newbie in web development, I have just started my first project. While I feel comfortable with HTML and CSS, jQuery is proving to be a bit challenging for me. In the head section of my intranet page, I have the following cod ...

Assign the two values to variables in the CSS script

My challenge lies in passing two values to the CSS within the <style> tags. The values are: (background-color: --placeholder; color: --placeholdtext;). I am able to pass either one of the values successfully, but not both at the same time. When I cop ...

Display validation messages when the user either clicks out of the textbox or finishes typing

Here are some validation messages: <form name="dnaform" novalidate> <div style="padding-bottom:5px" ng-show="dnaform.uEmail.$pristine || dnaform.uEmail.$valid">Active directory account </div> <div style="padding-bottom:5px;" n ...

Aligning a collapsible feature while ensuring that the content descends

My dilemma involves a centered menu with a dropdown feature. The issue arises from the absolute positioning, causing the entire menu to shift upward when the dropdown is activated due to the increased height. I am struggling to find an effective solution ...

Display a single button on hover in Angular 2+ framework

I'm looking to have the 'Edit' button only appear when I hover over a selected row. Here's the code: https://stackblitz.com/edit/inline-edit-change-edit2-j8b2jb?file=app%2Fapp.component.html I want the 'Edit' button to show u ...

It is not possible to place the cursor before a non-contenteditable div within a contenteditable div

Here is the layout I am working with: <div contenteditable=true> /// <div contenteditable=false>not editable</div> /// </div> One problem I'm facing is that when the non-contenteditable div is the first element on a ...

Placing two images within one div tag

I've been attempting to place two images within a single div, but they're not appearing how I intended. I'd like there to be some space between the images, but that's not happening. Here's the CSS I'm using: .sidebarBody { ...

What is preventing the addition of links to the navigation bar when using a sticky navigation bar?

Currently, I am in the process of developing a blog website using Django. One of the features I have successfully implemented is a sticky navigation bar. However, I am facing a challenge with adding links to the menu on the navigation bar due to existing ...

fullpage.js: the content exceeds the height limit

I am currently working on customizing the jquery script fullpage.js for a website built on the French CMS "SPIP" (). This script is used to create a one-page website with both horizontal and vertical navigation. However, I have encountered an issue with ...

The bubble dialogue box in my picture

Looking to create a panel with images that, when clicked on, display an info window similar to Google Map's pin maker. When you click on an image, a balloon should appear containing additional information. Check out this example <a id="infowindow ...

What is the best way to ensure that all of my images are the same size?

I've been working on making my pictures of varying resolutions the same size here, but no matter what I try, it just doesn't seem to work. Here is what I'm getting: . When I change the state to 1, 2, 3, or 4, I want it to look like this: her ...

The Elusive Key to Perfect Layouts and its Transformation

Today I discovered the Holy Grail of Layouts while coding, but when I implemented it, the output in browsers was not as expected. The borders were incomplete and strange: Here is the code snippet that caused the issue: #container { border: 10px soli ...

Is it possible to utilize arrow functions in Vue while using style binding?

As I venture into the world of Vue JS HTML templates, I am exploring how to bind styles using arrow functions. My goal is to toggle the visibility of a div that originates from the Vuex store. Below is my current attempt at achieving this. The main_activi ...