Display the list items within a div container without the need for negative margins

I'm in the process of learning CSS.

Here's some HTML code I've been working on:

<body>

    <div id="header">
        <span id="websiteName">
            VISHAL
        </span>

        <div id="languageBar">
            <ul class="nav">
                <li><a href="#">English</a></li>
                <li><a href="#">Hindi</a></li>
                <li><a href="#">Marathi</a></li>
                <li><a href="#">Gujarati</a></li>
            </ul>
        </div>

    </div>

</body>

This is a snippet of my CSS code:

* 
{
    margin: 0px auto;
    padding: 0px;
}

div#header 
{
    background: #353535;
    box-shadow: 0px 5px 5px #808080;
}

    div#header > span#websiteName
    {
        font-family: TEMPUS SANS ITC;
        font-size: 44px;
        font-weight:bold;
        color: #0094ff;
        margin-left:18%;
    }

ul.nav
{
    list-style:none;
}
    ul.nav li 
    {
        width:100px;
        float:right;
        display:inline;
    }

        ul.nav li a
        {
            display:block;
            width:100px;
            /*height:31px;*/ 
            float:left;
            /*margin-top:150px;*/
            /*text-decoration:none;*/
            /*color:black;*/
        }

My current output:

To see the result, visit this jsFiddle link.

One change I aim for in the output is:

The list items displayed are currently below the black box. My goal is to have them contained within that black box.

Answer №1

Take a look at this link fiddle

Try changing the padding instead of margin and utilize float: right

Answer №2

To achieve this effect, you can use the CSS position property.

 section#main-content 
{
background: #f2f2f2;
box-shadow: 2px 2px 3px #888888;
position:relative;
}

ul.navigation
{
list-style:none;
position:absolute;
top:20px;
right:10px;
}

See it in action here!

Answer №3

The issue lies in the floating of your menu outside the boundaries of #header. To rectify this, it is essential to inform #header that there are indeed floated elements within it.

This can be achieved by incorporating overflow: auto; into its styling.

div#header {
    background: #353535;
    box-shadow: 0px 5px 5px #808080;
    overflow: auto;
}

View Updated Fiddle Here.

Answer №4

Effective methods to adjust the placement of links:

Utilizing negative margins:

#navigation {
  margin-top: -20px;
}

Alternatively, using relative positioning:

#navigation {
  position: relative;
  top: -20px;
}

Another option is to float the links to the right if the title is floated to the left:

#navigation {
  float: right;
}

Answer №5

To align the #languageBar div to the right, simply apply a float property.

Live Demo Here

#languageBar {
    float: right;
}

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

Creating an HTML form that spans across multiple pages: Tips and tricks

When creating a shopping form that includes items like mugs and tshirts, is it permissible according to website standards to design a form that spans multiple pages? One option could be to add radio buttons for choosing a color directly on the main form ...

parsing HTML code to find the Facebook page link using regular expressions

I am attempting to extract the Facebook page address from website HTML using a regular expression search. Typically, the link will appear like this: <a href="http://www.facebook.com/googlechrome">Facebook</a> However, sometimes the address ...

Attempting to serialize a form using Ajax and jQuery

I am facing an issue with submitting user input from a modal using jQuery and AJAX. The problem is that the AJAX call is not capturing the user input. Even though the AJAX call is successful, when I check on the server side, the user input appears to be bl ...

Explore a Variety of Albums with our Bootstrap LightBox Gallery

I'd like to incorporate this code into my gallery website, but I'm encountering a couple of issues. Firstly, when one album's slides finish, I want it to loop back to the first image. Additionally, there seems to be an intermittent problem w ...

Ways to automatically style the child divs within a parent div

I'm trying to figure out how to float a parent div with child divs of different widths and heights while maximizing the use of space and not being affected by absolutely positioned elements. For reference, here's an example: http://jsfiddle.net ...

Center 3 elements horizontally using Flexbox, ensuring the center element is aligned properly

Is it possible to center 3 elements using flexbox on a page? The middle element should be centered on the page, while the left and right elements can vary in width. Please refer to the image below: https://i.sstatic.net/IVdz8.png I am not certain if this ...

The image width does not match the width of the table element

Currently, I am working on an HTML newsletter and facing an issue where the width of my image does not match the full width of the td tag, despite both having the same pixel value. The image appears as 180 pixels wide, whereas the td tag is set to be 186 ...

The slider components have an endless loop feature that only runs once before coming to a

I'm working on creating an infinite slider that loops continuously without the need for navigation buttons. Instead, I plan to allow users to control the slider using touch gestures (although this functionality is not yet implemented in the code snipp ...

Concealing the content of multiple items through sorting with the help of jquery

I need help with hiding the content of all elements while dragging or sorting the list. Can someone assist me with this? Here is my working fiddle. My jQuery code used in the fiddle is: $( function() { $( "#sortable" ).sortable(); $( "#sortable" ).disable ...

Ways to display an icon in Angular 10 only when it is hovered over

Just getting started with Angular and still learning the concepts. Trying to figure out how to show an icon only when it's hovered over. Can anyone assist me? Sorry, can't share the code because of company rules. The icons are for sorting and ...

Inspect HTML elements using Jinja2 placeholders

I am a beginner in the world of web development, currently learning how to create Flask webapps. I am using Atom along with some addons like PreviewHTML to help me visualize live previews of my HTML code. However, I am facing an issue where I cannot see a ...

Responsive grid made with HTML and CSS to create dynamic DIVs

Currently, I am working on a project that requires a responsive page layout using divs. The layout should resemble a grid, with 3 columns that reorganize into 2 columns on smaller screens. Here is the basic HTML structure I have so far: <div id="main" ...

Using the "this" keyword in JavaScript to access the "rel"

Take a look at the JSFIDDLE , where you will notice that the rel attribute in the alert is shown as 'undefined' : var ItemTypeArray = $('input[name^=ItemType]:checked').map(function(){ alert(this.id + ' , r= ' + this.rel) ...

The mouse coordinates do not align with the drawing of a rectangle on the canvas

I am having some issues with drawing a rectangle on mouse drag over the canvas that is overlayed on an HTML5 video JS player. The rectangle is being drawn, but it does not align correctly with the mouse coordinates. I suspect that the canvas, which is ove ...

Turn off jQuery on certain pages while keeping the styling intact?

Hey there, I'm currently in the process of developing a mobile website using jquery. I was wondering if it's possible to disable jQuery on certain pages while still retaining the styles, such as the navigation bar. The reason for wanting to disab ...

Is there a way to dynamically alter the background style of a div by clicking on it multiple times using Javascript?

I am attempting to create a calendar where each day changes its background color between blue and green when clicked, using JavaScript and CSS. It should function similar to a toggle feature. The default color is blue, and I have successfully made the days ...

The speed at which Angular is hiding elements is too rapid for any animation to keep up with

Utilizing Angular 1.2 in conjunction with the animate.css library available at The animations I'm implementing are geared towards simple ng-show="somevalue". The ng-show animations are functioning properly, smoothly fading in. However, when the eleme ...

Tips for Positioning Sidebar Elements Relative to Content IDs

My goal is to have sidebar elements align with specific id tags in the main content, rather than just stacking up one after another. I want the sidebar elements to align with certain id tags mentioned in the main post content. The image at shows the basic ...

Seamless Navigation with Bootstrap Navbar and SmoothScroll

Currently, I have a custom-built navbar that functions perfectly, with full mobile responsiveness. However, I am facing an issue with the nav-item's (headings). The nav-item's direct users to different sections of the same page using #. I have i ...

Utilizing the dojo.xhrget() Method to Showcase Information in the Dojogrid

Trying to showcase a dataset retrieved asynchronously from the server using the dojo.xhrget() method. The intention is to update the grid with new values each time a user interacts with the content pane, without refreshing the entire page. However, running ...