What is the best way to style my navigation bar using HTML and CSS?

Exploring the world of web design as a recent enthusiast, I delved into the basics some time ago only to forget them shortly after. My interest reignited a few months back, leading me to create my own web pages for practice and skill enhancement. However, I've hit a roadblock with my navigation bar display. Here is the HTML code snippet for my navigation bar:

<div class="nav">
    <ul class="nav">
        <li class="nav"><a class="nav" href="#">Home</a></li>
        <li class="nav"><a class="nav" href="#">Coffee</a></li>
        <li class="nav"><a class="nav" href="#">Food</a></li>
        <li class="nav"><a class="nav" href="#">Catering</a></li>
        <li class="nav"><a class="nav" href="#">About</a></li>
        <li class="nav"><a class="nav" href="#">Contact</a></li>
    </ul>
</div>
<!--Navigation bar.-->

Below is the CSS code that pertains to the elements (div, ul, li & a):

div{    
    border: 2px;
    border-radius: 3px;
    margin: 0 auto 60 auto;
    padding: 10px;
    width: 980; 
}
/*BASIC DIV ELEMENT.*/

/*LINKS.*/
a{
    color: #545454;
    font-family: lucida grande, lucida sans, sans-serif;
    font-size: 14px;
    text-decoration: none;
}

a:hover, a:active {
    color: #191919;
}
/*LINKS.*/

/*NAV BAR*/
a.nav:link{
    background-color: #D7C5CC;
    color: #191919;
    display: inline-block;
    padding: 15px;
    text-align:center;
    width: 90px;
}

a.nav:hover{
    color: #191919;
    background-color: #EDD9DF;
}

li.nav{
    float: left;
}

ul.nav{
    display: center;
    margin: 0 auto 0 auto;
}
/*NAV BAR*/

My knowledge of HTML and CSS is at a beginner level, so I apologize for any mistakes in the coding. The issues I'm facing with my navigation bar are twofold:

  1. I am unable to achieve proper round corners for the navigation bar. I've attempted setting "border-radius: 10;" on ul.nav and li.nav without success.
  2. The centering of the navigation bar on my Chrome testing page has proven to be a challenge. While other divs center flawlessly, my efforts to modify "display" and "float" attributes have been fruitless.

Despite searching extensively on Stackoverflow, none of the solutions provided have yielded the desired outcome.

EDIT: My objective is to create a seamless navigation bar where only the outer corners are rounded, akin to the design showcased here:

Answer №1

To achieve this effect, simply include the following CSS:

ul.nav li:first-child a {
    border-top-left-radius: 3px;
    border-bottom-left-radius: 3px;
}
ul.nav li:last-child a {
    border-top-right-radius: 3px;
    border-bottom-right-radius: 3px;
}

For a visual demonstration, check out this jsFiddle.

If you prefer an exact replica of the example you provided a link to, you can view it on this jsFiddle.

Answer №2

Make sure to review this: Example.

In order to include a border-radius, the CSS code I inserted is as follows:

a.link:visited {
     border-radius: 15px;
}

To properly align the navigation bar in the center, I implemented this CSS code:

ul.navbar{
    text-align: center;
}

Furthermore, I eliminated the float: left attribute from li.item and updated it with the following CSS:

li.item{
    display: inline-block;
}

Answer №3

Initially, your class is invalid because you are applying it at different levels. This can lead to conflicts or the properties not working as intended.

My recommendation in this scenario is the following:

<nav>
    <ul class="navbar">
        <li class="nav-item selected"><a href="#">Home</a></li>
        <li class="nav-item"><a href="#">Coffee</a></li>
        <li class="nav-item"><a href="#">Food</a></li>
        <li class="nav-item"><a href="#">Catering</a></li>
        <li class="nav-item"><a href="#">About</a></li>
        <li class="nav-item"><a href="#">Contact</a></li>
    </ul>
</nav>

<style>
nav{
   position:fixed;
}

.nav-item{
   color: #000;
   border: 1px solid blue;
   background-color: rgba(255, 255, 255, .6 ) 
}

.nav-item:hover
{
        background-color: rgba(0, 255, 255, .6 ) 
}

.selected{
   color: #058;
   border: 1px solid red;
   background-color: rgba(255, 0, 255, .6 ) 
}

nav: denotes an HTML5 element for creating navigation bars class {

navbar: represents the collection of items and is used to set general options and distribution for all items in the navbar.

nav-item: is used to customize options for each individual item, allowing for effects to be applied.

selected: another class used to highlight a selected item. Multiple classes can be applied to the same item by separating them with a space. Using JavaScript, it is possible to add or remove a class dynamically, as shown in this example

<script>
$(".nav-item").click(function(){
    $(".nav-item").removeClass("selected");
    $(this).className = $(this).className + "selected";
});
</script>

This function switches the selected class to the clicked item.

}

The style provided is just a basic example.

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

`CSS border issues`

I am currently attempting to create a square consisting of 4 smaller squares inside, but I have been facing challenges with the method I was using. Here is the code snippet: #grandbox { position: absolute; width: 204px; height: 204px; border: so ...

What could be causing the issue with my validation for alphabetical input?

I am currently working on a registration form that only accepts alphabetical input. However, I am facing an issue where my error message appears regardless of whether I input an alphabetical or special character. According to my understanding, the code sho ...

Sending Javascript Variable through Form

I'm a newcomer to JavaScript and struggling to solve a particular issue in my script. I need help passing the variable "outVal" to a PHP script when the submit form is clicked. The value of "outVal" should come from the "output" value in the script. I ...

Code Wizard

I am currently working on a project to develop an HTML editor. How it Needs to Function Elements Inside: Text Area - Used for typing HTML as text Iframe - Displays the typed HTML as real [renders string HTML to UI] Various Buttons Functionality: When ...

How can I generate a checkbox in a database with a field that allows for enabling and disabling?

I am looking to design a table that includes questions, checkboxes, and text boxes. All the questions are stored in a database and called through an array. In addition, I want to create disabled textboxes that become enabled when their corresponding checkb ...

Eliminate Bootstrap's validation glyphs

Issue Description I am attempting to eliminate the validation icons from Bootstrap, such as the "x" and "check" symbols. Despite my efforts to search thoroughly, I have been unable to locate where these icons are located in the code. Code Sample You can ...

Integrating objects into the <select> element through the combination of C#, JavaScript, and HTML connected to a SQL

Can someone assist me in resolving this issue? I am trying to populate an HTML element with database fields using C# and JavaScript, but so far my code is not producing any output. I have also attempted to include a button that calls the "loadGrp" function ...

Making the switch from lazy-loaded images to `loading="auto"` once the page has finished loading

I'm exploring a new approach to implement lazy loading of images on a CMS (Sitecore) website, and I have some concerns about potential issues that may arise since I haven't come across any examples of this method being used before. The main goal ...

Decorate the elements that do not contain a specific child class

I'm currently working on an angular project with primeng for the UI components. My focus at the moment is on customizing the p-menu component, specifically the appearance of list items that are not active. The challenge I'm facing is that the act ...

Tips on aligning carousel images and captions next to each other

Would anyone be able to assist me in creating a carousel similar to this? I haven't been able to locate any examples or tutorials that match the design of my desired carousel. Here is the design I am referring to ...

Utilizing CSS or JavaScript to define the input type

I currently have a group of checkboxes displayed on a webpage within a DIV labeled OPTIONS, shown below: <div id="options"> <input type="checkbox"..... > <input type="checkbox"..... > <input type="checkbox"..... > <input t ...

A hobby project involving the creation of a webmail app using a combination of PHP

As a beginner in web development, I am eager to tackle a hobby project that will help me learn more about PHP and other web-related technologies. I have been considering creating a simple webmail client, but I'm not sure where to begin. I anticipate ...

What is the best way to customize the appearance of a form element within an angular-schema-form schema without affecting the overall

Currently, I am in the process of constructing a form using the incredible angular-schema-form. Creating the form schema object has been a success so far. My goal is to configure all the form components within the schema using the x-schema-form property in ...

"Problem with the Hide/Show Load feature: it's not functioning

After a user submits a form, my script shows 2 divs and hides 1 div. The form's target is an Iframe on the same page. I am looking to delay the Hide/Show events until the Iframe loads. I was successful in accomplishing this with a loading animation, ...

Tips for animating elements to move on scroll while navigating through the webpage

I came across a fascinating website that has a unique scrolling effect. As you scroll down the page, only the elements and images move while the page itself remains static, creating an interesting visual effect. I tried to replicate this on my own websit ...

Text field value dynamically changes on key press update

I am currently working on the following code snippet: {% for item in app.session.get('aBasket') %} <input id="product_quantity_{{ item['product_id'] }}" class="form-control quantity" type="text" value="{{ item['product_quan ...

The ngbDatepicker within the Bootstrap angular framework is designed to seamlessly integrate with ngbPanelContent without causing overflow issues

I've integrated the ngbDatepicker into a form as shown below <ngb-panel> <ng-template ngbPanelTitle> <div class="row"> <ui-switch (change)="onChange($event,2)" [checked]="profession ...

Activate the Chrome Extension that allows you to open a link in a new tab with just a middle click or regular click, without closing the popup

When I try to click a link in my extension popup and open it in a new tab using "middle click -> open link in a new tab", the popup closes. Is there a way to keep the popup open so I can click on multiple links from my extension without interruption? A ...

Is there a way to use a specific keyboard input to alter the characteristics of shapes on my webpage?

Is there a way to change certain attributes of a shape onscreen when a specific key is pressed by the user? For example, can I make it so that pressing "a" changes the color of the shape? I attempted to modify a mouse rollover event to work with the desir ...

Pseudo-element fails to display in React when applied to a paragraph tag, even with display block property set

I am experiencing an issue where the pseudo element ::after is not appearing in my browser. I am currently working with React.js and Material UI's makeStyles. Here is the code snippet causing the problem: modalTitle: { borderBottom: '2px sol ...