<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CSS &#8211; Johnny Morano&#039;s Tech Articles</title>
	<atom:link href="https://jmorano.moretrix.com/tag/css/feed/" rel="self" type="application/rss+xml" />
	<link>https://jmorano.moretrix.com</link>
	<description>Ramblings of an old-fashioned space cowboy</description>
	<lastBuildDate>Wed, 15 Dec 2010 13:50:09 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.2</generator>

<image>
	<url>https://jmorano.moretrix.com/wp-content/uploads/2022/04/cropped-jmorano_emblem-32x32.png</url>
	<title>CSS &#8211; Johnny Morano&#039;s Tech Articles</title>
	<link>https://jmorano.moretrix.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Centering a CSS div layer in your browser window</title>
		<link>https://jmorano.moretrix.com/2010/12/centering-a-css-div-layer-in-your-browser-window/</link>
		
		<dc:creator><![CDATA[insaniac]]></dc:creator>
		<pubDate>Wed, 15 Dec 2010 13:50:09 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Dev]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">http://jmorano.moretrix.com/?p=435</guid>

					<description><![CDATA[Some time ago I needed to create a popup window on a web page, and it had be&#8230;]]></description>
										<content:encoded><![CDATA[<p>Some time ago I needed to create a popup window on a web page, and it had be centered in the browser window. &#8220;Couldn&#8217;t be that hard&#8221; was my first thought, although I haven&#8217;t programmed anything in JavasScript for years.</p>
<p>So using CSS and Javascript, this was actually a pretty easy job to do. The only hard thing to do, was making it browser indepedent. But in the end, even this wasn&#8217;t so hard at all since most of these steps have been thoroughly described on the Interweb.<br />
<span id="more-435"></span><br />
What we will need is</p>
<ul>
<li>a CSS file</li>
<li>a JavaScript file</li>
<li>a HTML file</li>
</ul>
<p>The CSS portion is actually very easy:<br />
&#8211; Save this into a file called <em>style.css</em></p>
<pre class="brush:css">
#blanket {
    display: block;
    position:absolute;
    width:100%;
    height: 100%;
    top:0px;
    left:0px;
    z-index: 9998;
    background-color:#111;
    opacity: 0.65;
    filter:alpha(opacity=65);
}
#popUpDiv {
    display: block;
    position:absolute;
    width:450px;
    height:500px;
    z-index: 9999;
    overflow: auto;
    padding-left: 4px;
    padding-right: 4px;
    background-color:#eeeeee;
}
</pre>
<p>As you can see: no rocket science. We create two layers: blanket and popUpDiv. The blanket layer will create a gray transparent layer over the entire page. The popUpDiv layer will create &#8230; well, I think it&#8217;s clear what this layer will do <img src="https://s.w.org/images/core/emoji/15.0.3/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>The JavaScript portion contains a bit more code. It will contain four functions:</p>
<ul>
<li>toggle(): toggle the visibility of a layer</li>
<li>create_blanket(): calculate the size of the blanket and display</li>
<li>create_popup(): calculate the position of the popup layer and display</li>
<li>popup(): wrapper function which calls toggle(), create_blanket(), create_popup()</li>
</ul>
<p>Let&#8217;s start with the <em>toggle()</em> function, because that&#8217;s the shortest and easiest one.</p>
<pre class="brush:javascript">
function toggle(div_id) {
        var el = document.getElementById(div_id);

        if (el.style.display == 'none') {
                el.style.display = 'block';
        }
        else {
                el.style.display = 'none';
        }
}
</pre>
<p>Again, no rocket science. This procedure has been described and explained a million times on the Interweb, I will not redo it overhere.</p>
<p>Secondly, the <em>create_blanket()</em> function requires a bit more attention, since it&#8217;s more than 4 lines of code. <img src="https://s.w.org/images/core/emoji/15.0.3/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /><br />
In this function, we will try to get the window height. We&#8217;ll do this in two different ways: one for Mozilla based browsers, one for Internet Exploder browsers.</p>
<pre class="brush:javascript">
if (typeof window.innerHeight != 'undefined') {
    height = window.innerHeight;
}
else {
    height = document.documentElement.clientHeight;
}
</pre>
<p>But then again &#8230; we could have just called <code>document.documentElement.clientHeight</code> since this is also supported by Mozilla based browsers.<br />
The variable <em>height</em> will contain the browser height.<br />
But, since we don&#8217;t all have huge screens which eliminates the usage of scrollbars, we will have to check if this <em>height</em> variable is smaller or bigger than the scroll area of the browser. </p>
<pre class="brush:javascript">
if ((height > document.body.parentNode.scrollHeight)
        && (height > document.body.parentNode.clientHeight)) {

        blanket_height = height;
}
else {
        if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
                blanket_height = document.body.parentNode.clientHeight;
        }
        else {
                blanket_height = document.body.parentNode.scrollHeight;
        }
}
</pre>
<p>And finally we&#8217;ll create this blanket:</p>
<pre class="brush:javascript">
var blanket = document.getElementById('blanket');
blanket.style.height = blanket_height + 'px';
</pre>
<p>So on with the <em>create_popup()</em> function. This function will first retrieve information about the different dimensions required to calculate the position of the popup. The following information is required:</p>
<ul>
<li>scroll_width: amount of pixels scrolled from left</li>
<li>scroll_height: amount of pixels scrolled from top</li>
<li>client_width: width of the browser (without scrollbars, menubars, padding, &#8230;)</li>
<li>client_height: height of the browser (without scrollbars, menubars, padding, &#8230;)</li>
<li>popup_width: popup width defined in CSS stylesheet file</li>
<li>popup_height: popup height defined in CSS stylesheet file</li>
</ul>
<p>First the scroll width and scroll height:</p>
<pre class="brush:javascript">
if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        scroll_height = window.pageYOffset;
        scroll_width = window.pageXOffset;
} 
else if( document.body
        && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        scroll_height = document.body.scrollTop;
        scroll_width = document.body.scrollLeft;
} 
else if( document.documentElement
        && ( document.documentElement.scrollLeft
            || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        scroll_height = document.documentElement.scrollTop;
        scroll_width = document.documentElement.scrollLeft;
}
</pre>
<p>Secondly the client height and width:</p>
<pre class="brush:javascript">
var client_height = document.body.parentNode.clientHeight;
var client_width  = document.body.parentNode.clientWidth;
</pre>
<p>And last the popup height and width as stated in the CSS stylesheet file:</p>
<pre class="brush:javascript">
var popup_height = parseInt(window.getComputedStyle(popUpDiv, "").getPropertyValue('height'));
var popup_width  = parseInt(window.getComputedStyle(popUpDiv, "").getPropertyValue('width'));
</pre>
<p>We need to wrap these values from the CSS stylesheet file around the JavaScript function <em>parseInt()</em> since the <em>.getPropertyValue()</em> function will return the values with &#8216;px&#8217; at the end. (e.g.: 400px)</p>
<p>Based on the above information, the position from top and left will be calculated:</p>
<pre class="brush:javascript">
var left    = client_width  / 2 + scroll_width  - (popup_width / 2);
var top     = client_height / 2 + scroll_height - (popup_height / 2);
</pre>
<p>The only thing left to do is, is sending these position values to the browser by simply changing the CSS values for this DIV object:</p>
<pre class="brush:javascript">
popUpDiv.style.left  = left + 'px';
popUpDiv.style.top   = top + 'px';
</pre>
<p>The final function, popup(), will be the wrapper function called from within the HTML code.</p>
<pre class="brush:javascript">
function popup(windowname) {
    create_blanket(windowname);
    create_popup(windowname);
    toggle('blanket');
    toggle(windowname);
}   
</pre>
<p>So now we only need some HTML code to test this!<br />
<a href="http://moretrix.com/~insaniac/popup/">Click here</a> to have a demo page (so where you could do right click + &#8216;View Source&#8217;)</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
