Your question has two parts: the first is to make the <ul> element stretch to fill the viewport, and the second is to vertically and horizontally center the contents of the <li> . However, centering requires changes in your layout. We can wrap all content in <li> using the <div> elements.
To center for this we can use CSS3 flexbox. It will be a JS-free solution , although it does not support cross-browser. For the viewport size, we can use the units vw and vh respectively.
ul { display: flex; flex-direction: column; list-style: none; margin: 0; padding: 0; width: 100vw; height: 100vh; } li { display: flex; align-items: center; justify-content: center; flex-grow: 1; } li div { } li:nth-child(odd) { background-color: #ddd; }
<link href="http://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.2/normalize.min.css" rel="stylesheet"/> <ul> <li><div>One</div></li> <li><div>Two</div></li> <li><div>Three</div></li> <li><div>Three <br>a Half</div></li> </ul>
However, there may be situations where using CSS flexbox and view units are not ideal - for example, iOS7 has a well-documented rendering error that does not correctly calculate vh . In this case, we may have to rely on JS. The height of each <li> simply divided by the number of <li> present in the container.
var calcHeight = function() { var h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0); var li = document.querySelectorAll("ul li"); for(var i=0; i<li.length; i++) { li[i].style.height = (h/li.length)+'px'; } } calcHeight(); window.onresize = calcHeight();
ul { list-style: none; margin: 0; padding: 0; width: 100%; height: 100vh; } li { display: block; width: 100%; position: relative; } li div { position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%); } li:nth-child(odd) { background-color: #ddd; }
<link href="http://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.2/normalize.min.css" rel="stylesheet"/> <ul> <li><div>One</div></li> <li><div>Two</div></li> <li><div>Three</div></li> <li><div>Three <br>a Half</div></li> </ul>
source share