What's new

Welcome to the forum 👋, Visitor

To access the forum content and all our services, you must register or log in to the forum. Becoming a member of the forum is completely free.

[TIPS] Use percentages instead of absolute numbers

Koyyiko

New member
Joined
Aug 22, 2022
Messages
17
Reaction score
0
HTML Coins
0
As a beginner, one of your first concerns will be positioning your elements on the web page. Since most websites start by defining the width of elements in pixel numbers, it is often inevitable to begin placing these numbers in your code.

Code:
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Absolute Numbers</title>
  </head>
  <style>
      div {
        background-color: #e7e7e7;
        width:500px;
        height: 100px;
        font-weight: bold;
        font-size: 28px;
      }
  </style>
  <body>
      <div> Hi!! I am a div box and I am awesome. </div>
  </body>
</html>


The above code creates a 100-pixel wide element with the applied background color on the web page.

100-pixel wide element


However, there is a flaw with this approach. This works well since you developed it as per your screen and tested it.

What if I now display the same web page on a mobile device?

100-pixel-wide-element-web-page-on-a-mobile-device


Our box did not render entirely on the mobile device as it did on the desktop where we tested it.

Also, when we render the web page on a tablet, such as the iPad Pro, as shown below, the div box is half the size of the screen.

web page on a tablet


It would be less than ⅓ of the screen on a desktop (depending on the resolution). This problem is easy to identify as only one element is located here. However, developing a complete web page on your desktop and checking many elements will surprise you.

The CSS tricks to avoiding such surprises are using percentage values instead of absolute numbers. For the same code, let’s change the width from 500px to 30% as follows:

Code:
width:30%;


The desktop representation of the div remains the same. But notice how it changes on the same mobile device selected as shown above.

same mobile device selected


Except for the ugly part of text-overflow in the above screenshot, notice that the div box still covers 30% of the device width. This is extremely helpful in resolving browser compatibility issues and developing a consistent website.
 

Theme customization system

You can customize some areas of the forum theme from this menu.

  • Wide/Narrow view

    You can control a structure that you can use to use your theme wide or narrow.

    Grid view forum list

    You can control the layout of the forum list in a grid or ordinary listing style structure.

    Picture grid mode

    You can control the structure where you can open/close images in the grid forum list.

    Close sidebar

    You can get rid of the crowded view in the forum by closing the sidebar.

    Fixed sidebar

    You can make it more useful and easier to access by pinning the sidebar.

    Close radius

    You can use the radius at the corners of the blocks according to your taste by closing/opening it.

  • Choose the color combination that reflects your taste
    Background images
    Color gradient backgrounds
Back