How To Have Html Elements Side By Side

In this blog post, we will discuss different ways to position HTML elements side by side. There are several methods you can use to achieve this, including floats, inline-block, and flexbox. We will cover each of these techniques in detail below.

1. Using Floats

The float property is a classic method to position HTML elements side by side. When an element has a float value other than its default value ‘none’, it moves to the left or right side of its container, allowing other elements to wrap around it.

Here’s an example of using floats to position two <div> elements side by side:

&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
    &lt;style&gt;
        .box {
            float: left;
            width: 50%;
            background-color: lightblue;
            padding: 10px;
            box-sizing: border-box;
        }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div class="box"&gt;Box 1&lt;/div&gt;
    &lt;div class="box"&gt;Box 2&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

However, you should be aware that using floats can cause layout issues, such as a collapsed parent container. You’ll often need to use a clearfix to resolve these issues.

2. Using Inline-Block

Another way to place HTML elements side by side is by setting the display property to inline-block. When you do this, the elements will behave like inline elements but with the ability to set width and height, making it easy to arrange them side by side.

Here’s the same example as before, but this time using inline-block:

&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
    &lt;style&gt;
        .box {
            display: inline-block;
            width: 50%;
            background-color: lightblue;
            padding: 10px;
            box-sizing: border-box;
        }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div class="box"&gt;Box 1&lt;/div&gt;
    &lt;div class="box"&gt;Box 2&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

3. Using Flexbox

Flexbox is a modern CSS layout module that makes it easy to design responsive, flexible layouts. You can use flexbox to place HTML elements side by side by applying the display: flex property to the parent container.

Here’s the same example as before, but this time using flexbox:

&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
    &lt;style&gt;
        .container {
            display: flex;
        }

        .box {
            width: 50%;
            background-color: lightblue;
            padding: 10px;
            box-sizing: border-box;
        }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div class="container"&gt;
        &lt;div class="box"&gt;Box 1&lt;/div&gt;
        &lt;div class="box"&gt;Box 2&lt;/div&gt;
    &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

Flexbox has many advantages over floats and inline-block, such as automatic alignment and easy responsiveness. It’s highly recommended for modern web layouts.

Conclusion

In this blog post, we’ve covered three methods to position HTML elements side by side: floats, inline-block, and flexbox. While floats and inline-block are older techniques that can still be useful in certain situations, flexbox is the most recommended approach for modern, flexible layouts. Make sure to choose the method that best suits your project’s requirements and browser compatibility needs.