WT Unit-2 CSS
WT Unit-2 CSS
CSS selectors are used to "find" (or select) the HTML elements
you want to style.
Example
Here, all <p> elements on the page will be center-aligned, with a
red text color:
p {
text-align: center;
color: red;
}
The CSS id Selector
The id selector uses the id attribute of an HTML element to select
a specific element.
Example
The CSS rule below will be applied to the HTML element with
id="para1":
#para1 {
text-align: center;
color: red;
}
Example
In this example all HTML elements with class="center" will be red
and center-aligned:
.center {
text-align: center;
color: red;
}
You can also specify that only specific HTML elements should be
affected by a class.
Example
In this example only <p> elements with class="center" will be
red and center-aligned:
p.center {
text-align: center;
color: red;
}
Example
The CSS rule below will affect every HTML element on the page:
* {
text-align: center;
color: blue;
}
h1 {
text-align: center;
color: red;
}
h2 {
text-align: center;
color: red;
}
p {
text-align: center;
color: red;
}
Example
In this example we have grouped the selectors from the code
above:
h1, h2, p {
text-align: center;
color: red;
}
element,element,.. div, p Selects all <div> elements and all <p> elements
Block Elements and Objects: Block elements stack vertically, and objects like
images can be styled.
Lists and Tables: Ordered and unordered lists create lists, while tables display
data in rows and columns.
CSS Id and Class: Use id for unique styling and class for reusable styles
across multiple elements.
Box Model: Composed of content, padding, border, and margin, all of
which can be adjusted to control element spacing and layout.
Block elements in HTML are elements that always start on a new line
and take up the full width available. Common examples include <div>,
<h1>, <p>, <section>, and <article>. These elements typically stack on
top of each other vertically.
<div>This is a block element</div>
Here both <div> and <p> are block-level elements. They will be stacked
on top of each other, each taking up the entire width of the page.
Lists
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<ol>
<li>First</li>
<li>Second</li>
<li>Third</li>
</ol>
Here, <ul> creates an unordered list with bullet points, and <ol> creates
an ordered list with numbers.
Styling Lists:
<ul class="unordered-list">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<ol class="ordered-list">
<li>First</li>
<li>Second</li>
<li>Third</li>
</ol>
.unordered-list {
list-style-type: square;
padding-left: 40px;
.ordered-list {
list-style-type: decimal;
color: green;
padding-left: 40px;
Tables
Tables are used to display data in a structured format, using rows and
columns.
Example:
<table class="styled-table">
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
</tr>
<tr>
</tr>
</table>
Here, the <table> element contains rows (<tr>), and each row contains
table data cells (<td>) or header cells (<th>).
.styled-table {
width: 100%;
border-collapse: collapse;
padding: 10px;
text-align: center;
.styled-table th {
background-color: navy;
color: white;
#unique {
color: red;
Class: The class attribute allows multiple elements to share the same
styling by grouping them under the same class name.
Example:
<p class="common">This is a paragraph with a class.</p>
.common {
color: blue;
The CSS box model defines how elements are structured and spaced on
a webpage. Every HTML element is considered a box, which consists of
four parts:
Content: The text or images inside the element. The content of the box,
where text and images appear
Padding: The space between the content and the border. The padding is
transparent
Border: A line surrounding the padding (optional).
Margin: The space outside the border, separating the element from
neighboring elements.
width: 300px;
padding: 20px;
margin: 30px;
Border Properties:
Example:
div {
Padding Properties:
padding refers to the space between the content of the box and its
border. You can set the padding on all sides or individually.
Example:
div {
div {
padding-top: 10px;
padding-right: 15px;
padding-bottom: 20px;
padding-left: 25px;
Margin Properties:
margin defines the space between the border of the box and the
surrounding elements.
Example:
div {
margin: 50px; /* Sets 50px margin on all sides */
Or
div {
margin-top: 10px;
margin-right: 20px;
margin-bottom: 30px;
margin-left: 40px;
nav a: This selector applies to all anchor (<a>) elements inside the
<nav> tag. It means the rule will style all links within the navigation
bar.
When a user hovers their mouse pointer over one of the navigation links, the
background color of that link will change from its default color (in this case,
likely the color set for the <nav a>) to a dark gray (#575757).
block:
It will take up the full width available (by default), and a new line will
start after it.
inline:
It only takes up as much width as its content and does not force a line
break after it.
inline-block:
The element behaves like an inline element, but it also allows setting
width and height (like a block element).
It stays inline without forcing a new line but can have its dimensions
controlled.
none:
text-decoration Property:
none:
This is commonly used to remove the underline from links ( <a> tags).
underline:
overline:
Grouping: The same styles (color and font) are applied to both h1 and h2 using a grouped
selector.
Dimensions: The .box has specific width and height defined, and uses max-width to prevent
overflow.
Display: .inline-box is an inline-block element while .block-box is a block-level
element.
Positioning: .relative-box uses relative positioning, while .absolute-box is positioned
absolutely in relation to its nearest positioned ancestor.
Floating: .float-left and .float-right elements are floated to the left and right, causing
text or elements to wrap around them.
Aligning:
Grouping in CSS
CSS grouping allows you to apply the same styles to multiple elements without repeating
code. This can be done by separating selectors with commas.
h1, h2, h3 {
color: blue;
font-family: Arial, sans-serif;
Here, h1, h2, and h3 elements all have the same color and font-family properties.
Dimensions in CSS
The dimension properties in CSS allow you to control the size of elements using width,
height, max-width, max-height, etc.
div {
width: 200px;
height: 100px;
max-width: 100%;
the div element will have a width of 200px, a height of 100px, but will not exceed the maximum
width of 100% of its parent container.
Display in CSS
The display property determines how elements are displayed in the browser, such as
whether they are treated as block, inline, or other display types.
p{
display: block;
span {
display: inline;
paragraphs (p) are treated as block elements, occupying the full width, while span elements remain
inline, flowing with the text.
Positioning in CSS
CSS positioning allows you to control the exact placement of elements on the page using
properties like static, relative, absolute, fixed, or sticky.
div {
position: relative;
top: 20px;
left: 10px;
In this case, the div will be moved 20px down and 10px to the right from its normal position.
Floating in CSS
Floating allows you to float elements to the left or right within their container, causing other
content to wrap around them.
img {
float: left;
margin: 10px;
p{
float: right;
width: 50%;
}
The image will float to the left, and the paragraph will float to the right, with 50% of the container
width.
Aligning in CSS
CSS provides different ways to align elements, such as text alignment (text-align) or vertical
alignment (vertical-align).
h1 {
text-align: center;
img {
vertical-align: middle;
}
The h1 heading will be centered within its container, and the image will be vertically aligned in the
middle of its surrounding text.