8:00p EST / 7:00p CST / 5:00p PST
A few default properties of CSS can lead to unexpected layout behavior. There are two common culprits:
box-sizing
The CSS box-sizing
property determines how we calculate the total width and height of an element. It has two values:
/* content-box is the default box-sizing
value of all elements */
box-sizing: content-box;
/* border-box will be your layout's best friend! */
box-sizing: border-box;
box-sizing
property of all elements in your project to border-box
/* This declaration will target every element in the project */
* {
box-sizing: border-box;
}
Use CSS resets.
You can create your own reset rules or use any number of well-known reset stylesheets such as:
link
tag in the head
section of your HTML page
universal
selector selects all elements* {
box-sizing: border-box;
}
selector list
groups selectors and applies the same style(s) to themp, h2, h4 {
color: purple;
}
/* All p, h2, and h4 elements will be purple. */
img
tag
Inline images can be styled using CSS properties such as:
background-image
property
div {
background-image: url('wallpaper.png');
}
div {
background-image: url('wallpaper.png');
background-repeat: no-repeat;
background-size: cover; /* cover, contain, or absolute size */
background-position: center center;
width: 400px;
height: 400px;
}
A design and development approach to creating web pages and applications that respond or adapt to different devices and screen sizes.
We can make our pages responsive using three tools in CSS:
*CSS Flexbox and CSS Grid are topics in Web Essentials 2
breakpoint
, that is, the screen size at which to apply a specific style
@media (min-width: 600px){
/* When the screen is at least 600px wide,
apply the following CSS rule: */
p {
font-size: 14px;
}
}
Media queries can be sized up with min-width
@media (min-width: 600px){
/* When the screen is at least 600px wide, do this... */
selector {
property: value;
}
}
Or sized down with max-width
@media (max-width: 600px){
/* When the screen is 600px wide or less, do this... */
selector {
property: value;
}
}
There are common device breakpoints:
But you can also define your own breakpoints to fit your layout!
We can set breakpoint minimum and maximum ranges:
@media (min-width: 600px) and (max-width: 900px){
/* When the screen is between 600px and 900px,
do this... */
}
Media queries also work for screen orientation:
@media (orientation: landscape){
/* When the screen is rotated, do this... */
}
box-sizing
rule to your CSS