





















Studirajte zahvaljujući brojnim resursima koji su dostupni na Docsity-u
Zaradite bodove pomažući drugim studentima ili ih kupite uz Premium plan
Ova stranica nije vidljiva u pregledu
Ne propustite važne delove!






















Credits
Author
Aravind Shenoy
Reviewer
Radek Stepan
Lead Technical Editor
Sweny Sukumaran
Technical Editor
Dennis John
Proofreader Stephen Copestake
Production Coordinator Aparna Bhagat Adonia Jones
Cover Work Prachali Bhiwandkar
About the Author
the Manipal Institute of Technology, his core interests include technical writing, web designing, and software testing. He is a native of Mumbai, India, and currently resides there. He has penned down books such as Hadoop: A Big Data Initiative and Thinking in JavaScript. He has also authored the bestselling book: HTML5 and CSS3 Transition, Transformation, and Animation, Packt Publishing , (http://www.packtpub.com/html5-and-css3-for- transition-transformation-animation/book). He is a music buff with The Doors , Oasis , and R.E.M ruling his playlists.
Overview
You will gain an intermediate knowledge of CSS after reading this book. Instead of wandering through loads of theory, we will understand CSS more practically so that we can design a webpage using CSS. We have used Notepad for the examples in this book. Alternatively, you can also use Notepad++ or any advanced editor. All that you need to do is copy the code and paste it into Notepad. Upon execution, you will get the output as depicted in the screenshots. Screenshots are provided for each sample code.
Coding gets better with practice. The examples in this book are compatible with almost every browser. Instead of using the verbatim code, you can modify the code and see the change in the output, thereby understanding the subtle nuances of CSS. By the end of the book, with practice, you can achieve better things and get more acquainted with CSS.
Thinking in CSS
HTML originated from a prototype of a language created by Tim Berners-Lee in 1992. Berners-Lee felt that there was a possibility of linking documents together using hypertext, and the concept of HTML evolved from this. A breakthrough in this field of development was the introduction of CSS along with HTML 4.0. Prior to the introduction CSS, web designers and developers were using HTML for formatting purposes. Formatting tags and styling using HTML defeats the purpose of HTML as HTML elements and attributes must only define the structure of a webpage. The purpose of CSS was to take styling out from structural markup.
With the introduction of CSS, we could separate presentation from content. As a result, presentation could be removed from the HTML document and stored in a separate file, which would be included in the document using a link tag. Thus, all of the presentational HTML elements and attributes were replaced by CSS to provide versatility and better accessibility. Now, we can just define or modify the look of a webpage by making changes in the stylesheet without actually touching the code. In this book, we will take a look at the essentials. We are going to touch on those topics that every web designer or developer needs to know. Instead of wandering through loads of theory, we will understand CSS more practically so that we can design a web page using CSS by the time you are done with the book. We expect you to know the basics of HTML as we will incorporate CSS in simple HTML code.
Incorporating CSS in HTML
We can incorporate CSS in HTML in the following ways:
f Using inline styles f Using internal styles f Using external stylesheets f Using the import command
The output of the code would be as follows:
Using external stylesheets
External stylesheets are the preferred and appropriate way to incorporate CSS in HTML code. We must always remember that HTML and CSS should be kept separate. It is good practice to keep the CSS stylesheet as a standalone document. We need to use the following code snippet in the head section of the HTML code to invoke the CSS stylesheet:
The following code is a simple HTML code. We will define the CSS sheet after the HTML code. In the following example code, our CSS file would be called main.css:
Packt Publishing has an online library called PacktLIB
Now we create a main.css file (an external stylesheet) and define the div element as follows:
div { color: red; }
The output of the code would be as follows:
Imagine we have a lot of web pages, which is the norm for any website. If we use the inline or internal way of defining CSS, we would have to search for the styled elements in each HTML page. However, if we maintain a separate standalone stylesheet for CSS, we just have to modify the properties in the stylesheet and the modification would be very systematic. It is good practice to keep the CSS stylesheet separate from the HTML page. Moreover, if we have lots of common styles defined for several HTML elements, all we need to do is redefine the styles in the standalone CSS stylesheet, thereby maintaining a more semantic approach.
Using the import command
Let's understand how the import command works. Suppose we have defined an external stylesheet for a website. Say a new requirement comes up where we need the color of the font to be changed to yellow for the first page when it is actually defined as blue in the main. css CSS stylesheet. We use the import command for this purpose. Hence, we create a new stylesheet and give it a name of our choice, say change.css. Then we invoke the change. css stylesheet using the import command. This way, only the first page is affected, and the rest stay the same. The following code snippet shows how we incorporate the import command in our HTML code:
We have to define the import command between the style tags.
We have discussed the various ways in which we can apply CSS to a webpage.
However, we need to remember that the most appropriate way to include CSS is to maintain a standalone stylesheet. It is more systematic and easily modifiable that way.
In this book, for the sake of convenience, we will use the internal way of implementing CSS.
Defining CSS
Let's look at the basic structure of CSS:
selector { property:value; }
The selector (in this case, div) is to be written first. The property and value have to be defined within the curly brackets. color, text-decoration, and font-size are examples of properties used in CSS. A value is given to each property. For example, red is a value given to the color property. A colon separates the property and the value. The semicolon that follows the property completes the structure. If you observe the previous code snippet, you can see that we have defined property twice. It demonstrates that multiple properties can be defined and are separated by semicolons. Let's have a look at the following code snippet:
div { color: red; margin: 20px; }
Grouping selectors
In CSS, we can group any number of selectors to define properties that are common. We do not have to define selectors separately, as that would create a humongous amount of code. Grouping helps us in writing less and systematic code. Let's have a look at the following code to see how it works:
Example of Grouping selectors
Packt Publishing
Packt Online LibraryThe output of the code would be as follows:
If we observe the output, it is evident that the color property is common for the p and div elements. Hence, the text defined between the p and div elements has the same navy color. See how we have separated p and div by a comma. We can also club ID and class selectors along with the other selectors.
The following code snippet will show you how it is done:
p, #packt, .pubman { color: red; }
As we can see, the p element, the packt ID, and the pubman class have been clubbed together and have been assigned the same property and value.
Let's now have a look at the concept of nesting selectors within one another.
Nesting selectors
We can apply styles to a selector within a selector. Let's have a look at the following code to see how it is done:
Nesting Selectors
#packt p {color: orangered;}
Packt Publishing
Packt Publishing, Always finding a wayPackt Online Library
Packt Publishing Article Network
The output of the code would be as follows:
In the previous code, we have defined the lime color for all the p elements initially. After that, we have defined the yellow background for the div element, which is represented by a unique packt (#packt) ID. Inside the div element, we have defined a p element to which we have assigned the orangered color. It is evident from the output that the p elements outside div are lime in color. However, the style for the intrinsic p element in div is orangered in color. Hence, Packt Publishing and Packt Publishing Article Network are lime in color whereas Packt Online Library is orangered in color.
Fonts and text
In HTML, the size of a font can be increased using tags such as h1 and h2 to mention a few. However, as we already know, HTML is to be used for markup, whereas CSS is to be used for styling. Hence, fonts are imperative in CSS. Let's have a look at the following code to understand the various font styles in CSS:
Fonts in CSS
text-decoration: underline; text-align: center; } #pubman { color: orangered; text-align: left; text-indent:50px; } div { text-transform:uppercase; color: navy; text-decoration: line-through; text-align:justify; }
Packt Publishing
Packt Online Library
Packt Article Network
Packt publishes books customized for you on various technologiesThe output of the code would be as follows:
text-decoration determines whether a line runs over, under, or through the text. text- align will position the text to the right, left, center, or justify it according to the layout of the web page. For indentation of the text, we use the text-indent property. text-transform will change the case of the text. For example, we can change the text to uppercase or lowercase using the text-transform property.
If you observe the output of the code, you can see that line text-decoration: line- through inserts a line through the text, whereas text-transform:uppercase changes the case of the line to uppercase. So you can see how fonts and text can be manipulated in CSS.
Anchor pseudo classes
If you check out modern websites nowadays, the hyperlinks are in blue color and the color changes to purple if that web page has been visited. We achieve this using the anchor pseudo classes in CSS. Let's have a look at the following code to see how this works:
Anchor Pseudo Classes in CSS
Packt Publishing
Packt Online Library
The output of the code would initially be as follows:
Say we click on the Packt Publishing link; we would see the link turn lime. The output would be as follows:
The output of the code would be as follows:
If we observe the previous code and the subsequent output, we can see that the border has a dotted pattern. We can also use solid or dashed values to define a border-style. The border-width property can set the width of the border. The border-radius property is a CSS3 property and defines the curvature of the border. The border-color helps us to assign a color to the border. In the previous code, we have used a dotted border of lime color with a border width of 2 pixels and a border radius of 25 pixels. In the previous code, we have used the margin and padding properties, which will be explained in the next section.
Margin and padding
Margin and padding are used extensively in designing. Margin is the space outside any defined element, whereas padding is the space inside any defined element.
Let's have a look at the following code to understand it better:
Margin and Padding in CSS
Packt Publishing
Packt Publishing
The output of the code would be as follows:
In the previous code, the element defined in CSS by the ID selector has the margin defined. Hence, in the output, the position of the element referenced by ID is different from the previous element that has no margin defined. An element has four sides: top, bottom, left, and right. Hence, the margin is the distance from each side to the neighboring element or the borders of the page. If we observe the output of the code, the element referenced by the packt ID is at a different position from the other one, which has no margin. The margin cannot be seen as it has no background color and is transparent.
There is a shorthand way in which margin can be defined as shown in the following code snippet:
p { margin: 50px 30px;}
The top and bottom margin for the element would be 50 pixels whereas the right and left margin would be 30 pixels.
We will now discuss padding. Padding is different from margin in the sense that the distance between elements is not taken into consideration. In padding, what matters is the distance between border and content of an element. We will have a look at the same HTML code, but we will tweak it a bit in this case and introduce padding for the defined elements. Let's have a look at the following code to understand it better:
Margin and Padding in CSS
Packt Publishing