What is the best way to position the main div in the center when it contains other div classes within it?

I've been working on adding background images to the buttons on my homepage. However, I'm facing an issue where everything aligns to the left after adding additional divs inside the main "myclass" div. What I really want is for everything to be centered. Is there a way to achieve this?

<div class="myclass">
        <a href="link"><div id="Home"></div></a>
        <a href="link"><div id="About"></div></a>
        <a href="link"><div id="FAQ"></div></a>
        <a href="link"><div id="Contact"></div></a>
</div>

.myclas {
    background:#F5CA0D;
    max-width:100%;
    display: block;
text-align: center;
}

#Home {
  margin:0 auto;
  width:211px;
  height:109px;
  transition: background-image 1s ease-in-out;
  background-image:url("images/Home.jpg");
    float:left;
  clear:right;
}

#Home:hover {
  background-image:url("images/Home-hover.jpg");

}

#About {
  margin:0 auto;
  width:211px;
  height:109px;
  transition: background-image 1s ease-in-out;
  background-image:url("images/About.jpg");
    float:left;
  clear:right;
}

#About:hover {
  background-image:url("images/Oahu-hover.jpg");

}

#FAQ {
  margin:0 auto;
  width:211px;
  height:109px;
  transition: background-image 1s ease-in-out;
  background-image:url("images/FAQ.jpg");
    float:left;
  clear:right;
}

#FAQ:hover {
  background-image:url("images/FAQ-hover.jpg");

}

#Contact {
  margin:0 auto;
  width:211px;
  height:109px;
  transition: background-image 1s ease-in-out;
  background-image:url("images/Contact.jpg");
    float:left;
  clear:right;
}

Answer №1

Revise the typo in .myclass first, then proceed with the following steps

JSFiddle Link

CSS Styling

.myclass {
    background:#F5CA0D;
    max-width:100%;
    display: block;
    text-align: center;
    width: 100%;
}

.myclass > a, div {
    display: inline-block;

}

#Home {
  margin:0 auto;
  width:211px;
  height:109px;
  transition: background-image 1s ease-in-out;
  background-image:url("http://maps.google.com/mapfiles/kml/pal3/icon55.png");
}

#Home:hover {
  background-image:url("http://maps.google.com/mapfiles/kml/pal3/icon55.png");

}

#About {
  margin:0 auto;
  width:211px;
  height:109px;
  transition: background-image 1s ease-in-out;
  background-image:url("http://maps.google.com/mapfiles/kml/pal3/icon55.png");

}

#About:hover {
  background-image:url("http://maps.google.com/mapfiles/kml/pal3/icon55.png");

}

#FAQ {
  margin:0 auto;
  width:211px;
  height:109px;
  transition: background-image 1s ease-in-out;
  background-image:url("http://maps.google.com/mapfiles/kml/pal3/icon55.png");

}

#FAQ:hover {
  background-image:url("http://maps.google.com/mapfiles/kml/pal3/icon55.png");

}

#Contact {
  margin:0 auto;
  width:211px;
  height:109px;
  transition: background-image 1s ease-in-out;
  background-image:url("http://maps.google.com/mapfiles/kml/pal3/icon55.png");

}

HTML Structure

<div class="myclass">
        <a href="link"><div id="Home"></div>4</a>
        <a href="link"><div id="About"></div>3</a>
        <a href="link"><div id="FAQ"></div>2</a>
        <a href="link"><div id="Contact"></div>1</a>
</div>

Answer №2

Consider removing the float: left rule that is applied to every element as it may not be necessary for your layout. You can also evaluate if the clear property is needed in your design.

Answer №3

Remove all instances of float:left and unnecessary clear:right. Simplify the script for faster browser loading speed.

Answer №4

First of all, the class name you are using in your HTML is incorrect (missing 's' in myclas).

In addition, you cannot center the main div when using 100% width. Try setting a fixed width and then use margin: 0 auto; This should solve the issue.

Answer №5

Your CSS contains a typo with the class name .myclas {

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 method for calculating the 100% width of a flex child?

Adjusting the width of the .sidebar to 100% seems to make it smaller in size. Interestingly, removing the body's font-size: 1.3rem and toggling the .sidebar's width: 100% results in a slightly larger appearance. It is expected that setting the ...

Eliminating white space - A CSS and HTML page with no room for gaps

After finally getting my website up and running using style.css, I am faced with the following CSS code: html { height:100%; } { position: relative; z-index: 0; width: 100%; left: 0; top: 0; cursor:default; overflow:visible; } body { ...

Tips for personalizing the sidebar on your WordPress site and incorporating a collapsible menu feature

Is there a way to customize the sidebar in WordPress so that all the categories and their children are easily accessible? For example, on https://www.w3schools.com, clicking on the header category "HTML" displays all related links in the left sidebar. I&a ...

Include a name in the set_values function using Rvest

I'm currently working on scraping data from this website. I have a code written in rvest for login, but each time the page refreshes, the form name changes. library(rvest) loginpage <- "https://demo.glpi-project.org/" pagesession <- html_sess ...

Is there a way to make the text scroll up automatically when it overflows?

I have a straightforward div element that occupies the header's height but currently has a fixed height of 400px for testing purposes. Here is how it currently appears: https://i.sstatic.net/gg6kQ.png I am utilizing the type-it library to dynamical ...

What's the best way to include a div on the right side of my adaptable divs?

I need the "div 1" label to always appear on the right side of the divs, while still maintaining responsiveness. Is there a way to achieve this effectively? Struggling to find a wrapper that meets my responsiveness requirements. #wrapper { width: aut ...

What causes a custom element to suddenly crumble?

I am attempting to create a custom field that can display either an SVG or a Canvas, but the rendering is not as expected. I anticipate two boxes that are 400 pixels wide and 300 pixels high, however, they appear to collapse in an unusual manner. How can I ...

Having Problems with Facebook's "og:image" not refreshing?

I'm having trouble updating the image on my website, www.lux-boutique.co.uk. Despite my efforts, the image displayed on Facebook remains unchanged. Any suggestions? Even after using the Facebook debugger tool, the old image persists. I've delet ...

Fade-in with jQuery Javascript when clicked and fade-out once loading is complete

$('.showloader').click(function () { $('.loader').fadeIn(); }); I have a loading css called .loader It is default Display:none I want the .loader to be displayed when the .showloader is clicked. This will submit a registrati ...

I am encountering an issue with opening a dropdown select option upon clicking in C# while using Selenium

I am encountering an issue while attempting to open the Register page from my account. The UI developer utilized Bootstrap code, and Bootstrap developers have included a JS function on click. However, when I execute this code, an error is thrown: "OpenQA.S ...

Do my media queries accurately reflect the design?

I am new to web design and currently taking a course. As part of my assignment, I need to make my website responsive. However, I am having trouble with my media queries. Despite hours of research and tweaking, some elements are not responding as expected. ...

Use jQuery to save a list of URLs in an array, then trigger the opening of each URL when a button is

Explanation of my approach, described in two different ways to assist understanding First Approach Gather all elements structured like this: <a href="etc">url1</a> within the <div class=".resources"></div> and place them in an a ...

What causes these images to stack on top of one another rather than align in rows? [HTML][CSS][Bootstrap]

I've implemented Bootstrap's grid system to showcase two rows, each containing two images. Here is the code: <section class="container"> <div class="row"> <figure class="column-sm-6"> <p>floor pl ...

Text overflowing from image on Bootstrap 4 card with image overlay

Having an issue with the text overlay on my image, especially on mobile (small/xs) screens where the play button and title are extending beyond the image due to long paragraph text. Which bootstrap class should I adjust to fix this issue? I was expecting t ...

Step-by-step guide to implementing a floating action button on the bottom-right corner of a screen using Material-UI in a React application

I have been attempting to place the FloatingActionButton on the bottom right corner of the screen. To achieve this, I am utilizing the following library: http://www.material-ui.com/#/components/floating-action-button import React, { Component } from "reac ...

Issues with responsiveness of images on Squarespace website

Check out my friend's website that was created using Squarespace: www.diamondathome.com. The site is designed to be mobile responsive, but there seems to be an issue with the Facebook and Linkedin icons at the top of the homepage. They appear too lar ...

Support for the percent sign in right-to-left languages

Imagine you have the following code snippet: <div dir="rtl"> test 50% </div> It currently displays as test 50%. However, according to this wiki page, for Arabic language it should be shown as %50 test, with the percentage sign before the n ...

A guide to implementing lazy loading using BXSlider

I have implemented an image gallery using Bxslider, but I am facing an issue with the loading time as there are more than 15 images in each slide. To address this problem, I want to load images one by one when a particular slide appears. I came across an e ...

Leverage the navigator clipboard feature to save an image in PNG format from a canvas

I've been attempting to save a canvas blob to the navigator clipboard graphicDiv.addEventListener( 'click', (event) => { const canvas = <HTMLCanvasElement> document.getElementById('canvas'); can ...

What's the best way to place my image inside a Bootstrap Accordion so that it is housed within the accordion-item?

I've been facing an issue with a Bootstrap Accordion. Everything works fine, except when I try to insert an image <img src="https://placehold.co/600x400" class="img-fluid" /> into an accordion-item <div class="accord ...