首页 > 解决方案 > Hidden content in Chrome with Flexbox horizontally centered layout

问题描述

Based on this example i'm trying to center horizontally a content with flex css, and usually it goes right. But when the content is very large the left part goes hidden. It happens in Chrome, and the horizontal scrollbar doesn't help. As you can see in the snippet. What I have to change to get it right?

.flex-container {
    padding: 0;
    margin: 0;
    display: -webkit-box;
    display: -moz-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    align-items: center;
    justify-content: center;
}
.row {
    width: auto;
    border: 1px solid blue;

}
.flex-item {
    background-color: tomato;
    padding: 0px;
    white-space: nowrap; /* added to simulate large content */
    margin: 0px;
    color: white;
    font-weight: bold;
    font-size: 2em;
    text-align: left;
}
<div class="flex-container">
    <div class="row"> 
        <div class="flex-item">hidden.............................somewhere.....................................................................................................end</div>
        <div class="flex-item">where</div>
        <div class="flex-item">I</div>
        <div class="flex-item">am?</div>
    </div>
</div>

enter image description here

As you can see, this is a different formulation of this problem with flexbox: here

标签: htmlcssflexbox

解决方案


请尝试以下代码。

html, body {
    height: 100%;
}
body {
    margin: 0;
}
.flex-container {
    width: 100%;
    padding-right: 15px;
    padding-left: 15px;
    margin-right: auto;
    margin-left: auto;
}
.row {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
    margin-right: -15px;
    margin-left: -15px;
}
.flex-item {
    background-color: tomato;
    margin: 0px;
    color: white;
    font-weight: bold;
    font-size: 2em;
    text-align: left;
    max-width: 100%;
    flex-grow: 1;
    padding-right: 15px;
    padding-left: 15px;
}
<div class="flex-container">
    <div class="row">
        <div class="flex-item">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
        <div class="flex-item">where</div>
        <div class="flex-item">I</div>
        <div class="flex-item">am?</div>
    </div>
</div>


推荐阅读