How to center objects #327
Answered
by
Gaurav-Gosain
RoastBeefer00
asked this question in
Q&A
-
If I make a text block with padding and a background how can I center it on the terminal? In CSS I'd use margin auto but that's not an option here. |
Beta Was this translation helpful? Give feedback.
Answered by
Gaurav-Gosain
Jul 2, 2024
Replies: 1 comment 1 reply
-
I would do something along the lines of the following package main
import (
"fmt"
"os"
"github.com/charmbracelet/lipgloss"
"golang.org/x/term"
)
func main() {
physicalWidth, _, _ := term.GetSize(int(os.Stdout.Fd()))
child := lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("420")).
Padding(1, 2)
parent := lipgloss.NewStyle().
Width(physicalWidth).
AlignHorizontal(lipgloss.Center).
Padding(2, 0)
fmt.Println(parent.Render(child.Render("Hello World!")))
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
RoastBeefer00
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would do something along the lines of the following