Floating Elements Centralized

I have a relatively straightforward HTML layout with a heading div at the top, followed by a Main div that contains a NavBar on the left side and a ContentDiv on the right.

The issue I'm facing is that I can't seem to get my NavBar and ContentDiv to display in the center horizontally; they always align to the left (with the NavBar starting at x position zero).

Do you have any insight into why the NavBar and ContentDiv are positioned to the left instead of being centered?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/homepage.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>Kamalei - Home Page</title>
    <style type="text/css">
    <!--
        html, body, div, form, fieldset, legend, label, img {  margin: 0;  padding: 0;  }  table {  border-collapse: collapse;  border-spacing: 0; }  th, td {  text-align: left;  }  h1, h2, h3, h4, h5, h6, th, td, caption { font-weight:normal; }  img { border: 0; } 

        body { text-align: center; }

        #backgroundImg  { z-index: -1; position: absolute; left: 0px; top: 0px; }

        #heading        { height: 300px; }
        #main           { display: inline; }
        #navBar         { height: 700px; }
        #content        { height: 700px; }

        /* The above code displays the navbar above the content div but both
           elements are centred
        */

        /* The below code gets the navbar to be displayed to the left of the 
           content div which is what I want but now the 2 elements are not centred 
           they are on the left of the screen

           How do I get them centred?
        */

        #navBar         { float: left; height: 700px; }
        #content        { float: left; height: 700px; }
    -->
    </style>

</head>

<body>

    <div id="heading"> 
        abc
    </div>

    <div id="main">
        <div id="navBar">
            <!-- This is the background image for this element, I know, I know I should use CSS but it doesn't resize the images that way -->
            <img src="images/navBackground.png" alt="" width="200px" height="700px"/>
        </div>

        <div id="content">
            <!-- This is the background image for this element -->
            <img src="images/contentBackground.png" alt="" width="800px" height="700px"/>
        </div>
    </div>


    <!-- Must keep the background image at the bottom of body -->
    <img id="backgroundImg" src="images/background.png" alt="" width="javascript:getScreenSize()['width']+px" height="1000px"/>

</body>

</html>

Answer №1

Check out this solution:

#navBar         { display: inline; height: 700px; }
#content        { display: inline; height: 700px; }
#main           { margin: 0 auto; }

UPDATE:

I have made some changes to the code. Please review and see if it meets your requirements.

Answer №2

The two final css declarations indicate float: left, which is why they align to the left side.

Edit:

Ah, I understand now.... So you're aiming for the navigation to be on the left of the content, but both elements should still be centered overall?

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

Avoid making numerous http requests by making use of the fetch API

Currently, I am in the process of developing a Chrome extension that collects user input using prompts and sends it to the server via an HTTP request. However, I'm encountering a problem where the data is being duplicated, leading to multiple requests ...

Query the database with a dynamic filter criteria

In this scenario, I have created a row using an additional jQuery mechanism to fetch the parameters required for data search. $(document).ready(function() { DataProvide(); ManipulationHere(); $('table').delegate('input[type=tex ...

I aim to trigger a Redux action utilizing react-router-dom

In light of a route adjustment, I am in search of an improved method for invoking the redux action. One option is to invoke a redux action through the render function, as shown below: render(){ const filterName = this.props.match.params.product this.p ...

Trying my hand at using JQuery to dynamically update the image source of a draggable element upon dropping

Essentially, I have an object being dragged with the class .dragme, a dropzone with the class .dropzone1 for it to be dropped at, and an image that I want the .dragme object to become once it has been dropped. My current code looks like this: $(".dropzon ...

At times, Vue.js may encounter difficulties when attempting to load a component

Recently, a strange issue has been occurring in my production code. Although nothing has been changed, I am now receiving reports that occasionally a template fails to load causing the page to crash. I am currently using vue 2.16. The error messages being ...

Utilizing vue-router to create two separate navigation bars where only one link is highlighted as active

In my setup, I have implemented two sections with navigation structured as follows: <ul class="navigation-list"> <li v-for="(route, index) in navRoutes"> <router-link :to="route.path"> <span>{{ route.name }}</span> ...

Converting R functions into JavaScript using Emscripten

Attempting to convert R functions written in C to JavaScript using Emscripten is proving to be a challenging task. The primary objective at this stage is to migrate a function called pf. The source code for the function can be accessed here. As I navigate ...

Stopping the interval in Vue before the component is destroyed

I am currently facing an issue with a countdown timer implemented on a component. The timer functions properly, however, I want it to stop counting down once the user navigates away from the page where the component is located. I have attempted to use cl ...

Verify the attribute of the child element

In my child component, I have a property named files, which is an input type=file. This property allows me to determine if the user has selected a file for upload so that I can disable the submit button if no files are present in the input field. The issue ...

Is there a way to incorporate TypeScript type definitions into a JavaScript module without fully transitioning to TypeScript?

Although the title may be a bit confusing, it encapsulates my query in a succinct manner. So, here's what I'm aiming to achieve: I currently have an npm module written in JavaScript, not TypeScript. Some of the users of this module prefer using ...

Upgrading to Postgres 9.3 for Improved JSON Handling and Date Object Support

Currently working with PostgreSQL 9.3 and exploring options for utilizing TIMESTAMPTZ as authentic JavaScript date objects. Aware of JavaScript lacking a Date literal notation, I considered '{ "key" : new Date(datevalue) }'::JSON as a possibl ...

Error encountered in Next.js when defining columns for a MUI data grid

Currently, I am integrating MUI Data Grid into a Next.js app under development. While attempting to define a more intricate column structure, I encountered an error: { field: 'home_address', valueGetter: (value, row) => { retur ...

Sending calendar events through ajax response in jQuery FullCalendar can be accomplished by following these steps:

I'm currently using jQuery Fullcalendar in conjunction with Codeigniter. Initially, I have a table that displays schedule times and corresponding names. When I load the calendar page, I send data to fullcalendar.js directly, but I would prefer to rece ...

What is the method to retrieve an object by using a variable containing a string?

I am facing an issue with linking object names and anchor titles in my code. I want to store the title of an anchor element clicked by the user in a variable called sprite, which should then correspond to the name of an object. Here is the current version ...

Setting the default value of a multi-select dropdown in AngularJS from the controller

Currently, I have implemented AngularJS multi select drop down functionality on my website. The code for this can be found at the following source: MultiSelectDropDown In my HTML page, I have included the same drop down twice and I want to customize the ...

Using PHP and an HTML form to insert data into a MySQL database

I'm having an issue with inserting data into a MySQL database using PHP and an HTML form. After hitting submit, I receive a message stating that the item was inserted successfully. However, when I check phpMyAdmin, the table appears to be empty. The ...

AngularJS directive for Ionic Leaflet - Utilizing Service to switch tileLayer from side menu

I am currently experimenting with developing an ionic app for the leaflet-angularjs-directive. Unfortunately, there are not many demos available for me to test out. The specific demo I am using is called ionic-leafletjs-map-demo by calendee on GitHub whic ...

Sharing application state between different routes in React Router v4 can be achieved by using methods such

I'm seeking solutions to address the following challenge: Without utilizing any state management library, I solely rely on React. The primary application state is contained within the <MyFoodApp/> component, which includes an array of restaura ...

What is the best way to transfer the value of a radio button, retrieved from a database, to a textbox?

Greetings, I am trying to figure out how to pass the value of a radio button to a textbox using jQuery and PHP. The radio buttons are generated dynamically based on rows from my database, so I set the row ID as the ID for each radio button. However, the co ...

I desire to incorporate a subtle fading effect into the script

I have written the script code and now I am looking to add a fade effect to it. Can anyone guide me on how to achieve this? Your help is much appreciated! ※I used an online translator as English is not my native language. Please bear with any awkward ph ...