Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aligning div right next to the list items

Tags:

I am trying to create div elements and print items on to them dynamically. I've created a demo to show where I've reached.

The issue with my code is that it doesn't show up right next to the list where I want it. Instead it is displayed at the bottom. Is it possible to show the new div right next to the element that I'm hovering over?

$(".bubble").live({     mouseenter : function() {         $("#bubble").show();         $("#bubble").html($(this).attr('id'));     },     mouseleave : function() {         $("#bubble").empty();     } }); 
#bubble{     width:100px;     height:20px;     background-color:#666666;     position:absolute;     display:hidden; } 
<ul>     <li><span class="bubble" id="test1">test1</span></li>     <li><span class="bubble" id="test2">test2</span></li>     <li><span class="bubble" id="test3">test3</span></li>     <li><span class="bubble" id="test4">test4</span></li>     <li><span class="bubble" id="test5">test5</span></li> </ul> <div id="bubble"></div> 
like image 625
user1371896 Avatar asked Dec 19 '12 12:12

user1371896


People also ask

How do I align a div to the right side?

Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side. float:right; This property is used for those elements(div) that will float on right side.

How do I align a div element next to each other?

With CSS properties, you can easily put two <div> next to each other in HTML. Use the CSS property float to achieve this. With that, add height:100px and set margin.

How do you align items side by side in HTML?

The most common way to place two divs side by side is by using inline-block css property. The inline-block property on the parent placed the two divs side by side and as this is inline-block the text-align feature worked here just like an inline element does.

How do I make a div align?

You can do this by setting the display property to "flex." Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.


2 Answers

You need to set the position of #bubble relative to the li which is being moused over. Try this:

$("ul").on('hover', '.bubble', function(e) {     if (e.type == 'mouseenter') {         var $el = $(this);         $("#bubble")             .html($el.attr('id'))             .css({                 top: $el.offset().top,                 left: $el.offset().left + $el.width()             })             .show();     }     else {         $("#bubble").empty();     } }); 

Example fiddle

Note that I have removed the use of live() as it has been deprecated, and used on() with a delegate instead. Also I used the hover event.

like image 100
Rory McCrossan Avatar answered Nov 29 '22 08:11

Rory McCrossan


What about a pure CSS solution?

HTML:

<ul> <li>     <span class="bubble" id="test1">test1</span>     <div>test1</div> </li> <li>     <span class="bubble" id="test2">test2</span>     <div>test2</div> </li> <li>     <span class="bubble" id="test3">test3</span>     <div>test3</div> </li> <li>     <span class="bubble" id="test4">test4</span>     <div>test4</div> </li> <li>     <span class="bubble" id="test5">test5</span>     <div>test5</div> </li> </ul> 

CSS:

ul li div {     width: 100px;     height: 10px;     background-color: #666666;     position: absolute;       display: none; }  ul li:hover div {     display: inline;    } 

JSFiddle: http://jsfiddle.net/H2ZMc/

like image 27
mkey Avatar answered Nov 29 '22 09:11

mkey