Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Tracy Excell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tracy Excell
Front End Web Development Techdegree Graduate 15,333 Points

aligning a tag titles in a border

Hi,

I am trying to align my a tags heading "Home etc" for a navigation bar, inside a white border, however the text is showing to the left hand size of the border and I can not get it to align in the center of the border. Can anyone see what I am doing wrong?

Nav css

.main-header, .nav { display: flex;
flex-direction: column; align-self: center; }

.main-header h1 { align-self: center; }

.nav li{ align-self: center; }

.nav li a{ align-self: center; }

/* main header and navigation styles*/ - base css

.nav li{ list-style-type: none; display: block; border: solid 2px white; background-color: white; padding: 5px; margin-bottom: 10px; width: 70vw; margin-left: -10%;

} .nav li a{ text-decoration:none; color: black; text-align: center; margin: auto;

}

HTML

<div id="wrapper"> <div id="header_info"> <header class="main-header"> <h1>Tracy Excell</h1> <ul class="nav"> <li><a id="home">Home</a></li> <li><a href="#portfolio">Portfolio</a></li> <li><a href="#contact">Contact</a></li> </ul>

3 Answers

Erik Nuber
Erik Nuber
20,629 Points

This is a great site for all things flex, it shows not just talks about how to do everything flex.

https://css-tricks.com/snippets/css/a-guide-to-flexbox/

your text should just need a text-align: center but, you have to be sure you are targeting the correct item. Also, it might help instead of margins: auto to say margins: 0 auto; as this will take care of the left and right side.

Also, it appears you are trying to create a flex column instead of a row. Because of this when you then say align-self: center, it isn't actually doing anything because it isn't a row. Align-self aligns items that are in a row so saying align-self: flex-start would align things to the top, align-self: flex-end would align to the bottom and align-self: center aligns things in the middle. This has nothing to do with the text however.

.main-header, .nav { 
display: flex;
flex-direction: column; 
align-self: center; 
}

.main-header h1, .nav li,
.nav li a { align-self: center; }

/* main header and navigation styles- base css*/ 

.nav li { 
list-style-type: none; 
display: block; 
border: solid 2px white; 
background-color: white; 
padding: 5px; 
margin-bottom: 10px; 
width: 70vw; 
margin-left: -10%;

} 

.nav li a{ 
text-decoration:none; 
color: black; 
text-align: center; 
margin: 0 auto;

}

Just have to check as well, this looks like it may not be correct or complete.

there is a <nav> tag so your unorderlist should be within a nav tag instead of calling your ul class nav. Your href tags should be pointing to the pages you are building.

The wrapper might be causing you an issue, I would put the wrapper div below the header div if you want the header to go across the screen. The wrapper will generally be used to give a max width to the body itself. It is unclear why you would need the div tag for header info as I think you are trying to do to many different id and classes when you can target things without all of that. Also, none of your tags have closing tags. So have to assume you are just showing a snippet of code here. The div and header tags don't have a closing tag.

<div id="wrapper">
<div id="header_info">
<header class="main-header"> 
<h1>Tracy Excell</h1>
 <ul class="nav">
      <li><a id="home">Home</a></li>
     <li><a href="#portfolio">Portfolio</a></li>
     <li><a href="#contact">Contact</a></li>
 </ul>

clean it up to something like this...

         <header>
            <a href="index.html">
                <h1>Tracy Excell</h1>
            </a>
            <nav class = 'container'>
                <ul>
                    <li><a href="index.html" class="selected">Home</a></li>
                    <li><a href="portfolio.html">Portfolio</a></li>
                    <li><a href="contact.html">Contact</a></li>
                </ul>
            </nav>
        </header>

        <div id="wrapper">

        </div>

so css could be done like this

.container {
   display: flex;
  flex-direction: column;
  flex-wrap: nowrap;   //your choice based on how you want it to wrap or not may not matter with a column lay out
  justify-content: center //this may not be necessary for a column, if you wanted your nav as a row then you need to pick
  align-content: center //has no effect when only one item on a line so may not be useful for a column nav
}

nav {
   text-align: center;   //will align your text to center
   padding: 10px 0;  // will give some padding on whole nav bar
    margin: 15px 0;  // will give some space between top and bottom of the actual nav bar
   font-size: 1.55em; //you can give your font a fixed or relative unit
  background: #000; //give your nav color  
  color: #fff;  //give text color
}

nav ul {
  list-style: none;   //will remove any unwanted styles 
  margin: 0 20px;  //this is if you want to target margins of actual unordered list, this is the box inside the nav box.
}

nav li {
  padding: .15em   //this gives the individual list items a bit of padding
  margin: .20em // this gives space between individual nave items
}

nav a {
    padding: 5px 30px; //this gives the actual words within your nav a larger clickable area which is important.
}
Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

Without really looking into details...you have a lot of centering and left margins. Do you have a float right anywhere?

Tracy Excell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tracy Excell
Front End Web Development Techdegree Graduate 15,333 Points

Hi thanks for your help. I am trying to use the flex property to make a responsive site and am working on the mobile size first. I can't get the nav links to position quite right. Any ideas appreciated.