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

Gabriel Ward
Gabriel Ward
20,222 Points

Hover effects and revealing text

I really like the hover effects that the images have on the homepage of this website:

http://wellmadestudio.com/

Does anyone have any suggestions as to how to go about doing this? I know a good place to start is CSS transitions and animations?

One thing I'm trying to work out is how the text that is revealed when the images are hovered over, could be positioned in the centre of the container.

Thanks for any help!

4 Answers

Hi Gabriel,

To horizontally centre text you would just do -

text-align: center;

To vertically centre text is a bit more tricky and there are a few routes you can take. Here's one solution using transform -

<div class="box">
  <p>Some text.</p>
</div>
.box {
    width: 960px;
    height: 60px;
    border: 1px solid gray;
    text-align: center;
}

.box p {
    position: relative;
    top: 50%;
    -webkit-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);
    margin: 0;
}

So working from mtch's codepen code, you would apply the styles of my '.box p' to the h1. That should work.

Hope that helps,

Ede

That's a cool approach!

Yup, I discovered it recently. It always seems more of a pain than it should be to vertically align.

I should give it a shot since I only used transform for animations.

Gabriel Ward
Gabriel Ward
20,222 Points

Wow thanks so much Ede and mtch!

Ede, I'm just a bit unsure, but where is the h1 you're referring to? I understand how mtch's code works. But I'm just taking a minute to get my head around how to add yours to it. I've added your pen for box p to mtch's text p. It's aligned on the Y axis, but not on the X. Where should the text-align go?

Hi!

I made you a quick codepen example of the text opacity transition.

http://codepen.io/mtch/pen/raGKry

 h1 {
  opacity: 0;
  transition: opacity .5s ease;
}
h1:hover {
  opacity: 1;
} 

You could always absolute position the text (put it in to a seperate div) inside the image and make it appear on the hover state, everything done with css. Just hold on a minute I should have it somewhere in my files since I used it on a website I did some time ago.

Gabriel Ward
Gabriel Ward
20,222 Points

Hi mtch,

Thanks for the great example! I'm just wondering, how would you position the text in the centre of an image? Something to do with relative and absolute positioning obviously?

Here you go, hopefuly it will do the trick.

http://codepen.io/mtch/pen/raGKry

Thank you Guil Hernandez :)

Hi Gabriel,

It sounds like you've added the code correctly for the vertical centering. The h1 I was referring to was in mtch's codepen code - but I've just checked now and he must have changed it to a p tag :)

I'm not sure what your code looks like now but add the text-align property to the text element you want to center and you should have no problem. Text-align follows the same pattern as in word-processing programs, so you can -

text-align: center;
text-align: left;
text-align: right;
text-align: justify;

You can also apply it to the parent element of something and it will work on all containing elements. Like in my last answer, I assigned it to the "box" class and it applied to the paragraph within the box, "box p".

Hope that clears things up,

Ede

Gabriel Ward
Gabriel Ward
20,222 Points

Hi Ede,

The text is centred on the Y axis, but it's aligned to the left on the X. Here's my code:

<div class="image">
    <div class="text">
        <p>text</p>
    </div>
</div>
body {
  margin: 0 auto;
  width: 500px;
}

.image {
    width: 150px;
    height: 150px;
    background: tomato;
    position: relative;
}

.text {
    width: 150px;
    height: 150px;
    background: deepskyblue;
    position: absolute;
    margin: auto;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    opacity: 0;
    transition: opacity 0.7s ease;
}

.text p {
    width: 50px;
    height: 50px;
    position: absolute;
    margin: auto;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    font-family: 'lato';
    text-align: center;
  position: relative;
    top: 50%;
    -webkit-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);
    margin: 0;
}

.text:hover {
    opacity: 1;
}

Hi Gabriel,

Hmm, I'll have a proper look at it in a bit and get back to you. You also have a bit of redundant code so I'll look at that too.

All the best,

Ede

Gabriel Ward
Gabriel Ward
20,222 Points

Ok cool thanks. I'm curious, how long have you been learning code for?

Excuse me guys for the sudden changes in code, I have swapped the h1 with an p tag because h1 tags are supposed to be of consistent appearance throughout the web page.

For future reading on that please see https://github.com/CSSLint/csslint/wiki/Disallow-qualified-headings

Gabriel, how about removing the position: relative, transform and margin: 0; ? You will notice that with my absolute centering trick both the text div and the nested p tag are centered.

But if you would like to go on with Ede trick, remove the absolute positioning, the top/bottom etc. properties and remove the margin: 0; so the margin will be set to auto.

Your code should look like this:

.text p {
    width: 50px;
    height: 50px;
    font-family: 'lato';
    text-align: center;
    position: relative;
    top: 50%;
    -webkit-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);
    margin: auto;
}

Hi Gabriel,

Looks like mtch got it to it before me, so hopefully that will sort it for you. Cheers mtch.

I've been learning code for about 2 years. I've found a really helpful thing is to get a practical application - volunteer to build a website for a charity, for example. The deadline, working to a brief and the 'live' context will speed you along no end and make you realise what needs to be learnt.

All the best,

Ede

Gabriel Ward
Gabriel Ward
20,222 Points

Ede and mtch,

Thank you so much.

There's one final thing here I'm a little unsure about. And that's why the position is 'relative' for the 'text p', and not absolute. I would've thought it should be absolutely positioned just as the 'text.' Or is it set to relative because 'text' has an 'absolute'?

Ede, thanks for the advice. I've only been coding since early November, so not too long really. I totally understand what you're saying. I'm currently working on a website for an online magazine that I want to start with some friends, and a couple of friend's bands websites. I'm finding that just really getting my head around one thing a day helps and adds up over a week.

How many hours would you code a day? Sorry if this is heaps, I'm just really interested in everyone's process. If there's one thing I find hard to gauge, it's if I'm picking this stuff up at something of a 'normal' rate or not, and also what's a reasonable expectation of how much coding can get done in a day.

Cheers for any further discussion :)

Hi Gabriel,

Feel free to use my positioning method as it does not require any vendor prefixing. And yes, most of the time absolute positioned elements should be nested inside relative positioned parents, I had a look how it would be like with an absolute positioned element instead of relative, but it would require some tweeking to center it properly I am afraid.

Hi Gabriel,

No problem! Discussion is a good thing. And your magazine project sounds great. I myself was thinking about starting some sort of blog too. I'd love to see any works-in-progress or the final thing once it's out there :)

How many hours a day I spend coding? I spend quite a lot between designing, coding and learning. About 5 and upwards. I would resist the temptation to be hard on yourself about filling the 'correct quota of hours' though. As long as you are progressing and repeating bits that you have learnt so nothing is forgotten, it will just build up over time naturally, like you said.

All the best,

Ede

Gabriel Ward
Gabriel Ward
20,222 Points

Hi mtch and Ede,

mtch, I'm definitely using your positioning method. It helped me no end in refreshing what I learnt from the CSS Foundations course when I started a couple of months ago. I'm just wanting to get my head around alternative methods and improve. I'd be interested to here what your process is as well :)

Ede, I currently have a basic page I'm working on. Can send you a screenshot if you're interested. I spend a good 6 hours a day between reading, watching videos, and at the moment messing around with the layout/design of this page I'm working on. I must admit I do find it difficult not to be hard on myself. I feel like I'm not picking this stuff up very quickly. I guess it's important to be able to put in some good work everyday and then be able to relax once you've put in a few hours.