.table-cell宽度达到100%,<a><img/> is clickable in full width

时间:2017-12-15 23:59:45

标签: html css wordpress css-tables

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 -->

1 个答案:

答案 0 :(得分:0)

我希望我能正确理解你的问题。

如果你想水平和垂直对齐div元素或其他东西, 您可以使用此CSS执行以下任一操作:

使用绝对定位

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

使用flex

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