How To Arrange Your Web Page Layout With The Box Template

0

CSS margin, frontier, and padding Properties are used on any HTML element to create a unique effect. They work together to make sure your web page is organized and presentable.

Of these three properties, the frontier The property may look more familiar as it is usually easily identified on a web page. However, using the frontier the property would not look as presentable as it would on most websites if the developer did not also edit the padding and margin Properties.

In this tutorial, you’ll learn about the box model and how each of its layers works together to organize content on a website.

What is the CSS box template?

The CSS box template is basically a box that encloses every HTML element. The box model consists of four layers (as you can see in the image below), and each layer has a unique purpose.

Box model

The first layer is in the center of the box model. This is the position given to each HTML element. In the picture above the auto x auto the value is currently displayed from the center position, but this value will be replaced by the width x height of each HTML element.

The padding The property is located between the HTML element and the frontier property, and the margin the property is located on the exterior side of the frontier goods. The padding and frontier properties usually do not have visible defaults for a given CSS element. However, a flaw margin the value of the property is on some HTML elements, namely the p element — which defaults to 16px on top and bottom.

Using the CSS margin property

The margin property has four sub-properties, namely high margin, right-margin, low margin, and left-margin. These properties are used individually to create the margin size on a specific side of an item, or as a group just using the shortcut margin goods.

Margin ownership structure

Selector {
margin: margin-top margin-right margin-left margin-bottom;
}

The example above represents the basic structure of the margin goods. The first value in the stack of values ​​assigned to the margin The property targets the top, the second value targets the right, the third targets the left, and the fourth targets the bottom of an item.

Related: How to Target Part of a Webpage Using CSS Selectors

Example of using the margin property

p{
margin: 20px 10px 20px 10px;
}

The above code effectively assigns a margin of 20px on top, 10px on the right, 20px on the bottom and 10px on the left of all p items on a specific web page. However, the same effect produced by the above code can be achieved with less code:

p{
margin: 20px 10px;
}

The above code will produce the same result as the previous code snippet. The first value assigned to margin the property will target the top and bottom, and the second value will target the right and left sides of all p elements on a web page.

If there is a case where the margin that should be assigned to all four sides of an HTML element is the same, you can use the following code to achieve this:

p{
margin: 20px;
}

The above code assigns a margin 20px on all four sides of the p elements on a web page.

Unpack the CSS border property

In the CSS box template, the frontier property is the third layer. As margin, the frontier The property has several sub-properties that you can use in a stack value. But, unlike the margin property, not all frontier sub-properties must be present for the frontier property to function properly. There is only one property that must be present: the border style goods.

Basic border property structure example

Selector{
border: border-style;
}

The border style The property in the example above can take the form of one of several values. For example, if you want a solid frontier around all the paragraphs on your web page, the following code may help:

p{
border: solid;
}

The frontier The property also allows a developer to target specific sides of an HTML element with the following four sub-properties:

  • border-left
  • border-right
  • high border
  • Bottom border

If there is a case where you need to place a frontier on only one side of an item, you can use the appropriate subproperty from the list above.

Unpacking the border-style property

Although not always visible, each HTML element is surrounded by the frontier goods. Your inability to see the frontier in some cases, this is because the default value of the border style the property is null. You can assign many different values ​​to the border style property, some of the most popular being:

Using the border property with a stack value

Three values ​​can be assigned to the frontier property to get an accurate result. These values ​​are the border width, border style, and border color Properties. Therefore, if you want to create a solid red frontier around a paragraph that is 2px wide, the following code will accomplish this:

p{
border: 2px solid red;
}

You can also use the stack value on one side of an item by replacing the frontier in the example above with the sub property that targets the appropriate side. For example, if you want to target only the left side of all paragraphs with the same stack value above, you can simply replace “border” in the example above with the “border-left” property.

Using the CSS Padding property

CSS padding property is very similar to the margin goods. The padding the property has four sub-properties, padding above, padding-right, padding-stockings, and fill-left. You can use each sub-property individually or feed them as a stack value in the padding goods.

As margin, if only two values ​​are assigned to the padding property, the first will target the top and bottom sides of the element, and the second will target the left and right sides. If only one value is provided, all sides will have the same padding Cut.

The following three sets of code will produce the same result on all p elements on a web page.

Using four padding values

p{
padding: 20px 20px 20px 20px;
}

Using two padding values

p{
padding: 20px 20px;
}

Using a padding value

p{
padding: 20px 20px;
}

Using the CSS Box Model

Using the frontier, margin, and padding properties will help you organize your web page, regardless of the page type. Here’s how to use them in tandem:

Basic HTML document example





The Box Model



Heading One



Lorem ipsum dolor sit amet consectetur adipisicing elit.
Impedit rem recusandae id est. Rem, quod odio. Doloremque nemo libero,
fuga suscipit dignissimos soluta iusto ullam ducimus rerum labore necessitatibus facilis.
Lorem ipsum dolor sit amet consectetur adipisicing elit.
Impedit rem recusandae id est. Rem, quod odio. Doloremque nemo libero,
fuga suscipit dignissimos soluta iusto ullam ducimus rerum labore necessitatibus facilis.




Heading Two



Lorem ipsum dolor sit amet consectetur adipisicing elit.
Impedit rem recusandae id est. Rem, quod odio. Doloremque nemo libero,
fuga suscipit dignissimos soluta iusto ullam ducimus rerum labore necessitatibus facilis.
Lorem ipsum dolor sit amet consectetur adipisicing elit.
Impedit rem recusandae id est. Rem, quod odio. Doloremque nemo libero,
fuga suscipit dignissimos soluta iusto ullam ducimus rerum labore necessitatibus facilis.




Related: Best Sites For Quality HTML Coding Examples

The above code will produce the following output in the browser:

Basic layout of the html document

The image above shows two div elements each containing a title and a paragraph. Each div the element has a margin, frontier, and padding of 0px, width of 1042px and height of 112,438px as you can see in the box template below.

Box template of a default div

Using the frontier The property provides a clearer perspective of what is happening on the page.

Using the border property

  .box{
border: solid;
}

Using the CSS code above will produce the following output in your browser:

Using the border property

Now that the frontier property is visible, it has a default width of 3px, as seen in the box template below.

Box template using the border property

As you can see from the box model above, the margin the property is outside frontier. Therefore, you can use it to create a space between the two div elements.

Using the margin property

        .box{
border: solid;
margin: 20px;
}

Presentation margin with the above code will create the following output in your browser:

Using the margin property

It’s a little better, isn’t it? There is sufficient space between the div elements. All sides of each div element now have a margin of 20px, as you can see in the box template below.

Box model using the margin property

You can now focus on the inside of the border, where the padding property falls into the box model.

Using the padding property

        .box{
border: solid;
margin: 20px;
padding: 10px;
}

The above code will produce the following output in your browser:

Using the padding property

As you can see in the image above, the text inside the border has now moved away from the edges due to the padding goods. The box model below shows that all the layers in the box model are now in use.

Experiment with CSS properties

You now have an understanding of the box model and how each element in it works together to achieve a specific result. You can try to assign a stack value to the frontier property and modify the frontier color to red, or you can replace the frontier property with its border-left sub-property and see what happens.

There are plenty of other CSS properties to explore, and with the CSS cheat sheet you can experiment with each one.


Screenshot of examples of CSS properties used in a code snippet
The CSS3 essential properties cheat sheet

Master essential CSS syntax with our CSS3 properties cheat sheet.

Read more


About the Author


Source link

Share.

About Author

Leave A Reply