9

Hi there and thanks for your help. I have a div (180px to 75px) in which I need to place 3 paragraphes and an image. Now I need to place those elements in all of the divs corners. It should look something like this -> (I'm not allowed to post pictures yet. I hope you'll understand it anyway.)

This is what the div should look like (every color is an element), but I can't seem to get the description to the right.

http://i.imgur.com/bE87euS.png

But no matter how I play around with the "display: inline-block" or the "float" I can't get it to work.

I hope someon of you can give me the answer?

<div style="width:180px; height: 75px; background-color: green;" id="achievement">
    <div>
      <p style="display: inline-block; margin: 0px" id="title">Title Title Title</p>
      <p style="margin:0px; float:right;" id="exp">500 exp</p>
    </div>
    <div>
      <img style="padding-left: 10px;" id="img"width="50" height="50" src="image.png"/>
      <p style="float:right; margin: 0px;" id="desc">Bla Bla Bla this is a description</p>
    </div>
</div>

(I use an extern css file of course. I just used the style tag to make it easier for you to understand.)

TylerH
  • 20,816
  • 57
  • 73
  • 92
Lumnezia
  • 777
  • 2
  • 8
  • 26

3 Answers3

20

Use position:relative on the parent container to establish a positioning context. Then use position:absolute on all the children to put them in the appropriate corners.

#parent {
    position:relative;
    border:3px solid blue;
    height:300px;
    width:500px;
    padding:0;
}
p {
    position:absolute;
    border:2px solid;
    margin:0;
    padding:5px;
}
p:nth-child(1) {
    border-color:green;
    top:0;
    left:0;    
}
p:nth-child(2) {
    border-color:red;
    top:0;
    right:0;
}
p:nth-child(3) {
    border-color:yellow;
    bottom:0;
    left:0;
}
p:nth-child(4) {
    border-color:pink;
    bottom:0;
    right:0;
}
<div id="parent">
    <p>First</p>
    <p>Second</p>
    <p>Third</p>
    <p>Fourth</p>
</div>    

Sample implementation here

TylerH
  • 20,816
  • 57
  • 73
  • 92
Niels Keurentjes
  • 39,856
  • 8
  • 91
  • 133
  • Works great too. Thanks for your time. :) – Lumnezia Jun 11 '13 at 22:06
  • It's also a hopelessly better implementation than depending on `text-align` so I sure hope you'll reconsider which one to implement :X – Niels Keurentjes Jun 11 '13 at 22:07
  • I'll sure take a look at that. That is AFTER I went to sleep :) – Lumnezia Jun 11 '13 at 22:07
  • 1
    @NielsKeurentjes I agree completely with you. Mine was just sort of a quick-fix ;) – Nilzone- Jun 11 '13 at 22:08
  • The main point is that this implementation is not in any way dependent on the contents of the sub-elements for positioning. The bottom elements are **always** in the corner, because your CSS **explicitly** placed them there. They cannot escape it. – Niels Keurentjes Jun 11 '13 at 22:08
  • @NielsKeurentjes Your JSFiddle looks great at first, but as soon as you write a bigger text in the fourth element. It will go over ther third element. :( – Lumnezia Jun 12 '13 at 07:25
  • Add a max-width of 50% to the elements to force linebreaks at proper position :) since the parent is fixed size every solution will have its 'drawbacks' logically. – Niels Keurentjes Jun 12 '13 at 07:55
  • I'm trying to complete the entire positions (top center / bottom center / center left etc') => but don't have much success with position: absolute. anyone have an idea how can i position the text for all the options? (9 options total). – Kosem Nov 18 '18 at 19:12
2

Use the text-align:right That did the trick for me anyway.

http://jsfiddle.net/Neaw7/

Nilzone-
  • 2,736
  • 6
  • 33
  • 70
  • Thank you for the REALLY fast answer but sadly it doesn't seem to work like that :( The Description is placed under the div even if I change the display and the float parameters. http://i.imgur.com/DLwFAIi.png – Lumnezia Jun 11 '13 at 22:00
  • I think that's just because your width is to small. Set it to something bigger. – Nilzone- Jun 11 '13 at 22:03
  • Oh... Nevermind, seems like I made a mistake adapting your answer. Works now. Thanks a lot – Lumnezia Jun 11 '13 at 22:04
0

If you are using boostrap5 you can achieve the goal using the class position-relative on the parent, and position-absolute top-0 end-0 or similar on the child, see https://getbootstrap.com/docs/5.1/utilities/position/#position-values .

If you are using boostrap4, you need to explicitely add top:0; left:0; or similar to the style of the element, since top-0 is not a class designation in bootstrap4.

am70
  • 531
  • 6
  • 8
  • this said, the answer by @niels-keurentjes is what really helped me fix the problem in boostrap4 – am70 Mar 21 '22 at 08:49