Struggling to align a div at the center of a screen

Snippet of HTML Code:

<body>
<!-- Social Media Icons -->
<div id="sm1"><a href="/"><img src="images/facebook.png" height=40 width=40></img></a>
</div>
<div id="sm2"><a href="/"><img src="images/twitter.png" height=40 width=40></img><a></div>
    <!-- Logo -->
    <div id="logo"><img src="images/logo.png"</img></div>
    <div id="slogan"><blockquote>Insert slogan here...</blockquote></div>
    <!-- Slideshow -->
    <div id="slideshow"></div>
    <!-- Box -->
    <div id="box1" class="boxes"></div>
    <div id="box2" class="boxes"></div>
    <div id="box3" class="boxes"></div>
    <div id="box4" class="boxes"></div>
    <!-- Footer -->
    <div id="footer">
        <div id="footerimg">
        <center>
            <img src="images/facebook.png" height=20 width=20></img>
            <img src="images/twitter.png" height=20 width=20></img>
            </div>
            <div id="footertext">
            &copy; 2014 My Designs. All Rights Reserved.
        </div>
    </div>

CSS Styling:


body {
position:absolute;
margin-left:100px;
}
blockquote {
font-style:italic;
}
#logo { 
position:absolute;
width:150px;
height:40px;
margin-top:20px;
}
#sm1 {
position:absolute;
width:40px;
height:40px;
margin-top:20px;
margin-left:650px;
}
#sm2 {
position:absolute;
width:40px;
height:40px;
margin-top:20px;
margin-left:710px;
}
#slideshow {
width:750px;
height:400px;
background-color:purple;
margin-top:80px;
}
#box1 {
background-color:aqua;
}
#box2 {
margin-left:190px;
background-color:fuchsia;
}
#box3 {
margin-left:380px;
background-color:gray;
}
#box4 {
margin-left:570px;
background-color:green;
}
#footer {
width:750px;
height:50px;
background-color:olive;
position:absolute;
margin-top:140px;
text-align:center;
font-family:sans-serif;
}
#slogan {
position:absolute;
margin-left:120px;
margin-top:15px;
}
#footerimg {
margin-top:5px;
}
#footertext {
color:white;
}
.boxes {
margin-top:10px;
position:absolute;
width:180px;
height:120px;
}

I need assistance in centering the content within <body></body> and <footer></footer>. I have tried various solutions without success. Experts, please help optimize my code layout for better appearance.

Thank you! Visit the JSFiddle link here.

Answer №1

You must make several adjustments.

Here is the latest version of the code.

Eliminate the use of position: absolute for elements with classes .footer, .boxes, and body.

Also, remove the left margin for all instances of #boxes.

Lastly, incorporate the use of .wrapper to centralize the content.

Apply margin: 0 auto to the .footer.

Answer №2

If you want to align the elements in the middle of a page, one way to do it is by enclosing all the content within a div right after the body tag. This div is commonly referred to as a container div (you can give it any id, but it's usually named #container). Then, you can add the following style to the container div:

#container{
   margin:0 auto;
   width:    ; // Adjust the width accordingly until the layout appears centered
}

Answer №3

Kindly adjust the width to be 750px;

 #footer {

        width:750px;
        height:50px;
        background-color:olive;
        position:absolute;
        margin-top:140px;
        text-align:center;
        font-family:sans-serif;
    }

This solution worked for me; I hope it aligns with your requirements.

Update: please refer to this http://jsfiddle.net/sheralam/b2mzrgxo/ , if this matches your desired outcome.

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

What is the significance of using flexGrow in the parent div of a Material UI grid?

I am currently working on understanding a code example located at https://codesandbox.io/s/9rvlm which originates from the Material UI documentation (https://material-ui.com/components/grid/): import React from 'react'; import PropTypes from &ap ...

What is the best way to create a fixed positioning for a div within a Material-UI table container?

When using the Material UI Table, I encountered an issue with applying the sticky property. The table itself works fine, but I also have a button inside the TableContainer that should be sticky alongside the table head. I attempted to achieve this by using ...

Organize three image links side by side using HTML layout

Hello there! I'm currently working on a website project and I'm struggling with arranging some image links in a specific layout. The grey rectangles you see represent where the images should be placed, all of them are uniform in size (275 x 186) ...

How can I use the store command to retrieve the href value of a link using a css selector in Selenium?

Is there a way to store a URL from a link using CSS instead of xpath? store //tr[td[contains(.,'6 Day')]][1]/td[8]/a@href my_var open $my_var If so, how can I achieve this goal with css? I managed to use the following locator: store css= ...

Using JQuery to implement custom validation on a text field is an effective way

How can I validate a text field using Regex and create my own validation rule for starting with "com."? Can you provide me with an example of how to do this? I want the text field to only accept values that start with com. <input id="appPackage" name=" ...

What is the best way to declare media queries for resolutions both above and below 1366?

Is it possible to define different CSS files based on screen resolution in HTML? For example, one file for screens with width of 1366px or lower and another for higher resolutions. Could you kindly provide me with the correct HTML code for this? ...

Ensure that dynamically generated fields are verified whenever one of them is modified

I am working on a program that generates text boxes and drop down lists dynamically. I need to find a way to validate these fields only when one of them is changed. The validation should occur if a date is entered in the text box, it should then check if t ...

Automatic Textbox Closer

Connected to this AngularJS issue on IE10+ where textarea with placeholder causes "Invalid argument." and also this AngularJS 1.1.5 problem in Internet Explorer with ng-show and objects (Bug), we had to resort to using the textarea element as a self-closin ...

A sleek CSS text link for a stylish video carousel

I am attempting to create a CSS-only text link to video slider within our Umbraco CMS. Due to the limitations of TinyMCE WYSIWYG, I am restricted in the amount of code I can use as it will strip out most of it. So far, I have developed a basic CSS slider ...

Guide on adding HTML to a specific div element in jQuery by utilizing the keyword "(this)"

I have been working on a script for my PHP page in Wordpress to add titles to images in my photo gallery that open in a lightbox. Here's what I developed: $(".responsive1").bind("click", function() { $(this).("<div class='imgTitle ...

When using selenium with python, the function excecute_script('return variable') may not technically return variables, even though the variable does exist

My attempt to retrieve a variable from javascript code using selenium is encountering difficulties. Despite the presence of the variable (confirmed by inspecting the source code before executing the script), the command driver.execute_script('return v ...

How can I extract a substring from a URL and then save it to the clipboard?

Hi there! I'm working on a project for my school and could really use your expertise. I need help extracting a substring from a URL, like this one: URL = https://www.example.com/blah/blah&code=12432 The substring is: 12432 I also want to display ...

The exact measurement of width is not specified in the custom element that extends HTMLCanvasElement

I have created a custom element that extends a canvas, but when I try to add it to the DOM, the width appears to be undefined. Here is my code snippet. class Renderer extends HTMLCanvasElement { constructor() { super(); } } customElements.def ...

Exploring the process of implementing tab-link ripple effects in Angular Material

Is it possible to apply the ripple effect to tabs in Angular elements using a similar attribute to md-ink-ripple, like we can with grid or title elements? For example, check out https://material.angularjs.org/latest/demo/tabs If there isn't a specif ...

What is the best way to ensure input text fills the entire grid?

Take a look at this code snippet: <div class="form-group col-md-6"> <label class="col-md-4">Last Updated (Real Time)</label> <input class="form-control col-md-8" type="text" ng-model="status.lastUpdated" ng-readonly="true"/ ...

What methods are available for generating a short-lived banner using JavaScript?

I recently entered this industry and am in the process of constructing a website. Utilizing HTML and CSS, I crafted a cookie notification banner. I am seeking guidance on how to ensure that it only appears once "every 24 hours for each user." I attempted ...

Having trouble with setting image source using Jquery?

The functionality of my razor in setting an image source is functioning properly. However, when I use my jQuery method to retrieve data, it returns a garbled URL: ���� Refreshing the page does not affect the HTML code, as it continues to work as e ...

Is there a way to set the same href link for the active class in the navbar using bootstrap-vue?

I am currently utilizing this navbar. It works fine when the href links are unique, but I encounter an issue when two pages share the same link - the active class is applied to both list items with that link in the manner I have written. Vue.component(& ...

I am having trouble with the functionality of my bootstrap navbar toggler

Can you please review the code and help me identify the issue? The toggle button is not functioning properly, it should display the menu bar when clicked. <body> <!--navigation bar--> <nav class="navbar navbar-expand-sm navbar-dark bg-dan ...

Retrieve the webpage content (including any iframes) using a Firefox plugin

Hello there! I am currently working on retrieving data from a webpage that is updated using Javascript. Initially, I attempted to create a Java program to periodically fetch this page from the server, but the information was being updated too slowly. The ...