Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for CSS layout & how to position elements on your webpage
Nevulo
Nevulo

Posted on • Originally published atnevulo.xyz

     

CSS layout & how to position elements on your webpage

One of the most fundamental requirements to creating a great website is being able to move elements around on the page, instead of having everything appear in one big blob in a corner.

Layout modes

If you’ve styled elements with CSS, you know that it’s essentially putting a bunch of properties and values on a selected element in just the right way to make things look the way you want. Add somecolor: red here, a dash ofbackground: black there.

So, we know how to visually differentiate elements, including size, colour, etc., but how do we actually move an element to where we want, for the resolutions we want?

CSS comes baked with a bunch of differentlayout modes for adjusting thebehaviour of elements so you can lay out your page the way you want.

Whether you need a grid pattern, a table, columns, and much more, you’ll just need to find the right layout mode for what you need.

It’s important to understand that different layout modes will affect the way elements display differently, and not all CSS properties will work across all layout modes.

The default layout mode is called “flow”, where elements flow down the page.

Thedisplay property

Thedisplay CSS property is used to set whether or not an element uses“block” or “inline” layout, and controls what layout child elements use, such as flow layout, or flexbox.

Different types of layout modes

Normal flow (default)

Normal flow is the default way webpages lay themselves out when you haven’t applied any CSS to change how elements appear. Elements will appear at the top of the document and stack downwards, each one appearing on a new line below the last, as space is needed.

Flexbox

The flexible box layout model is a one-dimensional box where content can be positioned and distributed across an axis (horizontal or vertical).

This is helpful if you want to display a collection of elements in one dimension, like all on the same row for a navigation bar for example, or in a list.

Grid

Whereas Flexbox works in onlyone dimension, CSS Grid layout, which you can consider a successor to Flexbox, works withtwo dimensions, and you can align elements intocolumns and rows.

The main advantage of Grid over Flexbox is that it’s much more suited for creating responsive, collapsible layouts that work in a more predictable way.

Positioning with theposition property

If we want to shift elements without affecting other elements on the page heavily, such as shifting a button a little bit to the left, positioning is perfect. Use thetop,left,right andbottom properties on an element with units as values to move the element.

  • Relative positioning
    • Used for moving elements relative to themselves, where they’d normally be
  • Absolute positioning
    • Used for displaying an element in anexact location on the page, a certain distance from the edge of the page
    • Other elements won’t be affected by absolutely positioned elements
  • Fixed positioning
    • Similar to absolute positioning, butanchors orfixes an element in place relative to the closest positioned ancestor
  • Sticky positioning
    • A hybrid between relative and fixed positioning. Element will act as if it were relatively positioned until it is scrolled to a certain threshold (e.g.,10px from the top of the container), after which it becomes fixed

Floats

The float layout will pretty much “pull” an element to either the left or right side of the screen, usingfloat: left orfloat: right.

On a technical level, it’ll take the respective element out of the “normal flow” of the document, shifting it to the left, or right, until it touches the edge of its containing box, or another floated element.

Elements and text can still wrap around floated elements, unlikeabsolutely positioned elements.

Table layout

Specifically designed for creating tables with rows and columns, like what you’d find in Excel. Great for rendering table data and makes it easy to customise tables such as spacing between cells, alignment of elements in the cells, etc.

Table layout is also a desirable fallback for older clients trying to replicate something like Flexbox or Grid where it’s not supported (ie. email clients… 😡).

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
andrewbaisden profile image
Andrew Baisden
Software Developer | Content Creator | AI, Tech, Programming
  • Location
    London, UK
  • Education
    Bachelor Degree Computer Science
  • Work
    Software Developer
  • Joined

Email development is such a pain can't say that I miss those dark days 😅 It would be soooooo super simple if we could use Flexbox or Grid for emails instead of tables and floats from back in the day 🤣

CollapseExpand
 
nevulo profile image
Nevulo
I'm a software engineer working at Flux, passionate about giving users the best experiences in apps.
  • Location
    Melbourne, Australia
  • Work
    Junior Software Engineer at Flux
  • Joined

Hahaha, totally agree! Thanks for reading Andrew 😁🤩

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I'm a software engineer working at Flux, passionate about giving users the best experiences in apps.
  • Location
    Melbourne, Australia
  • Work
    Junior Software Engineer at Flux
  • Joined

More fromNevulo

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp