<?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/"
xmlns:series="http://unfoldingneurons.com/"
><channel><title>David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞. &#187; CSS</title> <atom:link href="http://davidwalsh.name/tutorials/css/feed" rel="self" type="application/rss+xml" /><link>http://davidwalsh.name</link> <description>Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</description> <lastBuildDate>Sat, 04 Feb 2012 19:28:58 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3</generator> <item><title>Interesting -webkit CSS&#160;Properties</title><link>http://davidwalsh.name/webkit-css</link> <comments>http://davidwalsh.name/webkit-css#comments</comments> <pubDate>Tue, 10 Jan 2012 15:33:52 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[Browsers]]></category> <category><![CDATA[CSS]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5364</guid> <description><![CDATA[A few weeks back I touched on a handful of Mozilla-specific CSS properties that I found to be interesting. This week I&#8217;d like to share a few WebKit-specific CSS properties that make me all tingly inside. -webkit-touch-callout The -webkit-touch-callout property allows you to dictate what does or doesn&#8217;t happen when a user taps and holds [...]<p><a
href="http://davidwalsh.name/webkit-css">Interesting -webkit CSS&nbsp;Properties</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<a
href="http://davidwalsh.name/webkit-appearance"><img
src="http://davidwalsh.name/dw-content/browsers-webnightly.png" alt="WebKit CSS" class="image" /></a><p>A few weeks back I touched on a handful of <a
href="http://davidwalsh.name/moz-css">Mozilla-specific CSS properties</a> that I found to be interesting.  This week I&#8217;d like to share a few WebKit-specific CSS properties that make me all tingly inside.</p><h2><a
href="http://css-infos.net/property/-webkit-touch-callout"&nbsp;rel="nofollow">-webkit-touch-callout</a></h2><p>The <code>-webkit-touch-callout</code> property allows you to dictate what does or doesn&#8217;t happen when a user taps and holds on a link on iOS.  The default value is <code>default</code> and tap-holding on a link brings up the link bubble dialog;  by using the value of <code>none</code>, that bubble never comes up.</p><pre class="css">
a.js-only {
	-webkit-touch-callout: none;
}
</pre><p>This would be very useful on apps that use A elements which aren&#8217;t traditional links, but simply trigger AJAX / JavaScript functions.</p><h2><a
href="http://css-infos.net/property/-webkit-user-drag"&nbsp;rel="nofollow">-webkit-user-drag</a></h2><p>The <code>-webkit-user-drag</code> property specifies that an entire element should be draggable instead of its contents:</p><pre class="css">
/* no dragging at all */
.content p.noDrag {
	-webkit-user-drag: none;
}

/* drags entire element, not the text/selection */
.sidebar div.elDrag {
	-webkit-user-drag: element;
}
</pre><h2><a&nbsp;href="http://davidwalsh.name/webkit-appearance">-webkit-appearance</a></h2><p>Using the <code>-webkit-appearance</code> property, you can make a SPAN tag look like a radio button, or textarea, or SELECT dropdown, or any of the other 50 supported properties.</p><pre class="css">
span.lookLikeRadio {
	-webkit-appearance: radio;
}

span.lookLikeTextarea {
	-webkit-appearance: textarea;
}

span.lookLikeScrollbar {
	-webkit-appearance: scrollbartrack-horizontal;
}
</pre><p>Wanna see this one in action?  Check out my post: <a
href="http://davidwalsh.name/webkit-appearance">WebKit-Specific Style: -webkit-appearance</a>.</p><h2><a
href="http://css-infos.net/property/-webkit-text-security"&nbsp;rel="nofollow">-webkit-text-security</a></h2><p>Who knew you could customize the character which hides password characters?</p><pre class="css">
input[type="password"] {
	-webkit-text-security: square;
}
</pre><p>Not necessarily useful but interesting that WebKit gives us this ability.</p><h2><a
href="http://css-infos.net/property/-webkit-user-select"&nbsp;rel="nofollow">-webkit-user-select</a></h2><p>The <code>-webkit-user-select</code> property allows us to prevent users from selecting text within a given element:</p><pre class="css">
div {
	-webkit-user-select: none;
}
</pre><p>Preventing selection within a node can be helpful when on nodes which you prefer only be clicked.</p><p>I disliked browser-specific functionality when I was younger because I had the wrong mentality;  you use them as <em>enhancers</em>, not for core functionality.  Have a favorite WebKit-specific CSS property?  Share it!</p><p><a
href="http://davidwalsh.name/webkit-css">Interesting -webkit CSS&nbsp;Properties</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/webkit-css/feed</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>CSS&#160;Circles</title><link>http://davidwalsh.name/css-circles</link> <comments>http://davidwalsh.name/css-circles#comments</comments> <pubDate>Thu, 29 Dec 2011 15:01:59 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[CSS]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5355</guid> <description><![CDATA[A while back I shared a clever technique for creating triangles with only CSS. Over the past year, I&#8217;ve found CSS triangles incredibly effective, especially when looking to create tooltips or design elements with a likewise pointer pattern. There&#8217;s another common shape that&#8217;s easy to create, and that is the circle. Using border-radius, you can [...]<p><a
href="http://davidwalsh.name/css-circles">CSS&nbsp;Circles</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<a
href="http://davidwalsh.name/dw-content/css-circles.php"><img
src="http://davidwalsh.name/dw-content/css-circles.png" alt="CSS Circle" class="image" /></a><p>A while back I shared a <a
href="http://davidwalsh.name/css-triangles">clever technique for creating triangles with only CSS</a>.  Over the past year, I&#8217;ve found CSS triangles incredibly effective, especially when looking to create tooltips or design elements with a likewise pointer pattern.  There&#8217;s another common shape that&#8217;s easy to create, and that is the circle.  Using border-radius, you can create wonderful CSS circles.</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/css-circles.php" class="demo">View Demo</a><div
class="clear"></div></div><h2>The&nbsp;CSS</h2><p>Setting the border-radius of each side of an element to 50% will create the circle display at any size:</p><pre class="css">
.circle {
	border-radius: 50%;
	width: 200px;
	height: 200px; 
	/* width and height can be anything, as long as they're equal */
}
</pre><p>It&#8217;s really that simple&#8230;but I can&#8217;t let this post go without touching on <a
href="http://davidwalsh.name/css-gradients">CSS gradients</a> and <a
href="http://davidwalsh.name/css-spin-revisited">basic spin animation</a>:</p><pre class="css">
/* Create the animation blocks */
@-webkit-keyframes spin {
	from { -webkit-transform: rotate(0deg); }
	to { -webkit-transform: rotate(360deg); }
}

@-moz-keyframes spin {
	from { -moz-transform: rotate(0deg); }
	to { -moz-transform: rotate(360deg); }
}

@-ms-keyframes spin {
	from { -ms-transform: rotate(0deg); }
	to { -ms-transform: rotate(360deg); }
}

/* Spinning, gradient circle; CSS only! */
#advanced {
	width: 200px;
	height: 200px;
	
	background-image: -moz-radial-gradient(45px 45px 45deg, circle cover, yellow 0%, orange 100%, red 95%);
	background-image: -webkit-radial-gradient(45px 45px, circle cover, yellow, orange);
	background-image: radial-gradient(45px 45px 45deg, circle cover, yellow 0%, orange 100%, red 95%);
	
	
	/* webkit chrome, safari, mobile */
	-webkit-animation-name: spin; 
	-webkit-animation-duration: 3s; /* 3 seconds */
	-webkit-animation-iteration-count: infinite; 
	-webkit-animation-timing-function: linear;

	/* mozilla ff */
	-moz-animation-name: spin; 
	-moz-animation-duration: 3s; /* 3 seconds */
	-moz-animation-iteration-count: infinite; 
	-moz-animation-timing-function: linear;

	/* microsoft ie */
	-ms-animation-name: spin; 
	-ms-animation-duration: 3s; /* 3 seconds */
	-ms-animation-iteration-count: infinite; 
	-ms-animation-timing-function: linear;
}
</pre><p>Voila.  There&#8217;s an awesome CSS circle!</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/css-circles.php" class="demo">View Demo</a><div
class="clear"></div></div><p>CSS circles don&#8217;t immediately appear as useful as CSS triangles, but they surely have value within design.  An animated set of circles could act as a loading animation; creative use of the circle is up to you.  Can you think of a good CSS circle usage?  Share!</p><p><a
href="http://davidwalsh.name/css-circles">CSS&nbsp;Circles</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/css-circles/feed</wfw:commentRss> <slash:comments>15</slash:comments> </item> <item><title>Interesting -moz CSS&#160;Properties</title><link>http://davidwalsh.name/moz-css</link> <comments>http://davidwalsh.name/moz-css#comments</comments> <pubDate>Mon, 26 Dec 2011 18:32:12 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[Browsers]]></category> <category><![CDATA[CSS]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5353</guid> <description><![CDATA[I&#8217;m always on the lookout for interesting vendor-specific features and prefixes. The beauty in them is that they allow developers to enhance where possible; they aren&#8217;t taken into account as core design, but provide nice little touches. I was poking around Mozilla&#8217;s MDN and found a great list of proprietary -moz properties. Here are a [...]<p><a
href="http://davidwalsh.name/moz-css">Interesting -moz CSS&nbsp;Properties</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<p>I&#8217;m always on the lookout for interesting vendor-specific features and prefixes.  The beauty in them is that they allow developers to enhance where possible;  they aren&#8217;t taken into account as core design, but provide nice little touches.  I was poking around Mozilla&#8217;s MDN and found a great <a
href="https://developer.mozilla.org/en/CSS_Reference/Mozilla_Extensions" rel="nofollow">list of proprietary <code>-moz</code> properties</a>.  Here are a few of that I found interesting and useful.</p><h2><a
href="https://developer.mozilla.org/en/CSS/%3A-moz-list-bullet"&nbsp;rel="nofollow">::-moz-list-bullet</a></h2><p><code>::-moz-list-bullet</code> allows developers to style the bullet bullet text in lists;  not the content, but the bullet text itself:</p><pre class="css">
ul.customList li::-moz-list-bullet {
	color: #999;
	font-size: 24px;
	text-decoration: underline;
}
</pre><h2><a
href="https://developer.mozilla.org/en/CSS/-moz-force-broken-image-icon"&nbsp;rel="nofollow">-moz-force-broken-image-icon</a></h2><p>Firefox presently displays <code>alt</code> text for every broken image on the page.  This may not be helpful for the purposes of debugging because the text may blend into the page.  By using <code>-moz-force-broken-image-icon</code>, you can fore the broken image icon instead of the <code>alt</code> text:</p><pre class="css">
img {
	-moz-force-broken-image-icon: 1;
}
</pre><h2><a
href="https://developer.mozilla.org/en/CSS/-moz-margin-start" rel="nofollow">-moz-margin-start</a> and <a
href="https://developer.mozilla.org/en/CSS/-moz-margin-end"&nbsp;rel="nofollow">-moz-margin-end</a></h2><p>Both work very much like <code>margin-left</code> and <code>margin-right</code>, but reverse position when in RTL mode.  Very useful if you&#8217;re trying to create a flexible website built for LTR or RTL reading.</p><pre class="css">
.myBlock {
	-moz-margin-start: 10px;
	-moz-margin-end: 5px;
}
</pre><h2><a
href="https://developer.mozilla.org/en/CSS/-moz-orient"&nbsp;rel="nofollow">-moz-orient</a></h2><p>The <code>-moz-orient</code> property currently only applies to <code>progressbar</code> elements, allowing them to be vertical or horizontal:</p><pre class="css">
progress.up {
	-moz-orient: vertical;
}
</pre><h2><a
href="https://developer.mozilla.org/en/CSS/-moz-window-shadow"&nbsp;rel="nofollow">-moz-window-shadow</a></h2><p><code>-moz-window-shadow</code> specifies whether a window in OS X will have a shadow.</p><pre class="css">
.panel {
	-moz-window-shadow: none;
}
</pre><p>There are a few hundred proprietary properties available so take a few moments and let me know if you find any of them interesting, and if you&#8217;ve used any of them for your projects!</p><p><a
href="http://davidwalsh.name/moz-css">Interesting -moz CSS&nbsp;Properties</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/moz-css/feed</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>CSS&#160;Filters</title><link>http://davidwalsh.name/css-filters</link> <comments>http://davidwalsh.name/css-filters#comments</comments> <pubDate>Fri, 23 Dec 2011 14:45:10 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[Browsers]]></category> <category><![CDATA[CSS]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5352</guid> <description><![CDATA[CSS filter support recently landed within WebKit nightlies. CSS filters provide a method for modifying the rendering of a basic DOM element, image, or video. CSS filters allow for blurring, warping, and modifying the color intensity of elements. Let&#8217;s have a look at how CSS filters work and how you can quickly create elements that [...]<p><a
href="http://davidwalsh.name/css-filters">CSS&nbsp;Filters</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<img
src="http://davidwalsh.name/dw-content/css-filters.png" alt="CSS Filters" class="image" /><p>CSS filter support recently landed within WebKit nightlies. <a
href="https://dvcs.w3.org/hg/FXTF/raw-file/tip/filters/index.html">CSS filters</a> provide a method for modifying the rendering of a basic DOM element, image, or video.  CSS filters allow for blurring, warping, and modifying the color intensity of elements.  Let&#8217;s have a look at how CSS filters work and how you can quickly create elements that are beautifully filtered!</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/css-filters.php" class="demo">View Demo</a><div
class="clear"></div></div><p>There are many CSS filters to choose from: <code>grayscale</code>, <code>blur</code>, <code>sepia</code>, <code>saturate</code>, <code>opacity</code>, <code>brightness</code>, <code>contrast</code>, <code>hue-rotate</code>, and <code>invert</code>.  While each property value generally falls between 0 and 1, there are a few exceptions. The <code>blur</code> property uses a unit of pixels and can be any whole number, and the <code>hue-rotate</code> filter value is a whole number with a &#8220;deg&#8221; unit.</p><p>The following CSS snippet will <code>blur</code> an element:</p><pre class="css">
.myElement {
	-webkit-filter: blur(2px);
}
</pre><p>Multiple filters are separated by spaces, so we could also add <code>grayscale</code> and <code>opacity</code> values to the blur:</p><pre class="css">
.myElement {
	-webkit-filter: blur(2px) grayscale (.5) opacity(0.8);
}
</pre><p>Let&#8217;s get crazy with <code>hue-rotate</code> as well:</p><pre class="css">
.myElement {
	-webkit-filter: blur(2px) grayscale (.5) opacity(0.8) hue-rotate(120deg);
}
</pre><p>If static filtering isn&#8217;t enough for you, CSS filters also animate with <code>@-webkit-keyframes</code>:</p><pre class="css">
@-webkit-keyframes testAnim {
	0% {
		-webkit-filter: grayscale(0.5) blur(1px) saturate(2);
	}
	100% {
		-webkit-filter: grayscale(0.2) blur(6px) saturate(9);
	}
}

/* the photo to be animated via filters */
#animatePhoto {}
#animatePhoto:hover {
	-webkit-animation-name: testAnim;
	 -webkit-animation-duration: 2s;
	 -webkit-animation-iteration-count: 1;
	 -webkit-animation-direction: alternate;
	 -webkit-animation-timing-function: ease-out;
	 -webkit-animation-fill-mode: forwards;
	 -webkit-animation-delay: 0s;
}
</pre><p>Expect a performance hit with heavy CSS filter usage;  be sure to heavily test your site wherever filtering is used.  CSS filters have not yet hit mobile, but assume that CSS filters will be taxing in that environment as well.</p><em>Note:  I&#8217;ve yet to successfully filter a &lt;video&gt; tag;  it&#8217;s possible that feature isn&#8217;t in the first implementation.</em><div
class="actions"><a
href="http://davidwalsh.name/dw-content/css-filters.php" class="demo">View Demo</a><div
class="clear"></div></div><p>The additional of CSS filters to the web provides a new level of customization for images, video, and DOM nodes in general.  We should see some solid, practical uses for CSS filters emerge in the coming years.  Have fun playing around with CSS filters and let me know if you can think of some immediate, practical uses!</p><p><a
href="http://davidwalsh.name/css-filters">CSS&nbsp;Filters</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/css-filters/feed</wfw:commentRss> <slash:comments>8</slash:comments> </item> <item><title>Facebook Tooltip HTML and&#160;CSS</title><link>http://davidwalsh.name/facebook-tooltip</link> <comments>http://davidwalsh.name/facebook-tooltip#comments</comments> <pubDate>Tue, 08 Nov 2011 14:46:23 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[APIs]]></category> <category><![CDATA[CSS]]></category> <category><![CDATA[Markup]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5329</guid> <description><![CDATA[Facebook recently implemented a new, lighter tooltip. I say the tooltip is lighter because it seems a lot quicker and more elegant than their previous effort. I took a few moments to grab the HTML structure and CSS rules to see how they did it. The&#160;HTML The tooltip structure consists of five elements: {content here}{content [...]<p><a
href="http://davidwalsh.name/facebook-tooltip">Facebook Tooltip HTML and&nbsp;CSS</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<img
src="http://davidwalsh.name/dw-content/fb-tip.png" alt="Facebook Tooltip" class="image" /><p>Facebook recently implemented a new, lighter tooltip.  I say the tooltip is lighter because it seems a lot quicker and more elegant than their previous effort.  I took a few moments to grab the HTML structure and CSS rules to see how they did it.</p><h2>The&nbsp;HTML</h2><p>The tooltip structure consists of five elements:</p><pre class="html">
<div class="uiContextualDialogPositioner uiContextualDialogLeft" style="top: 20px; left: 600px;">
	<div class="uiOverlay uiContextualDialog uiOverlayArrowRight" style="width: 347px; top: 0px; ">
		<div class="uiOverlayContent">
			<div class="uiOverlayContentHolder">
				{content here}{content here}{content here}{content here}{content here}{content here}{content here}{content here}{content here}
			</div>
		</div>
		<div class="uiOverlayArrow" style="top: 15px; margin-top: 0px;"></div>
	</div>
</div>
</pre><p>The root element dictates the position of the tooltip (which is most likely injected to the body).  The sole child element controls the width of the tooltip.  That element contains two elements:  the content container and the and the arrow node (which I&#8217;ve changed from an I element to a DIV).  The last, innermost DIV element will hold the content and provide padding.</p><h2>The&nbsp;CSS</h2><p>The CSS to create the tooltip layout is actually very minimal:</p><pre class="css">
body {
	font-size: 11px;
	font-family: "lucida grande",tahoma,verdana,arial,sans-serif;
	color: #333;
	line-height: 1.28; 
	text-align: left;
	direction: ltr;
}

.uiContextualDialogPositioner, .uiContextualDialogPositioner .uiContextualDialog {
	position: absolute;
	z-index: 200;
}

.uiContextualDialogLeft .uiContextualDialog {
	right: 0;
}

.uiOverlayArrowRight {
	padding-right: 10px;
}

.uiOverlay {
	position: relative;
	z-index: 200;
}

.uiContextualDialog, .uiContextualDialog:focus {
	outline: 0 solid transparent;
}

.uiOverlayContent {
	background: white;
	border: 1px solid #8C8C8C;
	border: 1px solid rgba(0, 0, 0, .45);
	border-bottom: 1px solid #666;
	-moz-box-shadow: 0 3px 8px rgba(0, 0, 0, .3);
	-webkit-box-shadow: 0 3px 8px rgba(0, 0, 0, .3);
	box-shadow: 0 3px 8px rgba(0, 0, 0, .3);
	position: relative;
}

.uiOverlayContentHolder {
	padding: 10px;
}

.uiOverlayArrow {
	position: absolute;
	overflow: hidden;
}

.uiOverlayArrowRight .uiOverlayArrow {
	background-image: url(sprite.png);
	background-repeat: no-repeat;
	background-position: -177px -309px;
	height: 16px;
	right: 2px;
	width: 9px;
}
</pre><p>The content pane contains the majority of the CSS rules, include the box-shadow and border, both of which use rgba color for a more detailed effect.  Showing and hiding of the tooltip may be done via CSS key-frames or JavaScript &#8212; the choice would be up to the individual implementing the tooltip.</p><h2>Why Show&nbsp;This?</h2><p>Two reasons.  The first is that I appreciate well-coded features like this.  The second, more important reason, is that I&#8217;ll be creating a JavaScript-powered version of this functionality which accounts for content size, position on page, stacking/z-index management, etc.  Do I create as a jQuery and MooTools plugin?  Do I create it as a standalone JavaScript project.  Let me know your thoughts!</p><p><a
href="http://davidwalsh.name/facebook-tooltip">Facebook Tooltip HTML and&nbsp;CSS</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/facebook-tooltip/feed</wfw:commentRss> <slash:comments>22</slash:comments> </item> <item><title>Create Spinning Rays with CSS3:&#160;Revisited</title><link>http://davidwalsh.name/css-spin-revisited</link> <comments>http://davidwalsh.name/css-spin-revisited#comments</comments> <pubDate>Mon, 07 Nov 2011 15:07:00 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[Browsers]]></category> <category><![CDATA[CSS]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5331</guid> <description><![CDATA[Last December I wrote a blog post titled Create Spinning Rays with CSS3 Animations &#38; JavaScript where I explained how easy it was to create a spinning rays animation with a bit of CSS and JavaScript. The post became quite popular so I thought I&#8217;d take some time to look at it and improve it. [...]<p><a
href="http://davidwalsh.name/css-spin-revisited">Create Spinning Rays with CSS3:&nbsp;Revisited</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<a
href="http://davidwalsh.name/dw-content/css-spin-revisited.php"><img
src="http://davidwalsh.name/dw-content/rotate-rays.png" alt="CSS Spinning Rays" class="image" /></a><p>Last December I wrote a blog post titled <a
href="http://davidwalsh.name/javascript-spin">Create Spinning Rays with CSS3 Animations &amp; JavaScript</a> where I explained how easy it was to create a spinning rays animation with a bit of CSS and JavaScript.  The post became quite popular so I thought I&#8217;d take some time to look at it and improve it.  It occurred to me that one solution I didn&#8217;t present was a much lighter solution;  a solution that required only CSS!</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/css-spin-revisited.php" class="demo">View Demo</a><div
class="clear"></div></div><h2>The HTML&nbsp;Structure</h2><p>The structure will stay the same from the last post:</p><pre class="html">
&lt;div id="raysDemoHolder"&gt;
	&lt;a href="/" id="raysLogo"&gt;David Walsh Blog&lt;/a&gt;
	&lt;div id="rays"&gt;&lt;/div&gt;
&lt;/div&gt;
</pre><p>One parent element (with position:relative and fixed dimensions) with two child elements: the logo and the ray container.</p><h2>The&nbsp;CSS</h2><p>This CSS-only version uses CSS transforms like the previous boasted, but now we&#8217;ll introduce @keyframes.  The base goal we&#8217;ll have for the keyframes is starting at rotate(0deg), animating to rotate(360deg):</p><pre class="css">
/* keyframes for animation;  simple 0 to 360 */
@-webkit-keyframes spin {
	from { -webkit-transform: rotate(0deg); }
	to { -webkit-transform: rotate(360deg); }
}

@-moz-keyframes spin {
	from { -moz-transform: rotate(0deg); }
	to { -moz-transform: rotate(360deg); }
}

@-ms-keyframes spin {
	from { -ms-transform: rotate(0deg); }
	to { -ms-transform: rotate(360deg); }
}

/* basic structure for the rays setup */
#raysDemoHolder	{ 
	position: relative; 
	width: 490px; 
	height: 490px; 
	margin: 100px 0 0 200px; 
}
#raysLogo { 
	width: 300px; 
	height: 233px; 
	text-indent: -3000px; 
	background: url(logo.png) 0 0 no-repeat; 
	display: block; 
	position: absolute; 
	top: 0; 
	left: 0; 
	z-index: 2; 
}
#rays	{ /* with animation properties */
	background: url(rays.png) 0 0 no-repeat; 
	position: absolute; 
	top: -100px; 
	left: -100px; 
	width: 490px; 
	height: 490px; 
	
	/* webkit chrome, safari, mobile */
	-webkit-animation-name: spin; 
	-webkit-animation-duration: 40000ms; /* 40 seconds */
	-webkit-animation-iteration-count: infinite; 
	-webkit-animation-timing-function: linear;
	
	/* mozilla ff */
	-moz-animation-name: spin; 
	-moz-animation-duration: 40000ms; /* 40 seconds */
	-moz-animation-iteration-count: infinite; 
	-moz-animation-timing-function: linear;
	
	/* microsoft ie */
	-ms-animation-name: spin; 
	-ms-animation-duration: 40000ms; /* 40 seconds */
	-ms-animation-iteration-count: infinite; 
	-ms-animation-timing-function: linear;
}

#rays:hover {
	/* -webkit-animation-duration: 10000ms; 10 seconds - speed it up on hover! */
	/* resets the position though!  sucks */
}
</pre><p>Using the <code>animation-timing-function</code>, <code>animation-duration</code>, and <code>animation-iteration-count</code> will allow us to make the animation linear (consistent), well-timed, and allow the animation to continue forever!    You&#8217;ll also notice that the animation is much smoother than the JavaScript-powered counterpart.</p><p>Now we have a problem though:  Opera does not yet support <code>@keyframes</code>.  Luckily Opera&#8217;s default functionality allow for us to create this never-ending animation:</p><pre class="css">
/* boooo opera */
-o-transition: rotate(3600deg); /* works */
</pre><p>Just that bit drives the transformation continuously.  As for Internet Explorer, I tried using <code>-ms-transform</code> / <code>-ms-translation</code> to support IE9 but couldn&#8217;t get it to work.</p><h2>The&nbsp;JavaScript</h2><p>Like HBO&#8217;s website said about Oz:  &#8220;gone but never forgotten.&#8221;  One problem with the CSS-only version is that changing the animation-duration property upon hover (speeding it up when the user&#8217;s mouse enters the rays) restarts the animation and thus makes it look awkward, but that&#8217;s just one of the current limitations of CSS animations.</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/css-spin-revisited.php" class="demo">View Demo</a><div
class="clear"></div></div><p>It&#8217;s always good to revisit your code to ensure it&#8217;s optimal.  While this CSS-only version doesn&#8217;t animate gracefully in Internet Explorer, the other browsers animate quite well.  If you must support IE, adding a <a
href="http://davidwalsh.name/ie-conditional-comments">conditional comment</a> with a JavaScript version would be best practice.  Happy spinning!</p><p><a
href="http://davidwalsh.name/css-spin-revisited">Create Spinning Rays with CSS3:&nbsp;Revisited</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/css-spin-revisited/feed</wfw:commentRss> <slash:comments>9</slash:comments> </item> <item><title>iPhone Scrollbars with&#160;iScroll</title><link>http://davidwalsh.name/iphone-scrollbars</link> <comments>http://davidwalsh.name/iphone-scrollbars#comments</comments> <pubDate>Mon, 31 Oct 2011 14:45:04 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[CSS]]></category> <category><![CDATA[JavaScript]]></category> <category><![CDATA[Mobile]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5324</guid> <description><![CDATA[Since we&#8217;ve had web browsers and JavaScript, we&#8217;ve been intent on replacing native browser functionalities, and for many reasons. Whether it be that the native look or functionality is ugly, doesn&#8217;t work the same across browsers, or isn&#8217;t as feature-rich as it should be, we&#8217;ve always pushed the browser&#8217;s limits to do better. One functionality [...]<p><a
href="http://davidwalsh.name/iphone-scrollbars">iPhone Scrollbars with&nbsp;iScroll</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<p>Since we&#8217;ve had web browsers and JavaScript, we&#8217;ve been intent on replacing native browser functionalities, and for many reasons.  Whether it be that the native look or functionality is ugly, doesn&#8217;t work the same across browsers, or isn&#8217;t as feature-rich as it should be, we&#8217;ve always pushed the browser&#8217;s limits to do better.  One functionality we&#8217;ve tried desperately to emulate is scrollbars. <a
href="http://cubiq.org/iscroll-4" rel="nofollow">iScroll</a>, a fairly new scrolling lib, has done an exceptional job in emulating scrolling both within desktop and mobile browsers.  iScroll also allows for scrolling of elements with overflow on older versions of Mobile Safari.  Let&#8217;s have a look at iScroll!</p><div
class="actions"> <a
href="http://davidwalsh.name/dw-content/iscroll.html" class="demo">Desktop Demo</a> <a
href="http://davidwalsh.name/dw-content/iscroll/examples/pull-to-refresh/" class="demo">Mobile Demo</a></div><h2>The&nbsp;HTML</h2><p>iScroll requires a two-DIV pattern for declaring where iScroll will be used.  The first DIV is the wrapper, the second DIV is the scrollable area:</p><pre class="html">
&lt;div id="wrapper"&gt;
	&lt;div id="scroller"&gt;
		&lt;div style="padding:15px 30px;"&gt; &lt;!-- padding for content --&gt;
		
			&lt;!-- content goes here --&gt;
			
		&lt;/div&gt;
	&lt;/div&gt;
&lt;/div&gt;
</pre><p>iScroll will create and inject the scrollbar within the <code>wrapper</code> DIV.  The content is held within the <code>scroller</code> DIV.</p><h2>The&nbsp;CSS</h2><p>The CSS is where iScroll can get a bit fuzzy.  For iScroll to work optimally, both the wrapper and scroller DIVs need to be positioned absolutely and be styled to widths of 100%:</p><pre class="css">
#wrapper {
	position:absolute;
	z-index:1;
	top:0; 
	bottom:0; 
	left:0;
	width:100%;
	overflow:auto;
}

#scroller {
	position:absolute; z-index:1;
/*	-webkit-touch-callout:none;*/
	-webkit-tap-highlight-color:rgba(0,0,0,0);
	width:100%;
	padding:0;
}
</pre><p>As a result, the third DIV of the structure needs the contain enough padding-right to keep the text and scrollbar far enough way from each other.  Be sure to position those elements properly or iScroll wont work at all <em>(as I found out the hard way)</em>!</p><h2>The&nbsp;JavaScript</h2><p>The most obvious piece of using iScroll is including its .js file:</p><pre class="html">
&lt;script type="text/javascript" src="iscroll/src/iscroll.js"&gt;&lt;/script&gt;
</pre><p>With iScroll now available within the page, the next step is creating the iScroll instance that suits the needs of your desired usage.  The most simple of uses provides only the wrapper ID:</p><pre class="js">
var scroller = new iScroll('wrapper');
</pre><p>Awesome; the page&#8217;s native scrollbar disappears and is replaced by an iOS-style scrollbar!  But like every good JavaScript lib, iScroll provides a host of features that allow you to customize your scrollbar.  Options include setting flags for bounce, momentum, fade and hide settings, and whether scrollbars should be allowed both vertically and horizontally.  Here&#8217;s another example of how you can create a pull-to-refresh scrollbar:</p><pre class="js">
var myScroll,
	pullDownEl, pullDownOffset,
	pullUpEl, pullUpOffset,
	generatedCount = 0;

function pullDownAction () {
	setTimeout(function () {	// &lt;-- Simulate network congestion, remove setTimeout from production!
		var el, li, i;
		el = document.getElementById('thelist');

		for (i=0; i&lt;3; i++) {
			li = document.createElement('li');
			li.innerText = 'Generated row ' + (++generatedCount);
			el.insertBefore(li, el.childNodes[0]);
		}
		
		myScroll.refresh();		// Remember to refresh when contents are loaded (ie: on ajax completion)
	}, 1000);	// &lt;-- Simulate network congestion, remove setTimeout from production!
}

function pullUpAction () {
	setTimeout(function () {	// &lt;-- Simulate network congestion, remove setTimeout from production!
		var el, li, i;
		el = document.getElementById('thelist');

		for (i=0; i&lt;3; i++) {
			li = document.createElement('li');
			li.innerText = 'Generated row ' + (++generatedCount);
			el.appendChild(li, el.childNodes[0]);
		}
		
		myScroll.refresh();		// Remember to refresh when contents are loaded (ie: on ajax completion)
	}, 1000);	// &lt;-- Simulate network congestion, remove setTimeout from production!
}

function loaded() {
	pullDownEl = document.getElementById('pullDown');
	pullDownOffset = pullDownEl.offsetHeight;
	pullUpEl = document.getElementById('pullUp');	
	pullUpOffset = pullUpEl.offsetHeight;
	
	myScroll = new iScroll('wrapper', {
		useTransition: true,
		topOffset: pullDownOffset,
		onRefresh: function () {
			if (pullDownEl.className.match('loading')) {
				pullDownEl.className = '';
				pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Pull down to refresh...';
			} else if (pullUpEl.className.match('loading')) {
				pullUpEl.className = '';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Pull up to load more...';
			}
		},
		onScrollMove: function () {
			if (this.y &gt; 5 &amp;&amp; !pullDownEl.className.match('flip')) {
				pullDownEl.className = 'flip';
				pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Release to refresh...';
				this.minScrollY = 0;
			} else if (this.y &lt; 5 &amp;&amp; pullDownEl.className.match('flip')) {
				pullDownEl.className = '';
				pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Pull down to refresh...';
				this.minScrollY = -pullDownOffset;
			} else if (this.y &lt; (this.maxScrollY - 5) &amp;&amp; !pullUpEl.className.match('flip')) {
				pullUpEl.className = 'flip';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Release to refresh...';
				this.maxScrollY = this.maxScrollY;
			} else if (this.y &gt; (this.maxScrollY + 5) &amp;&amp; pullUpEl.className.match('flip')) {
				pullUpEl.className = '';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Pull up to load more...';
				this.maxScrollY = pullUpOffset;
			}
		},
		onScrollEnd: function () {
			if (pullDownEl.className.match('flip')) {
				pullDownEl.className = 'loading';
				pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Loading...';				
				pullDownAction();	// Execute custom function (ajax call?)
			} else if (pullUpEl.className.match('flip')) {
				pullUpEl.className = 'loading';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Loading...';				
				pullUpAction();	// Execute custom function (ajax call?)
			}
		}
	});
	
	setTimeout(function () { document.getElementById('wrapper').style.left = '0'; }, 800);
}
</pre><p>Since we live in the world of AJAX-driven websites that allow content to come and go, calling the refresh method is all you need to do for iScroll to reevaluate the scrollbar position and size:</p><pre class="js">
// When the AJAX is done, refresh the scrollbar sizing and positioning...
scroller.refresh();
</pre><p>It&#8217;s also important to point out that iScroll allows zooming and pinching, as well as snapping to elements:</p><pre class="js">
var myScroll = new iScroll('wrapper', {
	/* snap: true, */ 		// Would snap logically
	snap: "p",				// Snaps to each "P" tag
	momentum: false,
	hScrollbar: false,
	vScrollbar: false 
});
</pre><p>Lastly, iScroll-Lite is available for those looking to support only mobile browsers (iScroll allows for desktop support as well).  The mischievous part of me would prefer iOS-style scrollbars  instead of native browser scrollbars!</p><div
class="actions"> <a
href="http://davidwalsh.name/dw-content/iscroll.html" class="demo">Desktop Demo</a> <a
href="http://davidwalsh.name/dw-content/iscroll/examples/pull-to-refresh/" class="demo">Mobile Demo</a></div><p>Possibly my favorite part of iScroll is that it&#8217;s a standalone library;  no external JavaScript library is required.  iScroll has many configuration parameters so I encourage you to <a
href="http://cubiq.org/iscroll-4" rel="nofollow">visit the iScroll page</a> to check out everything you can do. Matteo Spinelli&#8217;s iScroll is an outstanding piece of work;  grab iScroll and start controlling your scrollbars today!</p><p><a
href="http://davidwalsh.name/iphone-scrollbars">iPhone Scrollbars with&nbsp;iScroll</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/iphone-scrollbars/feed</wfw:commentRss> <slash:comments>8</slash:comments> </item> <item><title>Control Element Outline Position with&#160;outline-offset</title><link>http://davidwalsh.name/outline-offset</link> <comments>http://davidwalsh.name/outline-offset#comments</comments> <pubDate>Thu, 27 Oct 2011 15:52:59 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[CSS]]></category> <category><![CDATA[Markup]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5325</guid> <description><![CDATA[I was recently working on a project which featured tables that were keyboard navigable so obviously using cell outlining via traditional tabIndex=0 and element outlines was a big part of allowing the user navigate quickly and intelligently. Unfortunately I ran into a Firefox 3.6 bug where the element outline forced a horizontal scrollbar within its [...]<p><a
href="http://davidwalsh.name/outline-offset">Control Element Outline Position with&nbsp;outline-offset</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<p>I was recently working on a project which featured tables that were keyboard navigable so obviously using cell outlining via traditional <code>tabIndex=0</code> and element outlines was a big part of allowing the user navigate quickly and intelligently.  Unfortunately I ran into a Firefox 3.6 bug where the element outline forced a horizontal scrollbar within its parent, causing the table to look&#8230;.gross.  After a bit of research, I discovered the <a
href="https://developer.mozilla.org/en/CSS/outline-offset" rel="nofollow"><code>outline-offset</code> CSS property</a>.  Let me show you what it is and how it fixed the outline bug!</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/outline-offset.php" class="demo">View Demo</a><div
class="clear"></div></div><h2>The&nbsp;CSS</h2><p>Assuming you have an <a
href="http://davidwalsh.name/tabindex-focus">element that&#8217;s focusable (usually via <code>tabIndex</code>)</a>, the CSS to adjust the outline is as simple as you would expect it to be:</p><pre class="css">
.tableWidget td {
	outline-offset: 2px;
}
</pre><p>A positive <code>outline-offset</code> moves the offset further outside the element, whereas you can create an inner outline using negative <code>outline-offset</code>:</p><pre class="css">
.tableWidget td {
	outline-offset: -1px;
}
</pre><p>In the case of the bug I mentioned above, using a negative <code>outline-offset</code> fixed the problem because the outline stays within element.</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/outline-offset.php" class="demo">View Demo</a><div
class="clear"></div></div><p>I wasn&#8217;t aware of the <code>outline-offset</code> property before coming across this issue and I&#8217;m certainly glad I know about it now.  Removing the outline property is never a good idea because usability and accessibility, so the ability to control its placement relative to the element border is very valuable.</p><p><a
href="http://davidwalsh.name/outline-offset">Control Element Outline Position with&nbsp;outline-offset</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/outline-offset/feed</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>Fix WebKit Checkbox&#160;Overflow</title><link>http://davidwalsh.name/webkit-checkbox-overflow</link> <comments>http://davidwalsh.name/webkit-checkbox-overflow#comments</comments> <pubDate>Mon, 24 Oct 2011 15:21:46 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[Browsers]]></category> <category><![CDATA[CSS]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5319</guid> <description><![CDATA[I want to share a quick tip about working with checkboxes in Chrome and Safari, the WebKit-based browsers. I was recently working with a series of checkboxes when I noticed that Safari and Chrome were doing some funky &#8220;cut off&#8221; with them: What&#8217;s even weirder is that they were randomly fix and unfix themselves as [...]<p><a
href="http://davidwalsh.name/webkit-checkbox-overflow">Fix WebKit Checkbox&nbsp;Overflow</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<p>I want to share a quick tip about working with checkboxes in Chrome and Safari, the WebKit-based browsers.  I was recently working with a series of checkboxes when I noticed that Safari and Chrome were doing some funky &#8220;cut off&#8221; with them:</p><p><img
src="http://davidwalsh.name/dw-content/webkit-checkbox.png" alt="WebKit Checkbox Bug" /></p><p>What&#8217;s even weirder is that they were randomly fix and unfix themselves as you hover over them.  Horrible!  After a bit of tinkering, I discovered the solution was adding a <code>line-height</code> of <code>20px</code> to the parent the checkbox:</p><pre class="css">
.checkboxList li { /* ..or whatever the parent is */
	line-height: 20px;
}
</pre><p>Twenty pixels should be considered the minimum &#8212; anything smaller wont prevent the problem completely.</p><p><a
href="http://davidwalsh.name/webkit-checkbox-overflow">Fix WebKit Checkbox&nbsp;Overflow</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/webkit-checkbox-overflow/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>DOM Manipulation with&#160;put-selector</title><link>http://davidwalsh.name/put-selector</link> <comments>http://davidwalsh.name/put-selector#comments</comments> <pubDate>Mon, 17 Oct 2011 15:12:08 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[CSS]]></category> <category><![CDATA[JavaScript]]></category> <category><![CDATA[Markup]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5316</guid> <description><![CDATA[DOM node manipulation is a big part of the web today; just look at how popular the jQuery JavaScript framework has gotten. The ability to easily work with the DOM allows us to do a lot of work in a little bit of code. Thanks to a new JavaScript module by Dojo Toolkit developer Kris [...]<p><a
href="http://davidwalsh.name/put-selector">DOM Manipulation with&nbsp;put-selector</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<p>DOM node manipulation is a big part of the web today;  just look at how popular the jQuery JavaScript framework has gotten.  The ability to easily work with the DOM allows us to do a lot of work in a little bit of code.  Thanks to a new JavaScript module by Dojo Toolkit developer Kris Zyp, working with the DOM just got a lot more compact.  With a name as short as the syntax itself, <a
href="https://github.com/kriszyp/put-selector" rel="nofollow">put</a> could make you change the way you work with the DOM.</p><h2>Creating DOM&nbsp;Nodes</h2><p>Creating new nodes is just about as simple as it gets:</p><pre class="js">
// Create an empty DIV
var div = put("div");
</pre><p>Boom, there&#8217;s your new DIV element!  And if you want your new code to have a few CSS classes:</p><pre class="js">
// Create a DIV with some CSS classes
var div2 = put("div.classOne.classTwo");
</pre><p>How about creating nodes with attributes?</p><pre class="js">
// Create a DIV with some CSS classes and attributes
var div3 = put("div.classOne.classTwo[title=Hover over me][style=display:inline-block]");
</pre><p>The syntax for inserting the DOM node is a bit different because the parent then becomes the first argument in the put signature:</p><pre class="js">
// Create an INPUT with an ID and an attribute, place it into the body
// The body text goes into the third argument;  not(!) innerHTML, just text
var div4 = put(document.body, "input[type=text]#myInput", "This is inner html");
</pre><h2>Manipulating Existing DOM&nbsp;Nodes</h2><p>Manipulating DOM nodes is actually very much like creating the nodes themselves:</p><pre class="js">
var myInput = document.getElementById("myInput");
put(myInput, ".anotherClass");

// Add CSS classes attributes to the element
put(myInput, "[placeholder=first name][required=true][title=first name element].yetAnotherClass");
</pre><p>Just remove the tagName and you can modify the node.</p><h2>Node, Class, and Attribute&nbsp;Deletion</h2><p>The &#8220;!&#8221; character is meaningful in that it represents deletion within put.  Let&#8217;s remove a few classes and attributes from a given node, then let&#8217;s remove the node itself:</p><pre class="js">
// Remove a CSS class from the INPUT element
put(myInput, "!anotherClass");

// Remove attributes from the INPUT element
put(myInput, "[!required][!title]");

// Delete a node!
put(myInput, "!");
</pre><p>The syntax for deletion is short but sweet.  The only criticism I have is that if this could create a maintenance problem if developers are not experience with put.</p><h2>Creating and Managing Child&nbsp;Nodes</h2><p>Creating childNodes for an existing node is another functionality made easier by put:</p><pre class="js">
// Create a base node so we can add nodes around it
var baseNode = put(document.body, "div#baseNode");

// Add two DIVs *after* the baseNode
put(baseNode, "+div +div");

// Add a SPAN element *before* the baseNode
put(baseNode, "-span");

// Create the parent list element 
var ul = put(document.body, "ul");

// Create a child list item
var firstItem = put(ul, "li");
</pre><h2>Moving and ReParenting&nbsp;Nodes</h2><p>Reparenting and moving elements within the DOM is another basic task that, when you use JavaScript&#8217;s native functions, takes far too much code.  Let&#8217;s have a look at moving nodes around with put:</p><pre class="js">
// Move one node to another parent
// parent > child
put(ul, ">", firstItem);

// Put one element after the first
put(div1, "+", div2);
</pre><p>A simple series of symbols designates where within the node try an element should go!</p><h2>More Traditional Node Property&nbsp;Management</h2><p>For those who prefer a more verbose element creation syntax, put-selector provides that as well:</p><pre class="js">
var span = put(parent, "span", {
	title: "Hover over me!",
	style: "font-style: italic;"
});
</pre><p>Kris&#8217; tool is great in that it&#8217;s ultra-efficient, modular, and easy to learn.  On the other side, it&#8217;s possible that the syntax may be a bit <em>too</em> compact for persons try to maintain an application that they didn&#8217;t write.  What do you think of put?  An easy to use utility or a &#8220;simple but complicated&#8221; resource?  If I&#8217;m honest, I&#8217;m teetering on the fence &#8212; maybe you can convince me?</p><p><a
href="http://davidwalsh.name/put-selector">DOM Manipulation with&nbsp;put-selector</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/put-selector/feed</wfw:commentRss> <slash:comments>6</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced (User agent is rejected)
Database Caching 1/66 queries in 0.054 seconds using disk: basic
Object Caching 1435/1543 objects using disk: basic

Served from: davidwalsh.name @ 2012-02-09 00:24:16 -->
