Challenges with Aligning Divs

After a decade-long break from web design, I am easing my way back into the world of div properties (which have become much more popular since my last stint). Currently, I am working on creating a layout like this:

Logo | Icons Div Banner Div Link Div Content Div Footer Div

My current challenge lies in centering the Link Div, which contains:

<div class="menu">
    <a class="link1" href="#">Home</a>
    <a class="link2" href="#">About Us</a>
    <a class="link3" href="#">Services</a>
    <a class="link4" href="#">Portfolio</a>
    <a class="link5" href="#">Contant Us</a>
</div>

The CSS I am currently using:

.menu   
    {
        TO BE USED
    }

.menu a
    {
        display: block;
        float: left;
        line-height: 144px;
        margin: 5px;
        opacity: 0.3;
        text-align: center;
        width: 144px;
    }

.link1
    {
        background: #fdd22a;
    }

.link2
    {
        background: #009fe3;
    }

.link3
    {
        background: #574696;
    }
.link4
    {   
        background: #ee7202;
    }
.link5
    {
        background: #e61c67;
    }
.link6
    {
        background: #96c11f;
    }

You can see a sample of my work here: .

Currently, with not enough links or knowledge yet to align them perfectly within the margins (lining up with the logo and social media icons), I think the next best step for me is to focus on centering the link divs. The code I linked may be a bit messy as I haven't had the chance to tidy things up just yet.

Any suggestions or feedback would be greatly appreciated.

Answer №1

To center the links, remove the float:left property from both .menu and .menu a, and instead add the following:

margin: 0 auto;

Apply this style only to the .menu class.

Next, change the display property of .menu a from block to inline-block.

Make sure to set a width for the .menu class. For example, a width of 61% may work well based on experimentation.

Alternatively,

Add the following:

text-align: center;

To the .menu class.

Further, switch the display property of .menu a to inline-block.

No need to specify a width for .menu when using this method. It could be the simplest solution to achieve centered links.

Answer №2

Referenced from CSS Layout Learning:

.header {
    width: 800px;
    margin: 0 auto;
}

By defining the width of a block-level element, you can control its expansion to the left and right edges of its container. Then, using auto for the left and right margins will center the element horizontally within the container. The element will occupy the designated width, with any remaining space evenly distributed between the two margins.

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

Display the vertical scroll bar of the page when the mouse hovers over it

Struggling to reveal the main page scrollbar upon hover? While working on my portfolio, I hid the page scrollbar. Trying to display the scrollbar when hovering over the main container. Below is a link to my portfolio page and corresponding code. Any assis ...

Arranging a Table with unconventional column headings using a hash map in Java

As a newcomer in the world of Java, I have been able to successfully set up a table with regular headers using a hash map. However, I now face the challenge of setting irregular headers in the table by grouping multiple columns under one main heading. //~ ...

Strategies for smoothly navigating the page to a specific div every time

Currently, I am working on a form that requires submitting multiple child forms. To enhance user experience, I have incorporated jQuery functionality to display a message at the top of the page upon each submission. Now, my goal is to implement a feature w ...

Angular dropdown within another dropdown

I am encountering an issue with a dropdown within another dropdown on my HTML page. The structure is as follows: Dropdown |_ First Dropdown | |_ aaa | |_ bbb | |_ ccc |_ Second Dropdown |_ ddd Even though everything appears ...

What is the method to display the width of a drop-down menu when using overflow:hidden?

Are there any solutions available for displaying a dropdown menu while using overflow in bxslider? The div currently has overflow:hidden to properly display the carousel. Any ideas on how to resolve this issue with z? .member{ position: absolute; ...

CSS problem: HTML element moving to the right upon clicking

Currently, I am utilizing a jQuery popup to present additional information to users. The setup involves having a link on the page that triggers the popup to appear from the top. The popup script being used is sourced from CodePen. However, an issue arises ...

HTML form for sending data to an express route with specific parameters

Looking for some help on routing in express. I currently have a form that collects user input and submits it to the /images route, but I would like it to instead submit to /images/:s1. This way, I can utilize the URL parameters effectively. Any suggestio ...

Can you demonstrate how to display the chosen toggle button within Angular 4 alongside the remaining options?

I have created a custom toggle button component in Angular that I can reuse in different parts of my application. However, I am facing an issue that I need help resolving. I want to display only the selected toggle button and hide the others. When I click ...

Is there a way to display a div upon clicking a li element?

How do I trigger the display of a <div> by clicking on a <li> element? <ul id="courseNav"> <li class="title"><a href="#">Overview</a></li> <hr style="margin-top: -0.8px"> <li c ...

Expand Tooltip on the Fly

Is there a way to make a tooltip expand horizontally instead of overflowing below the page? I have a tooltip that can be quite lengthy, and due to its max-width being set at 200px, it extends beyond the bottom edge of the page. I am currently able to set ...

Responsive Bootstrap column with a fixed-width card

My Bootstrap-card is currently not adjusting its width properly. I want the card to occupy the full width of the column it's in. Below is a snippet of my code and a screenshot showing the current width along with the desired width indicated by a blue ...

particular shade for button border and background

I'm looking to create a button using Material UI that has a specific design. Right now, I've only been able to achieve this: https://i.stack.imgur.com/xvv7s.png Here is the code I am currently using. If anyone can help me identify what I'm ...

Adjusting the div's background color according to a pre-determined choice in a select menu generated by PHP

My webpage features a select option menu generated by PHP, offering the choices 'Unverified', 'Verified', and 'Banned'. I am working on automatically changing the background color of the statusField based on the pre-selected ...

Show picture inside a table cell with Jquery jTable

I have implemented the JQuery jTable plugin to showcase my table data. $(document).ready(function () { //Setting up the jTable $('#PeopleTableContainer').jtable({ title: 'List of individuals', p ...

Back to top button displayed exclusively on desktop view (not visible on mobile devices)

I managed to create a scroll-to-top button that only appears when the user has scrolled down 25px from the top of the document. I achieved this using JavaScript by following a tutorial, as I am still unfamiliar with this programming language. However, I w ...

Add styles to IMG elements only if there are no existing styles already applied

Feeling a bit embarrassed asking such a basic question, but I seem to be struggling with it. I want to use CSS to style all IMG elements on my page that do not have any other class applied to them. For instance, I want all images to have a red border by ...

Adjust the select box size to be in line with the text box dimensions

I'm struggling to understand why there are two boxes visible in my navigation bar. For example, when looking at the Home link, I notice that there are actually two boxes; one containing the text "Home" and another one that extends outwards to the rig ...

Error encountered: Required closing JSX tag for <CCol> was missing

Encountered a strange bug in vscode...while developing an app with react.js. Initially, the tags are displaying correctly in the code file. However, upon saving, the code format changes causing errors during runtime. For instance, here is an example of the ...

What is the best way to position content at the bottom of a div?

I've been attempting to align my menu div with the bottom of my logo div on the same line within my header section. <div id="header"> <div id="top-bar"> </div> <div id="clear"></div> ...

PHP: Utilizing database queries to retrieve values and performing necessary transformations before inserting the data

While it may be simpler with javascript, I'm limited to MySQL, PHP, and Apache (as well as HTML and CSS) for this project, and I'm not proficient in PHP. The issue I'm facing is related to saving data from two dropdowns. The user selects tw ...