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

Mosaic-inspired image gallery with HTML and CSS styling

Can anyone help me create a couple of styled "blocks" in HTML and CSS similar to the design shown in the image? I've searched for templates but can't find anything that matches my vision, so any assistance would be appreciated. (links not require ...

Discrepancy in CSS rendering in Chrome and Firefox

When viewing in Chrome, the stats display properly within the gray box at the bottom left corner. However, when using Firefox, the stats appear positioned much lower below the box, with the bottom half of them hidden from view. Here is my CSS code being ...

Looking to make changes to the updateActivePagerLink setting in JQuery Cycle?

I'm in the process of developing a basic slideshow with 3 images, and I am relatively new to Jquery and Cycle. The slideshow is functioning properly, and the 3 pager links are also functional. The only remaining task for me is to implement "activeSli ...

Tips for Implementing CSS Styles on Ruby on Rails HTML Pages

Just starting out with RoR and trying to customize my form with CSS classes. Here's what I have so far: <%= form_tag :action => 'create' do %> <p><label for = "name">Name</label>: <%= text_field ...

Tips for choosing elements in JavaScript using querySelector even after they've been included in the code using innerHTML

Within the scenario below, a parent element is present in the HTML code and the span element with a class of 'child' is nested within the parent element using the createChild function. Subsequently, the content of the child element is modified el ...

Experiencing a problem with inline JavaScript onclick

I recently came across a fantastic pure javascript code for a smooth scrolling function, but I'm struggling with integrating it into an inline onclick function. My understanding of javascript is quite limited and I could really use some help... <d ...

Use ajax to dynamically update the contents of a textbox

As a newbie programmer, I recently created my own JavaScript update function for my program. However, the code is not updating as expected. Can someone please help me troubleshoot and get it working properly? What I want to achieve is that when I change t ...

Using a JSON file as a database for a project featuring HTML, CSS, and Vanilla JavaScript

Our task was to create a project that exclusively utilized JSON files for data storage. The data structure we were working with resembles the following: [ { "aircraftName": "Boeing 747", "departDate": 1640173020000, ...

Stop HTML audio playback upon clicking on a specific element

I'm looking to add background music to my website with a twist - a music video that pops up when the play button is clicked. But, I need help figuring out how to pause the background music once the user hits play. Play Button HTML - The play button t ...

Leveraging both Angular Material UI and Tailwind CSS simultaneously

Recently, I've been incorporating Material UI with Angular. With the deprecation of flexlayout and the rise of Tailwind as a recommended alternative, I'm wondering if it's feasible to utilize both Material UI and Tailwind in tandem. How can ...

What is the best method to remove the x and up/down arrow components of an input date field?

My main objective is to have the orange triangle display in the box, but I'm unsure if CSS or another method is needed to remove the two elements on the left side of the triangle. Is there a way to achieve this? Currently, I am using the input type d ...

How to efficiently retrieve a form's data from multiple forms with identical ids in JavaScript

I am facing a challenge with multiple forms on the html page that have the same ID. I need to send each form's information to the server based on user selection, but I haven't been able to find a relevant solution. For my Authorization polic ...

Unique CSS-created chronological schedule with alternating design

I am looking to customize my CSS timeline by removing the first line and changing the color of each individual circle. How can I achieve both of these modifications? I attempted to add a class called .not_complete to one of the timeline containers, but it ...

effortlessly eliminate the Google MyMap watermark from an iframe using ReactJS

I am new to ReactJS and I would like help on how to hide the watermark on a Google My Map. Can someone please assist me with this? <iframe src="https://www.google.com/maps/d/u/1/embed?mid=1OMSkPKZi-U-CnmBr71zByNxp8HYi-vOc&ehbc=2E312F" fram ...

What is the best way to manage DOM modifications in a responsive design layout?

Developing a responsive website with only one breakpoint can be challenging, especially when restructuring the DOM to accommodate different screen sizes. It's important to consider not just CSS and media queries, but also how the elements are arranged ...

JavaScript drag functionality is jerky on iPads, not seamless

I am currently attempting to implement a feature where I can drag a div (#drag) within its parent container (#container) using only pure JavaScript. This functionality is specifically required to work on iPad devices only. After writing a script that func ...

Ajax activates a slider

One element I have is a menu div, along with a content div Initially, when the slider (which is a jquery plugin) is placed in the index.php, everything appears to be working well, as shown in the image below However, my objective is to have each page&apo ...

Troubleshooting: Inline Styles not displaying Background Div Images in Next.Js

I am currently attempting to display images in a component by using a map and an external JS file to store the images as objects. I then loop through them to set each one as a background image for every created div. However, it seems like my current implem ...

What is a reliable method to retrieve the text from the current LI if all LI elements in the list share the

I'm encountering an issue with retrieving the text from LI elements because I have 10 list items and they all have the same class name. When I attempt to fetch the text using the class name or id, I only get the text of the last item. Here is my code ...

Create a right-to-left (RTL) CSS file within a create-react-app project and dynamically switch between them depending on changes

I am currently working on a multi-language project using create-react-app. My goal is to incorporate a library like "cssJanus" or "rtlcss" to transform the Sass generated CSS file into a separate file. This way, I can utilize the newly created file when sw ...