Here’s a comprehensive overview of commonly used CSS elements and properties that are essential for styling web pages. This includes various selectors, box model properties, typography, backgrounds, positioning, transitions, and more.
Selectors are used to target HTML elements for styling. Commonly used selectors include:
p {
color: blue;
}
.my-class {
font-size: 20px;
}
#my-id {
margin: 10px;
}
a[target="_blank"] {
color: red;
}
a:hover {
text-decoration: underline;
}
p::first-line {
font-weight: bold;
}
As discussed earlier, the box model consists of:
.box {
width: 200px;
height: 100px;
}
.box {
padding: 15px;
}
.box {
border: 2px solid black;
}
.box {
margin: 20px;
}
Styling text elements involves the following properties:
body {
font-family: Arial, sans-serif;
}
h1 {
font-size: 24px;
}
strong {
font-weight: bold;
}
p {
color: #333;
}
h2 {
text-align: center;
}
p {
line-height: 1.5;
}
a {
text-decoration: none; /* no underline */
}
Styling background properties can enhance visual appeal:
div {
background-color: lightgray;
}
header {
background-image: url('header-bg.jpg');
background-size: cover; /* cover or contain */
}
div {
background-position: center center;
}
div {
background-repeat: no-repeat;
}
Common layout properties help control the positioning and flow of elements:
.container {
display: flex; /* or block, inline, inline-block, grid */
}
.flex {
display: flex;
justify-content: center; /* center, space-between, space-around */
align-items: center; /* center, flex-start, flex-end */
}
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.absolute {
position: absolute;
top: 10px;
left: 20px;
}
Defining colors effectively can enhance readability and aesthetics:
p {
color: blue;
}
body {
background-color: #f0f0f0;
}
.transparent {
opacity: 0.5; /* 0 to 1 scale */
}
Adding transitions and animations makes web pages more interactive:
.box {
transition: background-color 0.3s ease;
}
.box:hover {
background-color: lightblue;
}
@keyframes example {
from {background-color: red;}
to {background-color: yellow;}
}
.animated {
animation: example 4s infinite;
}
Borders and shadows can add depth and definition to elements:
.box {
border: 2px solid #000;
}
.box {
box-shadow: 2px 2px 10px rgba(0,0,0,0.5);
}
h1 {
text-shadow: 2px 2px 4px #aaa;
}
Styling lists can improve presentation and user experience:
ul {
list-style-type: square; /* disc, circle, none */
}
ul {
padding-left: 20px; /* Indentation */
}
Media queries are essential for responsive design, allowing styles to adapt to different screen sizes:
@media (max-width: 768px) {
.container {
flex-direction: column;
}
}
These commonly used CSS elements and properties form the building blocks of web design. Mastering these will allow you to create well-structured, visually appealing, and responsive web pages. CSS provides flexibility and creativity, enabling developers to implement various design techniques effectively.