CSS Flexible Box Layout, or Flexbox, is a CSS3 web layout model allowing responsive design elements within a container to be automatically arranged depending upon screen size.
Say you have a single header tag on multiple pages throughout your site, but you want certain headers to display in a different color. You could do so by overriding the existing styling method using the !important
override rule:
h1 {
color: blue;
}
div.section h1 {
color: red !important;
}
All h1
header tags will be blue on all pages except for ones that are nested within a div
tag with class section
.
This should be used sparingly. If you add the !important rule to every CSS rule in your code, things could get messy quickly. It's best to use this rule only when necessary.
You can read more about CSS specificity and the !important rule here.
This allows you to stretch and center an image within a div
tag so the image inside the container stretches to each edge without skewing the image:
div.photo {
width: 100px;
height: 100px;
border-radius: 50% 50% 50% 50%;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
overflow: hidden;
}
div.photo img {
width: 100%;
height: 100%;
object-fit: cover;
}
The .photo
class' container creates a rounded edge surrounding the photo with the border-radius
property and hides the excess display outside the container by setting the overflow
property value to hidden
.
This is especially useful for tiled images or profile photos. Here's a quick example to illustrate how it could work with a profile photo:
There are four main pseudo-classes you can use to control link states.
A CSS pseudo-class is a keyword added to a selector that specifies a state for that particular element.
a:link {
color: #ff0000;
}
a:visited {
color: #00ff00;
}
a:hover {
color: #ff00ff;
}
a:active {
color: #0000ff;
}
:link
: styling for an unvisited link:visited
: styling for a visited link:hover
: styling for a mouse hover link:active
: styling for a selected / clicked linkYou can add a :hover
selector to any HTML element, allowing you to add additional styling to an element that a mouse cursor hovers over:
div.text {
color: blue;
}
div.text:hover {
color: red;
}
Here, we're setting the font color of the .text
class element to blue. And when you hover over the element with your mouse, the font color changes to red.
You can also assign transitions when hovering over elements:
div.text:hover {
color: red;
transition: 0.25s all ease-in-out;
}
This will fade the color from blue to red on mouse hover, completing the transition in a fourth of a second, and will be repeated every time you hover the mouse cursor over the element.
The :before
and :after
selectors work similarly, and both use the content
property for content placement within the selector, so I've combined them for this next part.
#my_div {
width: 100%;
font-size: 12pt;
color: white;
}
#my_div:before {
font-size: 20pt;
color: red;
content: "Read: ";
}
#my_div:after {
color: blue;
content: "Read More...";
}
Here, we're creating a div
element with ID my_div
, and a white 12pt font. The :before
selector defines the text "Read: " that will be prepended to the content in the element at a red 20pt font, and the :after
selector defines the text "Read More..." that will be appended to the content in the element with a blue font color.
Here's how it will look:
If certain HTML elements of your page or screen share the same styling features, you don't have to define each set of rules separately. You can separate each of your selectors or classes in a comma-delimited list before defining your CSS rules:
div, .content, .sidebar {
background-color: black;
}
One of the most common scenarios I've run into over the years of web design is text overflow on a particular element. Thankfully, with CSS, there's an easy solution to prevent your text from dropping to multiple lines when this isn't the desired result:
.truncate {
width: 300px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
The container's width is set to 300px to illustrate the text truncation. Setting the white-space
property to nowrap
tells the browser to never push the text to a new line. And the text-overflow
property is set to add an ellipsis to the end of the first line, successfully truncating the text:
There are many effective CSS tricks and techniques you can use in your web projects. These were just among the handful that I have used regularly throughout multiple projects and will use again in projects to come.