CSS 2: Flex, flex page layout and background-image

Flex property and image background

Flex and page layout

The Flexbox layout justifies and aligns items within its container, while flex child elements expand their width to fill it dynamically.

//we still need to set height, and justify won't change how the space managed
<div class="stripe">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</div>

.goleador{
    display: flex;
    height: 50px;
    flex-direction: column/ row;
}

.goleador div:nth-child(odd){
    flex: 1;
    background-color: purple;
}

.goleador div:nth-child(even){
    flex: 1;
    background-color: yellow;
}
column and row

The values for both justify-content and align-items:

We can use inline-flex to use flex only on a text element width:

chevron-rightInline-flex, triple border and limited border guide hashtag

We use font-awesome icons <i> that work as text:

We use inline-flex to have the border circle only the text width and not the entire container, and also to use all flex properties:

To get a smaller border we reduced the container:

row and column flex layout

The flex-wrap property sets if flex element will fit in one line (nowrap) or can warp onto multiple lines, we can use flex with % to set more precise for the elements.

We don't need to create row containers, the CSS can split by itself.

We can also use wrap-reverse if we needed to change the order.

33% flex layout

When using a flex-container with multiple items we can space them using align-content (different from align-items) or gap:

chevron-rightAlign-content and gap guidehashtag

We have a flex-container that flex wraps,

We use align-content to space the vertical space of multiple flex items:

If we want to define the space between the flex elements we use gap:

align-content and gap

CSS reset and flex navbar

For a flex navbar, we need:

We use CSS reset to cut default CSS styling from the browser:

HTML <img> and Background image

We use the <img> tag for images, it creates a space to link the image to:

chevron-rightImage tag and flex order examplehashtag
<img> with flex order

The aspect-ratio property regulates the proportions between the height/width of the element independently of the screen size.

We can use both the <img> and background-Image to set a low-opacity image background to some centered text.

The text zIndex and position-absolute avoids the backgroundImage opacity.

The background sets the container's height

centered index text on background-image

Background-position and background-size work similarly to how they worked with gradients, on CSS1.

The background-position set with % won't work for 100% background-size, due to how the background interacts with its container. For more details check THIS stackOverflowarrow-up-right.

By default, if a background-image is smaller than the element it's in will repeat itself to fill it.

We use background-clip to extend it underneath its border-box, padding-box, or content-box.

chevron-rightbackround-repeat and background-clip guidehashtag

We can set the background-repeat on the X/Y of the element, or not use it.

while background-clip:

Background.clip and background-repeat different uses

We can include most properties with a background shorthand and put multiple background-images in one element.

chevron-rightMultiple background and shorthand propertyhashtag

Any space that is not filled by the image can be filled by background-color:

We can add multiple backgrounds to an image:

background shorthand using multiple images and background-color

We use a fixed background-image to have a fixed scroll image:

fixed background-image with fixed top navbar

CSS box-shadow

The box-shadow property adds internal/external shadow effects to an element frame.

chevron-rightBox-shadow basic guidehashtag

Box-shadow requires at least 2 values, X/Y offset, it will be solid border/outline:

The third value is blur, it pushes the shadow forward but also blurs it.

Then there is spread, which can expand/reduce the box-shadow from the element size:

Then we can use inset to put the shadow inside the element

box-shadow with different offsets/blur and spread

We can also have single-side box-shadow effects:

chevron-rightSingle side box-shadow guidehashtag

Box-shadow directions are bottom/right, on inset its reversed, also we need a negative spread to avoid any other side shadow from being visible:

single side box-shadow inset effect

CSS scrollbar styling

We use pseudo-elements to style the scrollbar/scrollbar-track/scrollbar-thumb:

chevron-rightScrollbar style guidehashtag

We need to set the scroll overflow in the container first:

The bottom/right scrollbar area is set by height/width:

while scrollbar-track is the actual areas

We use scrollbar-thumb for the css thumb:

CSS scrollbar

The ::-webkit-scrollbar pseudo selector doesn't work in the Firefox browser, we use scrollbar-width and scrollbar-color props.

We create a carousel made of form cards through radio buttons:

CSS form carousel

First, we create the navbar using radio buttons:

chevron-rightRadio buttons navbar guidehashtag

We create radio buttons with their own label:

We then hide the radio buttons with CSS:

radio buttons navbar

Then use CSS to create and move the carousel.

Form card

Last updated