user2860957
user2860957

Reputation: 476

absolute positioning is driving me nuts

can any body explain me the out put, which my code is genrating, as it driving me nuts, there is no syntax error, i am following the tutorial on you tube and i was able to genrate the right out put with this code, but today i decided that i will understood this code fully, and it driving me crazy

**Note: No syntax error, just looking for explanation about the out put, and please read the comment in the code

First look at the code html**

 <html>
        <head>
            <title>TODO supply a title</title>
            <link rel="stylesheet" href="menu.css"></link>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
        </head>
        <body>
        <label for="show-menu" class="show-menu">Show Menu</label>
        <input type="checkbox" id="show-menu" >

        <ul id="menu">
        <li ><a href="">Home</a></li>
    //This section is giving me trouble, please see below i explain my problem in detail there
        <li ><a href="">about</a>
        <ul class="hidden">
        <li ><a href="">who are we</a></li>
        <li ><a href="">what we do</a></li>
        </ul>
        </li>
        //This section is giving me trouble, please see below i explain my problem in detail there.
        <li ><a href="">portfolio</a>
        <ul class="hidden">
        <li ><a href="">photography</a></li>
        <li ><a href="">web &amp; interface design</a></li>
        <li ><a href="">illustration</a></li>
        </ul>
        </li>
        <li ><a href="">news</a></li>
        <li ><a href="">contacts</a></li>
        </ul>

        </body>
    </html>

and this is my css

 /*strip styling from the list*/

    ul{
        margin:0;
        padding:0;
        list-style-type:none;
        position:absolute;
    }


    li{
        display:inline-block;
        float:left;
        margin-right:1px;
    }
//only this section of the code is driving me crazy, and in the explanation i type this section again and again. Please see below in there i explain my problem in detail. 
    li a {
        display:block;
        }

Problem if you look at the out put in this link https://codepen.io/arif_suhail_123/pen/pwdYXp

i am confused about this-- look at the about section, i was expecting who we are and what we do, to appear as block element, as i gave li a {display:block} but they dont, they are appearing as inline block element or inline element, i am not sure,

but i get more confused when i see the next portion portfolio, as in there, all the li are appearing as block element, means photography, web & interface design and illustration appear on the next line, which i was accepting, after giving this style li a {display:block}

and after that i completly lost my mind when i added min-width property, see the link https://codepen.io/arif_suhail_123/pen/ZyaZEj i changed nothing, i only added in li a {} min-width, so my style is this li a {display:block; min-width:140px;}

i still have the problem, what we do appear under the portfolio, this problem i desribed already(in the last paragraph), but after adding min-width:140px; i have new problem; if you look at the out put, web & interface design appear under the news, first of all i did not expected it to appear there, and second of all if you read the html code web & interface design is second li means why it appearing in this order first li -- photography, than third li -- illustration and second li -- web & design, under the news??

Can any body please explain me, and last of all, what i understand about the absolute positioning is this, that it take the element out of normal flow, and put it back at the given position, i ran one example, and it confirm what i think may be i am wrong but have look https://codepen.io/arif_suhail_123/pen/LLOaXv on this link third box did not appear, as i was expecting it, not to appear, and about the block element, i understand that they suppose to appear on the new line.

see these two picture i think my question will become more clear. enter image description here

enter image description here

Upvotes: 1

Views: 71

Answers (3)

KyleS
KyleS

Reputation: 401

Absolute Positioning - Take the Width of Parent

Ok so I looked through your code and I made a small example out of it. I took out some things to make the example more clear (and because of width limitations in these posts).

Ok, first, look at the background colors I put on both of the <ul> lists. Your sub-lists are in red. Your main <ul> is in yellow.

Now you are correct in saying that position: absolute; takes the element out of the "flow" of other elements in order to display them as usual. Absolute positioning takes a lot of special attention.

Run the code and look at the words "Who are we". Now this makes you think, why does "photograpy" appear next to it? What happened to "what we do"? Its behind it. The reason this occurs is because both of those lists are positioned absolutely under their parent element. Without giving anything like top or left they are just going to overlap eachother and the latter ends up displaying ontop of the previous list. Absolutely positioned elements don't care what is next to them or if they overlap something. They go where ever you tell them to and thats typically what absolute positioning is for. You tell it to break away from normal flow, and you give it a specific location to appear.

Play around with the code, delete "photography" and "illustration" and run it again. You'll see that "what we do" was there all along, just behind it.

Also see Russell's Answer.

ul{
  margin:0;
  padding:0;
  list-style-type:none;
  position:absolute;
  background-color: yellow;
}

ul ul{
  display: block;
  padding: 5px;
  background-color: red;
}

#secondList{
  background-color: pink;
}

ul li{
  float:left;
  margin: 5px;
  width: 120px;
}

ul ul li a{
  display: block;
}
<ul id="menu">
  <li><a href="">Home</a></li>
  <li><a href="">about</a>
    <ul class="hidden">
      <li><a href="">who are we</a></li>
      <li><a href="">what we do</a></li>
    </ul>
  </li>

  <li ><a href="">portfolio</a>
    <ul class="hidden" id="secondList">
      <li><a href="">photography</a></li>
      <li><a href="">illustration</a></li>
    </ul>
  </li>
  <li><a href="">news</a></li>
</ul>

Upvotes: 1

Russell
Russell

Reputation: 496

i am confused about this-- look at the about section, i was expecting who we are and what we do, to appear as block element, as i gave li a {display:block} but they dont, they are appearing as inline block element or inline element, i am not sure,

They do appear as block elements. I think the 'problem' is that you have li declared as inline-block elements. So you have block elements in a container set to inline block which effectively makes them display as inline-block.

but i get more confused when i see the next portion portfolio, as in there, all the li are appearing as block element, means photography, web & interface design and illustration appear on the next line, which i was accepting, after giving this style

This is being displayed the same, but the pen you entered has some kind of arbitrary width assigned to it. I'm not sure if it's because of the viewport settings or not, but try shortening some of the link names and you'll see it's actually displaying them the same as the other list. IE inline-block just like was specified.

Can any body please explain me, and last of all, what i understand about the absolute positioning is this, that it take the element out of normal flow, and put it back at the given position

Absolute positioning takes the element out of normal flow and positions it relative to it first positioned parent. The browser does not set aside space for the element either.

https://developer.mozilla.org/en-US/docs/Web/CSS/position

Upvotes: 1

Chris
Chris

Reputation: 424

I'm not 100% sure if I understood the problem but I'll give it a shot:

position: absolute;

makes the element ignore every single element. That is what makes them on top of each others. It just displays whereever you tell it to display. Wich is in the top left corner by default. And that's also why there is an

z-index: ...;

the z-index indicates wich layer the element is displayed on, for example z-index: -10; makes it on layer -10 and an element with z-index -9 would display on top I think(pretty sure, otherwise its the opposite) there are infinite layers btw

Hope this is what you were looking for

Upvotes: 0

Related Questions