Css tips: How to Use CSS to make menus horizontally

Source: Internet
Author: User

In the previous section, we talked about vertical navigation. however, most websites still use horizontal navigation, so you can learn how to implement horizontal navigation. how can we achieve the following results. you only need to avoid line breaks between the list items, so you need to convert <li> to inline.

The HTML and CSS codes are as follows:

Copy to ClipboardReference: [www.bkjia.com] <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Strict // EN"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>
<Html xmlns = "http://www.w3.org/1999/xhtml" lang = "en-US">
<Head>
<Title> Lists as navigation-bkjia.com </title>
<Meta http-equiv = "content-type"
Content = "text/html; charset = UTF-8"/>
<Link rel = "stylesheet" type = "text/css" href = "listnav_horiz.css"/>
</Head>
<Body>
<Div id = "navigation">
<Ul>
<Li> <a href = "#"> Recipes </a> </li>
<Li> <a href = "#"> Contact Us </a> </li>
<Li> <a href = "#"> Articles </a> </li>
<Li> <a href = "#"> Buy Online </a> </li>
</Ul>
</Div>
</Body>
</Html>

Body {
Padding: 1em;
}
# Navigation {
Font-size: 90%;
}
# Navigation ul {
List-style: none;
Margin: 0;
Padding: 0;
Padding-top: 1em;
}
# Navigation li {
Display: inline;
}
# Navigation a: link, # navigation a: visited {
Padding: 0.4em 1em 0.4em 1em;
Color: # FFFFFF;
Background-color: # B51032;
Text-decoration: none;
Border: 1px solid #711515;
}
# Navigation a: hover {
Color: # FFFFFF;
Background-color: #711515;
}

Analysis:

Create a div container and add a list to it:

Copy to ClipboardReference: [www.bkjia.com] <div id = "navigation">
<Ul>
<Li> <a href = "#"> Recipes </a> </li>
<Li> <a href = "#"> Contact Us </a> </li>
<Li> <a href = "#"> Articles </a> </li>
<Li> <a href = "#"> Buy Online </a> </li>
</Ul>
</Div>

Add some basic information in the # navigation container, such as the font size. in the CSS layout, this container may also add some additional information, such as: determine the location of the navigation on the page.

Copy to ClipboardReference: [www.bkjia.com] # navigation {
Font-size: 90%;
}

Next, in the <ul> object, we clear the default dots and the default indentation.

 

Copy to ClipboardReference: [www.bkjia.com] # navigation ul {
List-style: none;
Margin: 0;
Padding: 0;
Padding-top: 1em;
}

Now it is the most important part of horizontal navigation, because without this step, it is not called horizontal navigation. ^ _ ^. Set <li> as an inline element, thus avoiding line breaks.

Copy to ClipboardReference: [www.bkjia.com] # navigation li {
Display: inline;
}

Finally, add some effects to the navigation:

Copy to ClipboardReference: [www.bkjia.com] # navigation a: link, # navigation a: visited {
Padding: 0.4em 1em 0.4em 1em;
Color: # FFFFFF;
Background-color: # B51032;
Text-decoration: none;
Border: 1px solid #711515;
}
# Navigation a: hover {
Color: # FFFFFF;
Background-color: #711515;
}

PS: If you want to increase the distance between the text and the border, you need to set left padding and right padding. if you want to increase the distance between items, you must set left margin and right margin.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.