Sept 6, 2022
We mentioned before that each HTML element is surrounded by a box. There are many CSS properties that allow us to make changes to this box, like changing the size, adding borders, manipulating padding and margins, and showing/hiding boxes.
The default size of a box is the size of its contents. The width and height properties allow us to set our own dimensions. Sizes are specified in pixels, percentages or ems. For percentages, the percentage specified is the percentage of the containing element.
p { width: 50%;
height: 500px; }
The min-* properties specify the minimum size an element’s box can be. The max-* properties specify the maximum size the element’s box can be.
p { max-width: 450px;
min-height: 100px; }
When we are setting our own dimensions, there is a chance that our content will take up more space than we’ve allowed. The overflow property tells the browser what to do in this case. * hidden - hides extra content that doesn’t fit * scroll - adds a scrollbar, so content can be scrolled
p { max-height: 20px;
overflow: scroll; }
Every box has three additional properties that can affect its appearance:
The border-width property specifies the width of the border. The value is specified in pixels or “thin”, “medium”, or “thick”
p { border-width: 1px; }
The border-style property specifies the style of the border line * solid - solid line * dotted - square dots * dashed - series of short lines * groove - carved style * none - no border
p { border-style: dotted; }
The border-color style specifies the color of the border using the color styles we learned earlier.
p { border-color: #ee3e80; }
There is a border shorthand that allows you to set some of the border properties in one go. They must be in the order of width, style, and color.
p { border: 2px dashed DarkCyan; }
A new property in CSS3, border-radius allows us to have rounded corners. The value indicates the size of the radius in pixels.
p { border-radius: 5px; }
The padding properties specifies the size of the area between the content of the box and the border of the box.
p { padding: 10px; }
The margin property specifies the area between boxes. Special note: if a box sits on top of another box, the margins are collapsed down to be just the larger margin.
p { margin: 10px; }
The display property allows us to override an element’s default. It can also be used to hide an element.
The list-style-type property specifies the style of the bullet point.
The default styling on tables doesn’t help much to follow the data, so if you are presenting tabular data, follow these rules to make sure that the data are easy to follow:
By default, empty cells sometimes cause problems with the display of borders or sizing of cells. The empty-cells property can help with these issues
table { empty-cells: show; }
The border-spacing property controls the gap between cells.
table { border-spacing: 5px; }
The border-collapse allows you to collapse the borders into a single border. border-spacing and empty-cells properties will be ignored.
table { border-collapse: collapse; }
As with tables, the default styling of forms is not as usable as it could be. We can use CSS properties to make the user’s experience of filling out the form less arduous. Consider these properties:
Additionally, input alignment is often a problem in the default styling. We can use the float property and text-align to make sure our input boxes will appear in the same alignment.
.label { float: left;
width: 100px;
text-align: right;
padding-right: 10px; }
There are four types of positioning that we can accomplish in CSS, in addition to the ability to float elements.
A fixed width layout is one that has a set pixel value, which means the layout designs do not change as the user increases or decreases the size of the browser window
A liquid layout will stretch and contract to fill the user’s browser window size. Dimensions are often set with percentages.
A grid structure is often a useful tool for setting the placement or arrangement of content. Grid sets are used to provide consistent proportions for various pieces of content.
CSS frameworks are a quick way to get up and running with common grid sets, but they often add “bloat” to you app, since you usually don’t need everything they offer.