Skip to content Skip to sidebar Skip to footer

How To Make Relative Layout Add Elements In A Horizontal Direction?

Why are relative elements added vertically, in a column. Is it possible to have elements displayed horizontally instead of vertically? Like a row instead of a column.

Solution 1:

Setting the position to relative doesnt necessarily dictate order in terms of horzontal vs vertical placing. This is the difference between block level and inline elements.

Block level elements (e.g. div) display on a new line by default, inline elements (e.g. span) appear on the same line, if the available width allows it.

You can see this in effect here

You can override the default display for elements by setting it in your CSS.

More on Block level elements from MDN

"Block-level" is categorization of HTML (Hypertext Markup Language) elements, as contrasted with "inline" elements. Block-level elements may appear only within a element. Their most significant characteristic is that they typically are formatted with a line break before and after the element (thereby creating a stand-alone block of content). That is, they take up the width of their containers.

More on Inline elements

"Inline" is categorization of HTML elements, as contrasted with "block-level" elements. Inline elements can appear within block-level or other inline elements. They take up the width of their contents. The differences between inline and block-level elements include [...] formatting, by default, inline elements do not begin with new line.

Solution 2:

I believe what you are looking for is the display:inline-block CSS property. By default, elements are displayed using display:block which is the reason for them displaying like a "column".

Try this as an example.

HTML:

<div>Relative Div1</div>
<div>Relative Div2</div>
<div>Relative Div3</div>

CSS: (added some lines to make it more visible as to what it's doing.)

div {
    width: 50px;
    height:50px;
    background:#ff0000;
    margin:5px;
    position:relative;
    display: inline-block;
}

WORKING DEMO

Solution 3:

You question is not very clear...maybe your elements are in default display (display:block). You can try put display: inline-block in your elements.

example:

<style>.whatever{display:inline-block;}
  </style><divclass="whatever">....</div><divclass="whatever">....</div><divclass="whatever">....</div><divclass="whatever">....</div>

Post a Comment for "How To Make Relative Layout Add Elements In A Horizontal Direction?"