Thoughts on programming, web development and design.

Circuit Board

Category: CSS Page 2 of 10

Tips for Debugging CSS

Your CSS isn’t working right. Sometimes your styles don’t do what you expect them to do. What can you do to find the problem and fix it? Debugging CSS can be challenging.

Tips for Debugging CSS

Photo by Yan Krukov from Pexels

Check for syntax errors

Typos and other mistakes can creep into your CSS. Read it from the bottom to the top. When you read it backwards, you may spot errors.

If you don’t see anything wrong, try using the W3C CSS Validator. It may find something that you missed.

Use the Browser DevTools

Most browsers include developer tools. Use these to help diagnose the problem. You can change, update or comment out code. On MDN Web Docs, you can learn more about FireFox’s Developer Tools. If you prefer Chrome, check out Chrome DevTools.

Does your browser support it?

If the browser you are using doesn’t support the CSS property and value you are using, it will ignore it. You can use Can I use to learn if your browser supports this feature.

Comment out or disable the code

When you comment out the code, you can test and figure out where conflicts are occurring. If that doesn’t work, use DevTools to see if one rule is overriding the one you are working on.

Use borders

Add a border to styles that are causing you trouble. The border can help you to see the relationships between elements.

Double check you are editing the right file

Are you sure that you were editing the right file? Did you copy it from your local development machine to the production server? When you are writing code, it can be easy to have many files open at once. Double check to be sure you edited the right one.

Take a break

Sometimes you need to take a break. Go for a walk, talk to a friend, get some water. Time away from a problem can help you figure it out.

Debugging CSS still not working?

Explain the problem to a coworker or a pet. Sometime talking to someone else about the problem helps you figure out what to do next.

Layouts with FlexBox

You can use a CSS library to design a large scale website. What do you do if your needs are simpler? A CSS library like Bootstrap may be too much for your project. Smaller projects may need a simple CSS solution.

Layouts with FlexBox

Image by Gerd Altmann from Pixabay

FlexBox or flexible box can be for design small-scale layouts or applications. For larger projects, you can use it with your favorite CSS library. FlexBox provides you with tools to create layouts that grow and shrink as you need them to.

When would you use FlexBox for layout?

If you are creating navigation menus, web forms, media items or card layouts. Even simple basic grid layouts can be done with FlexBox.

You can use FlexBox to create different kinds of grids. Grids like 3X3, Masonry or Alternating rows. Tobia Sahlin shows you how to build these basic layouts.

What problems can you solve with it?

You can tackle some problems with FlexBox that were difficult using CSS alone. Problems like Vertical Center, Sticky Footers, Input Add-ons and more. Solved By FlexBox shows you 6 different UI solutions that you can do. It includes solutions like vertical centering.

Where do I learn more?

What can you do with FlexBox?

FlexBox is a flexible box layout model for the web. This model gives you a way to automatically rearrange responsive elements. They can adjust in size either increasing or decreasing depending on the device size. FlexBox can be a great addition to your CSS toolbox. It helps you to write adjustable and adaptive CSS.

What can you do with flexbox

Image by Magic Creative from Pixabay

How do I get started?

You can start by reading guides on how to use FlexBox. The Mozilla Developer Network has a guide on the Basic Concepts of Flexbox. Or try CSS-Tricks’ A guide to FlexBox.

If you need to fix a problem right away, you can use Solved By Flexbox to find a solution to common problems.

3 different ways to use it in your projects

1. Centering items on a page

Without FlexBox, it can be challenging to center items like text or images on a page. Sometimes, you have to resort to hacks to get it to look the way you want. FlexBox makes it so much easier. Use these tips to center items either horizontally or vertically.

2. Photogallery

You want to create a photo gallery that is responsive. Use this photo gallery to make one that looks like Flickr or Google Photos.

3. Responsive Tables

Tables are not responsive. You can make them be by using a little bit of FlexBox without having to redesign them.

Need more?

Flexbox Patterns has solutions to problems that you can use. It demonstrates the solution and shows you how to create the flexible box layout yourself.

Getting Started with CSS Variables

What are CSS Variables?

Most programming languages allow you to use variables. Variables are used to store information and manipulated by the program. In CSS, you previously need a preprocessor like SASS or LESS to use variables. CSS Variables are custom properties that you define with specific values and reuse throughout your CSS document.

Getting Started with CSS Variables

Photo by Pexels from Pixabay

In CSS as well as programming languages, you can declare your variables either globally or locally. A global variable is a custom property that is available to your entire HTML document. To declare a global custom property, you use the :root pseudo-class.

For example, you want to define your theme colors. You can use the :root pseudo-class to do that.


:root {
    --main-color: blue;
    --main-text-color: aqua;
}

You can also declare a variable locally. By declaring a custom property locally, you limit the scope or impact that the variable can have.


.primary-background-color {
    --background-color: teal;
}

When you create a CSS Variable, you can overwrite the value. By taking advantage of cascade, you can create a new style that changes the default value of a global declare variable.


.text-color {
    --main-text-color: orange;
}

Var()

Preprocessors allow you to use a variable by referencing its name. You use the Var() function when inserting the value of a custom property.


.featured-content-box {
    background-color: var(--main-color)
}

Calc()

Sometimes you need to do a little math to get your styles to work they way you want them to. Calc() is a native CSS way to do simple math calculations. With it, you can do addition, subtraction, multiplication and division. CSS Tricks talks about use cases for Calc().

For example, you may want to change the width of a style.


.box {
    width: calc(100% - 80px);
}

Why use?

CSS Variables can be overwritten, cascade down and used in JavaScript. If you don’t want to use a preprocessor like SASS or LESS, you can use custom variables to make managing your styles a lot simpler.

In Web Dev Simplified’s CSS Variables Tutorial, he gives you a quick introduction on how to use them. You learn what they are, how to write them and use them with JavaScript.

Summary

At first, CSS Variables sounds complicated. They can help you to make your CSS easier to maintain. Variables are a lot easier to use than hex or hsl codes.

How do you fix problems in print CSS?

A print stylesheet allows you to print a webpage. Testing a print stylesheet can be challenging. You create print styles to manage how the page prints, but it still doesn’t look right. What do you do when you need to fix or find a problem that occurs when the page is printed?

How Do You Fix Problems In Print CSS

Photo by Fernando Arcos from Pexels

You have two options use the Print Preview or the browser’s developer tools.

Print Preview lets you see how your web page looks on the printed page. You can see which elements to hide like navigation bars, footers or certain images. It also helps you to identify simple problems. Problems like large text, too much space between elements and extra styling that may use too much ink.

What do you do when you can’t find the source of a problem? Use your browser. Chrome, FireFox and Safari allow you to display print styles directly in your browser. This option is great for seeing how your design looks without print the page out. You can use the inspector to diagnose issues with your CSS. It can help you identify design issues that need a print style either created or modified.

For example, I had an app that printed a receipt. It printed on two pages instead of one. First, I used the Print Preview to find out what elements to hide when printed. After that, I saw that it was still printing on two pages. I couldn’t see what was causing the issue.

I changed the Developer Tools to show the print CSS. When I used this option to view the receipt with the print styles, I found the source of the problem. The problem was in the footer. I started to look at the styles for it. The footer had a height of 10.75 ems. I experimented by turning off the height. It changed from printing on one page instead of two. To fix this issue, I added a new print style for the footer and made the height auto. When I looked in print preview, it printed an one page.

Page 2 of 10

Powered by WordPress & Theme by Anders Norén