1

I can't seem to move the alignment of text within a dbc card. Using below, I want to move 100 in card 2 down. I'm trying everything to move it up or down but nothing seems to work. I have certain card sizes I need to maintain so I'd really like to keep the height of those fixed and just be able to shift the text margin.

I don't necessarily want the text to be exactly ordered. I need to be able to shift up and down.

I'd ideally like to use cards but I'm not against using a separate option.

import dash
from dash import Dash, dcc, html
import dash_bootstrap_components as dbc

external_stylesheets = [dbc.themes.SPACELAB, dbc.icons.BOOTSTRAP]

app = dash.Dash(__name__, external_stylesheets = external_stylesheets)

cards = dbc.CardGroup(
    [
        dbc.Card(
            dbc.CardBody(
                [
                    html.H5("Card 1", className="card-title"),
                    html.P(
                        "This card has some text content, which is a little "
                        "bit longer than the second card.",
                        className="card-text",
                    ),
                    html.P(
                        100,
                        className="card-text text-center align-self-bottom",
                    ),
                ]
            )
         ),
        dbc.Card(
            dbc.CardBody(
                [
                    html.H5("Card 2", className="card-title"),
                    html.P(
                        "This card has some text content.",
                        className="card-text",
                    ),
                    html.P(
                        100,
                        style = {"marginBottom": -50, 'paddingBottom':-500},
                        className="card-text text-center align-self-bottom",
                    ),
                ]
            )
        ),
        dbc.Card(
            dbc.CardBody(
                [
                    html.H5("Card 3", className="card-title"),
                    html.P(
                        "This card has some text content, which is longer "
                        "than both of the other two cards, in order to "
                        "demonstrate the equal height property of cards in a "
                        "card group.",
                        className="card-text",
                    ),
                    html.P(
                        100,
                        className="card-text text-center align-self-bottom",
                    ),
                ]
            )
        ),
    ]
)

app.layout = dbc.Container([
    
dbc.Row(
    [
        dbc.Col(cards),
    ]
)

    ])

if __name__ == '__main__':
       app.run_server(debug=True, port = 8050)

enter image description here

1 Answer 1

2
+50

The simplest would be to keep this content outside the card body, ie. as a child of dbc.Card() that comes after dbc.CardBody() :

cards = dbc.CardGroup([
    dbc.Card([
        dbc.CardBody([
            html.H5("Card 1", className="card-title"),
            html.P(
                "This card has some text content, which is a little "
                "bit longer than the second card.",
                className="card-text",
            )
        ]),
        html.P(
            100,
            className="card-text text-center",
        )
    ]),
    dbc.Card([
        dbc.CardBody([
            html.H5("Card 2", className="card-title"),
            html.P(
                "This card has some text content.",
                className="card-text",
            )
        ]),
        html.P(
            100,
            className="card-text text-center",
        )
    ]),
    dbc.Card([
        dbc.CardBody([
            html.H5("Card 3", className="card-title"),
            html.P(
                "This card has some text content, which is longer "
                "than both of the other two cards, in order to "
                "demonstrate the equal height property of cards in a "
                "card group.",
                className="card-text",
            )
        ]),
        html.P(
            100,
            className="card-text text-center",
        )
    ])
])

screenshot 1


[Edit]

I'd ideally align 100 up in all three cards so it it's centered horizontally AND aligned half way between the text above and the border below.

In this case, you can add the class card-body to each paragraph so that they are stretched the same way as the dbc.CardBody component above them (ie. the two bodies receive the same amount of space to fit the card height), and then adjust by adding some styles :

        html.P(
            100,
            className="card-text text-center card-body",
            style={
                'paddingTop': '0',
                'position': 'relative',  # <- if needed set this and
                'top': '-2px'            # <- adjust here
            }
        )

screenshot 2

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @EricLavault but I want more autonomy over the text. Not necessarily in a horizontal row. Using your example, I want to move 100 up for Card 1 and 2.
@tonydanza123 ok I see, I think I (may) have a solution for that but I'm confused about what determines the selection of card 1 and 2 ? is it a static or a dynamic choice ? I mean, is it an arbitrary choice that is already known (ie. you can use specific classes to distinguish each "kind" of card and adjust their shift arbitrarily using the corresponding css selectors) ? Or does it depends on the available space left in the card body (which is dictated by the height of the parent card minus the height of the card with the highest height in the same row) ?
The latter, with the height of the cards and available space altering for each specific component. Using above, I'd ideally align 100 up in all three cards so it it's centered horizontally AND aligned half way between the text above and the border below.
Ok thanks, rephrased like this it's clearer, see revised answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.