2017-05-30 150 views
2

我想用css flex property“图像内容”文本中心对齐块,但左对齐)就像下面的图像。如何让文本与块对齐但居中对齐?

enter image description here

但我得到这个

.wrapper { 
 
    max-width:300px; 
 
} 
 
.main { 
 
    width:300px; 
 
    display:flex; 
 
    justify-content:center; 
 
    align-items:center; 
 
    flex-flow:column nowrap; 
 
    background:#458bc3; 
 
    color:#fff; 
 
    height:300px; 
 
    overflow:hidden; 
 
} 
 
.content { 
 
    color:#fff; 
 
    background:#a6d8ff; 
 
    height:100px; 
 
    display:flex; 
 
    justify-content:center; 
 
    align-items:center; 
 
    flex-flow:column nowrap; 
 
} 
 
.content p { 
 
    margin:0px; 
 
}
<div class="wrapper"> 
 
    <div class="main"> 
 
    IMAGE 
 
    </div> 
 
    <div class="content"> 
 
    <p> 
 
     Image 
 
    </p> 
 
    <p> 
 
     Content 
 
    </p> 
 
    </div> 
 
</div>

我需要(“图像内容”文本对齐与块中心,但左对齐像图像)。 任何人都可以帮助我如何获得这种方法与flex?

回答

1

text-align: left只是包装您的内容imagecontent段的内div

试试这个:

检查演示here

HTML:

<div class="wrapper"> 
    <div class="main"> 
    IMAGE 
    </div> 
    <div class="content"> 
    <div class="text-left"> 
     <p> 
     Image 
     </p> 
     <p> 
     Content 
     </p> 
    </div> 

    </div> 
</div> 

CSS:

.wrapper { 
    max-width:300px; 
} 
.text-left { 
    text-align: left; 
} 
.main { 
    width:300px; 
    display:flex; 
    justify-content:center; 
    align-items:center; 
    flex-flow:column nowrap; 
    background:#458bc3; 
    color:#fff; 
    height:300px; 
    overflow:hidden; 
} 
.content { 
    color:#fff; 
    background:#a6d8ff; 
    height:100px; 
    display:flex; 
    justify-content:center; 
    align-items:center; 
    flex-flow:column nowrap; 
} 
.content p { 
    margin:0px; 
} 
+0

哇...它正在工作,因为我期待,非常感谢...... – LKG

+0

您的欢迎:) –

1

align-items:center;保持flex的子元素与center对齐。将值更改为left将最左对齐。再一次,你必须调整<p>元素来获得你想要的输出。我宁愿margin-left与价值一些%,类似下面:

.wrapper { 
    max-width:300px; 
} 
.main { 
    width:300px; 
    display:flex; 
    justify-content:center; 
    align-items:center; 
    flex-flow:column nowrap; 
    background:#458bc3; 
    color:#fff; 
    height:300px; 
    overflow:hidden; 
} 
.content { 
    color:#fff; 
    background:#a6d8ff; 
    height:100px; 
    display:flex; 
    justify-content:center; 
    align-items:left; 
    flex-flow:column nowrap; 
} 
.content p { 
    margin-left: 40%; 
} 
+0

感谢答案..但文本不是左对齐检查你的答案。检查我发布的图片 – LKG

+0

您需要将“图片内容”左对齐吗?我以为你想要文本对齐中心。 – Sridhar

+0

是的,我需要“图像内容”的中心,但像图像左对齐,如果可能的话,更喜欢用flex回答 – LKG