Is it possible that 50% plus 50% is not equal to 100%? And what could be causing the <div> elements to not align properly

I inspected it using Chrome, analyzed every associated div, and I still can't figure out why the two columns won't align next to each other when set to 50% width each?

My guess is that there might be some default margin or padding causing this issue. How can I override them if that's the problem?

CSS file

.stats-left {
  display: inline-block;
  border : 1px solid black;
  width: 50%;
}

.stats-right {
  vertical-align: top;
  display: inline-block;
  border : 1px solid black;
  width: 50%;
}

.count-padding {
  display: inline-block;
  padding-top: .3em;
  padding-bottom: .3em;
}

.blue-background {
  background-color: #446CB3;
  border : 1px solid black;
}

If I remove the 50% width from both:

I'm unable to eliminate the white gap between the two blue columns.

_count.html.erb

<% @user ||= current_user %>
<div class="stats-left">
  <div class="blue-background">
    <a href="<%= following_user_path(@user) %>">
      <div class="count-padding">
        <%= @user.following.count %>
        Following
      </div>
    </a>
  </div>
  <div class="blue-background">
    <a href="<%= following_user_path(@user) %>">
      <div class="count-padding">
        <%= @user.habits.count %>
        Challenges
      </div>
    </a>
  </div>
  <div class="blue-background">
    <a href="<%= following_user_path(@user) %>">
      <div class="count-padding">
        <%= @user.goals.unaccomplished.count %>
        Current
      </div>
    </a>
  </div>
  <div class="blue-background">
    <a href="<%= following_user_path(@user) %>">
      <div class="count-padding">
        <%= @user.quantifieds.count %>
        Tracked
      </div>
    </a>
  </div>
  <div class="blue-background">
    <a href="<%= following_user_path(@user) %>", id: white>
      <div class="count-padding">
        <%= @user.valuations.count %>
        Values
      </div>
    </a>
  </div>
</div>
<div class="stats-right">
  <div class="blue-background">
    <a href="<%= followers_user_path(@user) %>">
      <div class="count-padding">
        <%= @user.followers.count %>
        Followers
      </div>
    </a>
  </div>
  <div class="blue-background">
    <a href="<%= followers_user_path(@user) %>">
      <div class="count-padding">
        <%= @user.followers.count %>
        Mastered
      </div>
    </a>
  </div>
  <div class="blue-background">
    <a href="<%= followers_user_path(@user) %>">
      <div class="count-padding">
        <%= @user.goals.accomplished.count %>
        Accomplished
      </div>
    </a>
  </div>
  <div class="blue-background">
    <a href="<%= followers_user_path(@user) %>">
      <div class="count-padding">
        <%= @user.results.good.count %>
        Good Results
      </div>
    </a>
  </div>
</div>

Answer №1

Inline elements will have a space between them, even if they are set to inline-block.

To align your elements to the left:

.stats-left,
.stats-right {
    float: left;
    border: 1px solid black;
    width: 50%;
    box-sizing: border-box; /* including padding and border in width */
}
.count-padding {
  display: inline-block;
  padding-top: 0.3em;
  padding-bottom: 0.3em;
}
/* .blue-background { */
.stats-left > div,
.stats-right > div {
    background-color: #446CB3;
    border: 1px solid black;
}

Try out the code on jsFiddle: http://jsfiddle.net/mekt103e/

Alternatively, you can use either of these methods (both considered hacks):

1) Remove spaces between inline-block elements in your markup:

<div></div><div></div><div></div>

2) Insert comments between them:

<div></div><!--
--><div></div><!--
--><div></div>

3) According to @rgthree, apply font-size: 0; to the container and then reset the font size for each inline-block element.

The widths may not sum up as expected due to the additional border taking up space, so it's actually: 50% + border. To address this, use box-sizing: border-box;. With border-box, the set width accounts for any added padding or borders, making it so that border + padding + ? = 50%. Refer to the CSS provided above.

Answer №2

It's true that they are 50% in width, but keep in mind that each div also includes a 1px border (for a total of 4 extra px), causing the total width to exceed 100%:

[1px + 50% + 1px] + [1px + 50% + 1px] > 100%

To accurately calculate the width of each div, you can utilize the calc function: width: calc(50% - 2px).

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

How can I implement a CSS property on a child class when the parent class is toggled on and off?

Looking to style a child div within a parent div that toggles classes based on a flag. The parent div can have either the .pricing-section or .new-pricing-section class. Here's the structure of the parent div: const togglePriceSection = isPricingDetai ...

Why is the event handler producing no value?

Imagine if I tried to attach a jQuery click event handler to one of the functions in my objects, but for some reason it keeps returning undefined on my properties. Why is this happening? var buttonView = { label : 'underscore', ...

Use percentages for the width in CSS and pixels for the height, ensuring that both dimensions are equal

I'm attempting to create a square that adjusts its size according to the screen size. Currently, I have the width set to 25%. Is there a way to ensure that the height remains the same length in pixels as the width? Appreciate any assistance on this ...

Issues with creating modal images using only JavaScript

I am facing an issue with modal popup images. I have a collection of images and attempted to implement this code (with some modifications): https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_modal_img However, it seems to only work for the initi ...

Dealing with problem of highlighting links in Android WebView

I am currently using an html app that is being loaded in a WebView. My goal is to prevent a specific link from loading when clicked by using shouldOverrideUrlLoading, where I return true to stop the URL from loading. While this method works well, the link ...

Adjust the height of one div based on the height of another div

I need assistance with creating a dynamic div height based on the content length of another div. For instance, when DIV A contains certain information and DIV B displays feedback related to DIV A. If DIV B has more content than DIV A, I want DIV A to rema ...

Customizing CSS Styles in ASP.NET MVC for Multilingual Websites

We are looking to make our ASP-MVC website multicilingual and support different languages. One challenge I am facing is how to apply distinct style-sheets for each language, especially when dealing with right-to-left (RTL) languages like Hebrew or Arabic c ...

What could be causing my Bootstrap Carousel to malfunction?

Currently, I am developing a MEAN stack application with Angular 7. In this project, I have incorporated a Bootstrap Carousel, but unfortunately, it seems to be malfunctioning. #slider .item { height: 500px; } .carousel-caption { font-size: 18px; } ...

Interactions of selecting dates in datepicker widget

Currently, I am working on developing my personal work website. One issue I have encountered is with the calendar feature. The "From" date can be selected before today's date, which is not ideal. Additionally, if a date 5 days from now is chosen as th ...

What could be the reason behind Chrome abandoning this gradient configuration?

My Chrome browser isn't showing the gradient effect, but it seems to work perfectly fine in Safari and Firefox. Here's the code snippet I'm using: .accounts-form #auth-form { background: -moz-linear-gradient(#FEFEFE, #F4F4F4) repeat scr ...

The CSS background design is experiencing issues in IE9

Check out this code snippet I have for customizing the background of a webpage: body { font-family: "HelveticaNeue", Helvetica, Arial, sans-serif; font-size: 12px; background: -webkit-radial-gradient(50% 20%, circle farthest-side, transparent ...

Having trouble understanding how to create a continuous loop for a CSS animation within the KreatureMedia Layerslider

Currently, I am using KreatureMedias Layerslider and trying to make a fullwidth wave bob up and down. This is what I have at the moment... <img class="ls-layer" data-ls="durationin:1500;scalein:1.1;offsetxin:-50;loopcount:-1;loopoffsetx:0sw;loopoffset ...

How come padding-bottom isn't effective in increasing the position of an element?

My goal is to adjust the position of the orange plus sign (located in the lower right corner of the screen) so that it aligns better with the other icons, directly below them. I've tried using padding-bottom, but it doesn't seem to work, while pa ...

Difficulty Aligning Header Elements - Combining Navigation and Logo on the Same Line

I am currently facing an issue with positioning a logo and navigation links within a <header> element. When I apply the CSS property text-align:center;, the logo aligns to the center on one line while the navigation links appear on another line direc ...

Is it possible to customize the appearance of elements that are disabled using the [disabled] attribute in Angular 8?

I currently have a button on my website that triggers a form: <button (click)="create()" *ngIf="data.new" [disabled]="!dataForm.valid" color="primary" mat-raised-button> Go </button> Under specific conditions, this button disables all fiel ...

The functionality of the controls is not functioning properly when attempting to play a video after clicking on an image in HTML5

While working with two HTML5 videos, I encountered an issue with the play/pause functionality. Despite writing Javascript code to control this, clicking on one video's poster sometimes results in the other video playing instead. This inconsistency is ...

What is the best way to leverage the $().button('toggle') method in order to manipulate a bootstrap 'radio-button list'?

<div class="btn-group btn-group-toggle" data-toggle="buttons"> <label class="btn btn-secondary active"> <input type="radio" name="options" id="option1" autocomplete="off" checked> Active </label> <lab ...

Problem with special characters in Localstorage for Internet Explorer 8

After developing a jQuery script that retrieves data from a service, stores it in local storage, and displays it on the page upon a specific event, I encountered an issue with displaying Chinese characters only on IE8+ browser. Unfortunately, IE is unabl ...

When verifying internet connectivity, it automatically opens the default Chrome browser using phonegap

I've been working on developing a web app using phonegap and initially, the code below worked perfectly. <script type="text/javascript" charset="utf-8> document.addEventListener("deviceready", onDeviceReady, false); ...

Stop the window from scrolling downwards

My website has a full-width slider as the main background and I am using jQuery to show or hide divs depending on the clicked link. However, when some sections are longer than the initial view, my page automatically scrolls to the bottom after a click. How ...