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 CSS Layout CSS Layout Techniques Positioning Elements

Jason Rich
seal-mask
.a{fill-rule:evenodd;}techdegree
Jason Rich
Front End Web Development Techdegree Student 10,048 Points

Logo's default relative position?

Isn't the default position of .logo relative to .card since .logo is a child of .card? I thought child elements were positioned relative to parent elements by default. Or am I missing something?

I understand absolute makes the position of .logo relative to the viewport but how does .logo know to use .card as the new relative reference simply by entering

.card {
  postion: relative;
}

Does position: absolute untether .logo element and then reinstating .card as relative element lasso the absolute element back into "relative" position? If that makes sense.

Or am I confusing the <div> element as being a parent when it is not?

index.html
<!DOCTYPE html>
<html>
<head>
    <title>CSS Positioning</title>
    <link href='https://fonts.googleapis.com/css?family=Varela+Round' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="page.css">
    <link rel="stylesheet" href="style.css">
</head>
    <body>
        <div class="banner">The best guide to the best city!</div>
        <div class="card">
            <img class="logo" src="city-logo.svg" alt="An illustration of a building">
            <h2>Best City Guide</h2>
            <p>Cheesecake oat cake marshmallow. Bonbon pastry apple pie danish donut bonbon marshmallow caramels. Bear claw chocolate bar lemon drops. Carrot cake jelly beans jelly beans pie.</p>
            <p>Candy sugar plum pudding marzipan cupcake toffee donut sweet roll. Sesame snaps cake liquorice halvah. Bonbon icing candy canes. Cookie powder topping chupa chups candy canes candy. Tart danish croissant macaroon biscuit. Cookie candy canes lemon drops jelly-o. Cheesecake gummi bears liquorice. Toffee bear claw cake gummi bears candy canes tiramisu pudding. Candy canes pie jelly beans jujubes marshmallow topping dessert marshmallow. Icing apple pie danish cake marzipan topping. Chocolate sweet roll candy canes tart croissant apple pie marshmallow ice cream. Jelly toffee halvah gingerbread bonbon sweet pie chocolate cake. Tart sweet roll pastry sweet roll.</p>
            <p>Cupcake biscuit chocolate cake. DragΓ©e gingerbread halvah marzipan bonbon donut chocolate cake wafer. Sweet roll bonbon jelly lollipop lollipop gummi bears chocolate cake halvah. Fruitcake biscuit jelly-o carrot cake. Brownie gingerbread gingerbread chocolate bar toffee tart jujubes icing. Lollipop cheesecake sweet roll lollipop cheesecake muffin chocolate bar carrot cake caramels. Liquorice cotton candy cheesecake macaroon. Tootsie roll marzipan chocolate jujubes jujubes.</p>
            <p>Carrot cake biscuit apple pie. Gummies cookie chocolate powder powder oat cake. Jujubes tootsie roll jelly beans biscuit marshmallow lemon drops cookie. Donut carrot cake fruitcake gummi bears danish marzipan cake cake chocolate cake. Biscuit dessert macaroon cupcake sweet muffin cake. Tootsie roll bear claw cheesecake bear claw lemon drops.</p>
        </div>
    </body>
</html>
style.css
/* Complete the challenge by writing CSS below */

.logo {
  position: absolute;
  top: -45px;
  left: 20px;
}
.card {
  position: relative;
}

1 Answer

Hi Jason,

Once you change the value of the CSS position property of .logo to absolute, the element is taken out of its normal flow and it’s position becomes relative to the viewport (if no parent element is positioned relatively).

Before then, you are correct that the position of the .logo element is (in a sense) relative to its parent element which is the element with a class of card. But in order to give an element top and left values, you must first specify its position value.

Jason Rich
seal-mask
.a{fill-rule:evenodd;}techdegree
Jason Rich
Front End Web Development Techdegree Student 10,048 Points

Brandon,

Thank you for the response. Your comment about the .logo element being "(in a sense) relative" cleared things up.

I was thinking that position: relative was the default but it's actually position: static. I found the following in the MDN Web Docs and it cleared things up a bit more.

If the position property is absolute, the containing block is formed by the edge of the adding box of the nearest ancestor element that has a position value other than static (fixed, absolute, relative, or sticky). --emphasis min

So, by giving .card the value of relative (rather than the default of static), the .logo now references it instead of the viewport. And, we need .logo to be absolute (instead of static "relative" to a static .card) in order to give it a top and left position, which is not possible while under static positioning.

.logo {
  position: absolute;
  top: -45px;
  left: 20px;
}
.card {
  position: relative;
}

I think I got it now. Thanks again.

Cheers!