The issue of exceeding margins

I am facing an issue with some CSS. Below is my HTML code:

<body>
<div id="cn">
    <div id="hd">
        <ul>
            <some li's>
        </ul>
    </div><!-- /#hd -->
    <div id="bd">
    </div><!-- /#bd -->
    <div id="ft">
    </div><!-- /#ft -->
</div><!-- /#cn -->

Here is the associated CSS:

div#cn {
    position: relative;
    width: 960px;
    margin: 0 auto;
    background: #f7f7f7;
}

div#cn > * {
    position: relative;
}

div#cn div#hd {
    height: 258px;
    background: #f7f7f7 url(../img/hd.jpg);
}

div#cn div#hd ul {
    margin: 50px 0 0 0;
    padding: 0;
    list-style-type: none;
}

div#cn div#hd ul li {
    float: left;
}

div#cn div#hd ul li a {
    display: block;
    height: 35px;
    padding: 20px 25px 0 25px;
    font-weight: bold;
    background: pink url(../img/nava.jpg) right 0 no-repeat;
}

Currently, the margin is not affecting the UL but the Div around it (#hd) in Firefox. It appears as if the margin is applied on #hd instead of the ul. Interestingly, setting a border to #hd seems to make it work! (Alternatively, placing a non-breaking space before the UL also works)

Can anyone explain why this behavior is happening and provide a proper solution to fix it without resorting to setting a border?

Answer №2

Recently, I encountered a comparable issue and successfully resolved it by including additional padding to the main element.

.container #header{ padding: 2px 0; margin: -2px 0; }

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

Use the given URL to set the background image

Having trouble setting a background image for an <a> tag. The image link is set in the background-image:url property, but the image isn't showing up as the background. Here's my code, what could be the issue? <a href="#" data-to ...

Implement CSS to globally style material.angular's mat-card by customizing the background color

Looking for a way to globally change the background of mat-card on material.angular.io. I attempted adding the following code snippet to styles.css with no success. mat-card { background-color: purple; } ...

Tips for splitting a word in the uib-typeahead menu

I have successfully implemented a uib-typeahead dropdown in my AngularJS application where I display two attributes. The API is called based on the first name, and the response contains an object with both first name and last name. However, currently, they ...

What is the best way to retrieve the parent element quickly using Jquery?

html: <span class="ui-spinner ui-widget ui-widget-content ui-corner-all"><input class="spinner1 ui-spinner-input" id="q1" name="value" value="1" min="1" aria-valuemin="1" aria-valuenow="2" autocomplete="off" role="spinbutton"><a class="ui ...

Reference ngFor for Input Validation Template

I'm currently facing an issue with validating input fields within a *ngFor loop. I am struggling to create unique template references for each input field. Basically, I need all input fields to be required on submit unless at least one of them is fill ...

Using session variables in HTML allows developers to store and retrieve

My index.html file includes a login form that submits to login.php. Once the user is verified, they are redirected back to index.html and the verified username is stored in a session variable called username. I am looking for a way to display this username ...

Incorporate personalized design elements within the popup component of Material-UI's DataGrid Toolbar

I am in the process of customizing a Data Grid Toolbar component by making adjustments to the existing Grid Toolbar components sourced from Material-UI. For reference, you can view the official example of the Grid Toolbar components here. Upon clicking o ...

Locate grandchildren elements using getElementById

My task is to modify the content and attributes of the table. The DOM structure is generated by a third-party tool, and I am using JavaScript to make changes in various sections. <div id="myId"> <div> <div> <table&g ...

Utilizing margins in CSS for various div elements

Update: I have included Javascript and Masonry tags in my project. Despite having modules of the same size, I am exploring how masonry can assist me. However, I find it a bit puzzling at the moment as I am not aiming to align elements of different sizes. I ...

obtain the string representation of the decimal HTML entity value

Similar Question: how to create iphone apps similar to ibeer, imilk, ibug, ibeer Using javascript to obtain raw html code Imagine having an html section like this: <div id="post_content"> <span>&#9654;<span> </div> ...

How can I locate specific images within a CSS sprite?

Are you searching for the correct process to pinpoint specific image locations within an image sprite? If, for example, you aim to create 10 div images in your header using the image provided below, how can you determine the exact location of each one? Is ...

Margin isn't functioning properly

My section is not centered and adding margin-left doesn't seem to be working. Any suggestions on how I can solve this? Take a look at how it appears. Here's the code: .margins { margin-left: 100px; } <link href="https://cdn.jsdelivr.net ...

Swapping out data points using JQuery

What could be causing line 10 to return null? Click here for the code file The code seems to function properly with line 40, but not with line 10. ...

The user uploads an image to the server, which is then assigned as the background-image for a div element

I am in search of a straightforward method to allow users to upload an image to my server and then use that uploaded image as the background-image for a div element. I need the image to be actually uploaded to the server rather than just displaying a local ...

AngularJS's ng-repeat feature is not properly re-embedding tweets

In my project, I am utilizing angularjs to showcase tweets. The implementation involves using ng-repeat alongside a filter that is directly linked to the state of a checkbox. When the checkbox is checked, the filter returns all the tweets (the default beha ...

Oh no! An operational error occurred while trying to access the page "/1" because it seems like the column

I recently started working with Django and I'm trying to figure out how to display all comments related to a specific listing on an online auction site. Can anyone help me find a solution for this? Models.py class Listing(models.Model): title = m ...

What is causing the text below the SVG path to appear thicker?

I am encountering an interesting issue with my SVG green marker. The text inside appears bolder than the same text with the same attributes when it is standalone. https://i.sstatic.net/NYmXX.png Upon further inspection, I noticed that the text within the ...

The dropdown menu is dynamically populated based on the selection from another dropdown menu, with options retrieved from

Apologies if this has been addressed previously, but the existing solutions do not seem to work for me. I have 5 dropdowns: Brand, Model, Color, Engine No., and Chassis No. My query pertains to how I can link the dropdown options for Model based on the sel ...

Material-UI icons refusing to show up on the display

I've been working on creating a sidebar component and importing various icons to enhance the UI, but for some reason they are not displaying on the screen. I even tried other suggested solutions without success. <SidebarOption Icon = {InsertComment ...

It is impossible for 4 div elements to align in a horizontal position

Having an issue where I am trying to align 4 boxes horizontally like cards, but they are appearing in a staircase formation instead. Any help would be appreciated. I have provided the code with the issue on this jsfiddle link #first { ...