CSS Grid Layout is a powerful 2-dimensional layout system for the web. It allows developers to create complex layouts with ease, using rows and columns.
To start using CSS Grid, define a container element as a grid using display: grid;
.
Set the number of columns and rows using properties like grid-template-columns
and grid-template-rows
.
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: auto;
}
Use properties like grid-column
and grid-row
to position items within the grid.
CSS Grid allows for responsive design, adapting the layout based on the screen size using media queries.
Example Grid Layout:
.grid-item { grid-column: 1 / span 2; }
Flexbox is a one-dimensional layout method for arranging items in rows or columns. It provides powerful alignment capabilities.
Define a flex container using display: flex;
. This establishes a flex context for its children.
Use flex-direction
to specify the direction of the flex items, either row or column.
.flex-container {
display: flex;
flex-direction: row;
justify-content: space-between;
}
The align-items
property aligns items vertically within the flex container.
Flexbox is inherently responsive, making it easy to create flexible layouts that adapt to different screen sizes.
Example Flexbox Layout:
.flex-item { flex: 1; }
By default, elements are positioned static, meaning they follow the normal flow of the document.
Position an element relative to its normal position using position: relative;
.
An element with position: absolute;
is positioned relative to its nearest positioned ancestor.
.positioned-element {
position: absolute;
top: 50px;
left: 100px;
}
Elements with position: fixed;
remain in the same place even when the page is scrolled.
A sticky element toggles between relative and fixed positioning, depending on the scroll position.
Example Positioning:
.sticky-element { position: sticky; top: 0; }
Media queries are used to apply styles based on the device's characteristics, such as screen size, resolution, and orientation.
A media query consists of a media type and one or more expressions that check for conditions of particular media features.
Apply different styles to elements based on the conditions specified in the media query.
@media (max-width: 600px) {
.responsive-element {
display: block;
width: 100%;
}
}
Multiple media queries can be combined using logical operators such as and
, not
, and only
.
Common media features include width
, height
, resolution
, and orientation
.
Example Media Query:
@media (min-width: 768px) { .menu { display: flex; } }
The CSS box model describes the rectangular boxes generated for elements in the document tree and consists of margins, borders, padding, and the content area.
The content area is the innermost part of the box, where the text and images appear.
Padding is the space between the content and the border, giving breathing room inside the element.
.box {
width: 300px;
padding: 20px;
border: 5px solid #f00;
margin: 10px;
}
Borders wrap the padding and content, creating a visible line around the element.
Margins are the outermost layer, providing space between the element and other elements on the page.
Example Box Model:
.box { box-sizing: border-box; }
The float
property is used for positioning and formatting content, allowing elements to float to the left or right of their container.
The clear
property specifies whether an element should be moved below floating elements.
Floats are often used for wrapping text around images or creating column layouts before Flexbox and Grid.
.float-left {
float: left;
width: 50%;
}
.clearfix::after {
content: "";
clear: both;
display: table;
}
A clearfix is a way to clear floats without using additional markup, often implemented using the ::after
pseudo-element.
While floats are still useful, modern layout techniques like Flexbox and Grid have largely replaced them for most layout tasks.
Example Float and Clear:
.clearfix::after { content: ""; clear: both; display: table; }
Flexbox is best suited for one-dimensional layouts, aligning items in a row or column.
Grid is designed for two-dimensional layouts, allowing control over both rows and columns simultaneously.
Flexbox is ideal for distributing space within an item and aligning items in a container.
.flex-container {
display: flex;
justify-content: space-around;
}
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
Grid is perfect for complex layouts with multiple rows and columns, providing more control over the design.
Both Flexbox and Grid can be used together to create robust and flexible layouts, utilizing the strengths of each.
Example of Flexbox and Grid:
.flex-item { flex: 1; } .grid-item { grid-column: 1 / span 2; }
The z-index
property determines the stack order of elements, controlling which element appears on top when they overlap.
A higher z-index value means the element will be closer to the viewer, appearing above elements with lower z-index values.
For z-index to work, the element must have a positioning value other than static, like relative, absolute, or fixed.
.layer1 {
position: absolute;
z-index: 1;
}
.layer2 {
position: absolute;
z-index: 2;
}
Use z-index to manage overlapping elements, ensuring important content is visible and accessible.
Keep z-index values positive and as low as possible to maintain readability and avoid unexpected stacking issues.
Example Z-Index Usage:
.overlay { position: fixed; z-index: 10; }
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies