CSS Introduction 1
CSS Introduction 1
Page/Section Layout
display:flex display:grid display:table
display:none
display:block - This property in CSS defines an element as a block-level
element. Block-level elements take up the full width of their container and
start on a new line. Remember the following rules!
• It takes up the entire width of its parent container by default (unless a width is
specified).
• It starts on a new line, meaning any element before or after it will be pushed
above or below it.
• It respects width, height, padding, and margin.
• Block elements can contain other block or inline elements.
Examples: <div> <h1..h6> <p> <ul,ol,li> <header> <footer> <section> <article>
display:inline - The element only takes up as much width as necessary and
does not start on a new line.
• Width and height properties have no effect on inline elements.
• Inline elements respect horizontal padding and margin, but vertical padding and
margin may not behave as expected.
• Inline elements can be placed within block elements
If you set display: inline; for the parent and display: block; for the child,
and then apply a width to the child. What would be the output ?
center
flex-start
flex-end
space-around
space-between
IT Industry-Academia Bridge Program
Alignment Property (align-item)
The align-items property is used to align the flex items. This is cross-axis
version. This defines the default behavior for flex items laid out along
the cross axis.
align-items: stretch | flex-start | flex-end | center | baseline | first
baseline | last baseline | start | end | self-start | self-end + ... safe |
unsafe;
Alignment Property (align-content)
align-content aligns the entire set of flex lines (not individual items) within
the flex container. The values could be
• flex-start
• flex-end
• Center
• space-between
• space-around
• space-evenly
Flexbox Properties
gap: The gap property explicitly controls the space between flex items.
It can gap: | row-gap | column-gap container {
display: flex;
gap: 10px;
gap: 10px 20px; /* row-gap column gap */
row-gap: 10px;
column-gap: 20px;
}
Perfect Center:- To align flex item perfectly center, use the justify-
content and align-items properties. .flex-container {
display: flex;
height: 300px;
justify-content: center;
align-items: center;
}
Flex Example
Flex
<body>
<div class="container">
Example <style>
.main-content {
body { margin: 0; padding: 0; }
<!-- Main Header --> margin: 0px 50px 0px 50px;
<div class="header"> .container { padding: 20 20 20 20;
<div> Logo</div> height: 100vh; height: 100vh;
<ul> display: flex; display: flex;
<li>Home</li> flex-direction: column; background-color: bisque;
} }
<li>Contact</li>
.header { .right-sidebar , .left-sidebar {
<li>Search</li> display: flex;
display: flex;
</ul> width: 20%;
height: 100px;
<div>Login</div> background-color: blue; background-color: blueviolet;
</div> color: white; justify-content: center;
justify-content: space-between; }
<!-- Main Body Area --> align-items: center; .center {
padding: 10px; display: flex;
<div class="main-content">
} flex: auto;
<div class="left-sidebar">Left Sidebar</div> justify-content: center;
ul {display: flex;}
<div class="center">Center Body</div> align-items: center;
ul li { list-style-type: none; padding: 10px; }
<div class="right-sidbar">Right Sidebar</div> .footer { }
</div> color: white; </style>
<!-- Footer Area --> background-color: black;
<div class="footer"> <p>Footer </p></div> display: flex;
</div> height: 100px;
</body> justify-content: center;
}
Grid Layout
CSS Grid Layout is a powerful two-dimensional responsive layout system
for mobile, tablet, and desktop devices. It allows you to design complex
web layouts with rows and columns.
To start with Grid layout, you have to define a html container element
first with CSS display property as
.wrapper {display: grid}
Or
.wrapper {display: inline-grid}
All direct children of grid containers become grid items and display inside container ‘wrapper’ in single column by default
To create a flexible grid item length, fr unit is used to represent fraction of the available
space in the grid container.
Value “auto” can be use to cover remaining space grid-template-columns: 80px auto auto
Grid Elements properties
repeat- Large grids with many tracks can use the repeat() notation, to repeat all or a
section of the track listing.
grid-template-columns: 1fr 1fr 1fr; == grid-template-columns: repeat(3, 1fr);
- Mix of absolute size with flexible repeat
grid-template-columns: 70px repeat(3, 1fr) 70px;
grid-auto-row/grid-auto-column- These properties define the size for explicit
columns or rows implicitly (default)
.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 200px;
}
.box2 {
grid-column-start: 1;
grid-row-start: 3;
grid-row-end: 5;
}
.box1 {
grid-column: 1 / 4;
Line positioning can be defined by ‘/’ for shortest notation grid-row: 1 / 3;
}
grid-row / grid-column properties
Grid-row & grid-column properties define, which row of column an
element will be displayed on. You can use individual property as
grid-row: <row-start> / <row-end>; grid-row: 2 / 4;
grid-row: <row-start> / span <n>; grid-row: 2 / span 3;
<style> <style>
<style> .relative-container { .relative-container {
.relative-container { background-color: black; position: relative;
background-color: black; width: 200px; background-color: black;
width: 200px; padding: 20px; width: 200px;
padding: 20px; } padding: 20px;
} .box1 { }
.box1 { background-color: lightcoral; .box1 {
background-color: lightcoral; padding: 20px; background-color: lightcoral;
padding: 20px; } padding: 20px;
} .box2 { }
.box2 { position: relative; .box2 {
background-color: lightgreen; background-color: lightgreen; position: absolute;
padding: 20px; padding: 20px; top: 10px;
} top: -25 left: 100px;
</style> } background-color: lightgreen;
</style> padding: 20px;
}
</style>
Important properties
CSS z-index determines the stacking order. Elements with higher z-
index values will be layered on top.
CSS transform is a powerful property that allows you to apply 2D or 3D
transformations to an element, affecting its position, size, and shape
without altering the document flow. Its value could be translate, rotate,
scale, skew, materix.
translate(x, y): transform: translate(50px, 100px);
Moves an element from its original position.