Absolute Center Images With CSS

Here is a technique about how you can absolute center position an element on the horizontal and vertical in CSS.

Center Images Horizontally

To center something on the horizontal in CSS it's quite easy all you need to do is set the width on the element and apply an auto margin-left and margin-right on to the image. The browser will work out the exact margin on both the right and left side of the image. This will position the image in the center of the parent element just by using the width and the margin properties.

<img src="example.html" alt="Center Images" />

img {
     width:250px;
     margin: 0 auto;
}

Continue reading »

Use Box Sizing For Easier Widths

Here is a quick tip on working with width's in CSS. There are multiple things that make up the width's of the HTML elements, the width, the padding and any borders on the element.

Total Width = Width + Padding + Border.

This means that if the width of the element is 500px with a padding of 50px and a border of 10px the total width of the element is 620px.

620px = 500px + (50px x 2) + (10px x 2).

Continue reading »

Setup Apple Touch Icon

A device which lots of your visitors will use to access your website are iPhone and iPads. You can't ignore how important it is to allow users of these devices easy access to your site.

There are a few ways you will do this, one of them is responsive design. Use media queries to define how the website looks on these devices.

If you want to start working with media queries on apple devices here is a boilerplate for iPhone and iPad media queries.

Another thing you need to do for the apple devices is to define the apply touch icon in your head tag. This is a tag that defines an icon to use when on web clips. Web clips is a feature on apple devices to to display an icon on the home screen of iOS for the website.

To define the icon to use for the apple-touch-icon is done the same way as you define the favicon.

<link rel="apple-touch-icon" href="apple-touch-icon.png"/>

The default size of the icon should be 57px x 57px.

As some of the devices are different with retina displays you might want to define a different icon for different devices, if so use the following snippet.

<link rel="apple-touch-icon" href="touch-icon-iphone.png" />
<link rel="apple-touch-icon" sizes="72x72" href="touch-icon-ipad.png" />
<link rel="apple-touch-icon" sizes="114x114" href="touch-icon-iphone4.png" />
<link rel="apple-touch-icon" sizes="144x144" href="touch-icon-retina-ipad.png" />

Special Effects

On these icons iOS will add special effects to the icons such as rounded corners, drop shadows and reflected shines. If you don't want iOS to add these effects to the icons then you have to define the icon as precomposed.

<link rel="apple-touch-icon-precomposed" href="apple-touch-icon-precomposed.png">

Style Text Like Passwords With CSS

Webkit have lots of experimental CSS properties that you can currently use on your website, one of the properties we are looking at now is the text-security property.

This CSS property allows you to change the the shape of the letter that are displayed in text input boxes.

When the visitor types in a input password text box all the characters they type are converted into shapes to hide the password. But if you don't want to use a password input type then you have to use a text box but then people can see what your typing in.

This is where the -webkit-text-security property comes in you are able to style your textbox to be just like a password input type.

-webkit-text-security: value;
  • circle
  • disc
  • square
  • none

This property currently only exists in webkit browsers so you will only be able to see this in Chrome and Safari browsers.

Recreate Marquee’s With CSS

Back in the early days of the website there was a magically HTML tag which you would see everywhere...this tag is the marquee tag.

This was used on almost every website, if you don't know about this tag or don't remember it, then basically this tag will make any text you add inside it to scroll across the screen. You can variable the speed and direction the text moves across the screen.

Here is a marquee

Many people got annoyed with this tag and thankfully has been deprecated from the html specification.

But someone developing at webkit missed this tag so much they decided to build a webkit CSS property to create a marquee, which can be used as below.

-webkit-marquee: direction increment repetition style speed;

As it is a webkit property it can only be used on chrome or safari browsers.

The marquee property takes 5 parameters:

  • Direction - Defines the direction of the marquee you have the choice of, ahead, auto, backwards, down, forwards, left, reverse, right and up.
  • Increment - Defines the distance the marquee travels per loop.
  • Repetition - This is the number of times it loops around. If you want it to be infinite you can just use the value infinite.
  • Style - This defines the style of the motion on the marquee, alternate, none, scroll, slide.
  • Speed - This defines how fast the marquee will scroll fast, normal, slow

View the demo page to see the different effects created with -webkit-marquee.

.marqueeLeft {
  overflow-x: -webkit-marquee;
  -webkit-marquee: left large infinite slide slow;
  font-size:1.8em;
}
.marqueeAhead {
	overflow-x: -webkit-marquee;
  -webkit-marquee: ahead large infinite slide slow;
  font-size:1.8em;
}
.marqueeAuto {
	overflow-x: -webkit-marquee;
  -webkit-marquee: auto large infinite slide slow;
  font-size:1.8em;
}
.marqueeBackwards {
	overflow-x: -webkit-marquee;
  -webkit-marquee: backwards large infinite slide slow;
  font-size:1.8em;
}
.marqueeDown {
	overflow-x: -webkit-marquee;
  -webkit-marquee: down large infinite slide slow;
  font-size:1.8em;
}
.marqueeForwards {
	overflow-x: -webkit-marquee;
  -webkit-marquee: forwards large infinite slide slow;
  font-size:1.8em;
}
.marqueeReverse {
	overflow-x: -webkit-marquee;
  -webkit-marquee: reverse large infinite slide slow;
  font-size:1.8em;
}
.marqueeRight {
	overflow-x: -webkit-marquee;
  -webkit-marquee: right large infinite slide slow;
  font-size:1.8em;
}
.marqueeUp {
overflow-x: -webkit-marquee;
  -webkit-marquee: up large infinite slide slow;
  font-size:1.8em;
}

Demo

Use CSS To Add Stroke Around Text

With some of the new graphical features you see in CSS3 it can start to add more and more effects to your HTML elements the same way you used to have to use photoshop to do.

Things like box shadow and color gradients are now so easy to do in CSS that you don't even need photoshop anymore. Here is another CSS property that you might not realise existed but it's another effect you would normally do in Photoshop and that's adding a stroke around text, just by using the CSS property text-stroke.

Currently this property is only supported on webkit so it will only work on Chrome or Safari, and you can only use it by prefixing the property with -webkit-.

-webkit-text-stroke: <width>
<color>;

This accepts two values the width of the stroke and the colour of the stroke.

This property now allows you to create a cool outline of any font you are using on your website.

Black Stroke

Black Stroke

.blackandwhite{
	font-family: 'Courgette';
	font-size:3em;
	color: black;
	text-align: center;
        -webkit-text-fill-color: white;
        -webkit-text-stroke: 2px black;
}

Red Stroke

Red Stroke

.redandblack{
	font-family: 'Courgette';
	font-size:3em;
	text-align: center;
	color: red;
        -webkit-text-fill-color: black;
        -webkit-text-stroke: 2px red;
}

Green Stroke

Green Stroke

.greenneon{
	font-family: 'Courgette';
	font-size:3em;
	text-align: center;
	color: green;
        -webkit-text-fill-color: black;
        -webkit-text-stroke: 2px green;
}

As you can see from the above examples is that we have also added another webkit property -webkit-text-fill-color. This property will overwrite the text color with this value. This means that we can set the text color to be something default and this is what the other browsers will see.

Then adding the -webkit-text-fill-color will overwrite the color so we can now use the -webkit-text-stroke to add the webkit stroke.

View the demo to see these affects in different browsers.

Demo

Allow User Editable Content With CSS

On some CMS systems it allows users to directly edit content on the page so that they can preview the change immediately on screen. The way they do this is with Javascript to change any selected paragraph into a textarea and add the paragraph text into this textarea.

But there is a better way you can do this with just CSS, there is a new CSS property which will allow the user to edit the HTML content on-screen.

This property is -user-modify which will change the element to allow the user to edit the content, this is currently only supported on Webkit and Firefox browsers.

Webkit User Modify

-webkit-user-modify: read-only | read-write | read-write-plaintext-only;

This allows for 3 different values:

  • read-only - This is the default and will not allow the user to edit the element
  • read-write - This allows the user to write the content
  • read-write-plaintext-only - User can overwrite the content but only plain text so if you paste anything in here the formatting will be lost.

Moz User Modify

-moz-user-modify: read-only | read-write | write-only

This also allows three values but they are slightly different.

  • read-only - This is the default you can read the content but not write the content.
  • read-write - This allows the user to both read and write the content.
  • write-only - This will just allow the visitor to write then content.

View the demo to see how this will work

Demo

Use In CMS

The way this can be used in a CMS page is for admin user to preview a page of their content on the real page and not in a text editor. Then allow the admin user to click on a paragraph, you can then add the -user-modify CSS property to the clicked element. When this element loses focus you can grab the changed content and use AJAX to update the database with the new content.

Changing Appearance Of Element With CSS

I've recently just found this interesting CSS property that allows you to style any element you want as a browser default element.

Each browser has there own default styling for the standard HTML elements you can find on a page, for example a button in Chrome will look different than a button in Firefox.

With this CSS property you apply this to any HTML element and it will look like the default element in that browser. You can now style that anchor tag link to look exactly like that button, or make a paragraph look like a textbox.

All you have to do is use the CSS property -appearance.

This is currently only supported in webkit and firefox browsers so you need to prefix the property with the browser prefixes.

.lookLikeAButton{
     -webkit-appearance:button;
     -moz-appearance:button;
}
.lookLikeAListbox{
     -webkit-appearance:listbox;
     -moz-appearance:listbox;
}
.lookLikeAListitem{
     -webkit-appearance:listitem;
     -moz-appearance:listitem;
}
.lookLikeASearchfield{
     -webkit-appearance:searchfield;
     -moz-appearance:searchfield;
}
.lookLikeATextarea{
     -webkit-appearance:textarea;
     -moz-appearance:textarea;
}
.lookLikeAMenulist{
     -webkit-appearance:menulist;
     -moz-appearance:menulist;
}

Then you can use these CSS classes in the HTML.

<p class="lookLikeAButton">This is a paragraph
<p class="lookLikeAListbox">This is a paragraph
<p class="lookLikeAListitem">This is a paragraph
<p class="lookLikeASearchfield">This is a paragraph
<p class="lookLikeATextarea">This is a paragraph
<p class="lookLikeAMenulist">This is a paragraph

There different elements you can change the appearance to can be found here.
http://css-infos.net/property/-webkit-appearance

In the demo I change paragraphs to these different elements.

Demo

Disable Text Highlighting With CSS

In a previous article I wrote about how you can change the browser selection colour when you highlight text in your browser.

All you need to do is use a CSS selector class of ::selection and define the style to use on your text when a visitors tries to highlight the test.

But what if you want to do the opposite and want to disable your visitor from highlighting the text all together?

There is an experimental property called user select which will allow you to define new instructions when the visitor tries to highlight your content. This feature can be a good way of making it harder for people to highlight your content and copy it into their own website.

All you have to do is use this CSS property.

.disable_text_highlighting {
-webkit-touch-callout: none;
-webkit-user-select: none; /* Webkit */
-moz-user-select: none;    /* Firefox */
-ms-user-select: none;     /* IE 10  */
/* Currently not supported in Opera but will be soon */
-o-user-select: none;
user-select: none;
}

The values on this property are:

  • Auto - Visitors can select all content in the element.
  • None - Selecting content is disabled.
  • Text - Can only select text content.

Browser Support

This is currently support on webkit, firefox and IE 10. To use the property you need to prefix it with the different browser prefixes but you should also place the non-prefixed version to future proof the property.

View the demo to see what it can do.

Demo

Create A Full Page Background Image

In this snippet we are looking at what you can do with the CSS background-size property. This property will allow you to define the size of the image that you have defined as the background image, similar to how you will use the width and height when adding an image.

The value of the background-size property can either be:

  • Contain - This specifies that the image should be scaled to as large as possible while being less or equal to the background of the element.
  • Cover - This specifies that the image should be scaled to as small as possible while being greater or equal to the background of the element.
  • Percentage
  • Length
  • Auto

Therefore to make the image cover the entire background of the HTML element you will need to use the cover value just like the snippet below.
Continue reading »