The CSS treats each HTML element as if it lives in its own box.You can set several properties that affect the appearance of these boxes. In this article you will see how to:
- Control the dimension of your boxes
- Create borders around boxes
- Show and hide boxes
- Apply margins and padding for boxes
Dimension of Boxes
By default a box is sized just big enough to hold its contents. To set your own dimensions for a box you can use the height and width properties.The most popular ways to specify the size of a box are to use pixels, percentages, or ems. Traditionally, pixels have been the most popular method because they allow designers to accurately control their size.When you use percentages, the size of the box is relative to the size of the browser window or, if the box is encased within another box, it is a percentage of the size of the containing box.When you use ems, the size of the box is based on the size of text within it. Designers have recently started to use percentages and ems more for measurements as they try to create designs that are flexible across devices which have different-sized screens.
In the example above, you can see that a containing
element is used which is 600 pixels wide by 400 pixels high. Inside of this is a paragraph that is 80% of the width and 60% height of the containing element.
Create Borders Around Boxes
Every box has a border even if it is not visible or is specified to be 0 pixels wide.The border
separates the edge of one box
from another.If you specify a width
for a box, then the
borders, margin, and
padding are added to
its width and height.
The Margins sit outside the edge
of the border. You can set the
width of a margin to create a
gap between the borders of two
adjacent boxes. Padding is the space between
the border of a box and any
content contained within it.
Adding padding can increase the
readability of its contents..
WITHOUT MARGIN AND PADDING |
WITH MARGIN AND PADDING |
The margin provides the white space between boxes and the padding provides the white space between the borders and the content. The padding and
margin properties
are very helpful
in adding space
between various
items on the page.
Designers refer to the space
between items on a page as
white space. Imagine you had
a border around a box. You
would not want the text to touch
this border or it would become
harder to read.Or, imagine you had two boxes
sitting side by side (each with
a black border). You would not
necessarily want the boxes to
touch edges as this would make
the line look twice as thick on
the facing sides.
If the bottom margin of any
box touches the top margin of
another, the browser will render
it differently than you might
expect. It will only show the
larger of the two margins. If both
margins are the same size, it will
only show one.
Border Width
The border-width property
is used to control the width
of a border on any kind of HTML elements. The value of this
property can either be given
in pixels or using one of the
following values:
thin, medium, thick. You can control individual size, color, style of borders using the following syntax.
border: 5px solid #00BCD4;
The 5px specifies the width of the border, the solid specifies the type of the border and the Hex code specifies the color of the border.
You can control the style of a borders using border-style properties these are:
- solid - a single solid line
- double - two solid lines (the value of the border-width property creates the sum of the two lines)
- dotted - a series of square dots (if your border is 2px wide, then the dots are 2px squared with a 2px gap between each dot)
- dashed - a series of short lines
Show / Hide Boxes
The visibility property allows
you to hide boxes from users
but It leaves a space where the
element would have been.This property can take two
values:
hidden
This hides the element. The below example describes how to hide elements?.
visibility: hidden;
visible
This shows the element.
visibility: visible;
If the visibility of an element
is set to hidden, a blank space
will appear in its place.If you do not want a blank space
to appear, then you should use
the display property with
a value of none instead.Please note that anyone can view
the contents of any elements
whose visibility property has
been set to hidden by viewing
the source in their browser.
Padding
The padding property allows
you to specify how much space
should appear between the
content of an element and its
border. The value of this property is
most often specified in pixels
(although it is also possible to
use percentages or ems). If a
percentage is used, the padding
is a percentage of the browser
window (or of the containing box
if it is inside another box).
Please note: If a width is
specified for a box, padding is
added onto the width of the box.
You can specify different values
for each side of a box using:padding-top, padding-right, padding-bottom and padding-left properties. Or you can use a shorthand
(where the values are in
clockwise order: top, right,
bottom, left): padding: 10px 5px 3px 1px;
The example for padding between each box content is given below,
The value of the padding property is not inherited by child elements in
the same way that the color value of the font-family property is; so
you need to specify the padding for every element that needs to use it.
Please note: If the width of a box is specified then the margin is added to the width of the box.
You can specify values for each side of a box using:margin-top, margin-right, margin-bottom and margin-left. You can also use the shorthand (where the values are in clockwise order: top, right, bottom, left): margin: 2px 4px 6px 3px;
Sometimes you might see the following, which means that the left and right margins should be 5 pixels and the top and bottom margins should be 15 pixels: margin: 5px 15px;
The value of the margin property is not inherited by child elements in the same way that the color value of the font-family property is, so you need to specify the margin for every element that needs to use it. The example for margin is given below,
Margin
The margin property controls the gap between boxes. Its value is commonly given in pixels, although you may also use percentages or ems.If one box sits on top of another, margins are collapsed , which means the larger of the two margins will be used and the smaller will be disregarded.Please note: If the width of a box is specified then the margin is added to the width of the box.
You can specify values for each side of a box using:margin-top, margin-right, margin-bottom and margin-left. You can also use the shorthand (where the values are in clockwise order: top, right, bottom, left): margin: 2px 4px 6px 3px;
Sometimes you might see the following, which means that the left and right margins should be 5 pixels and the top and bottom margins should be 15 pixels: margin: 5px 15px;
The value of the margin property is not inherited by child elements in the same way that the color value of the font-family property is, so you need to specify the margin for every element that needs to use it. The example for margin is given below,
This article is created by bunch of references from online. If you like to learn more about the CSS you can leave a comment in the below comment section, we will write an article in those topics.
Comments