Tips for positioning a button directly under a horizontal navigation bar

How can I center a button just below a horizontal navigation menu, without overlapping the menu items and ensuring proper spacing when the menu expands?

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Welcome</title>
<style type="text/css">
    * {
    margin:0px;
    padding:0px;
}

body {
    background-color:#f7f0f0;
    font-family:Verdana;
    padding:50px;   /*all four paddings are 50px*/
}

h1 {
    text-align:center;
    border-bottom:2px solid #666;
    padding-bottom:10px;
}

ul#mainmenu,ul.sub1,ul.sub2,ul.sub3,ul.sub4,ul.sub5 {
    list-style-type:none;
    font-size:15px;
}

ul#mainmenu {
    padding-top:10px;
}

ul#mainmenu li{
    float:left;
    position:relative;
    width:350px;
    text-align:center;
    margin-right:3px;   /*renders a right margin of 5 pixels around the list items */
}

ul#mainmenu a {
    text-decoration:none;
    display:block;
    line-height:25px;   /*gives a vertical centering*/
    width:350px;    /*same as the width of the list items*/
    height:25px;
    background:linear-gradient(to right,#bdc2ed,white,#bdc2ed); /*create a color gradient to the right*/
    border-radius:5px;
    /*outline:1px solid red;*/  /*takes up the space outside of the elements's container (i.e. in this case the outside of the list items*/
}

ul#mainmenu .sub1 li {
    margin-top:2px;
}

ul#mainmenu .sub1 a {
    border:1px solid green; /*takes up the space outside of the content itself i.e. the anchor tags . That's where the border radius comes into the picture*/
}

ul#mainmenu .sub2 li,.sub3 li,.sub4 li,.sub5 li {
    margin-left:20px;
}

ul#mainmenu li:hover > a {
    text-transform:uppercase; /*select and style every <a> element where the the par */
    background:linear-gradient(to right,blue,white,blue);
    border:1px solid black;
}

ul#mainmenu li:hover a:hover {
    background:linear-gradient(to right,#718ce5,white,#718ce5);
}

ul#mainmenu ul.sub1 {
    display:none;
    position:absolute;
    top:26px;
    left:0px;
}

ul#mainmenu ul.sub2 {
    display:none;
    position:absolute;
    top:-2px;
    left:335px;
}

ul#mainmenu ul.sub3 {
    display:none;
    position:absolute;
    top:-2px;
    left:335px;
}

ul#mainmenu ul.sub4 {
    display:none;
    position:absolute;
    top:-2px;
    left:335px;
}

ul#mainmenu ul.sub5 {
    display:none;
    position:absolute;
    top:-2px;
    left:335px;
}

ul#mainmenu li:hover ul.sub1 
{
    display:block;
}

ul#mainmenu ul.sub1 li:hover ul.sub2
{
    display:block;
}

ul#mainmenu ul.sub2 li:hover ul.sub3
{
    display:block;
}

ul#mainmenu ul.sub2  li#submenu_shirts:hover ul.sub4
{
    display:block;
}

ul#mainmenu ul.sub1 li#submenu_shoes:hover ul.sub5
{
    display:block;
}
</style>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script type="text/javascript">
    $(document).ready(function () {

        $("button").click(function () {
            $("ul#mainmenu li:first").hide(500);
        });
    });
</script>
</head>
<body>

<h1>Welcome</h1>
<div>
<form action="" >
<ul id="mainmenu">
<li><a href="#">Home</a></li>

<li><a href="#">Products</a>
    <ul class="sub1">
        <li><a href="#">Clothing</a>
            <ul class="sub2">
                <li><a href="#">T-Shirts</a>
                    <ul class="sub3">
                            <li><a href="#">Polo</a></li>
                            <li><a href="#">V-Neck</a></li>
                            <li><a href="#">Henley</a></li>
                    </ul>
                </li>
                <li id="submenu_shirts"><a href="#">Shirts</a>
                    <ul class="sub4">
                            <li><a href="#">Half-Sleeve</a></li>
                            <li><a href="#">Full-Sleeve</a></li>
                    </ul>
                </li>
                <li><a href="#">Jeans</a></li>
                <li><a href="#">Trousers</a></li>
            </ul>
        </li>
        <li id="submenu_shoes"><a href="#">Shoes</a>
            <ul class="sub5">
                <li><a href="#">Sports-Shoes</a></li>
                <li><a href="#">Formal-Shoes</a></li> 
            </ul>
        </li>
        <li><a href="#">Accessories</a></li>
    </ul>
</li>

<li><a href="#">Contact Us</a></li>
</ul>
</form>
</div>

<div>
<input type="submit" value="Post"/>
</div>


</body>
</html>

Answer №1

Take a look at this jsFiddle example.

In my approach, I utilized jQuery and employed the hover selector with a toggle. I also centered the menu, although this can be adjusted by uncommenting the float:left for the unordered list. Normally, this would shift the input button downwards. However, the issue lies in the fact that the embedded unordered list has an absolute position for its list items, overriding the relatively positioned button input. To address this, I adjusted the button's position on hover for the product list item only, as it is the element causing the problem. Therefore, this solution is tailored specifically for the issue mentioned above.

$("li.products").hover(function(){
    $( ".btn" ).css( "top", "60px" );
}, function(){
    $( ".btn" ).css( "top", "0px" );
});

For additional information on the jQuery hover selector, consult these resources:

1.) jQuery API documentation - http://api.jquery.com/hover/

2.) Relevant Stack Overflow thread - toggle a css color on hover

Answer №2

Adjust the width values as shown below...

ul#mainmenu li{
    width:250px;


ul#mainmenu a {
    width:250px;

REPLACE:

OLD:

<form action="" >
<ul id="mainmenu">

NEW:

<form action="" >
<ul id="mainmenu" style="width:800px; margin:0 auto;">

Answer №3

    <!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Welcome to My Website</title>
<style type="text/css">
    * {
    margin:0px;
    padding:0px;
}

body {
    background-color:#f7f0f0;
    font-family:Verdana;
    padding:50px;   /*all four paddings are 50px*/
}

h1 {
    text-align:center;
    border-bottom:2px solid #666;
    padding-bottom:10px;
}

ul#mainmenu,ul.sub1,ul.sub2,ul.sub3,ul.sub4,ul.sub5 {
    list-style-type:none;
    font-size:15px;
}

ul#mainmenu {
    padding-top:10px;
}

ul#mainmenu li{
    float:left;
    position:relative;
    width:250px;
    text-align:center;
    margin-right:3px;   /*renders a right margin of 5 pixels around the list items */
}

ul#mainmenu a {
    text-decoration:none;
    display:block;
    line-height:25px;   /*gives a vertical centering*/
    width:250px;    /*same as the width of the list items*/
    height:25px;
    background:linear-gradient(to right,#bdc2ed,white,#bdc2ed); /*create a color gradient to the right*/
    border-radius:5px;
    /*outline:1px solid red;*/  /*takes up the space outside of the elements's container (i.e. in this case the outside of the list items*/
}

ul#mainmenu .sub1 li {
    margin-top:2px;
}

ul#mainmenu .sub1 a {
    border:1px solid green; /*takes up the space outside of the content itself i.e. the anchor tags . That's where the border radius comes into the picture*/
}

ul#mainmenu .sub2 li,.sub3 li,.sub4 li,.sub5 li {
    margin-left:20px;
}

ul#mainmenu li:hover > a {
    text-transform:uppercase; /*select and style every <a> element where the the par */
    background:linear-gradient(to right,blue,white,blue);
    border:1px solid black;
}

ul#mainmenu li:hover a:hover {
    background:linear-gradient(to right,#718ce5,white,#718ce5);
}

ul#mainmenu ul.sub1 {
    display:none;
    position:absolute;
    top:26px;
    left:0px;
}

ul#mainmenu ul.sub2 {
    display:none;
    position:absolute;
    top:-2px;
    left:335px;
}

ul#mainmenu ul.sub3 {
    display:none;
    position:absolute;
    top:-2px;
    left:335px;
}

ul#mainmenu ul.sub4 {
    display:none;
    position:absolute;
    top:-2px;
    left:335px;
}

ul#mainmenu ul.sub5 {
    display:none;
    position:absolute;
    top:-2px;
    left:335px;
}

ul#mainmenu li:hover ul.sub1 
{
    display:block;
}

ul#mainmenu ul.sub1 li:hover ul.sub2
{
    display:block;
}

ul#mainmenu ul.sub2 li:hover ul.sub3
{
    display:block;
}

ul#mainmenu ul.sub2  li#submenu_shirts:hover ul.sub4
{
    display:block;
}

ul#mainmenu ul.sub1 li#submenu_shoes:hover ul.sub5
{
    display:block;
}

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

</style>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script type="text/javascript">
    $(document).ready(function () {

        $("button").click(function () {
            $("ul#mainmenu li:first").hide(500);
        });
    });
</script>
</head>
<body>

<h1>Welcome to My Website</h1>
<div>
<form action="" >
<ul id="mainmenu" style="width:800px; margin:0 auto;">
<li><a href="#">Home</a></li>

<li><a href="#">Products</a>
    <ul class="sub1">
        <li><a href="#">Clothing</a>
            <ul class="sub2">
                <li><a href="#">T-Shirts</a>
                    <ul class="sub3">
                            <li><a href="#">Polo</a></li>
                            <li><a href="#">V-Neck</a></li>
                            <li><a href="#">Henley</a></li>
                    </ul>
                </li>
                <li id="submenu_shirts"><a href="#">Shirts</a>
                    <ul class="sub4">
                            <li><a href="#">Half-Sleeve</a></li>
                            <li><a href="#">Full-Sleeve</a></li>
                    </ul>
                </li>
                <li><a href="#">Jeans</a></li>
                <li><a href="#">Trousers</a></li>
            </ul>
        </li>
        <li id="submenu_shoes"><a href="#">Shoes</a>
            <ul class="sub5">
                <li><a href="#">Sports-Shoes</a></li>
                <li><a href="#">Formal-Shoes</a></li> 
            </ul>
        </li>
        <li><a href="#">Accessories</a></li>
    </ul>
</li>

<li><a href="#">Contact Us</a></li>
</ul>
</form>
</div>

<div>
<input type="submit" value="Post"/>
</div>


</body>
</html>

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

Is there an issue with this website link?

What is causing the Xul app to display the error message: XML Parsing Error: not well-formed when encountering this code snippet: <browser class="AppBar" type="content" src="test.html?img1=img1.jpg&img2=img2.jpg" flex="4"/> specifically ...

Transition from mouse wheel scroll to page scroll not functioning properly when overflow is enabled

The website has a fixed element that uses overflow:auto. Users can scroll this element by positioning the mouse over it. However, once the element reaches the end of its scroll, the page does not seamlessly take over the scrolling. Instead, there is about ...

Make sure your website design is responsive by setting the body to the device's

https://i.sstatic.net/3xHcx.png I'm struggling to make the body of my page responsive for mobile users. Whenever I try using this line of code, it just creates a messy layout. Any advice on how to fix this? <meta name="viewport" cont ...

Prevent CSS3 columns from reverting back to their original state after being rendered

Utilizing css3 columns, I have created a layout with an unordered list displayed in 3 columns. Each list item contains another list that can be toggled to show or hide by clicking on the title using jQuery. The structure of the html is as follows (with ex ...

"Email signature design with sleek black borders incorporated for a professional touch in responses

At my workplace, I use an HTML file as my email signature footer with IBM Notes. Everything looks great when I create a new email - the logo is positioned correctly, text is styled how I want it, and the overall layout is fine. However, things start to lo ...

Align the UL in HTML to be on the same line as other spans

When examining this jsFiddle demonstration at the following link: http://jsfiddle.net/jrXwf/1/ The UL tag showcases a star rating, with accompanying text displayed on the line below it. Is there a method to have everything appear on a single line? Appre ...

Issue with syntax when transferring an HTML element from the #div to the body

Attempting to duplicate an element and remove the original because of a z-index issue in IE. Planning to use conditional comments: <!--[if lte IE 7]> <script type="text/javascript"> $(document).ready(function() { var callCenter ...

Disable the default form input reset button generated by Internet Explorer 10

Just have one field form for search input. Below is the HTML code: <form class = "search-form hide-on-mobile"> <input class = "global" type = "text" placeholder = "Who are you looking for ?"> <but ...

Exploring XPath: strategies for excluding text within nested elements

if I come across some HTML structure like this: <div class=unique_id> <h1 class="parseasinTitle"> <span> Game Title </span> </h1> Game Developer </div> Is there a unique way to extract only the "Game Develo ...

How to resolve CORS error when using custom request header in Django with JavaScript and redirecting to OPTIONS request?

I am currently working on building an API library using Django. This API will be accessed by javascript, with the Django-API and javascript running on separate servers. The Django API library requires a custom request header from the javascript front end, ...

Expanding a feature that modifies the CSS properties of every input element

Creating a text tracking (letter-spacing) demonstration where range sliders are used to update CSS properties. I'm looking to improve the functionality so that the labels also update dynamically, without repeating output2.textContent = this.value;. An ...

Automatically updating markers on Google Maps v3

I'm utilizing the capabilities of Google Maps V3 to showcase various pins. My goal is to update these markers periodically without interfering with the current location or zoom level on the map. I aim for the markers to refresh every x seconds. How c ...

Retrieving localStorage data from another webpage

I recently created an account and I hope my question is clear. I have two separate pages on my website - one for a menu and the other for an HTML game. The menu has two buttons, one to start a new game and the other to continue from where you left off. How ...

Tips for adjusting the placement of enlarged images within colorbox

I recently discovered colorbox and decided to use it as my lightbox solution. However, I encountered a challenge with positioning the background (#cboxOverlay). I wanted to move it 120px to the left so that some content from the original page would still ...

Change URL link after login user with javascript

Is there a way to generate a URL link based on the user's name (gmail)? For example: The code below is somewhat suitable for my needs. However, I am trying to figure out how to automatically retrieve the username and populate it in the input field. ...

Just starting out with json/ajax and I received an error in the Console that says: Uncaught TypeError: Cannot read property 'length' of undefined

I’m encountering an issue with my code. I must emphasize that I’m brand new to this, so please bear with me if the solution is simple. I did attempt to solve it myself before reaching out for help and searched through various posts with similar errors, ...

The heights of columns in CSS column-count vary

I've been experimenting with creating a masonry grid of images using column-count to achieve a similar layout to this example. Despite following the CSS and HTML structure outlined in the link, I've encountered an issue where the columns don&apo ...

CSS query: How to eliminate the extra space at the top of a webpage?

How can I eliminate the gray area visible here: The padding in style.css is currently set to: padding: 0; I have attempted to modify this by changing the following: #page { margin-top: 0; } I have tried variations such as: #page { margin-top: 5px !im ...

Google Chrome - Automatically scroll to the bottom of the list when new elements are added in *ngFor loop

I have a basic webpage showcasing a list of videos. Towards the bottom of the page, there is a button labeled "Load more". When a user clicks on this button, an HTTP request is triggered to add more data to the existing array of videos. Here is a simplifi ...

CSS - Apply a captivating background to specific columns within a grid layout

Wow, I'm really struggling with CSS and this particular issue has me perplexed. I need to add background colors to specific columns but not all of them. The problem is that the padding gets messed up when I do this. Imagine having a headache while try ...