How to Change The Behavior of The Block Elements in HTML Tags

by | Apr 3, 2020 | Web Development Tutorials

Sometimes when we’re working with HTML or CSS, there may be the need, depending on the design in mind, to move objects from one side to another, or to add or reduce margins, to align objects next to each other or the one below the other, etc. And as these changes are being made, problems begin to appear as objects that refuse to position themselves together, or those that resist obeying the margins, or worse, those that allow themselves to move but completely disorganized the rest of the layout.

The problems can be many, however, the best solution to these is not specific but conceptual. Understanding how each of the alignments works (inline, block or inline-block), or how to access them through the property (display), will remove that headache.

All HTML tags can be classified into line or block tags. A line tag is one that occupies only the space necessary to display its content, and allows that other elements been placed next to it.

A   B   C

Instead, a block tag occupies the entire available screen width and does not allow another element to be placed next to it, even if it has enough space. 

A

B

C

The most common line tags are: 

<a>, <img>, <strong>, <span>, <input>, <code> 

The most common block tags are: 

<p>, <div>, <li>, <ul>, <h1>, <h2>, <h3>, <header>, <table>, <section>, <article>, <form>, <footer >, <nav> 

So the answer to the question of how to change the block behavior of these tags to adjust the layout freely is in the use of the property “display”. Applying the “display” on the element, for example, display: inline  or block,  or display: inline-block, you can arrange the movement at your discretion, but you must take into account the following considerations:

1. The display: inline, allows the side-by-side alignment of the objects but makes them no longer apply adjustments such as margins, padding, or even size settings. 

In this example, we have <span> tags, whose display type is inline, by default, with the letters A, B, and C inside. 

With some CSS styles, we have given them a background and some rules of size and space. However, we can see that these rules are not being respected. We got the alignment we wanted, but the sizes and margins are wrong.

2. The display: block, on the other hand, does respect the rules, but would then prevent us from displaying that content in the way that is wanted according to the design. 

The CSS styles have not been changed, only the display: block line has been added.

3. Fortunately, this duality can be solved with a third way, which is the display: inline-block. This represents the best of both worlds, allowing the desired alignment to be achieved without side effects. 

The CSS styles have not been changed, only the display: inline-block line has been added.

CONCLUSION

You can use display: inline-block, in preference over the previous ones. Note, however, that the relative, fixed, or absolute position of the container <div> also interferes with the position of its internal elements, so apply this logic not only to the particular element or object but to its dominant parent.

ADVICE

Manipulating the visualization of objects with “display” or floating them with “float”, (which forces, although stipulated otherwise, a block-like behavior), is not the best way to organize a layout or achieve alignments. I invite you to make incursions into the world of the flexbox or CSS grid, and to use the display: flex as a new paradigm.

——————-

We'd love to know your opinion. Leave us a message or follow us! we are on social networks

0 Comments

We have more articles and tutorials that may be of interest to you.

How to Migrate a Website Built In WordPress to the Localhost

This article is aimed for those people who, regardless of the reason, attempt to change their website or their blog developed in WordPress from an external hosting to a local one, localhost, but retaining the content and functionalities of their websites intact. This...

What Elements Cannot Be Missing in a Successful Website?

Creating a successful website requires attention to detail and a deep understanding of what users are looking for. From design to functionality, there are several elements that can not be missing in a successful website. In this article, we'll explore some of the key...

How To Improve The Loading Speed Of Your Website?

In today's fast-paced digital world, having a website that loads quickly is essential. A slow-loading website can have a negative impact on your user experience, leading to decreased engagement and conversion rates. In this article, we'll explore some of the ways you...

Highly recommended books for Marketing Management and Social Media Management:

Knowing the best books for marketing management and social media management is important for improving knowledge and skills, staying up-to-date with the latest trends and strategies, enhancing decision-making, boosting creativity and innovation, and personal and...

The Importance of Having a Website For Your Business

In today's digital age, having a website for your business has become more important than ever before. With the majority of people using the internet to find products and services, having an online presence is crucial to the success of your business. In this blog,...

How to choose the right design for your website?

Creating a website for your business or personal brand is a crucial step in establishing a strong online presence. A well-designed website can help attract and retain visitors, showcase your brand identity, and ultimately drive conversions. However, with so many...

4 Steps to Reach Your Customer

The sales funnel is one of the most influential tools today in digital marketing. It has become an icon of our time and is undoubtedly transforming an entire generation since it has been consolidated for a long time. Since the nineteenth century comes this terminology...

Pay Attention to Your Thoughts

There has been enough talk about the importance of having a positive and focused mind when confronting the different challenges of life, overcoming difficulties, or that such virtue is necessary for the quest of dreams. The scenarios can be extrapolated to themselves...

How to Give Functionality to a Button With Javascript

Javascript gives us the possibility to accessing what we call the DOM, which basically always happens because everything we do in javascript directly or indirectly infers on the HTML or CSS of the site, so there is no mystery in it. And a common way to do it is...

Social Media Tactics

We use the word "tactics" in the title because one must be tactful and not emotional when using social media for business exposure and growth. Social media in its very design is a picker and poker of vanity metrics and the desire for "fame." Did you know there are...