简体   繁体   中英

.table-cell width goes to 100%, <a><img> is clickable in full width

I needed to center the logo horizontally and vertically in the center so I used .table and .table-cell from bootstrap. The problem is, while the image is perfectly aligned in the center, the .table-cell goes 100% of it's parents length and the full width is clickable if wrapped in a <a href> .

The code:

<div class="header-content table">
    <div class=" table-cell">
        <?php
            if( function_exists( 'the_custom_logo' ) ) {
                if(has_custom_logo()) {
                    $custom_logo_id = get_theme_mod( 'custom_logo' );
                    $image = wp_get_attachment_image_src( $custom_logo_id , 'full' );

                    echo '<a href="'.get_home_url().'"><div class="harmony-logo background-image" style="background-image: url('.$image[0].');">';

                    echo '</div></a>';
                } else {
                    echo '<h1 class="site-title"><?php bloginfo("name"); ?></h1>';
                }
            } ?>

    </div><!-- .table-cell -->
</div><!-- .header-content -->

I hope I understood your question correctly.

If you want to align a div element or something horizontally and vertically too, you can use this CSS to do either of these:

Use of absolute positioning

.parentClass{ position: relative; }
.childClass{ /* The div you want to center */
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

Use of flex

.childClass{ /* The div you want to center */
    display: flex;
    align-items: center;
    justify-content: center;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM