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 trialBecky Pate
1,667 PointsImage positioning in CSS
Hello, I have just taken the first module in the website basics and am now attempting to build my own site.
I am trying to put a holding page up on the domain I've just bought with a simple image centrally aligned with a coming soon link. I am have trouble aligning my image though.
I've tried using the grid.sss, in-line position and in the style.css.
I am a COMPLETE beginner so know this is a very basic question but if anyone could point me int he right direction as none of the online forums are making sense.
Thank you very much in advance.
Becky
9 Answers
James Barnett
39,199 PointsBecky Pate - Your CSS is working fine, in CSS it's text-align: center;
it uses the American spelling center instead of the British spelling of centre I noted you were using.
James Barnett
39,199 Points@Becky Pate - Here's a quick primer on how to type code on the forum.
Check out codepen it's was invented getting help issues with CSS on forums.
Don't know how to use it? No problem!
Here's a quick guide on how to use codepen
James Barnett
39,199 PointsDo you guys run any meetups for TeamTreehouse members?
There are some member run meetups going in various cities. In London in particular Treehouse sponsors the Untangle the Web meetup.
Becky Pate
1,667 PointsGreat I've signed up! Thanks for the link to codepen too :)
Jon Wood
9,884 PointsI don't know too much about CSS, but maybe this StackOverflow link will help?
And I'm sure more people will chime in that have much more experience in it than me.
James Barnett
39,199 PointsJonathan Wood - In general here on the forum if you don't have specific experience with the video/code challenge they are having issues with it is best to move on to another question that is more in your area. It generally results in less confusing for the newbies.
Thanks and rock on with your forum'ing
Andy Wickes
5,093 PointsHi Becky,
Have you got a link we can look at so we can try and diganose what solution you need?
Becky Pate
1,667 PointsHey, I don't have a link as it's just on my local.
My html looks like this though:
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Rebecca Pate</title> <link rel="stylesheet" href="normalize.css" type="text/css" media="screen"> <link rel="stylesheet" href="grid.css" type="text/css" media="screen"> <link href='http://fonts.googleapis.com/css?family=Raleway' rel='stylesheet' type='text/css'> <link rel="stylesheet" href="style.css" type="text/css" media="screen, projection"> </head> <body> <div class="container clearfix"> <h1>Rebecca Jane Pate</h1> <div class="box"> <img src="Rebecca.jpg" alt="Rebecca Pate" class="img"> </div>
<p>coming soon</p>
</div> </body>
</html>
and my style.css like this:
h1{ color: #0066FF; text-align: centre; }
h2 { color: #9966FF; text-align: centre; }
body { font-family: 'Raleway', sans-serif; color: #0066FF; background: #FFFFED; text-align: centre;
}
box { text-align: centre; }
I am wondering whether it's something to do with the grid.css. As I said I am complete beginner so really appreciate your help.
Do you guys run any meetups for TeamTreehouse members?
Becky
Becky Pate
1,667 Pointsoh that didn't work! How is best to share with you?
Andy Wickes
5,093 PointsHiya,
Looks like the image part of the HTML is missing here - can you copy and paste all the HTML, followed by all the CSS into the forum and I'll take a look!
No apologies needed about being a beginner! We're all on here because we're learning something!
Not sure about meetups as I've not looked into that side of things - I'm sure there will be some sort of social side of things though!
Cheers
Jon Wood
9,884 PointsNo worries. I would suggest having a GitHub account (I really like what they're doing) and you can definitely give links out to your code that way.
There are a few sites that let you share CSS/HTML/JavaScript such as CodePen (It seems the Treehouse instructors use this quite a bit), Plunkr, and JSFiddle. I'm sure there are quite a few more out there, as well.
James Barnett
39,199 PointsGithub repos is a terrible solution for sharing one off bits of code, however Github gists is pretty good for backend code that codepen doesn't support.
Becky Pate
1,667 PointsSo that's what I tried to do above (copy and paste the html) but here's my second attempt:
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Rebecca Pate</title> <link rel="stylesheet" href="normalize.css" type="text/css" media="screen"> <link rel="stylesheet" href="grid.css" type="text/css" media="screen"> <link href='http://fonts.googleapis.com/css?family=Raleway' rel='stylesheet' type='text/css'> <link rel="stylesheet" href="style.css" type="text/css" media="screen, projection"> </head> <body> <div class="container clearfix"> <h1>Rebecca Jane Pate</h1> <div class="box"> <img src="Rebecca.jpg" alt="Rebecca Pate" class="img"> </div>
<p>coming soon</p>
</div>
and the CSS
h1{ color: #0066FF; text-align: centre; }
h2 { color: #9966FF; text-align: centre; }
body { font-family: 'Raleway', sans-serif; color: #0066FF; background: #FFFFED; text-align: centre;
}
box { text-align: centre; }
Becky Pate
1,667 Pointsoh dear. I can't even seem to copy and paste! I created a github account but not sure how to use it...
Jon Wood
9,884 PointsAwesome. Actually, I believe the Git courses on here also use GitHub and the help on the site is very useful. We're all here to help, if needed, as well!
Andy Wickes
5,093 PointsHi Becky,
Thanks - from the HTML and the CSS you pasted in, I'm not seeing any reference to an image? And I believe your problem is that you are trying to align an image?
I would expect to see something in your HTML along the lines of:
<img src="file.jpg">
Or in your CSS
.header {background-image: (url='file.jpg'); }
Is there something missing from the HTML and CSS you pasted?
If I was doing it I'd probably add this in:
<div align="center"> <img src="IMAGE NAME HERE" alt="my image"> </div>
This ought to align the image centrally within the DIV.
Cheers
Andy Wickes
5,093 PointsAhhh, I think the forum is stripping out HTML - just seen that my answer above is incomplete!
What I was going to suggest you do is either:
<div align="center">
<img src="image goes here" alt="alt text goes here">
</div>
Andy Wickes
5,093 PointsOr alternatively you could do this:
HTML
<img class="center" src="image goes here" alt="alt text goes here">
CSS
img.center {
display: block;
margin-left: auto;
margin-right: auto
} ... <IMG class="displayed" src="..." alt="...">
Becky Pate
1,667 PointsHi Andy, thank you so much for your help. I tried both of these options and they both worked :D
Out of interest, which is more commonly used? Is it better practice to use the div or the class?
Thanks again, really appreciate it :)
Andy Wickes
5,093 PointsHi Becky,
You're welcome! The CSS solution is the more commonly used solution as it follows the 'web standards' doctrine of keeping markup (HTML) and styling (CSS) separate.
So the HTML calls the image, and the CSS does the alignment bit - the first solution achieves the same result , but by adding the align='center' bit into the HTML we are essentially combining both markup and styling in the HTML document, which isn't ideal!
Cheers
Andy
Becky Pate
1,667 PointsOK, got it, thanks. Super helpful :)
Becky Pate
1,667 PointsBecky Pate
1,667 PointsHey James, I guess changed the spelling and it worked! Thanks for your help!