Skip to content

Instantly share code, notes, and snippets.

@alexsc6955
Last active September 22, 2021 21:30
Show Gist options
  • Save alexsc6955/101c95490460c9f779a858a40c1533f5 to your computer and use it in GitHub Desktop.
Save alexsc6955/101c95490460c9f779a858a40c1533f5 to your computer and use it in GitHub Desktop.
Stylus centered mixin
/**
* Name: Stylus -centered mixin
* Description: Mixin to center elements
* Author: Santiago Rincón
* Author URI: http://github.com/rincorpes
* Version: 2.0
* License: GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
* License URI: https://www.gnu.org/licenses/gpl-3.0.en.html
*/
-center()
if length(arguments) == 2
if arguments[0] is a 'ident' && arguments[1] is a 'unit'
margin-top unit(arguments[1], 'px') if top in arguments[0] || vertical in arguments[0]
margin-bottom unit(arguments[1], 'px') if bottom in arguments[0] || vertical in arguments[0]
width arguments[1] if width in arguments[0]
margin-left auto
margin-right auto

#Stylus center mixin

A simply mixin to center elements with stylus

##Usage

You can call the mixin with no params and it will center the element automaticly

// Stylus sintaxis
.element
	-center()
	
// CSS output
.element {
	margin-left: auto;
	margin-right: auto;
}

Yo can also center elements and give them vertical margins by using "top" or "bottom" and the value

// Stylus sintaxis
.element
	-center top 20
	
// CSS output
.element {
	margin-top: 20px;
	margin-left: auto;
	margin-right: auto;
}

If you want to give diferent values to the top and bottom margins you can use "vertical" andtwo values. The first one will be asign to the top margin and the next one to the bottom margin

// Stylus sintaxis
.element
	-margin vertical 40 20
	
// CSS output
.element {
	margin-top: 40px;
	margin-bottom: 20px;
	margin-left: auto;
	margin-right: auto;
}

You can also asign a width value

// Stylus sintaxis
.element
	-margin width 80%
	
// CSS output
.element {
	width: 80%;
	margin-left: auto;
	margin-right: auto;
}
@alexsc6955
Copy link
Author

Sencillo pero útil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment