Tony
Tony

Reputation: 3

display: inline not working even with float

I'm trying to create a horizontal nav bar using CSS and display: inline; but I'm having no joy with it. Using <ul><li>, I am getting a vertical list rather than a horizontal one. I've Googled the web and poured over SO but can't find a fix.

#menu ul {
  height: 15px;
  padding: 8px 0px;
  margin: 0px;
}
#menu li {
  text-decoration: none;
  display: inline-block;
  padding: 20px;
  float: left;
}
.logo {
  font-family: 'Mr De Haviland', cursive;
  font-size: 32px;
}
.logo a {
  color: black;
  text-decoration: none;
}
#nav a {
  color: black;
  text-decoration: none;
}
#nav {
  max-width: 600px;
  width: auto;
  height: 35px;
  font-size: 16px;
  font-family: Helvetica, Geneva, sans-serif;
  text-align: center;
  text-shadow: 3px 2px 3px #666666;
  background-color: #999999;
  border-radius: 8px;
  list-style-type: none;
}
<!DOCTYPE html>
<html lang="en-gb">

<head>
  <meta charset="utf-8">
  <link href='https://fonts.googleapis.com/css?family=Mr+De+Haviland' rel='stylesheet' type='text/css'>
  <link rel="stylesheet" type="text/css" href="index.css">
  <title>Anthony Jones</title>
</head>

<body>
  <div id="main">
    <div class="logo">
      <p align="center">
        <a class="logo" href="index.html">Anthony Jones</a>
      </p>
    </div>

    <div id="nav">
      <ul style="list-style-type: none;">
        <a href="about.html">
          <li>About</li>
        </a>
        <a href="cv.html">
          <li>Curriculum Vitae</li>
        </a>
        <a href="services.html">
          <li>Services</li>
        </a>
        <a href="random.html">
          <li>Random</li>
        </a>
        <a href="contact.html">
          <li>Contact Me</li>
        </a>
      </ul>
    </div>

  </div>
</body>

The ul style="list-style-type: none;" in the <style> tag is there because I couldn't remove the bullet points within the index.css file, this was the only solution I could find.

I've tried many variations on the code, rearranged it, deleted and started over and still no joy. Any ideas?

Upvotes: 0

Views: 1523

Answers (3)

UncaughtTypeError
UncaughtTypeError

Reputation: 8712

#nav li {
    display: inline-block;
}

Your list item was set to display as list-item, and not inline block.

These styles are doing nothing for you:

#menu li {
text-decoration: none;
display: inline-block;
padding: 20px;
float: left;
}

An element with the id #menuis not present in your mark-up.

.logo {
font-family: 'Mr De Haviland', cursive;
font-size: 32px;
}

.logo a {
color: black;
text-decoration: none;
}

#nav a {
color: black;
text-decoration: none;
}

#nav {
max-width: 600px;
width: auto;
height: 35px;
font-size: 16px;
font-family: Helvetica, Geneva, sans-serif;
text-align: center;
text-shadow: 3px 2px 3px #666666;
background-color: #999999;
border-radius: 8px;
list-style-type: none;
}

#nav li {
    display: inline-block;
}

#nav ul {
    padding: 0px;
}
<!DOCTYPE html>
<html lang="en-gb">
<head>
<meta charset="utf-8">
<link href='https://fonts.googleapis.com/css?family=Mr+De+Haviland' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="index.css">
<title>Anthony Jones</title>
</head>

<body>
<div id="main">
<div class="logo">
<p align="center">
<a class="logo" href="index.html">Anthony Jones</a>
</p>
</div>  

<div id="nav">
    <ul style="list-style-type: none;">
        <li><a href="about.html">About</a></li>
        <li><a href="cv.html">Curriculum Vitae</a></li>
        <li><a href="services.html">Services</a></li>
        <li><a href="random.html">Random</a></li>
        <li><a href="contact.html">Contact Me</a></li>
    </ul>
</div>

</div>
</body>

  1. Indent your mark-up properly so it's easier to read, and when necessary, to debug as well.
  2. Avoid wrapping block elements (like <li>) with inline elements (like <a>), this is invalid mark-up (see: Is it sound to wrap a list item in an anchor?)

Upvotes: 0

Mani
Mani

Reputation: 2655

use like this

<!DOCTYPE html>
<html lang="en-gb">
<head>
<meta charset="utf-8">
<link href='https://fonts.googleapis.com/css?family=Mr+De+Haviland' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="index.css">
<title>Anthony Jones</title>
<style>
#menu ul {
height: 15px;
padding: 8px 0px;
margin: 0px;
}

#nav li {
text-decoration: none;
display: inline;
padding: 20px;
float: left;
}

.logo {
font-family: 'Mr De Haviland', cursive;
font-size: 32px;
}

.logo a {
color: black;
text-decoration: none;
}

#nav a {
color: black;
text-decoration: none;
}

#nav {
max-width: 600px;
width: auto;
height: 35px;
font-size: 16px;
font-family: Helvetica, Geneva, sans-serif;
text-align: center;
text-shadow: 3px 2px 3px #666666;
background-color: #999999;
border-radius: 8px;
list-style-type: none;
}
</style>
</head>

<body>
<div id="main">
<div class="logo">
<p align="center">
<a class="logo" href="index.html">Anthony Jones</a>
</p>
</div>  

<div id="nav">
<ul style="list-style-type: none;">
<li><a href="about.html">About</a></li>
<li><a href="cv.html">Curriculum Vitae</a></li>
<li><a href="services.html">Services</a></li>
<li><a href="random.html">Random</a></li>
<li><a href="contact.html">Contact Me</a></li>
</ul>
</div>

</div>
</body>

Upvotes: 0

Sam Malayek
Sam Malayek

Reputation: 3745

1: you can't wrap an a tag around li. a tag must be inside li.

Like this:

<div id="nav">
<ul style="list-style-type: none;">
<li><a href="about.html">About</a></li>
<li><a href="cv.html">Curriculum Vitae</a></li>
<li><a href="services.html">Services</a></li>
<li><a href="random.html">Random</a></li>
<li><a href="contact.html">Contact Me</a></li>
</ul>
</div>

2: Set li to display: inline

Like this:

#nav li {
    display: inline;
}

Upvotes: 2

Related Questions