I have the following dummy code:
<!doctype html>
<html>
<head>
<style>
p {font-size: 20px;}
</style>
</head>
<body>
<script type="text/javascript">
window.onload = function () {
var body = document.getElementsByTagName('body')[0],
p = document.createElement('p'),
el;
p.style.height = '20px';
p.innerText = 'Some Test';
for (var i = 0, len=30; i<len; i++) {
el = p.cloneNode(true);
body.appendChild(el);
}
};
</script>
</body>
</html>
It's render some
elements and on page preview it looks like the following:
I need to add borders to first and last elements to be like the following:
Is it possible to make by using CSS and to get work in webkit?
EDIT: To everyone who advise the following css
p:nth-child(1){
border-top : solid 1px gray;
}
p:last-child{
border-top : solid 1px gray;
}
or
p:first-child {border-top : solid 1px gray;}
p:last-child {border-bottom : solid 1px gray;}
That dont's work for me because it works accross all pages and looks like this:
I need to work in chrome
If you do not give a damn about old browsers. Then this will work for you...
But if you want this to work with older browsers(especially IE) you'll have to use some JS technique.
You can use two containers and place one at the top of the page and the other one at the bottom. These containers must be placed in
position:fixed
and they will print in every page. This will work in Firefox, Opera and Internet Explorer but not in webkit browsers.Demo here
After a few tests, I think the best values for your CSS are the following:
and your HTML:
Try
first-child
andlast-child
.For Instance,
A more complicated solution would be to calculate the viewable page height (JavaScript, jQuery or the like) for each user and check how many items fit onto one page. Afterwards you can style the desired elements (nth-child(x)). Not the best solution but might work if you have conrete requirements (as a certain browser, OS, ...)
EDIT:
A possible solution could be: http://jsfiddle.net/VKDVd/
HTML: Just a few place-holder divs... Pay attention that you have set your doctype (otherwise $(window).height() will not work properly!)
JavaScript/jQuery:
PS: You could also add further code that updates the div borders if the viewport size changes (e.g. if the user resizes the browser window).
PPS: You can also play around with the numbers for count - as it certainly depends on the borders you add to the elements.