Having an alignment problem with my logo and inline block navigation due to CSS coding

Embracing the world of CSS as a newbie has been an exciting journey. However, I've encountered a hurdle in my coding process and could use some expert guidance.

In my website layout, I'm aiming to position my logo on the left side of the navigation bar. The main content of the site should be centered with a width of 960px and aligned at the top edge of the browser (0px from the top). To achieve this, I am utilizing the CSS properties display: inline along with li selectors.

I also intend to have the inline navigation sit snugly next to the logo. Additionally, I want to replicate the logo's top 4px border effect when hovering over the navigation links using the CSS a:hover selector. This hover effect should maintain a margin-top: 0px in the browser.

Here is the link to view my current logo/navigation layout:

Below is my CSS CODE:

.topbar {
    width: 960px;
    height: 87px;
    text-align: center;
}

.topbar-inner {
    width: 960px;
    margin: 0 auto 0 auto;
    text-align: center;
}

.logo {
    margin-top: 0px;
    display: inline;
}

img {
    float: left;
    border: 0;
    padding: 0;
    margin: 0;
}

.menu {
    display: inline;
    margin-top: 0px;
}

.menu > ul > li {
    display: inline-block;
    position: relative;
    border-top: 4px solid #FFF;
    margin-right: 0px;
    padding-top: 40px;
    min-width: 80px;
}

.menu > li {
    display: inline-block;
    list-style: none;
    margin-top: 50px;
}

.menu li a {
    color: #000;
    display: block;
    text-decoration: none;
}

.menu li:hover {
    border-top-color: #039;
}

.menu li:hover a {
    color: #039;
}

body { 
    font-family: Arial, Helvetica, sans-serif; 
    margin: 0;
}

.content { 
    height: 500px; 
    padding: 0px 20px 20px 20px; 
    text-align: left; 
    font-size: 12px; 
}

.footer { 
    border-top: 1px solid #DFDFDF; 
    width: 960px; 
    margin: 0 auto; 
    font-size: 11px; 
    text-align: center; 
}

And here is the HTML Code snippet:

<?php
// Including Setup document:
include('_config/setup.php');
?>
<!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">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title><?php echo $page_title; ?></title>
    <link rel="stylesheet" type="text/css" href="_css/styles.css">
</head>
<body>
    <div class="topbar">
        <div class="topbar-inner">
            <div class="logo"><img src="/vls/_images/mylogo.png" height="87" width="287"/></div>
            <!-- logo -->
            <div class="menu"><?php include('_template/nav_main.php'); ?></div>
            <!-- menu -->
        </div>
        <!-- topbar-inner -->
    </div>
    <!-- topbar -->
    <div class="bdy_hdr"><?php get_page_name($dbc, $pg); ?></div>                    
    <div class="content"><?php get_page_body($dbc, $pg); ?></div>
    <div class="footer"><?php include('_template/footer.php'); ?></div>
</body>
</html>

Thank you for your assistance!

Answer №1

It appears that everything is in order, except for the missing property "margin:0 auto;" which should be added to the topbar class instead of the topbar-inner class. The corrected code should look like this:

.topbar {
    width:960px;
    height:87px;
    text-align:center;
    margin:0 auto;
}

Please make sure to update the code accordingly.

Answer №2

All the hidden gems lie within the .topbar class. Remove the text-align:center, and instead insert a margin:0 auto. The revised code should appear as follows:

.topbar {
    width:960px;
    height:87px;
    margin:0 auto;
}

The text-align:center prevents the <ul> from being centered in the remaining space after the image floats, causing the top border to blend seamlessly.

The magic of margin:0 auto centers everything perfectly.


Add this line:

.menu ul {
    margin:0;
}

This adjustment ensures it stays at the top in the browser window. Although my previous comments about unnecessary clutter still stand :)


EDIT

Here is the reconstructed page with sleek markup and styling advice I suggested. There might be a couple of tricks that beginners may not grasp immediately, but you should comprehend most aspects easily.

By eliminating all the unnecessary divs, you can troubleshoot more efficiently, target elements accurately, reduce page weight (in bytes), and enhance the elegance of your solutions.

With this revamp, you achieve the same results as the original design in half the space.

HTML:

<body>
    <div class="topbar">
        <img src="logo.png" height="87" width="287"/>
        <ul class='menu'>
            <!- Avoiding rogue padding by staggering </li> though not ideal practice. -->
            <li><a href="index.php?page=home">Home</a>
            </li><li><a href="index.php?page=staffing">Staffing</a>
            </li><li><a href="index.php?page=jobs">Jobs</a>
            </li><li><a href="index.php?page=training">Training</a>
            </li><li><a href="index.php?page=gsa">GSA</a>
            </li><li><a href="index.php?page=why_us">Why Us</a>
            </li><li><a href="index.php?page=clients">Clients</a>
            </li><li><a href="index.php?page=contact">Contact</a></li>
        </ul>
    </div>
    ...remaining content goes here...
</body>

CSS:

body {
    font-family:Arial, Helvetica, sans-serif;
    margin:0;
}

.topbar {
    width:960px;
    height:87px;
    margin:0 auto;
}

.topbar img {
    float:left;
    border: 0;
}

ul.menu {
    margin:0 0 0 287px;
    padding:0;
}

ul.menu li {
    display:inline-block;
    position:relative;
    border-top:4px solid #FFF;
    list-style:none;
    padding-top:40px;
    min-width:80px;
}

ul.menu li a {
    color: #000;
    display: block;
    text-decoration:none;
}

ul.menu li:hover {
    border-top-color: #039;
}

ul.menu li:hover a {
    color:#039;
}

Answer №3

One way to organize your HTML structure is by placing your divs within a container, like this example:

<div id="container">
    <div class="topbar">
        <div class="topbar-inner">
            <div class="logo">
                <img src="/vls/_images/mylogo.png" height="87" width="287"/>
            </div>
            <div class="menu">
                <?php include('_template/nav_main.php'); ?>
            </div>
        </div>
    </div>
    <div class="bdy_hdr">
        <?php get_page_name($dbc, $pg); ?>
    </div>                    
    <div class="content">
        <?php get_page_body($dbc, $pg); ?>
    </div>
    <div class="footer">
        <?php include('_template/footer.php'); ?>
    </div>
 </div>

To style it in CSS, you can set the following for the top bar:

.topbar{
margin:0 auto;
}

For better layout control, consider defining a width for .bdy_hdr, .content, and .footer, and center them as well:

.bdy_hdr,
.content,
.footer{
width:960px;
margin:0 auto;
}

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

Problem arises when attempting to display a div after clicking on a hyperlink

I'm encountering an issue with displaying a div after clicking on a link. Upon clicking one of the links within the initial div, a new div is supposed to appear below it. All tests conducted in jsfiddle have shown successful results, but upon transf ...

html input type called array named AmountMap[1] is unable to be configured with <input type="textbox" size="15" name="amountMap[1]" value="0" >

**Having trouble setting the value of an HTML input type named like an array AmountMap[1] <input type="textbox" size="15" name="amountMap[1]" value="0" > When trying to set amountMap[1].value='10', ...

Uncertainty surrounding the effectiveness of the display: block CSS property - my demonstration is not yielding the expected results

I encountered an issue while using display: block. To better understand the problem I'm facing, please check out this example link: Take a look at the HTML code below: <div id="container"> <div id="titleBox"> <p id="myT ...

The hover effect and image opacity adjustment seem to be malfunctioning in my HTML/CSS code

Currently, I am in the midst of a web project and my goal is to implement a hover effect on the first card containing an image. The desired outcome is for the card to move upwards upon hovering, allowing the image to become fully visible with an opacity se ...

Retrieve text content from a specified element on a different page, not the one currently loaded, by

After implementing this dynamic content script, I realized that it is working well for the current page. However, since I cannot access the code-behind in the asp.net project, I am facing an issue when trying to fetch the text from "div.sampleheading" on t ...

What is causing the border-radius property to malfunction on a particular website when using Internet Explorer?

We create browser add-ons for popular browsers like Google Chrome, Firefox, Safari, and Internet Explorer. Our add-ons inject HTML elements into various websites such as mail.google.com and email14.secureserver.net. The HTML element we use has a border-ra ...

Alignment of text varies between canvas and HTML

How can I ensure that the labels in both canvas and HTML display overlap exactly, regardless of font family and size? The current code fails to center text inside an area when switching between canvas and HTML view. let canvas = document.getElementById( ...

The jQuery scripts are having trouble cooperating

Currently, I am in the process of developing a website. The main focus at the moment is on creating a responsive menu and incorporating jQuery scripts. However, I seem to be facing some challenges in getting everything to work seamlessly together. Each scr ...

Continuous scroll notification within the fixed menu until reaching the bottom

I'm looking to achieve a scrolling notification message that stays fixed at the bottom of a top-fixed menu while the body content continues to scroll normally. Here's an example in this fiddle: HTML: <div class="menu-fixed">I am a fixed me ...

Enabling the autowidth label expands the width of the td element

I am facing an issue where a label inside a table td grows in only one line when its width is greater than the td width. This results in the td expanding and not showing all the text. I would like the label to break into a new line when its width exceeds ...

Ways to enable users to showcase the files they have uploaded

I've successfully implemented code for file uploading. When saving the uploaded file in the database, I retrieve the uploader's username from another database. Additionally, I have a page that displays all uploaded files, functioning perfectly. N ...

What strategies can I use to include multiple spans within div elements without causing the code to become overly lengthy?

I'm working with 20 spans representing movie genres, each accompanied by another span that "closes" the genre when selected. The closing span has an .active class, similar to this example: https://i.sstatic.net/7CuuX.png My version currently looks li ...

The appearance of success in the form is not being adequately shown

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script> <link rel="stylesheet" href= ...

What is the method for squaring the sides of the top border?

Can the top border be squared (straightened) as shown in the image below? https://i.sstatic.net/7GKhj.png .nav.nav-tabs.custom-nav-tabs .nav-link { border-radius: 0px; } .nav.nav-tabs.custom-nav-tabs .nav-link.active { border-top: 2px solid gr ...

Is there a way to align these buttons in the middle?

I am currently designing a webpage at The challenge I'm facing is: How can I center those buttons on the page? They need to remain centered and responsive even when the page is resized. Additionally, the spacing between the buttons is inconsistent. ...

Prevent Image Distortion in HTML and CSS

Is there a way to prevent a background image from stretching in html/css? Take a look at the current code below, where the image behind a button appears like this: #backImage6{ /*@editable*/background-image:url(https://images.unsplash.com/phot ...

Transitions fail to appear correctly when the page first loads

I need to fill a gauge meter based on a variable sent to the html file. To achieve this, I have initially set the value to zero: transform:rotate(.0turn); transition: all 1.3s ease-in-out; However, when the HTML is first loaded or refreshed (with F5 ...

Placing text alongside an image at the top of the page

Here's the HTML code generated by JSF: <div align="center"> <img src="images/background_image.jpg" height="200px" width="30%" style="vertical-align: top"/> <span style=""> Welcome abc, <input type= ...

Respond.js appears to be without functionality

Can anyone lend a hand? I'm feeling incredibly frustrated trying to get respond.js to cooperate. Typically, we have no issues with using it on our mobile sites. However, I am currently facing challenges trying to achieve mobile compatibility on one o ...

Learn how to implement a split background effect by simply clicking the SPLIT button

let context = canvas.getContext("2d"); // for canvas size var window_width = window.innerWidth; var window_height = window.innerHeight; canvas.width = window_width; canvas.height = window_height; let hit_counter = 0; // object is created using class clas ...