An effective method to arrange divs in a line adjacent to each other

Apologies for the repetitive question, but I am in need of some answers.

In my opinion: Having 1 div with a width of 100% and 2 divs inside each with a width of 50%. Why does it not fit properly?

HTML:

<div id="top-menu">
   <div id="logo"></div>            
   <div id="menu-top"></div>
</div>

CSS:

#top-menu{
    width: 100%;
    height: 100px;
    position: relative;
    border: 1px solid black;
    background-color: #A3238E;
}

#logo{
    width: 50%;
    height: 100px;
    position: relative;
    display: inline-block;
    background: orange;
}

#menu-top{
    width: 50%;
    height: 100px;
    position: relative;
    display: inline-block;
    background: green;
    left: 0;
}

I notice a small blank space between the two divs, and I am unsure how to remove it... Also, setting the second one to Width: 49% seems to work... But it doesn't with 50%, presumably due to that little space between them.

Any solutions on making it function as intended?

Answer №1

To achieve the desired layout, you should utilize the following CSS:

#top-menu{
    width: 100%;
    height: 100px;
    position: relative;
    border: 1px solid black;
    background-color: #A3238E;
}

#logo{
    width: 50%;
    float:left;
    height: 100px;
    position: relative;
    display: inline-block;
    background: orange;
}

#menu-top{
    width: 49%;
    float:left;
    height: 100px;
    position: relative;
    display: inline-block;
    background: green;
    left: 0;
}

I made sure to add float:left; to the two divs that need to be placed next to each other. In your HTML code, apply the following structure:

<div id="top-menu">
   <div id="logo"></div>            
   <div id="menu-top"></div>
   <div style="clear:both;"></div>
</div>

Additionally, observe the inclusion of

<div style="clear:both;"></div>
which effectively clears the float:left;. Alternatively, you can also use
<div style="clear:left;"></div>
for the same purpose if preferred.

Answer №3

The issue is arising due to the utilization of inline-block: http://css-tricks.com/fighting-the-space-between-inline-block-elements/

To resolve this, replace display: inline-block; with float: left;

After making this change, you can also include overflow: hidden in the #top-menu to clear the floats.

UPDATE:

In order to successfully apply the overflow: hidden clearfix technique, it is advisable to eliminate height: 100px from #top-menu.

Answer №4

#top-menu{
    width: 100%;
    height: 100px;
    position: relative;
    border: 1px solid black;
    background-color: #A3238E;
    display: table;
}

#logo{
    width: 50%;
    height: 100px;
    position: relative;
    display: table-cell;
    background: orange;
}

#menu-top{
    width: 50%;
    height: 100px;
    position: relative;
    display: table-cell;
    background: green;
}

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

Display a new view upon clicking a button using AngularJS in a single-page web application

I am completely new to AngularJS and currently working on my first project. I apologize in advance if my question seems very basic. In my project, I have created a page using simple HTML with three buttons. When these buttons are clicked, I want to load v ...

Creating a Jquery method to handle multi-line ellipsis for long text

Check out this fiddle I made: http://jsfiddle.net/mupersan82/JYuSY/ This snippet handles multiline ellipsis: $(function() { var ellipsisText = $('.multilineEllipseText'); var textContainer = $('.incidentCellBottomRight').height(); whi ...

Can Selenium be used to validate resizing capabilities?

Looking to automate a test case focused on resizing a div element while ensuring that all content in the div remains unchanged, except for its size. Can Selenium help with this task? ...

JavaScript Slider for Color Selection

In my slider, I have added a .images class along with buttons for previous and next. To set the colors, I have used JavaScript to define an array like this: let colors = ['red', 'green',]; Currently, clicking the next-button displays ...

Simplifying the process of implementing Jquery CSS Toggles

I am planning to simplify the process of toggling visibility for 12 different divs when clicking on 12 different buttons. Instead of repeating the same code multiple times, I am considering a more efficient approach. My idea is to: Step A) Initially hide ...

Challenges with displaying css/bootstrap classes within angular2

Experiencing difficulties with CSS/Bootstrap integration when displayed in an angular2 component. When attempting to display the CSS and HTML content in the Index.html file (with proper CSS and JS references), everything functions correctly. However, once ...

Adjust the size and position of the text input field

I'm struggling to find a way to both resize and move the box without one action interfering with the other. Whenever I attempt to change the size of the box, it triggers the move event at the same time. Clicking on the box should make it resizable, bu ...

Transfer the value of a JavaScript variable to paste it into a fresh tab on Google Chrome

I have come across some examples where users can copy HTML text to the clipboard. However, I am working on something more dynamic. Here's what I'm trying to achieve: <button id="" ng-click="outputFolder()">Output Folder</button> $sc ...

Ensure that col-6 expands to fill the entire row in the event that the other col-6 is hidden on a

I'm currently working on a project that involves two columns. <div class="col-6"> <p> Here is some text for the paragraph. </p> </div> <div class="col-6 d-none d-sm-block"> <img class="goal-f1 rounded" src=" ...

Setting button height dynamically in React Native

I've implemented a button using React Native Elements in my layout. Here's the code snippet: <Button title="Login" buttonStyle={{ backgroundColor: Colour.green }} containerStyle={{ ...

Error: Code cannot be executed because the variable "sel" has not been defined in the HTML element

Every time I try to click on the div, I encounter an error message stating 'Uncaught ReferenceError: sel is not defined at HTMLDivElement.onclick' I am currently developing with Angular 8 and this error keeps popping up. I have read through simil ...

How to retrieve the value of an observable from a regular JavaScript array in Knockout JS?

Context In my project, I am working with a plain JavaScript array that starts off empty but gets populated with Knockout observables later on. These values are numbers and I need to compare them with values in another Knockout observable array. The issue ...

What is the best way to incorporate several php files into a WordPress post?

I have developed a system that retrieves information from a database and presents it in multiple text files. Now, I am looking to move this system to a page on a WordPress website. I have already created a custom page named page-slug.php and added the ne ...

Choose either nth-child or nth-of-type within a nested element

Here is the HTML code snippet: <div class="homepage-body"> <a href="#"> <div class="homepage-item"> <div class="homepage-icon"></div> <div class="homepage-text"> Test1 </di ...

What is the best way to delete the onclick event of an anchor element?

Struggling to remove the onclick attribute using jQuery? Here's my code: function getBusinesses(page){ if(page==0){ alert("you are already on First Page"); $("#previous a").removeAttr("onclick ...

Is it possible for the relative path of a gif image to become invalid when added to a div?

Question Context: In the view, I have a form that will show a loading 'spinner' gif when a user submits it. The Problem: If I place the spinner img element within its container div, the spinner is always displayed, which is not desired: https ...

Removing background color and opacity with JavaScript

Is there a way to eliminate the background-color and opacity attributes using JavaScript exclusively (without relying on jQuery)? I attempted the following: document.getElementById('darkOverlay').style.removeProperty("background-color"); docume ...

JavaScript struggles when dealing with dynamically loaded tables

I am currently working on integrating the javascript widget from addtocalendar.com into my website. The code example provided by them is displayed below. Everything functions as expected when I place it on a regular page. However, I am facing an issue wher ...

What are some techniques for highlighting the significance of specific items within an unorganized list?

My goal is to utilize semantic markup to highlight the importance or priority of each item. Let me illustrate what I mean with an example. <ul itemscope itemtype="http://schema.org/ItemList"> <h2 itemprop="name">Wish List</h2><br&g ...

Developing an if-else statement to showcase a different div depending on the URL

Having trouble with an if/else statement to display one div or another based on the URL: No matter what I try, only "Div 1" keeps showing. Here's my code snippet: <script> if (window.location.href.indexOf("pink") > -1) { document.getElemen ...