Quantcast
Viewing all articles
Browse latest Browse all 10

Learn how to Shorthand CSS Properties

There are a lot of CSS properties that share common names; for example, margin-top, margin-right, margin-bottom, margin-left. Sometimes it gets pretty tedious and confusing to type all of those similar properties, for that reason the CSS shorthand method was created. The CSS shorthand method helps us to increase speed, decrease file size, and be concise. Instead of typing four times the margin property, we can type margin and include all its specifications inside a single property. Let’s learn the different ways to do this:

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

STEP ONE:
Create a HTML page with different tags; such as, headings and paragraphs. Then, link a CSS file to it.
Choose any element of your page and within the CSS give it a different top, right, bottom, and left padding value:

Code Block
style.css
Different padding values:
.footer {
                padding-top30px;
                padding-right10px;
                padding-bottom5px;
                padding-left8px;
}

As you can see, I separately specified each value. Now, let’s learn how to use shorthand to simplify this property: 

Code Block
style.css
Specifying each value within a single padding property:
.footer {
                padding30px 10px 5px 8px;
}
Easy, huh?
I typed padding one time and I specified each value inside of it. 

NOTES:

  • Make sure to follow this order when specifying: top, right, bottom, and left.
  • The same method can be used for the margin property.

STEP TWO:
Now, let’s learn how to specify a property when all the values are the same.
For my paragraph tag I specified (using the old way) a padding of 15px for all its sides:

Code Block
style.css
Same padding values:
p {
                padding-top15px;
                padding-right15px;
                padding-bottom15px;
                padding-left15px;
}

There is no need to specify the same number over and over again, just type the following and you will have 15 pixels on every side of the paragraph box:

Code Block
style.css
Specifying similar values within a single padding property:
p {
                padding15px;
}

We stand behind Server Intellect and their support team. They offer dedicated servers, and they are now offering cloud hosting

STEP THREE:
What about if the top and bottom and right and left share the same values? No problem! You can abbreviate it as shown below:
Old way: 

Code Block
style.css
Top and bottom, and right and left properties share the same values:
h2 {
                margin-top5px;
                margin-right10px;
                margin-bottom5px;
                margin-left10px;
}

Using shorthand:

Code Block
style.css
Abbreviating two distinct values:
h2 {
                margin5px 10px;
}

I specified the top and bottom value first and then the right and left value.

STEP FOUR:
Yes, shorthand is also flexible with other CSS’s properties. Here is an example of how to use it for the background property:

Code Block
style.css
Regular specification for background property’s elements:
.header {
                background-colorwhite;
                background-imageurl(images/HeaderBg.jpg);
                background-repeatrepeat-x;
}

Beneath I used three different background’s properties to specify distinct elements, color, image, and repeat. In this case, the shorthand method is even more flexible, because it lets us specify them in any order. Here is how I shorthanded my background properties:

Code Block
style.css
How to shorthand the background property elements:
.header {
                backgroundwhite url(images/HeaderBg.jpg) repeat-x;
}

Now, it’s time to know if you learn how the shorthand technique works. Give it a try and abbreviate the following border properties:
border-top: 10px;
border-right: 8px;
border-bottom: 10px;
border-left: 15px

Let me know your answer!

We used over 10 web hosting companies before we found Server Intellect. Our new server with cloud hosting,was set up in less than 24 hours. We were able to confirm our order over the phone. They responded to our inquiries within an hour. Server Intellect’s customer support and assistance are the best we’ve ever experienced.

Download Shorthand CSS File


Viewing all articles
Browse latest Browse all 10

Trending Articles