I have tried to resolve the problem of vertical align using jQuery (And however I don’t know if the right section to post this is CSS). The code seems to go rightly, but there is an extra space on top-left (as you can see running the jsfiddle link), and it seems to be caused by the negative padding-top, but that negative padding-top is useful in this case. Please can you help me?
It’s because the height of the span on the first link will be smaller than the height for the span on the second link.
I’m sure there’s a better approach, but a quick and dirty way to vertically center the spans would be to get rid of the javascript entirely and just use this on the span.
ul li a span {
min-height: 50%;
position: relative;
top: 25%; bottom: 25%;
display: block;
background: mediumaquamarine;
}
The spans are vertically centered (which is what you asked for)…the text inside the spans is NOT. I was just trying to give you what you asked.for based on your current HTML. @PaulOB is just the css/html ninja who knows what you’re actually looking to do…
Yeah! In fact even if the PaulOB solution goes right, I have to change my html. In fact it doesn’t include unordered list for navigation and anchors within, and they are present in your code!! So, Dave, your solution is the best till now! Can you center also the text within?
You don’t need an unordered list inside the nav element unless course you can’t change the html. The nav element is meant to wrap navigation items and although most people insert ul inside a nav that is just wasted html.
It can be done using a ul but then you need a certain structure all the same.
I’m assuming you wanted the links to fill the cell otherwise you don’t need the inner nested table approach.
You might want to do search on the forums as we had this question before and I provided the nav version and Erik J had had an unordered list solution.
That’s what I looking for! I have another question, using nav and anchors, is it possible to do nested links, or at least nested links semantically correct?
As far as nesting anchors goes then no you can’t do that as it is invalid and a link can only have one destination so nesting makes no sense.
If on the other hand you want to wrap a link around a heading and some text then yes you are now allowed to do that in html5. The same rules apply though in that you cannot have nested links inside that parent anchor.
If you want to have little ‘link islands’ (for want of a better word) inside a bigger target area then you could absolutely place the anchor element on top of another anchor element and that would work if the design allowed.
So, at the end, is it possible to do the same using a jQuery function? (I know that CSS way is better, but, I would want to remove this little doubt! )
You almost never want to use js for doing css work. It will make the page slow down and be awkward to use and there is no need. If you find that you need js to do something like this then think again as you are most certainly taking the wrong approach to the problem.
Which items are to be vertically aligned? The span and the links or just the links?
Do you have a drawing of the desired result.
What needs to be aligned?
Just the text?
The whole block including background colour?
Does the whole background need to be a clickable area as in my example?
There are so many variables here that its hard for me to give you one answer:)