I'm working on creating tooltips for some content with jQuery and CSS. It works as it should in that the tooltip appears on mouseenter and disappears on mouseleave. My problem lies within my CSS layout. When the tooltip is rendered to the page, it is constricted to the height of its parent:
html:
<div id="allInvTable">
<div class="invItmCont" style="border-bottom:1px solid white;">
<div class="invItmItm">An Item</div>
<div class="invItmStats" style="display:none;">
<div style="clear:both;">
...Some content here...
</div>
<span style="display: none; top: -90px;">
<div style="clear:both;">
...The same content placed here via jquery to display as the tooltip...
</div>
</span>
</div>
</div>
</div>
css:
#allInvTable {
overflow:auto;
}
.invItmCont {
text-decoration:none;
display:block;
float:left;
padding:0 15px;
text-align:center;
position:relative;
clear: both;
}
.invItmCont span {
background-color: #000;
border: 5px solid #826217;
border-radius:15px;
-moz-border-radius:15px;
-o-border-radius:15px;
-webkit-border-radius:15px;
-moz-box-shadow: 2px 2px 2px #000;
-webkit-box-shadow: 2px 2px 2px #000;
box-shadow: 2px 2px 2px #000;
width:200px;
height:auto;
position:absolute;
display:none;
top:-90px;
color:#fff;
left:-37px;
}
Just for reference, the jquery:
<script>
$("#allInvTable div.invItmCont").append("<span></span>");
$("#allInvTable div.invItmCont").hover(function() {
$(this).find("span").animate({opacity:"show", top: "-70"}, "slow");
var itmStat = $(".invItmStats", this).html();
$(this).find("span").html(itmStat);
},
function() {
$(this).find("span").animate({opacity:"hide", top: "-90"}, "fast");
});
</script>
I know that it has to do with the overflow:auto; on #allInvTable because when i remove that attribute, it renders correctly, but the items flow out of their container. How do I fix this?
Um, I know this doesn't answer your question directly, but have you looked at existing tooltip libraries like Twitter's Bootstrap for example: http://twitter.github.com/bootstrap/javascript.html#tooltips
The reason I say this is because, I would rather spend time working on the core of my app rather than try and re-invent the wheel. Unless, you are indeed trying to learn the process of creating the wheel to begin with. Which is also good by the way. You learn a lot that way too.
One of the things you can do is just swap the "tooltip" content into the place you want to display it (that already has the visibility and formatting you want).
This approach treats a display:none element like a content stash.
$("#allInvTable div.invItmCont")
.hover(function() {
var outgoing = $(".invItmItm", this).html();
var incoming = $(".invItmStats", this).html();
$(".invItmItm", this).html(incoming);
$(".invItmStats", this).html(outgoing);
}, function() {
var outgoing = $(".invItmItm", this).html();
var incoming = $(".invItmStats", this).html();
$(".invItmItm", this).html(incoming);
$(".invItmStats", this).html(outgoing);
}
);
See: http://jsfiddle.net/zatz/mgtKD/6/
Alternatively, you can start futzing with z-layers which should be enough of a bad experience that you are ultimately driven to use/adapt one of the existing libraries.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Could anyone help me by describing how etc. I do to makes for a function as illustrated.
What I want is that when I mouse over a product box (have not fixed height),
I want to get a box with the buy button, etc. that looks like the picture.
Know that I do not put up the code or, but I do not know where to begin.
So if anyone has any tips or so, I'd be grateful!
Try
button {
display: none;
}
li:hover > button {
display: block;
}
<ul>
<li>Description 1<button>Buy</button></li>
<li>Description 2<button>Buy</button></li>
</ul>
The idea here is to use the > operator to tell CSS to change something in our target. The target being the Buy button inside the li tag.
http://jsfiddle.net/beautifulcoder/kj2XA/
1) First of all: make your items fixed size. This prevents later issues (in layout) and allows you to create effect you described. Like:
HTML (not complete):
<div class="item-wrapper">
<div class="item-content">
<!-- item images etc here -->
</div>
<div class="item-actions">
<button class="buy">Buy</button>
</div>
</div>
CSS:
.item-wrapper {
width: 200px;
overflow: visible;
float: left;
background: #999999;
margin: 5px;
position: relative;
border: 2px solid #fff; /* without this you have unwanted size effects on hover*/
}
.item-content {
width: 200px;
height: 300px;
}
.item-actions {
display: none;
position: absolute;
background: #888;
top:300px;
z-index: 10;
left: 0px;
width: 200px;
height: 100px;
}
2) create javascript with jquery for your items like:
$('.item-wrapper').hover(function () {
// Change css on hover .. this could be done also by changing class
$(this).css({'border':'2px solid #880088'});
$(this).find(".item-actions").slideDown("fast");
}, function(){
$(this).css({'border':'2px solid #fff'});
$(this).find(".item-actions").slideUp("fast");
});
Here is fiddle: http://jsfiddle.net/h23mY/
This is also nice effect: http://jsfiddle.net/ww53e/
lets say the item is enclosed by div tag, now use css hover on
<script>
//on document load item1-buy.hide(); dont forget to use jquery
</script>
<div id="item1">
//item goes here.
<input type="submit" id="item1-buy" value="Buy">
</div>
css:
#item1:hover {
//here you can style how ever you want. Add orange border and so on...
}
now on hover unhide the buy button using jquery #item1-buy.show();
Check the DEMO
I've made a simple markup to show you the idea:
<div class="item">
<img src="http://lorempixel.com/200/200/" alt="" />
<span>15$</span>
<div class="buy">BUY</div>
</div>
And the CSS:
.item {
float: left;
padding: 10px;
border: 1px solid #ccc;
margin: 10px 10px 0 0;
}
span{
display: block;
}
.buy {
padding: 5px;
background: green;
display: none;
}
.item:hover {
border: 1px solid yellow;
}
.item:hover .buy {
display: inline-block;
}
Update: still an issue with the last image in a row, but hope it helps: DEMO 2
<div class="item">
<img src="http://lorempixel.com/200/200/" alt="" />
<span>15$</span>
<div class="buy">
<span class="button">BUY</span>
</div>
</div>
Im using reference from here : http://www.cssnewbie.com/example/showhide-content/ and confused with the javascript
I want if I click see more, the see more wont become invisible, only activate the link
PS: I tried this at jsfiddle but dunno why didnt working, here it is: http://jsfiddle.net/cfp35/
The code:
<a href="#" id="example-show" class="showLink" onclick="showHide('example');return false;">
home
</a>
<div id="example" class="more">
<p>Congratulations! You've found the magic hidden text! Clicking the link below will hide this content again.</p>
<p>Hide this content.</p>
</div>
CSS:
.more {
display: none;
border-top: 1px solid #666;
border-bottom: 1px solid #666;
}
It's simple. If #shID element is not displayed, let it display.
No change in the buttom at all.
function showHide(shID) {
if (document.getElementById(shID)) {
if ((document.getElementById(shID).style.display == 'none')||(document.getElementById(shID).style.display == '')) {
document.getElementById(shID).style.display = 'block';
}
else {
document.getElementById(shID).style.display = 'none';
}
}
}
The company I work for has been using Flying Saucer for a while. Rencently, they have changed the header format to a complex one that looks like this:
THE COMPANY NAME
_____________________________________________________________________________
This is the name of the article | Date:04/17/2013
_____________________________________________________________________________
Regrettfully, I can't get it to look like above. THe closes I can get it to look like is where the top line is shorter then the bottom line:
THE COMPANY NAME
____________________________________________________________________
This is the name of the article | Date:04/17/2013
_____________________________________________________________________________
Here is the code:
#page {
size:letter;
margin-top: 0.8in;
margin-bottom: 0.5in;
padding: 5px;
border-top:#90aac5 solid 1px;
#top-left {content: element(leftHeader);}
#top-center{content: element(centerHeader);}
#top-right {content: element(rightHeader);}
}
#page-thisheader-center {
position:running(centerHeader);
border-bottom:#90aac5 1px solid;
font-size:9px;
height:52px;
color:#7e7f7f;
padding:2px;
}
#page-header-right {
position:running(rightHeader);
border-top:#90aac5 1px solid;
height: 25px;
font-size:9px;
color:#7e7f7f;
white-space:nowrap;
float:right;
padding-top:5px;
}
#page-header-left {
position:running(leftHeader);
border-top:#90aac5 1px solid;
height: 25px;
font-size:9px;
color:#7e7f7f;
float:left;
padding-top:5px;
}
.date {
height: 15px;
border-left: #90aac5 solid 1px;
float:right;
width:75px;
The tag looks like this for ids and classes above:
<div id ="page-header-left" align="left">
<div> <xsl:call-template name="get.title"/></div>
</div>
<div id ="page-header-right" align="right">
<div class="date"> <xsl:call-template name="get.date"/></div>
</div>
<div id ="page-thisheader-center">
<div> <xsl:call-template name="get.company.name"/></div>
</div>
I think that is all of it. I hope someone can help. I am totally stumped on how to correct the top line. Thanks!!
UPDATE
The short line is due to the longer titles wrapping. Is there a way to fix the #top-left, #top-center and #top-right margins to a fixed width so the title can wrap without causing the whole header to slide smaller/bigger depending on the title? BTW: I have tried whitespace: nowrap; on the #top-left margin but that just causes the whole header to slide off the right hand side of the page with long titles.
I hope this helps for a solution. Thanks in advance!!
I don't know if it's possible to do what you want using #top-left, #top-center and #top-right. When i try your sample code, I get something like this :
THE COMPANY NAME
____________________________________________________________________
_____ ____
title date
One solution to get what you want would be to use a single #top-center header, containing the three parts of the header.
The #top-center component will span on the full width of the page, and you can layout your
three divs inside it.
For example :
#page {
size: letter;
margin-top: 0.8in;
margin-bottom: 0.5in;
#top-center {content: element(centerHeader);}
}
#page-thisheader-center {
position: running(centerHeader);
}
#company {
border-bottom: #90aac5 1px solid;
text-align:center;
}
#line2 {
border-bottom: #90aac5 1px solid;
}
#article{
float:left;
}
#date {
border-left: #90aac5 solid 1px;
margin-left: 6in;
}
And :
<div id="page-thisheader-center">
<div id="company">THE COMPANY NAME</div>
<div id="line2">
<div id="article">Name of the article</div>
<div id="date">Date : 04/07/2013</div>
</div>
</div>
I'm trying to get two Divs to sit side by side. I want one div to take up as much width as is needed and the other to to take up the remaining width so both divs span 100% width. Is this possible? I've tried floating and a bunch of different positioning settings but I can't find a solution that works. I naturally thought that adding a float: left to the left most element would work, however when you try to add padding/margin/border to the right element the browser wont apply it. Here is some code that I've extended (from an existing answer) to illustrate the problem.
<style>
#foo {
float: left;
background: red;
height: 100%;
padding: 5px;
}
#bar {
background: green;
border: solid 1px blue;
padding: 5px;
height: 100%;
}
</style>
<div>
<div id="foo">foo</div>
<div id="bar">bar</div>
</div>
If you open this code up in a browser you'll notice that the bar div isn't padded, and the border isn't applied to it... I have no idea why.
Thanks for any help.
This works:
<style>
#foo {
float: left;
background: red;
}
#bar {
background: green;
}
</style>
<div>
<div id="foo">foo</div>
<div id="bar">bar</div>
</div>
http://pastehtml.com/view/19ldeqq.html
why not use a table, set the whole table width to 100% and then each of the rows without a width, like so:
<table width="100%" border="0">
<tr>
<td><div>DIV INFO LEFT</div></td>
<td><div>DIV INFO RIGHT</div></td>
</tr>
</table>
This is using javascript, but it is the only way I have found to do this.
<script type="text/javascript">
window.onload = shouldNotBeThisHard;
window.onresize = shouldNotBeThisHard;
function shouldNotBeThisHard() {
var j = document.getElementById('divThatYouWantAllOf');
var k = document.getElementById('divForRestOfScreen');
var jh = 0;
if (j)
jh = j.clientWidth;
var h = (window.innerWidth - (jh + 30));
k.style.width= h + "px";
}
</script>
with the HTML looking like this:
<div id="divForRestOfScreen" style="float:left;overflow:auto;">
asdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsd
sdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsd
sdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsds
dsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsd
sdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsd
</div>
<div id="divThatYouWantAllOf" style="float:right" >
This is why I hate web programming.
</div>
I have 2 nested divs inside outer one, which has width:100%. Both nested divs should be in one line and first should get it size from it's contents:
<div id="#outer" style="width:100%; border:1px">
<div id="#inner1" style="border:1px; display:inline">
inner div 1. Some text...
</div>
<div id="#inner2" style="width:100%????; border:1px; display:inline">
inner div 2...
</div>
</div>
Question is how to make #inner2 div to get rest of the horizontal space if width of the #inner1 div is not specified and depends on what it is inside?
P.S. All styles are in separate classes in my case, here I putted CSS into style attributes just for simplification.
I want result to work in IE7+ and FF 3.6
In more details for me it looks like this:
<style type="text/css">
.captionText
{
float:left;
}
.captionLine
{
height: 1px;
background-color:black;
margin: 0px;
margin-left: 5px;
margin-top: 5px;
border: 0px;
padding: 0px;
padding-top: 1px;
}
</style>
<table style="width:300px;">
<caption width="100%">
<div class="captionText">Some text</div>
<div class="captionLine"> </div>
</caption>
<tr>
<td>something</td>
</tr>
</table>
Here is the image of what I want:
The mysterious overflow: hidden; is your friend here. It stops elements adjacent to floats from extending behind the float — I think that’s the layout you’re looking for.
Here’s some slightly edited HTML: I don’t think you can have # characters in your ids:
<div id="outer">
<div id="inner1">
inner div 1. Some text...
</div>
<div id="inner2">
inner div 2...
</div>
</div>
And here’s the CSS to achieve the layout you want.
(I put in additional CSS for IE 6 with HTML conditional comments. I just noticed you didn’t actually need it to work in IE 6 too, but if you fancy being nice to the IE 6 users out there...)
<style type="text/css">
#outer {
overflow: hidden;/* Makes #outer contain its floated children */
width: 100%;
/* Colours and borders for illustration purposes */
border: solid 3px #666;
background: #ddd;
}
#inner1 {
float: left;/* Make this div as wide as its contents */
/* Colours and borders for illustration purposes */
border: solid 3px #c00;
background: #fdd;
}
#inner2 {
overflow: hidden;/* Make this div take up the rest of the horizontal space, and no more */
/* Colours and borders for illustration purposes */
border: solid 3px #00c;
background: #ddf;
}
</style>
<!--[if lte IE 6]>
<style type="text/css">
#inner2 {
zoom: 1;/* Make this div take up the rest of the horizontal space, and no more, in IE 6 */
}
#inner1 {
margin-right: -3px;/* Fix the 3-pixel gap that the previous rule introduces. (Shit like this is why web developers hate IE 6.) */
}
</style>
<![endif]-->
Tested and working in IE 6, 7, and 8; Firefox 3.5; and Chrome 4.
If you're reading this now you can probably use calc, so be thankful.
HTML
<div class="universe">
<div class="somewidth">
</div>
<div class="everythingelse">
</div>
</div>
CSS
.universe {
width: 100%;
height: 100%;
}
.somewidth {
width: 200px;
height: 100%;
}
.everythingelse {
width: 800px; /* fallback for emergencies */
width: calc(100% - 200px);
width: -moz-calc(100% - 200px);
width: -webkit-calc(100% - 200px);
height: 100%;
}
See the working example on JSFiddle.
You would need to float the inner1 div to the left, like so:
<div id="#outer" ....>
<div id='#inner1" style="float:left; border: 1px solid #000;">
blabla
</div>
<div id="#inner2" style="... DON'T USE WIDTH AND DISPLAY HERE! ...">
gnihihi
</div>
</div>
This should do the trick. Check it out!
bye
You do not need to use div for nested element, just use SPAN like this
<div>
<span style="display:inline-block;width: auto;border: solid 1px black;">
hey you
</span>
<span style="display:inline-block;marging: 0px 2px;border: solid 1px black;">
always use proper tools.
</span>
</div>
Expanding on #Nasser Hajloo's answer, this works for me (even in IE6)
<div style="width: 400px; border: solid 1px red;">
<span style="float:left;width: auto;border: solid 1px black;">
hey you
</span>
<div style="display:inline-block;margin: 0px 2px;border: solid 1px black;">always use proper tools.</div>
</div>
Try it with the main div smaller than 400px to see how it adjusts. (It also works with divs rather than spans - the key is the width: auto in the first div/span.)
Try this: nest inner1 inside inner2, and remove the display:inline from inner2, like this:
<div id="#outer" style="width:100%; border:1px solid red">
<div id="#inner2" style="width:100%; border:1px solid black;">
<div id="#inner1" style="border:1px solid blue; display:inline">
inner div 1. Some text...
</div>
inner div 2...
</div>
</div>
You can see it working here: http://jsbin.com/adiwi
From your code it looks like you are trying to get a horizontal line to fill the empty space in your div. If I'm correct your looking to create a visual effect with markup. Correct me if I'm wrong.
(Would be nice to see an image of what you want)
Example:
Title ---------------------------
or
Title: Caption ------------------
This is not best practice. You should try to get this effect with CSS.
Try making your code more semantic first:
<div id="#outer" style="width:100%; border:1px">
<h3 style="border:1px; display:inline">
Caption
</h3>
</div>
To get the line:
create an image with the color you
want
make its height the same that you
want the line to be in px
position it with the background
property
.
#outer h3 {
display: inline;
background-color: #000;
color: #FFF;
}
#outer {
width: 100%; /* is the default of block element but just for celerity */
background: #000 url('image path') center left; /* position the image */
}
Your first problem is that you are prefixing your ids with a '#'. The # is only used in CSS to refer to the element with that id, e.g. the CSS rule #outer{width:100%} refers to your element:
<div id="outer"></div>
Also you don't need to use width's on div's (or any other block elements) that aren't floated, as they already automatically take up 100% of the available width.
If you want to the 2 DIVs to appear on the same line you have to float the first one to the left. The adjacent DIV will then appear on the side, again you don't need to sepecify widthd for the second element. Here is your complete example including a different coloured border for each div.
I've made the borders bigger so you can see clearer whats going on.
<html><body>
<style type="text/css">
#outer {
border: solid 5px #c00;
}
#inner1 {
border: solid 5px #0c0;
float: left;
width: 200px;
height: 300px;
}
#inner2 {
border: solid 5px #00c;
height: 300px;
margin-left: 210px; /* 200px left width + 2 x 5px borders */
}
</style>
<div id="outer">
<div id="inner1">
inner div 1. Some text...
</div>
<div id="inner2">
inner div 2...
</div>
</div>
</body></html>
Another solution is to run a javascript which resizes the captionLine class when document has loaded like this.
Took some time to get it working under IE8, have not tried IE7 but should work.
2 things to note.
IE does not support getElementsByClassName, therefor this function is rewritten.
IE handles margins differently when objects are resized and moved with style.marginLeft, somehow IE seems to keep the margin in the class declaration and adds this to the new style.margin.
<body onload="resizeCaptionLine()">
<style>
caption {
border: 1px solid blue;
padding: 0px;
}
.captionText {
border: 1px solid red;
float: left;
}
.captionLine {
background-color:black;
margin: 0px;
margin: 5px 0px 0px 5px;
border: 0px;
padding: 0px;
padding-top: 1px;
}
</style>
<table style="width:300px;">
<caption width="100%" name="caption1">
<div class="captionText">Some text</div>
<div class="captionLine"> </div>
</caption>
<tr>
<td>something</td>
</tr>
</table>
<table style="width:300px;">
<caption width="100%" name="caption2">
<div class="captionText">Some text</div>
<div class="captionLine"> </div>
</caption>
<tr>
<td>something</td>
</tr>
</table>
<script type="text/javascript">
function getElementsByClassName(node, class_name) {
elems = node.all || node.getElementsByTagName('*');
var arr = new Array();
for(j = 0; j < elems.length; j++)
{
if (elems[j].className == class_name)
arr[arr.length] = elems[j];
}
return arr;
}
function resizeCaptionLine()
{
var elems = getElementsByClassName(document, 'captionLine');
for(i = 0; i < elems.length ; i++)
{
var parent = elems[i].parentNode;
var sibling = getElementsByClassName(parent, 'captionText');
var width = parent.offsetWidth - sibling[0].offsetWidth;
if(elems[i].currentStyle)
{
var currentMargin = elems[i].currentStyle.marginLeft;
var margin = parseInt(currentMargin.substr(0,currentMargin.length-2));
elems[i].style.marginLeft = (sibling[0].offsetWidth) + "px";
}
else if (document.defaultView && document.defaultView.getComputedStyle)
{
var currentStyle = document.defaultView.getComputedStyle(elems[i], '');
var currentMargin = currentStyle.marginLeft;
var margin = parseInt(currentMargin.substr(0,currentMargin.length-2));
elems[i].style.marginLeft = (sibling[0].offsetWidth + margin) + "px";
}
else
{
var currentMargin = elems[i].style.marginLeft;
var margin = parseInt(currentMargin.substr(0,currentMargin.length-2));
elems[i].style.marginLeft = (sibling[0].offsetWidth) + "px";
}
elems[i].style.width = (width - margin)+"px";
}
}
</script>
</body>
Answer is really simple! If you have fixed div (menu) on the left side, then give fixed div float: left and your right flexible div margin-left that is bigger then width of first fixed div.