Setting the margin to auto causes the div to be shifted out of the

I'm facing an issue with my div element. The styling properties margin-left:auto and margin-right:auto are pushing another div out of the navbar.

How can I keep both div elements within the navbar? Here is my code:

body, html {
margin:0;
padding:0;
}

#navbar {
width:100%;
height:50px;
background-color:#000000;
}

#div1 {
width:300px;
height:50px;
margin-left:auto;
margin-right:auto;
background-color:#FF0000;

}

#div2 {
width:100px;
height:50px;
float:left;
background-color:#00FF00;

}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />


</head>

<body>

<div id="navbar">

<div id="div1"></div>

<div id="div2"></div>


</div>



</body>
</html>

check out this codepen example

I've been ignoring this problem for a while, but now I really need to find a solution. Any help would be greatly appreciated :)

Answer №1

To ensure proper display, make sure that div2 is placed before div1 within your HTML structure.

body, html {
margin:0;
padding:0;
}

#navbar {
width:100%;
height:50px;
background-color:#000000;
 
}

#div1 {
width:300px;
height:50px;
  text-align: center;
  margin-left:auto;
margin-right:auto;
  left: 50px;
background-color:#FF0000;

}

#div2 {
width:100px;
height:50px;
float:left;
background-color:#00FF00;
}
<body>

<div id="navbar">

 <div id="div2"></div>

 <div id="div1"></div>


</div>



</body>

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

Unable to show chat communication

Currently, I am in the process of creating a chatroom for practice. However, I have encountered an unusual issue with my code: (For reference, I am running this on WampServer) <?php header('Content-Type: application/x-www-form-urlencoded' ...

Struggling to make the AJAX script function properly

I have a table containing a list of transactions and I am attempting to update the table contents at specific intervals. The web page is hosted on a Linux Red Hat server, and currently, only the AJAX functionality is not functioning as expected. <!do ...

Generate a random post on a Blogger page upon loading instead of clicking on it

I recently implemented a random post button on my website at . I am now looking to have a random post load automatically when the page loads instead of requiring users to click the button. Can anyone provide guidance on how I can achieve this? Below is t ...

Selenium navigating an unordered list without returning the expected text

I'm currently in the process of developing a search bot for Craigslist using Selenium. I've managed to successfully iterate through the unordered list of search results, but unfortunately, I'm not able to extract the specific link text that ...

Learn how to prevent two-finger swipe forward/backward on a trackpad using JavaScript or HTML

My app includes a functionality where the URL changes when transitioning from one state to another, for example from home/#state to home/#state2. However, I noticed that when I perform a two-finger swipe using the trackpad from home/#state2, the page navig ...

Scrape additional information within HTML tags using Scrapy without relying on XPath

Currently, I am developing a scrapy spider and my goal is to allow the user to input an HTML tag such as <span class="someclass"></span> or <a style="somestuff"></a>. The objective is then to utilize these tags to extract the text i ...

What is the best way to access the width property within the style attribute of a span element with a specific class

https://i.sstatic.net/1YQdP.png Currently, I'm attempting to extract the content inside "width: 100%" and specifically the numerical part. My approach involves using beautifulsoup in Python, and you can see the snippet of my code below: rat ...

How to conceal the bottom bar in JQuery Colorbox iframe

I have implemented colorbox to showcase an Iframe of the registration page on my website. Here is how it looks... https://i.sstatic.net/z1RGK.png Upon closer inspection, I noticed a white bar at the bottom of the Iframe where the close 'X' butt ...

How to Manage Empty Lines in HTML Documents within WordPress

Just recently, I launched my brand new website. Everything seems to be functioning properly except for one issue that I discovered in the HTML source code. There are 15 blank lines before "<!doctype html>", which is causing some problems with SEO and ...

Steps for showcasing varied templates, such as error pages, within a single Angular X application

Is there a way in Angular 8 to switch templates within the same application? For instance, if the app consists of a container component with a header, sidebar, and footer, and the content is always displayed inside the container. What if I want to show a ...

What is the best way to connect two web workers in HTML5?

I've been delving into the web workers documentation, but I'm coming up short on finding an API that facilitates direct communication between two web workers. The scenario I have is needing to establish a direct line of communication from worker1 ...

Is it possible to adjust the width of Material-UI TextField to match the width of the input text?

Is there a way for Material-UI to adjust the width of the TextField element automatically based on the input text? When creating a form view/edit page and rendering data into fields, I also have parameters set by the server. It would be convenient to have ...

JS focusing on incorrect entity

Check out the fiddle link below to see the issue: https://jsfiddle.net/0a0yo575/10/ The goal is to display a single box in the top-left corner based on user interaction. However, the current JavaScript is causing the point to disappear instead of the box ...

What could be the reason behind my table appearing all crammed up in a single

My table is appearing in a single cell, and I can't figure out what's wrong. Can someone please take a look and let me know where I messed up? <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" cont ...

Python's answer to Ruby's LESS gem

I'm excited about the sleekness of the Ruby LESS gem. Currently, I am immersed in a Python/Pylons web project where this tool would be incredibly beneficial. CSS has its flaws, as noted by someone in a recent article we all read with great interest he ...

Is there a way to arrange list items to resemble a stack?

Typically, when floating HTML elements they flow from left to right and wrap to the next line if the container width is exceeded. I'm wondering if there's a way to make them float at the bottom instead. This means the elements would stack upward ...

Achieving alignment of header text and body text on the same line

I am dealing with a formatting issue where the first item in my paragraphs is not lining up properly. Here is an example of the problem: Header waspeen: 3304.07 ananas: 24 appel: 3962.0 Header waspeen: 3304.07 ananas: 30 appel: 3962.0 Does anyone k ...

scraping information using xpath from the subsequent page

I've been attempting to extract data from a webpage located at , focusing on fund number 26. While I have no trouble retrieving information from the first page (funds number 1-25), I'm facing difficulties with scraping anything from the second p ...

Why do I have to press the button twice for it to actually work?

I am new to programming and I want to create something that has 3 buttons to control the display of 3 hidden images respectively. When I click the first button, the image appears immediately, but I have to click it twice to show the second image. Can anyon ...

Tips for making a Bootstrap dropdown submenu appear as a 'dropup'

I'm using a Bootstrap dropdown menu with a submenu that should drop upwards while the rest of the menu drops down. How can I achieve this? Here is the code snippet: <div class="dropdown"> <a class="inputBarA dropdown-toggle" data-toggle= ...