css_faq
FAQ
Align element center/center
Align
block element
center withmargin:auto
the browser assigns half the remaining space to margin-left and the other half to margin-right, this is only works for block element so <img> not working, you need to change itdisplay: block
ordisplay:inline-block
.Align text center
1
2
3
4
5
6
7.center {
margin: auto;
width: 50%;
}
.text {
text-align: center;
}inline, block, inline-block difference
Compared to display: inline, the major difference is that display: inline-block allows to set a width and height on the element, it’s always set for real inline elment that wants width or height.
Compared to display: block, the major difference is that display: inline-block does not add a line-break after the element, so the element can sit next to other elements
Note: for real inline element, you can set padding, border, background, but cant NOT set width and height.
what happens if content is too big to fix in the container?
The CSS overflow property controls what happens to content that is too big to fit into an area.
The overflow property specifies whether to clip the content or to add scrollbars when the content of an element is too big to fit in the specified area.
The overflow property has the following values:
- visible - Default. The overflow is not clipped. The content renders
outside the element's box
- hidden - The overflow is clipped, and the rest of the content will be
invisible
- scroll - The overflow is clipped, and a scrollbar is always added to see the rest of the content at X/Y.
- auto - Similar to scroll, but it adds scrollbars only when necessary.
web layout
float property used for what?
The float property is used for positioning and formatting content
e.g. let an image float left to the text in a container, the float limited inside the conainer!!!
.
The float property can have one of the following values:
- left - The element floats to the left of
its container in the same line
- right - The element floats to the right of its container
- none - The element does not float (will be displayed just where it occurs in the text). This is default
- inherit - The element inherits the float value of its parent
In its simplest use, the float property can be used to wrap text around images. The image as a child of p
tag or sibling of p
have different effect. it’s floated inside the container!!!
Another good example, float makes the <li> sit side by side in the same line
.
1 | li { |
display
The display property specifies if/how an element is displayed, every HTML element has a default display value depending on what type of element it is. The default display value for most elements is block or inline
.
A block-level element always starts on a new line and takes up the full width available (stretches out to the left and right as far as it can), we can prevent this by setting width property to prevent it from stretching out to the edges of its container.
1 | <div> |
An inline element does not start on a new line and only takes up as much width as necessary
1 | <span> |
As mentioned, every element has a default display value. However, you can override this, Setting the display property of an element only changes how the element is displayed
, NOT what kind of element it is. So, an inline element with display: block; is not allowed to have other block elements inside it but can set width, height the fake block!!!
.
1 | li { |
- display: none, take no space from layout, seem no element
- visibility:hidden; also hides an element. but it still takes the space(width and height) of layout, just can’t see it!!!
- width: 0 and overflow: hidden, can’t see the element as well, but the element still takes(height) of layout. just can’t see it!!!
Flex
Flexbox is made for one dimensional layouts, there are two concepts here, flex container and flex items, flex container is a block element, any element with display: flex, it will behaves like a box starts a new line, scretch full width of it's container
, all flex items behaves like inline-block, its width equals its content length
, they sit one by one in one direction, but they are flexible, that means their width can grow(should set flex-grow
) if there is free space left in the flex container or their width can shrink if there is less space in the conatiner. ther width are not fixed, even you set it explicitly. but there is the default behavior, you can change the default behavior by setting proper property.
As the width are not fixed, what’s the final value of flex items
initial value(width) = equal to content length or width if set explicitly
.- may grow or shrink if allowed to the final width
flex container property setting
1 | .flex_container { |
grow and shrink on each flex item
The flex-grow property specifies how much the item will grow relative to
the rest of the flexible items inside the same container, it grows only when there is free space left after the initial value
.
1 | .flex_item { |
More examples refer to Flex example;
Main difference between display: flex; and display: inline-flex; is that display: inline-flex; will make the flex container an inline element while its content maintains its flexbox properties
.
ALign of flex
- align-items: specifies the default alignment for items inside the flexible container
vertically
- justify-content: aligns the flexible container’s items when the items do not use all available space on the main-axis
(horizontally)
- align-content: modifies the behavior of the flex-wrap property. It is similar to align-items, but instead of aligning flex items, it
aligns flex lines
only valid with wrap property set, as in that case more lines can happen for flex items. - align-self: specifies the alignment for the selected item inside the flexible container, the flexible container’s align-items property.
It only works for the direct children(which is called flex items), it does not affect the elements inside each items
1 | <div class="flex"> |
Wrap means if flex item width is over the container’s width, the item starts with a new line, by default no wrap that means each item will shrink to hold the item in the same line.
Note: Use the align-self property of each item to override the align-items property
can fixed item be flex item?
element with position: fixed
itself can NOT be flex item, but Can be flex container. if you check flex layout with developer tool, you will see such item is not flex item!!! that means flex setting not work for such item!!!
Grid
Grid is made for two dimensional layouts, Row and Column. Grid will give you more flexibility, you can control how row and column display
. each row is spilitted into columns, the direct children(grid item) fall into each column automatically.
Grid Container
1 | .grid-container { |
Explicity container
1 | .grid-container { |
As explicit container only says part of column’s width and part of row’s height.
1 | .grid-container { |
dynamic columns
Above the grid has fixed column number, even there is free space left in the row, but auto-fill
keyword creates as many tracks as fit into the grid container without causing the grid to overflow it, same thing for auto-fit
except that after grid item placement it will only create as many tracks(columns) as needed and any empty repeated track collapses
.
1 | .grid { |
Grid item
By default each grid item takes one column from one row
but you can change the default behavior to let one item to take more columuns and more rows.
1 | .item1 { |
Most of time, we use named area to do this, as it’s easy to understand.
1 | //NOTE: NO "" quotes for each area name!!! |
Note
Flex and Grid are note standalone, you can use Flex inside Grid, combine them, also Flex can achieve grid system this’s boostrap4 does.
Bootstrap grid system
Bootstrap3 uses float/width and more hacks to achieve its grid system, from bootstrap4, it uses CSS flex box to do this, as flex is widely supported by most browsers, CSS Grid is more powerful, but not all browsers support it, but someday in future, after it’s widely supported, bootstrap may change its grid system to use CSS Grid.
CSS grid is currently not an official standard (it is a W3C Candidate Recommendation) although it has been adopted by most major browsers, as of October 2017, Chrome, Firefox, Safari and Edge all support CSS grid without vendor prefixes.[3][4][5] IE 10 and 11 support CSS grid but with an outdated specification. On mobile, all modern browsers support CSS grid except for Opera Mini and UC Browser.
Bootstrap grid vs CSS grid for the same layout
responsive layout
1 | <body> |
CSS Grid is more cleaner
1 | <body> |
1 | ... |
Browser extensions to CSS
CSS defines the standard way(property name, value) that all browser should support
, but some browsers support it in their own way that’s the extension for, browser extensions are only valid for that particular type of browser, if you only use the standard CSS property, the style may not take effect in some version of browser, so check that if browser supports it at canIuse;
Firefox | Chrome | Microsoft(IE) | |
---|---|---|---|
prefix | -moz-xxx | -webkit-xxx | -ms-xxx |
1 | @mixin border-radius($radius) { |
how to change the cursor when it moves to element
1 | <span style="cursor:pointer">pointer</span><br> |
input with icon
1 | input[type=text] { |
Note: In real world, you don’t need to write property for each browser as it could be done by tool like PostCSS;
how to make a round or oval
let’s say we have an element like div or img how to make it a round or oval, use border-radius: 50%
1 | .img { |
set multiple imgs as background
For some case, you may want to set multiple images as a background for an element, CSS allows us to do this and set peroperties for each imge seperately. The different background images are separated by commas, and the images are stacked on top of each other
, where the first image is closest to the viewer
, if overlap happens, you see the first image for overlap part.
1 | #example1 { |
gradients only for color!!!
CSS gradients let you display smooth transitions between two or more specified colors
1 | #grad { |
How to set shadow?
There are two kind of shadowns text-shadow
and box-shadow
;
1 | h1 { |
how to use web font which is not installed on client computer.
1 | @font-face { |
transition/transform/animation
transform
CSS transforms allow you to move, rotate, scale, and skew elements based on the center of element itself.
- 2D transforms: X, Y
- 3D transforms: X, Y, Z
Transform performs on any element.
Both 2D and 3D use the same property, but 2D only support X,Y function, 3D added Y function.
- The translate() method moves an element from its current position (according to the parameters given for the X-axis and the Y-axis)
- The rotate() method rotates an element clockwise or counter-clockwise according to a given degree
- The scale() method increases or decreases the size of an element (according to the parameters given for the width and height)
- The skew() method skews an element along the X and Y-axis by the given angles.
- The matrix() method combines all the 2D transform methods into one, the parameters are as follow: matrix(scaleX(),skewY(),skewX(),scaleY(),translateX(),translateY())
1 | .div { |
transition
CSS transitions allows you to change property values smoothly
, over a given duration. you have to specify
- allowed property(default all)
- when it happens
- how long it keeps
At last set the value change to trigger the transition
1 | div { |
animation
An animation lets an element gradually change from one style to another. the style includes several properites
You can change as many CSS properties you want, as many times you want
1 | div { |
negative value in top/left/right/bottom and margin
1 | .div { |
top/left/right/bottom value
If position: absolute; or position: fixed; - the left property sets the left edge of an element to a unit to the left of the left edge of its nearest positioned ancestor
If position: relative; - the left property sets the left edge of an element to a unit to the left/right of its normal position.
The value can be
- length Sets the left edge position in px, cm, etc. Negative values are allowed
- % Sets the left edge position in % of containing element. Negative values are allowed, 10% if container is 100px, 10% means 10px!!!
1 | div.b { |
what’s the difference for width and max-width
width has a fixed length, no matter the viewpoint, if viewpoint is small, scroll bar is added
max-width: it limits the max length of the element when brower is large, but when viewpoint is small, no scroll bar!
hence same value for width and max-width, max-width is responsive.
how to define large button
Acutally button size is determined by font-size, if font is large, the button is large as well. but you can set its width like this
1 | .button1 {width: 250px;} |
how to resize element by drag
The resize property specifies if (and how) an element should be resizable by the user.
1 | div { |
beatiful tooltips
1 |
|
debug css by firefox
The Rules view lists all the rules that apply to the selected element, ordered from most-specific to least-specific, some from itself, some from its parent. see below
cross-line with attribute mean it’s overwritten!
A warning icon appears next to unsupported or invalid styles. This can help you understand why certain styles are not being applied:
rule display
px/em/rem/vw/fr
- px: fixed size
- em: relative to it’s parent
- vw: 1vw = 1% of viewport width. If the viewport is 50cm wide, 1vw is 0.5cm. font-size is responsive!!!
1
2
3
4child {
font-size: 2em; //relative to parent's font-size
width: 2em; //relative to itself font-size
} - rem: relative to root
Note: font-size: 16px by default
1 | <style> |
css useful snippet
1 | //“Responsive” media |
what’s value for css property.
As you know that property can be inherited from its parent, but not ALL properties support this, that means for a property that’s not set explicitly from inline style, internal style, external style, its value depends on what type it’s
Value of a property which is Not set explicitly
- default value (non-inherited properity)
- its parent value (inherited property)
Actually, most of properties can be NOT inheritid.
Inherited Property
- color
- font-size
- font-family
- font-weight
- font-xxx
- list-style
- list-xxx
- visibility
- text-indent
- text-transform
- text-align
- text-shadow
- :hover
height and width for an element
defaut value
It depends on the type of elmenent, auto means different
inline element you can’t set height and width
height: auto
means the height of its contentwidth: auto
means the length of its content
block element you can set height and width
height: auto
means the height of it’s contentwidth: auto
means scratch the full of its container
element with position: fixed
like inline but without container
As fixed element out of document flow, its width and height equal its content length if not set explicitly
otherwise belong to user setting.
div with position: fixed
will not start a new line and scretch its container, as it out of document flow.
explicit setting
explicit setting has the highest priority than others, element always belongs to it if set.
1 | div.a { |
Note: position property can effect height and width as well if set in pairs
1 | div.a { |
how to use relative and absolute and fixed
- static Default value. Elements render in order, as they appear in the document flow
- absolute The element is positioned relative to its first positioned (not static) ancestor element, it must have a parent with(fixed, relative, sticky)
- The element is positioned relative to the browser window
- The element is positioned relative to its normal position, so “left:20px” adds 20 pixels to the element’s LEFT position
- The element is positioned based on the user’s scroll position
Note: the absolute and fixed element behaves like inline element not scretch at all even it’s block element. but relative still remains