Giter Club home page Giter Club logo

box-model's Introduction

The CSS Box Model

All elements in our HTML page are rendered as a rectangle (or square), even circles, as you will soon see. Let's explore how we can affect elements and our page's layout by setting parameters for a box.

Content

Let's change the shape of our box.

  1. Set the width of our .box to 300px
  2. Set the height of our .box to 300px

Width and height refer to the shape of the content of our box. Note the default value of auto for width/height, behaves differently for each property respectively.

Padding

Our box's content is rather close to the edges of the .box. Let's give ourselves a bit more breathing room.

  1. Set the padding of our .box to 15px

Note the physical size of our box has changed. Imagine scenarios where this might be problematic. Also notice the area around the .box that's been added takes the same colour of the background of the .box

Border

This box looks great, but we need to give our .box something around the edge.

  1. Set a border-width, border-style, border-color to 3px solid #000000 respectively.
  2. Combine these properties together as a single border
  3. Change the border's style to one of dashed, dotted or double.
  4. Give the box a border-radius of 5px
  5. Try changing the border-radius to 50%

Notice once again that the physical size of our box has changed.

Box sizing

  1. Set the box-sizing to border-box
  2. Set this property to all elements going forward with the * selector

Note the physical size of our box is now dictated by our content properties (width/height)

Margin

  1. Set the margin of our .box to 20px

Note the box moves out of it's physical place in the layout. Using our developer tools, notice that the box takes a margin all the way around. Nothing else can get into that space. Also notice something strange is happening to the right of the box.

Expanding padding, border-width, margin

Each of padding, border-width and margin can be set for each of the box's sides individually: top, right, bottom, left

  1. Set padding-top, padding-right, padding-bottom, padding-left to different sizes
  2. Set border-top-width, border-right-width, border-bottom-width, border-left-width to different sizes
  3. Set margin-top, margin-right, margin-bottom, margin-left to different sizes
  4. Abbreviate the original padding, border-width, margin with 2, 3, and 4 numbers

Horizontal centering

Remember the extra margin that ended up at the right of our box?

  1. Set the margin-left and margin-right to auto

This works to center all display:block elements within their container

Maximum width

We can also now consider our .box the first building block of our layout.

  1. Set the width of our element to a %
  2. Set the max-width of of our element to 800px

Notice we now have a layout that has responsive properties

Overflow

In general, we don't ever want to restrict the height of our content such that we may have content that spills outside of it's boundaries.

  1. Add some lorem ipsum to our box to ensure the content goes beyond the height of our box
  2. Set the overflow of our box to hidden
  3. Set the overflow of our box to scroll

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.