Spacing Div Issue

Trying to organize DIVs can be tricky. I have five DIVs that are each 30px wide and I want to place them inside another DIV that is 150px wide. However, it seems like the five DIVs do not fit as expected.

Even though 5 * 30 = 150, in reality it requires a 166px outer div for them to all fit inline.

You can view my code on this fiddle

<div class="A">
    <div class="B" >a</div>
    <div class="B" >b</div>
    <div class="B" >c</div>
    <div class="B" >d</div>        
    <div class="B" >e</div>
    <div class="B" >f</div>
</div>

div.A { background-color: Red; width: 150px;}
div.B { display: inline-block; height: 20px; width: 30px;}

I'm puzzled by the spacing behavior of browsers. Is there something crucial that I am overlooking?

Answer №1

Transforming the divs into inline elements causes the adjacent inline content to affect the layout, including the white space between the elements. This results in a few extra pixels being taken up by spacing between each div.

To eliminate the spaces between divs, simply remove the white space between them, allowing for five elements to neatly fit within a 150-pixel width:

http://example.com

Answer №2

Are you able to switch out display:inline-block with float:left without triggering any additional problems? It seems like this change could solve your current issue...

div.B { float:left; height: 20px; width: 30px;}

Answer №3

Implement the float:left CSS property for classes A and B to improve your layout.

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

Failed to load resource: The server returned a 404 error when deploying React on Github Pages

Currently working on a React App. Started by deploying on GH page with SASS, but encountered an issue. Can anyone provide guidance on resolving this? Unsure of the specific code needed, so just looking for which part of code is necessary to add. https:/ ...

Thumbnails failing to line up in a continuous row

Having an issue creating a row with 3 thumbnails as they are not aligning in a single row, instead each thumbnail is going into a different row. echo "<table>"; echo "<tr>"; echo "</tr>"; while($r ...

Align the text on the same horizontal line

I have been struggling with this issue for hours. Here is my Header.js <div className="navbar-inner"> <h2>Text1</h2> <h3>Text2</h3> </div> This is the content of my Header.css: .navbar-inner { ...

Can markers be displayed upon clicking a designated button?

Is there a better way to group markers on a map for improved readability? I have markers labeled ''Private Cloud - XXX'' that I want to display when specific buttons are clicked, like the button ''Private Cloud Regions'& ...

Looking for a method to quickly send an email via "mailto" in CSS (or any web development language) without the need to open Outlook or Apple Mail?

I'm currently in the process of developing my own website which includes a contact form with a message box. However, when I click submit or send, it redirects me to my Outlook email where all the entered data is collected. I then have to click send ag ...

Create a stylish frame around the interior triangle shape

I have created a div containing a triangle shape as shown below: #inner-div { width:200px; height:200px; background-color:black; position:relative; border:2px solid red; } #triangle { border-right:15px solid transparent; border-left:15px s ...

Tips for aligning an image within a div, using a specific aspect ratio, while maintaining the original height and width

Apologies for the confusing title, but I'm struggling to find the right words to explain my issue. How can I properly fit my image labeled as "carouselMap" within the div named "carouselItem" while maintaining an aspect ratio of 2 ...

Concealing words within a hyperlink

I have a few inquiries regarding the issue of concealing text within an anchor tag and exclusively showing a background image. To those who may be eager to inundate me with links to other articles and assert that this is a redundant query, let me clarify ...

What's the reason the bootstrap tooltip style isn't displaying?

Seeking assistance with styling text in a bootstrap tooltip. My request is straightforward: I would like the text in the tooltip on the first button to be displayed in red color. I have used data-html="true" attribute to allow HTML in the tooltip. Howev ...

The Limits of JavaScript Tables

Currently facing an issue with a webpage under development. To provide some context, here is the basic layout of the problematic section: The page features a form where users can select from four checkboxes and a dropdown menu. Once at least one checkbox ...

Printing from Chrome is becoming a time-consuming process for this particular webpage

I've been working on creating a print/PDF-friendly template for articles on a WordPress site by customizing the Tufte CSS template. However, I'm facing an issue where the print preview render (and subsequent printing/saving as PDF) is extremely ...

What is the best way to hover over multiple "div" elements simultaneously?

I have <div id="d1" class="hov"></div> as well as <div id="d2" class="hov"></div> with the following CSS applied .hov:hover{ background-color:#cde0c4; cursor:pointer; } Is there a way to make both d1 and d2 hover at th ...

What is the best way to cut off multiple lines of text and display the remaining strings at the

Currently, I am working on a project that involves implementing a gallery feature. The challenge lies in displaying file names which can be quite lengthy, and we are restricted to showing only 2 lines of text. While this can be achieved using line clamp an ...

Retrieve CSS content from a file hosted on the local server

Is it feasible to use a locally hosted CSS file for styling a website managed by a server-based CMS? I am interested in utilizing SASS for local CSS development while retrieving the HTML content from a centrally hosted CMS server. ...

Unique CSS animations that vary before and after

Looking to enhance a navbar with some interactive effects: Upon hovering over an item, I want it to disappear and be replaced by the same text in a different color. After the initial animation, I'm interested in adding another effect like a 360-degr ...

The webpage must be designed to be compatible with screen resolutions starting from 800 x 600 pixels and higher, utilizing Angular

I am working on developing a webpage that is specifically designed to work for resolutions of 800 x 600 pixels and higher. Any other resolutions will display the message "This website can only be accessed from desktops." Here is my approach using jQuery: ...

What is the correct way to enable overflow-y as visible while setting overflow-x to scroll within a div

How can I implement overflow-y: visible with overflow-x: scroll in my code? The number of buttons may vary, and when there are too many, they should scroll horizontally. However, when I click the Dropdown button, I want to make sure that the dropdown list ...

Show text overlay and add image opacity when hovering

My goal is to create a hover effect on an image where it becomes semi-transparent and displays text that is initially hidden using display: none. Right now, when I hover over an image, it does become semitransparent (.single-image:hover is working), but ...

Transform the Bootstrap container to fill the entire height of the browser

How can I make the container height 100% of the browser without breaking the contents inside? html, body { height: 100%; min-height: 100%; } .container { min-height: 100%; height: 100%; background-color:red; } <div class="contai ...

Arrange three images and three text lists in a horizontal layout with responsiveness

Struggling with a HTML/CSS challenge here! I'm attempting to vertically align 3 images and text in the pattern of image, text, image, text, image, text. Everything looks great on my 13" MacBook, but once I start resizing the window, chaos ensues. Any ...