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
John Faught
3,484 PointsHelp in HTML/CSS
I am working on a website and want a bar at the top that has text in front of it. I have tried giving the H1 a background color, but that doesn't work. Any ideas? Thank!
33 Answers
John Satterfield
13,695 PointsAre you correctly calling your div in your html page? This is what you should have:
HTML Page:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<div id="topBar">Image and Text here</div>
</body>
</html>
CSS Page:
html, body {
margin:0;
padding:0;
}
#topBar {
width:100%; /*Fits To Width Of Page*/
position:fixed; /*Keeps Div At The Top Of The Page*/
height: 56px;
background-color:#2d3538;
z-index:100%; /*Keeps Div Above Other Content*/
color: #FFF;
padding:10px;
}
Hope this clears up any issues.
Kevin Korte
28,149 PointsI'm not exactly sure what you're after, but maybe this will give you an idea?
Yue Liu
1,878 PointsYou can try this: <h1 id="bar"> Some Text </h1> in html, and this: #bar {background:some color} in css. If this doesn't work you can leave another post telling me.
John Satterfield
13,695 PointsCan you give more details about what you are wanting?
John Faught
3,484 PointsI am wanting a bar kind of like what's at the op of the Treehouse website. It's a straight bar that goes across the page. I wanted to have a picture and a H1, placed inside of it. Hope this helps!
Guil Hernandez
Treehouse TeacherHey John Faught ,
My video on CSS Positioning covers an example of just that.
John Satterfield
13,695 PointsHope this helps John Faught ,
John Faught
3,484 PointsThank you all very much. I appreciate the help!
John Faught
3,484 PointsJohn Satterfield, I added what you had, because it was exactly what I was looking for! Only problem is the color on it does not work! Do you know why that may be? Thanks, John
John Satterfield
13,695 PointsJohn Faught I used the Hex color value twitter has for their background. To change and edit the background color of the box use the following -
background-color: #(HEX Color Value);
Keep in mind you do not need to use a hex value. You may also use RGBa and HSLa values as well. To help find the color you are looking for go to http://www.colorpicker.com/ which gives you all values for the color you choose. You can also use color names, W3 has a list of web safe color names which you can find here: http://www.w3schools.com/html/html_colornames.asp
If you need additional assistance or explanation please let me know.
Happy coding! :)
John Satterfield
13,695 PointsI re-read your reply and realized you might be talking about the text color and not the background color. The same rules apply except to edit the text color you would use the following:
color: #(HEX Color Value);
I used #FFF as this is the abbreviated color code for white.
John Faught
3,484 PointsThe background color is not there when I put it in my website. But if I put it in a new document it is. I was curios to to why the background doesn't show up. Thanks,
John Faught
3,484 PointsYes it is, I copied and pasted straight from codepen!
John Satterfield
13,695 PointsWith your CSS code, are you attaching a separate .css file? Or are you including it in your html page?
John Faught
3,484 PointsIt's separate. But it is linked.
John Satterfield
13,695 PointsIs there a way you can show me the exact code you have for both your html and css files? It might help if I see exactly how you have everything set up.
John Faught
3,484 PointsJames Barnett
39,199 PointsJohn Faught - Check out this thread from yesterday on the same issue.
John Satterfield
13,695 PointsJohn Faught It looks like you calling the div id of topBar as a class. The "#" symbol is used to define an ID, and the "." symbol is used to define a class. So to correct the problem, in your html page you would use :
<div ID="topBar">Content Here</div>
John Faught
3,484 PointsThank you guys! Issue has been solved!
John Faught
3,484 PointsAnother quick question... The bar covers up my other content? Is there a way to stop this? (Sorry I am such a pest.)
Kevin Korte
28,149 PointsJust put a top margin on the next highest element on your page, that should push everything down so the bar wont cover anything until the page is scrolled down.
John Faught
3,484 PointsI am using a Grid layout, when I do that it only moves one thing down, on top of another.
Kevin Korte
28,149 PointsDepending on your grid layout you might have to put everything in a wrapper and give that wrapper a top padding.
Here's a codepen to give you a basic idea:
James Barnett
39,199 PointsAssuming you markup looks something like this:
<div ID="topBar">
<!-- nav bar goes here -->
</div>
<p>
<!-- content goes here -->
</p>
Use an adjacent selector like this: #topBar + p { margin-top: 75px; }
John Satterfield
13,695 PointsJohn Faught In the code I gave you I put a
position: fixed;
z-index: 100;
This is causing your top bar to stay above the other content.. Remove those and you should be fine.
John Faught
3,484 PointsAlright! Thank you very much!
John Faught
3,484 PointsI have one more question and I'm done! How would I align the text in the bar vertically? I tried align-vertical or whatever it is and it didn't seem to work.
John Satterfield
13,695 PointsI personally would place each section of text/images/etc in its own div element and float them to the left so they are side by side... I believe inline-block would help get the correct design as well..
James Barnett
39,199 PointsJohn Faught - You can use display: inline-block with vertical-align: middle to help with centering the text.
John Faught
3,484 PointsWhat class would I put that in? I'm using a h1 inside a #topBar
James Barnett
39,199 PointsJohn Faught - Can you create a codepen so we can see the bigger picture of what's going on in your code.
John Faught
3,484 Pointshttp://codepen.io/Johnfaught/pen/IadBj I'm wanting the White text next to the image to be Centered.