Search This Blog

  • ()
  • ()
Show more

Absolutely Position Element Within a Table Cell

Absolutely Position Element Within a Table Cell   Have you ever wanted to absolutely or relatively position a table cell? Well, I can't ...


Absolutely Position Element Within a Table Cell





Have you ever wanted to absolutely or relatively position a table cell? Well, I can't help you there. Table cell elements just won't take those position values. And thus, you also can't absolutely position elements within the context of those elements either. I can help you out with that issue.



The answer is just to make a generic wrapper element inside the table cells (the exact same size of the table cell) and use that as the relative positioning context. Non-semantic? Yep, but we'll chuck that markup in with JavaScript so it's not a big deal. Kludgy fix? You know it. Does it work? Sure does.

#Before


<table>
<tr>
<td>
<img src="yay.jpg" alt="">
</td>
</tr>
</table>

#After


<table>
<tr>
<td>
<div class="innerWrapper"> <!-- appended with JavaScript -->
<img src="yay.jpg" alt=""> <!-- original content of cell -->
</div>
</td>
</tr>
</table>

#jQuery Plugin


You could insert your inner div wrappers however you want, but there is appeal to using JavaScript/jQuery to do it because 1) it's pretty easy and 2) it keeps your markup the same / clean.

We'll make a quick jQuery plugin here, so that it can be called on any arbitrary set of elements.
$.fn.iWouldLikeToAbsolutelyPositionThingsInsideOfFrickingTableCellsPlease = function() {
var $el;
return this.each(function() {
$el = $(this);
var newDiv = $("<div />", {
"class": "innerWrapper",
"css" : {
"height" : $el.height(),
"width" : "100%",
"position": "relative"
}
});
$el.wrapInner(newDiv);
});
};

This plugin iterates through each element it is passed, creates a <div>which has relative positioning and is the same height and width of the parent cell, then wraps the inside of that cell within itself.

Bear in mind I'm not using all the generally-regarded-as-smart plugin conventions like this does.

#Usage


Load jQuery. Then run the above plugin code. Then use the plugin like this, either at the bottom of your web page or within a DOM ready statement.
$("th, td").iWouldLikeToAbsolutelyPositionThingsInsideOfFrickingTableCellsPlease();

That appends the inner wrapper to all table cells (<th> and <td>elements) although as you can see you can apply it to any arbitrary set of elements.

View Demo

COMMENTS

Name

apple,2,Article,14,At home,13,Author,14,Beauty,1,Biography,2,blackberry,1,Business,6,Cars,5,Celebrity,13,conspiraci,15,Fashion,5,galaxy,1,Gallery,1,Games,11,google,1,Hair,1,Health amp; Fitness,2,Histori,11,Home,169,HOSTING,27,HTML,7,imac,1,Image,1,iphone,1,Itcyber,7,Kuran,31,Lajme,34,Life amp; Love,1,Makeup amp; Skincare,1,mobile,5,monitor,1,Movies,2,News,5,No category,5,Photography,6,PHP,30,Poezi,24,POEZI amp; TEKSTE,2,Post,14,PROGRAMMIM,46,Relationships,1,review,6,samsung,1,Seo,7,Softvare,4,Sport,5,Sports,7,Stars,5,Tag,7,Tags,7,Tech,30,Teknologji,37,THEMES,3,Tutorials,191,Video,47,Videos,4,Vip News,2,Webhosting,67,WordPress,7,World,13,
ltr
item
Cr337: Absolutely Position Element Within a Table Cell
Absolutely Position Element Within a Table Cell
https://css-tricks.com/wp-content/uploads/2011/01/tablewithabsoluteposition.png
Cr337
https://cr337.blogspot.com/2016/05/absolutely-position-element-within.html
https://cr337.blogspot.com/
http://cr337.blogspot.com/
http://cr337.blogspot.com/2016/05/absolutely-position-element-within.html
true
5516280490839897951
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy