What Is CSS?: CSS Stands For Cascading Style Sheets Media
What Is CSS?: CSS Stands For Cascading Style Sheets Media
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
When tags like <font>, and color attributes were added to the HTML 3.2 specification, it
started a nightmare for web developers. Development of large websites, where fonts and
color information were added to every single page, became a long and expensive process.
To solve this problem, the World Wide Web Consortium (W3C) created CSS.
CSS Syntax
A CSS rule-set consists of a selector and a declaration block:
A CSS declaration always ends with a semicolon, and declaration blocks are surrounded by
curly braces.
CSS Selectors
CSS selectors are used to "find" (or select) HTML elements based on their element name, id,
class, attribute, and more.
element Selector: The element selector selects elements based on the element
name.
id selector: selects elements based on the element id.
class selector: To select elements with a specific class, write a period (.) character,
followed by the name of the class. Example “.center”
Grouping Selectors
h1, h2, p {
text-align: center;
color: red;
}
CSS Comments
Comments are used to explain the code, and may help when you edit the source code at a
later date.
A CSS comment starts with /* and ends with */. Comments can also span multiple lines:
COLORS
Colors are specified using predefined color names, or RGB,
HEX, HSL, RGBA, HSLA values.
CSS Backgrounds
background-color
background-image
background-repeat
background-attachment
background-position
Border Style
The border-style property specifies what kind of border to display.
The border-style property can have from one to four values (for the top border, right border,
bottom border, and the left border).
Example
p.dotted {border-style: dotted;}
p.dashed {border-style: dashed;}
p.solid {border-style: solid;}
p.double {border-style: double;}
p.groove {border-style: groove;}
p.ridge {border-style: ridge;}
p.inset {border-style: inset;}
p.outset {border-style: outset;}
p.none {border-style: none;}
p.hidden {border-style: hidden;}
p.mix {border-style: dotted dashed solid double;}
CSS Margins
The CSS margin properties are used to create space around elements, outside of any defined
borders.
With CSS, you have full control over the margins. There are properties for setting the margin
for each side of an element (top, right, bottom, and left).
CSS has properties for specifying the margin for each side of an element:
margin-top
margin-right
margin-bottom
margin-left
CSS Padding
The CSS padding properties are used to generate space around an element's content, inside
of any defined borders.
With CSS, you have full control over the padding. There are properties for setting the
padding for each side of an element (top, right, bottom, and left).
The height and width can be set to auto (this is default. Means that the browser calculates the
height and width), or be specified in length values, like px, cm, etc., or in percent (%) of the
containing block.
Setting max-width
The max-width property is used to set the maximum width of an element.
The max-width can be specified in length values, like px, cm, etc., or in percent (%) of the
containing block, or set to none (this is default. Means that there is no maximum width).
The problem with the <div> above occurs when the browser window is smaller than the
width of the element (500px). The browser then adds a horizontal scrollbar to the page.
Using max-width instead, in this situation, will improve the browser's handling of small
windows.
Note: Drag the browser window to smaller than 500px wide, to see the difference between
the two divs!
Content - The content of the box, where text and images appear
Padding - Clears an area around the content. The padding is transparent
Border - A border that goes around the padding and content
Margin - Clears an area outside the border. The margin is transparent
The box model allows us to add a border around elements, and to define space between
elements.
div {
width: 320px;
padding: 10px;
border: 5px solid gray;
margin: 0;
}
ALSO LOOK IN TO TEXT FORMATTING, TEXT ALLINGMENT, FONTS, LINKS AND ICONS.
CSS Lists
UNORDERED LIST AND ORDERED LIST.
unordered lists (<ul>) - the list items are marked with bullets
ordered lists (<ol>) - the list items are marked with numbers or letters
list-style-type
list-style-image
list-style-position
list-style-type: none; : This property can also be used to remove the markers/bullets. Note that the
list also has default margin and padding. To remove this, add margin:0 and padding:0 to <ul> or <ol>:
CSS Tables
<!DOCTYPE html>
<html>
<head>
<style>
#customers {
border-collapse: collapse;
width: 100%;
padding: 8px;
#customers th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #4CAF50;
color: white;
</style>
</head>
<body>
<table id="customers">
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Berglunds snabbköp</td>
<td>Christina Berglund</td>
<td>Sweden</td>
</tr>
<tr>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Königlich Essen</td>
<td>Philip Cramer</td>
<td>Germany</td>
</tr>
<tr>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td>Italy</td>
</tr>
<tr>
<td>North/South</td>
<td>Simon Crowther</td>
<td>UK</td>
</tr>
<tr>
<td>Paris spécialités</td>
<td>Marie Bertrand</td>
<td>France</td>
</tr>
</table>
</body>
</html>
The border-collapse property sets whether the table borders should be collapsed into a
single border:
The example below sets the width of the table to 100%, and the height of the <th> elements
to 50px:
Horizontal Alignment
The text-align property sets the horizontal alignment (like left, right, or center) of the content
in <th> or <td>. By default, the content of <th> elements are center-aligned and the content
of <td> elements are left-aligned.
Vertical Alignment
The vertical-align property sets the vertical alignment (like top, bottom, or middle) of the
content in <th> or <td>. By default, the vertical alignment of the content in a table is middle
(for both <th> and <td> elements).
Table Padding
To control the space between the border and the content in a table, use the padding
property on <td> and <th> elements:
Hoverable Table
Use the :hover selector on <tr> to highlight table rows on mouse over:
Striped Tables
For zebra-striped tables, use the nth-child() selector and add a background-color to all even (or odd)
table rows:
What is a Grid-View?
Many web pages are based on a grid-view, which means that the page is divided into
columns: Using a grid-view is very helpful when designing web pages. It makes it easier to
place elements on the page
A responsive grid-view often has 12 columns, and has a total width of 100%, and will shrink
and expand as you resize the browser window.
*{
box-sizing: border-box;
}
The CSS box-sizing property allows us to include the padding and border in an element's total width
and height.
However, we want to use a responsive grid-view with 12 columns, to have more control
over the web page. First we must calculate the percentage for one column: 100% / 12
columns = 8.33%.
Then we make one class for each of the 12 columns, class="col-" and a number defining how
many columns the section should span:
It uses the @media rule to include a block of CSS properties only if a certain condition is true.
HTML code
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: lightgreen;
body {
background-color: lightblue;
</style>
</head>
<body>
<p>Resize the browser window. When the width of this document is 600 pixels or less, the
background-color is "lightblue", otherwise it is "lightgreen".</p>
</body>
</html>
Add a Breakpoint
In case a web page made with rows and columns, and it was responsive, but it did not look
good on a small screen. Media queries can help with that. We can add a breakpoint where
certain parts of the design will behave differently on each side of the breakpoint.
You can add as many breakpoints as you like. We can also insert a breakpoint between
tablets and mobile phones.
For desktop: The first and the third section will both span 3 columns each. The middle
section will span 6 columns.
For tablets: The first section will span 3 columns, the second will span 9, and the third
section will be displayed below the first two sections, and it will span 12 columns:
<div class="row">
<div class="col-3 col-s-3">...</div>
<div class="col-6 col-s-9">...</div>
<div class="col-3 col-s-12">...</div>
</div>
/* Extra large devices (large laptops and desktops, 1200px and up) */
@media only screen and (min-width: 1200px) {...}
HTML CODE
<!DOCTYPE html>
<html>
<head>
<style>
.example {
padding: 20px;
color: white;
/* Small devices (portrait tablets and large phones, 600px and up) */
}
/* Medium devices (landscape tablets, 768px and up) */
/* Extra large devices (large laptops and desktops, 1200px and up) */
</style>
</head>
<body>
<p class="example">Resize the browser window to see how the background color of this paragraph
changes on different screen sizes.</p>
</body>
</html>