Git Product home page Git Product logo

center-that-thing's Introduction

โžก๏ธ Center. That. Thing! โฌ…๏ธ

Centers any HTML element horizontally โ€“ relative to the screen!

Try out the demo: Show. Me. THE. DEMO!!! ๐Ÿ”— Check out also the CSS comparison demo: CSS vs. CTT Comparison

Who is this favor?

  • For you my, dear designers! โค๏ธ Tell your developer colleagues to use this library.
  • For you, my dear developer colleagues. โค๏ธ Don't be afraid to implement this annoying centered thing anymore. Just use this library.

โฌ‡๏ธ Check out this lovely GIF:

A meme GIF about a harsh discussion between a designer and a developer about centering elements.

๐Ÿ“– Table of contents

๐Ÿง‘โ€๐Ÿ’ป Usage

Assumptions:

  • You are able to add a JavaScript library to your website's/project's <head>.
  • You can add IDs or classes to elements respectively extract element IDs.
  • You can do at least one of the following:
    • Execute some JavaScript at the end of your page.
    • Add custom HTML attribute to HTML elements.
    • Adjust the global CSS.

Add the script

Add the following script to one or every page:

<script src="https://cdn.jsdelivr.net/gh/peter-kuhmann/[email protected]/dist/center-that-thing.min.js"></script>

Center using JS

To center a specific element by executing some JavaScript, use:

// Use element reference
centerThatThing(myElementToCenter, { /* options */})

// Use CSS selector
centerThatThing("nav > .logo", { /* options */})

The <script> block must come ofter the element!

Center with CTT using HTML attributes

Add the attribute data-ctt-enable="true" to the element you want to center.

<div class="container">
    <div>Left</div>
    <div data-ctt-enable="true">To be centered</div>
    <div>Right</div>
</div>

Center with CTT using CSS variables (yes!)

Add a CSS variable to your global CSS:

/* You must add it to ":root"! */
:root {
    --center-that-thing: '[{"selector": "nav > .logo"}, {"selector": "footer > .logo", "collisionDetection": "siblings"}]';
}

Dynamic elements

If you want the library to center elements that will be dynamically added to your page but have e.g. the HTML attribute data-ctt-enable="true", then execute the following function:

enableCenterThatThingDynamicDiscovery()

๐Ÿ› ๏ธ Options

The library currently supports the following options:

Option Mandatory Type Options Description HTML attribute
collisionDetection NO false OR string false, "siblings" false turns collision detection of. "siblings" causes the library to only move the element between the siblings. data-ctt-collision-detection="{value}"

๐Ÿ›Ÿ The problem

The following images describe a problem scenario with flex:

Imagine you have a flex row container You add three elements. One very small, one very big, and one medium large one. Now you apply the flex setting "justify-content: space-between". You expect the element in the middle to be centered. But that's not the case, as the siblings are of different size.

What you want to achieve is: Position one element exactly in the center of the screen.

This is just one very simple problem example. There are others out there (container has an offset).

This library helps you to position that one element exactly in the center of the screen.

๐Ÿ’ก How it works

Let's make it short:

  • The library remembers your element.
  • The library gets the current position.
  • The library computes the ideal position (centered in the middle of the screen).
  • The library applies an offset using position: relative; and left: {offset}px;

The library does that again, when

  • the screen size changes or
  • the element size changes.

The library currently also supports a simple collision detection.

๐Ÿ’ฅ Collision detection

You may not want the element to be centered relative to the screen at any cost.

You may not want the element to cover other elements (e.g. in the navigation).

Therefor, the library supports also a "collision detection" mechanism. Currently, it supports:

  • no collision detection (false)
  • siblings/neighbour checks ("siblings")

Check out the demo, to understand better!

๐Ÿ˜– Can't I just use modern CSS?

Yes, you can. In many cases. But in some, not.

There are generally two CSS solutions:

  • Flex layout with the sibling elements using flex: 1; to we of equal width. This causes the center element to be sandwiched exactly in the middle.
  • Grid layout with grid-template-columns: 1fr auto 1fr;. Same principle as with the flex layout: Center element will be sandwiched.

When does that work?

  1. The container is centered.
  2. You have three (or an odd number) of elements.
  3. It's okay for you, if both siblings are of equal width.

When is this not a good idea?

  • You want the sibling elements to have different widths that use available space and the centered element should move.
  • You have two elements respectively an even number of elements.
  • The container is NOT centered.

Check out the CSS vs. CTT comparison: CSS vs. CTT Comparison

โ›”๏ธ Issues

Do you have an issue with that library?

โžก๏ธ Please create a GitHub issue! ๐Ÿ™

โœ๏ธ Author

Peter Kuhmann
Senior Software Engineer
[email protected]
https://www.peter-kuhmann.de

Peter Kuhmann Logo

center-that-thing's People

Contributors

peter-kuhmann avatar

Stargazers

 avatar Jay avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.