Tips on aligning two divs horizontally on the same line

doc.html

.column {
  background-color: orange;
  width: 75%;
  vertical-align: top;
  display: inline-block;
  height: 200px;
}
.nav {
  vertical-align: top;
  display: inline-block;
  width: 25%;
  background-color: lightgreen;
  height: 200px;
}
* {
  box-sizing: border-box;
  padding: 0px;
  margin: 0px;
}
<!DOCTYPE html>
<html>

<head>
  <link href="css2.css" rel="stylesheet" type="text/css">
</head>

<body>
  <div class="container elem">
    <div class="nav"></div>
    <div class="elem column"></div>
  </div>
</body>

</html>

I made doc.html and css2.css based on the information from this reference learnlayout. However, the page appears differently than expected. How can I align those two sections in one line? https://i.sstatic.net/CYkEE.png

Answer №1

The CSS you've written is accurate; however, there is a common problem with whitespace that you need to address. It's important to ensure that there are no spaces between the tags in your code:

<body>
    <div class="container elem"><div class="nav"></div><div class="elem column"></div></div>
</body>

This issue arises because your content is inline, causing the whitespace between .nav and .elem to create a small gap (approximately 4px) that separates your <div> elements and disrupts the layout.

By placing the closing bracket immediately after the opening bracket of the next element, any whitespace in between becomes part of the tag's internal spacing and does not affect the content (since tags can accommodate whitespace between attributes and tag names without any issues).

Answer №2

The issue of whitespace with inline-block elements is a common one. One way to address it is by setting font-size: 0; on the parent element.

.container.elem {
  font-size: 0;
}
/* Make sure to reset the font-size for child elements */

.column {
  background-color: orange;
  width: 75%;
  vertical-align: top;
  display: inline-block;
  height: 200px;
}
.nav {
  vertical-align: top;
  display: inline-block;
  width: 25%;
  background-color: lightgreen;
  height: 200px;
}
* {
  box-sizing: border-box;
  padding: 0px;
  margin: 0px;
}
<div class="container elem">
  <div class="nav"></div>
  <div class="elem column"></div>
</div>

Another option is to float both divs to the left, but this approach comes with its own challenges and intricacies, so I recommend sticking with inline-blocks.

Answer №3

The problem lies in the whitespace. To resolve it, add the following CSS rule to the container:

.container{ font-size:0; }

Answer №4

The process involves condensing them into a single line, unless the width of the parent element is specified and the total width of the combined elements exceeds that of the parent.

.container.elem div{
    float:left;
}

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

Customizing colors for the progress bar in Angular Material

In my Angular 5 project, I am using a material progress bar and hoping to customize the colors based on the percentage progress. Despite trying various methods from other sources (including previous SO questions), I have been unsuccessful in getting it to ...

Bootstrap4: What could be causing the adjacent row cell to remain left-justified instead of being centered as intended with mx-auto?

I've been working on creating a responsive header using Bootstrap4. The main goal is to have the logo displayed as left-justified text and the menu items as right-justified when the viewport is large, then transition to both being centered in a "small ...

Regular expression that can be used to extract information from a text file by filtering and returning only

When I open a text file, it contains several lines like the examples below. surname australia enter name j.jonhs enter name j.cause enter name f.london enter I am seeking assistance to modify the code snippet below so that it captures the first line m ...

Top method for centering a flexible SVG vertically once the page width becomes too narrow

Currently, I have two SVG images displayed side by side on a webpage. One SVG needs to maintain a fixed size while the other should scale as needed, and I have achieved this functionality. However, I am facing an issue where I want the two SVGs to align v ...

Customizing Navigation Color on Laravel Websites for Staging and Live Servers

I am seeking a solution to change the color of the Laravel navigation bar depending on whether it is the live or staging server. Our current setup involves testing code on a staging server before pushing it to the live server in production via Bitbucket. ...

Issue with multiple dropdown menus not closing when clicked on

The current implementation provides the functionality to convert select boxes into list items for styling purposes. However, a drawback of the current setup is that once a dropdown is opened, it can only be closed by clicking on the document or another dr ...

Styling elements with Css Flex in a single row

Hi there, I'm currently working on a project and I'm facing an issue with aligning all the items in the same row. I've attempted to use flexbox but the items end up overlapping each other and I'm unsure of how to resolve this issue. To ...

Exploring the tab-content features of Bootsrap 5 through anchor tags

In the previous version of Bootstrap, the tab-pane used to fade in active. However, in the new version, it is now: tab-pane fade show active. What other improvements can I make? <!DOCTYPE html> <html lang="en"> <head> <m ...

Issue encountered when attempting to invoke a PHP function using Javascript through AJAX

I'm currently working on incorporating a PHP function into my HTML file using Ajax (which also contains JavaScript). My goal is to retrieve all locally stored JSON files. I've been following a guide at , but I'm facing some challenges. When ...

The clickable areas for the href and onclick functions are extremely small and positioned inaccurately on the screen

The code below is supposed to load an image of a 'close window' button that should be clickable. However, when the page loads, the clickable area is only a couple of pixels wide and a pixel or two high, positioned just below or above the center o ...

Exploring the concept of nested arrays in Angular 2 using Typescript

I'm exploring the possibility of defining an array in Angular 2 that contains multiple other arrays within it. To clarify, here's what I had initially: export class PaymentDetails { account: any[]; bpNumber: number; } The issue with t ...

What is causing my radio button event to activate when a separate radio button is selected?

I have implemented the code below to display "pers" and hide "bus," and vice versa. JQuery: $('input[name="perorbus"]').click(function() { if ($(this).attr('id') == 'bus') { $('#showbus&apo ...

The layout is being disrupted by a bug in the nested list div in IE 7

I am currently in the process of creating a website with nested lists in the sidebar. The parent list contains children li elements. Initially, only 4 list items are displayed, and the rest should be hidden with an option to "Show All" above them. Here is ...

How can I implement a dynamic sidebar drawer in Bootstrap for my navbar?

Is it possible to achieve a layout similar to this example: I've come across some online examples using different versions of bootstrap, but they tend to alter the css too much, making it harder to grasp the concepts of bootstrap. I am curious if it ...

Leveraging personalized data-* attributes within the <template> tag in HTML5

I am currently developing a menu using a dom-repeat template as shown below: <template is="dom-repeat" items="{{appletsMenu}}"> <a data-route="{{item.dataRoute}}" href="{{item.href}}"> <iron-icon icon=" ...

Achieving perfect alignment of two elements using flexbox: centering one and aligning the other to the right

Can you help me with aligning two elements to the center and right edge using flex? I am trying to achieve a layout similar to the image. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX https://i.sstatic.net/cvmHX.png CSS Styles .flex{ display: flex; justify- ...

Tips for updating the background color of a specific row

I have a piece of code that I am trying to modify with a specific condition. If {row.BB} is less than or equal to 100, I want to change the background color of the row with this value to red. Can someone help me achieve this? The code is linked to a data ...

When utilizing a Slick carousel with three slides and setting autoPlay to true, the slider-nav behaves as if there are five slides

I am currently using the Slick carousel plugin and I want to feature a display of 3 photos. The issue is that when I set slidesToShow=2, everything works perfectly fine, but when I try to set slidesToShow=3, which equals the total number of slides, the bot ...

Modify the text tone within a specific cell

Welcome to my unique webpage where I have implemented a special feature. The text highlighted in red represents the unique identifier for each individual cell. Interestingly, below there is an input field containing the code "0099CC", which corresponds to ...

Implementing dynamic display of div based on dropdown selection in typescript

A solution is needed to display or hide specific div elements based on a dropdown selection using Typescript. Sample HTML file: <select class="browser-default custom-select"> <option selected>single</option> <option value="1"> ...