32

I have a view with a section defined thus:

<div id="QuestionBody">
        <p><%=ViewData.Model.Body %></p>
        <div id="QuestionEditLink"><%=Html.ActionLink ("Edit","EditQuestion","Question",new {id=Model.QuestionId},null) %></div>
        <div id="QuestionHistoryLink"><%=Html.ActionLink ("History","ShowHistory","Question",new {postId=Model.PostId,questionId=Model.QuestionId},null) %></div>  
        <div id="AnsweringUser"><a href="/Profile/Profile?userName=https%3A%2F%2Fwww.google.com%2Faccounts%2Fo8%2Fid%3Fid%3DAItOawnZ6IhK1C5cf_9wKstNNfSYIdnRp_zryW4">Answered by Sam</a></div>          
    </div>

and this produces a section with some text and a couple of links under it. I wanted the links to be next to each other. I'm new to css and web development but added this to my style sheet:

#QuestionEditLink
{
    color: #666666;
    width:auto;
    float:left;
    padding:2px;
}

#QuestionHistoryLink
{
    color: #666666;
    width:auto;
    float:left;
    padding:2px;
}

and hey presto the links were nicely aligned. unfortunately they are also not clickable and in fact the cursor doesn't change when moving over them either.

So what did i do wrong? how to I use the css to align the two links next to each other so they are still clickable?

EDIT:

This behaviour is in chrome 8.0.552.215 and in firefox 3.6. It works as I would expect in IE 8, annoyingly.

EDIT2:

I have added the page to JSBin : http://jsbin.com/odefa4/edit which shows the issue. Only the question is styled and shows the problem, the links for the answers work ok...

Sam Holder
  • 32,535
  • 13
  • 101
  • 181
  • could you link to the page so we can see the problem in action, might be easier to find a solution for you :) – Treemonkey Dec 10 '10 at 10:34
  • Does this happen in every browser? Why are you using a div instead of a Span if you want them next to each other? I just checked this and it works fine on Chrome and IE8, is that all your markup? – willvv Dec 10 '10 at 10:34
  • @Treemonkey, its not live anywhere, only on my dev machine, so unfortunately no I can't link to it... – Sam Holder Dec 10 '10 at 10:34
  • could u atleast add the HTML n CSS snippet to JSBin? – sv_in Dec 10 '10 at 11:09
  • @Shree, @Treemonkey I have added a link to JSBin which shows the issue (Thanks for pointing out JSBin shree, didn't know that existed) – Sam Holder Dec 10 '10 at 11:42
  • fyi you dont really need to use div element around the link! as really there is not a reason for this.. just bloating code right? I often attempt to use the minimum markup to achieve a layout doing so I find helps in future :> – Treemonkey Dec 10 '10 at 14:34

4 Answers4

81

The fix is pretty simple and cross browser too, add this (to your unclickable link):

#QuestionEditLink, #QuestionHistoryLink {
    position: relative;
    z-index: 10;
}

The fix here is the z-index, but it won't work if position isn't relative/absolute/fixed.

For more info on z-index see https://developer.mozilla.org/en-US/docs/Web/CSS/z-index

Have used this all the time, is simple, works in all browsers (IE6+, FF, Chrome, Safari, Opera).

Since others already said about your CSS issues there, I won't add to it. Just providing a solution, by the way, tested it with your provided JSBin, worked as always!

tomsseisums
  • 13,168
  • 19
  • 83
  • 145
  • Technically this might work, but (I admit I don't know that much about html/web development) but this feels hacky to me. Maybe its not, but it sets my teeth on edge, like I've got cotton wool in my mouth. My problem seems to be that I'm using unsuitable elements for my link containers. Interesting reading though. – Sam Holder Dec 10 '10 at 12:38
  • This is the answer, works great, thank you VERY VERY MUCH! – Solomon Closson Jan 29 '16 at 03:52
  • 1
    Wow, how *obvious*. :-) CSS voodoo to the rescue. Works perfectly. – Mars Nov 09 '16 at 19:33
  • 1
    This is *very* awesome solution. You know that `label` element passes clicks to an `input` element when we wrap it around the input (not working for single line float row) or use `for` attribute. On mobile, I love wrapping inputs that way because I can made the label bigger than the input - and it simplifies tapping to the input. I'm using `margin: -100%` and `padding-right: 100%` on a `label` element. But it has a drawback: it covers all the floating elements on the right side of the row. Your solution is the only one which works there: http://stackoverflow.com/a/42542011/84661 – Brian Cannard Mar 02 '17 at 20:06
  • 2
    This answer is 12 years old - and it still works. One button on a 14 year old Rails app running on a client's intranet stopped working in 08/2022. This coincided with the move to a new server, but can now also be seen in docker container running this app. position: relative and z-index to the rescue. I am shocked, but this worked! thank you – cokron Aug 31 '22 at 13:57
19

The usual reason is that there's a transparent layer on top. It's normally caused when a box is wider than you think and has a transparent border/padding. Use CSS to apply a temporary border to all items and you'll check whether it's the case.

Update #1

div, span, p{
    border: 1px solid red;
}

Update #2

I can see that #QuestionEditLink and #QuestionHistoryLink are floating. That means that they no longer use space in the page flow. So when you display #AskingUser next it starts at the same point and, being the last one on the page, it gets displayed on top of the two other boxes.

Your layout appears to be fully vertical. I presume you don't need any float: left at all.

BTW, you have lots of duplicate IDs.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
2

hmmm sounds like a simple bug often found with multiple floats, you could try adding a clearing element after the links i would use something like

<b class="clear"></b>

.clear{
float:none;
clear:both;
font-size:0px;
line-height:0px;
height:0px;
}

that should be fairly cross browser, you may need to add   inside so its

<b class="clear">&nbsp;</b>
Treemonkey
  • 2,133
  • 11
  • 24
2

Your code will be invalid as you are using IDs when this should be a job for classes. This will do what you want:

#AskingUser {
    border: none;
    float: right;
    width:auto;
    padding: 2px;
    position: relative;
    text-align: right;
}

and add

#QuestionBody {
    border-bottom: 1px dotted black;
    overflow: hidden;
}

I've left it as IDs but you should adjust all your CSS and HTML so that they are classes instead

.askingUser {} and <div class="askingUser"></div>

if that makes sense.

Ross
  • 18,117
  • 7
  • 44
  • 64
  • Thanks Ross I'll try this. Yeah I've realised that my ids should be classes. – Sam Holder Dec 10 '10 at 12:12
  • Whilst as an answer this works, I have marked @Alvaro's as the accepted answer as I learnt a lot more about what I was doing wrong from that. Cheers for the pointers though, it was most useful. – Sam Holder Dec 10 '10 at 12:34