X.Feliers
X.Feliers

Reputation: 47

CSS - putting my nav bar horizontaly

I am a newbie to css/html and I am trying to create a portofolio website for myself.

I would like to horizontally center my nav_bar in my page just under my image but I can't seem to make it work.

enter image description here

As you can see, the nav_bar is currently aligned vertically.

This is my code:

.index_navigation li {
  overflow: hidden;
  text-align: center;
  float: center;
  padding-right: 20px;
}

.index_navigation a {
  font-family: arial;
  color: black;
  text-align: center;
  padding: 14 px 16 px;
  text-decoration: none;
}

.center {
  width: 50%;
  text-align: center;
  display: block;
  background-color: transparent;
  margin-left: auto;
  border: 1px solid transparent;
  margin-right: auto;
  margin-bottom: 1px;
  float: center;
}
<div class="background_logo">
  <img src="img/logo_size.jpg" alt="Background Logo" class="center">
  <nav class="index_navigation">
    <ul>
      <li><a href="contact.html">Contact</a></li>
      <li><a href="projects.html">Projects</a></li>
      <li><a href="about.html">About</a></li>
    </ul>
  </nav>
</div>

Hopefully someone can help me :)

Thanks in advance

Upvotes: 1

Views: 51

Answers (5)

itgirl1234
itgirl1234

Reputation: 749

I would just made li display: inline-block and ul for text-align: center.

ul {
  text-align: center
}
.index_navigation li {
  overflow: hidden;
  text-align: center;
  float: center;
  padding-right: 20px;
  display: inline-block;
}

.index_navigation a {
  font-family: arial;
  color: black;
  text-align: center;
  padding: 14 px 16 px;
  text-decoration: none;
}

.center {
  width: 50%;
  text-align: center;
  display: block;
  background-color: transparent;
  margin-left: auto;
  border: 1px solid transparent;
  margin-right: auto;
  margin-bottom: 1px;
  float: center;
}
<div class="background_logo">
  <img src="img/logo_size.jpg" alt="Background Logo" class="center">
  <nav class="index_navigation">
    <ul>
      <li><a href="contact.html">Contact</a></li>
      <li><a href="projects.html">Projects</a></li>
      <li><a href="about.html">About</a></li>
    </ul>
  </nav>
</div>

Upvotes: 0

Mikser
Mikser

Reputation: 989

I'd do it like this:

.background_logo NAV {
    float: right;
    position: relative;
    left: -50%;
}

.index_navigation {
    position: relative;
    left: 50%;
}

.clearfix {
    clear: both;
}

And then add the right clearfix DIVs:

<div class ="background_logo">
    <img src="img/logo_size.jpg" alt="Background Logo" class ="center" >
    <div class="clearfix"/> <!-- added clearfix -->
    <nav class="index_navigation">
        <ul>
            <li><a href="contact.html">Contact</a></li>
            <li><a href="projects.html">Projects</a></li>
            <li><a href="about.html">About</a></li>
        </ul>
    </nav>
</div>
<div class="clearfix"/> <!-- added clearfix -->

Upvotes: 0

user9946742
user9946742

Reputation:

You could restructure the menu using <table>. Replacing <ul> with <tr> & replacing <li> with <td>.

Upvotes: 0

Ali Sheikhpour
Ali Sheikhpour

Reputation: 11055

Set display style of li to inline-blocks and apply text-align:center to its parent.

.index_navigation{
    text-align:center;
}

.index_navigation li{
    overflow: hidden;
    text-align: center;
    padding-right: 20px;
    display:inline-block;
    }

    .index_navigation a {

    font-family: arial;
    color:black;
    text-align: center;
    padding: 14 px 16 px;
    text-decoration: none; 
    }

    .center{
    width:50%;
    text-align:center;
    display:block;
    background-color: transparent;
    margin-left:auto;
    border: 1px solid transparent;
    margin-right: auto ;
    margin-bottom: 1px;
    float:center;
    }
<div class ="background_logo">
     <img src="img/logo_size.jpg" alt="Background Logo" class ="center" >
     <nav class="index_navigation">
         <ul>
             <li><a href="contact.html">Contact</a></li>
             <li><a href="projects.html">Projects</a></li>
             <li><a href="about.html">About</a></li>
         </ul>
     </nav>
</div>

Upvotes: 0

לבני מלכה
לבני מלכה

Reputation: 16251

Use display:flex; to ul and justify-content: center; to center it

.index_navigation ul{
display:flex;
justify-content: center;
}
.index_navigation li{
overflow: hidden;
text-align: center;
float: center;
padding-right: 20px;
}

.index_navigation a {

font-family: arial;
color:black;
text-align: center;
    padding: 14px 16px;
text-decoration: none; 
}

.center{
width:50%;
text-align:center;
display:block;
background-color: transparent;
margin-left:auto;
border: 1px solid transparent;
margin-right: auto ;
margin-bottom: 1px;
float:center;
}
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Xander Feliers - Portfolio</title>
    <meta name="description" content="Portfolio - Xander Feliers">
    <link rel="stylesheet" href="css/screen.css">
</head>
<body>
        <div class ="background_logo">
             <img src="https://material.angular.io/assets/img/examples/shiba1.jpg" alt="Background Logo" class ="center" >
             <nav class="index_navigation">
                 <ul>
                     <li><a href="contact.html">Contact</a></li>
                     <li><a href="projects.html">Projects</a></li>
                     <li><a href="about.html">About</a></li>
                 </ul>
             </nav>
        </div>
</body>
</html>

Upvotes: 3

Related Questions