简体   繁体   中英

Trying to left-align text in a centered element

I am trying to achieve what you see at the bottom of the panel in the image below. Each of the 3 items are centered but the text is left aligned.

I have developed the following basic CSS and HTML code. I am trying to use flexbox as much as possible for responsive layout. Anyone have any pure HTML/CSS solution?

I understand that the p tag is a block level element. So what are my options without setting the width of the p tag? Or maybe there is another tag I could use?

The HTML and CSS code I have provided below has the basic structure only.

在此输入图像描述

 .panel { display: flex; border: 1px solid black; min-height: 300px; flex-direction: column; max-width: 500px; } .panel-body { display: flex; flex: 1; flex-flow: row wrap; justify-content: center; } .panel-heading { padding: 10px 10px; } .panel-body div.chart { flex: 0 0 100%; min-height: 150px; background-color: green; } .panel-body div { text-align: center; flex: auto; background-color: red; display: flex; flex-direction: column; justify-content: space-around; } p { border: 0; padding: 0; margin: 0; text-align: left; } 
 <div class="panel"> <div class="panel-heading"> HEADING </div> <div class="panel-body"> <div class="chart"></div> <div> <p>HIGH <br/>144</p> </div> <div>MEDIUM <br/>2</div> <div>LOW <br/>3</div> </div> </div> 

Try this HTML code:

<div class="panel">
  <div class="panel-heading">
    HEADING
  </div>
  <div class="panel-body">
    <div class="chart"></div>
    <div><p>HIGH<br/>144</p></div>
    <div><p>MEDIUM<br/>2</p></div>
    <div><p>LOW<br/>3</p></div>
  </div>
</div>

It appears that you originally only put the div containing "HIGH" and "144" in a <p> tag, which, according to your css code, is the attribute that is being styled to have left-aligned text. However, the content within the other 2 <div> s were not enclosed within a <p> tag, and so they were not being styled.

Just changed styles of .panel-body div . Also there is no need for p tag here, consider removing it from markup. Demo:

 .panel { display: flex; border: 1px solid black; min-height: 300px; flex-direction: column; max-width: 500px; } .panel-body { display: flex; flex: 1; flex-flow: row wrap; justify-content: center; } .panel-heading { padding: 10px 10px; } .panel-body div.chart { flex: 0 0 100%; min-height: 150px; background-color: green; } .panel-body div { /* Take 33.33% width, allow grow and shrink */ flex: 1 1 33.33%; background-color: red; /* become a flex-container */ display: flex; /* align flex-items vertically */ flex-direction: column; /* center vertically */ justify-content: center; /* center horizontally */ align-items: center; } p { border: 0; padding: 0; margin: 0; text-align: left; } 
 <div class="panel"> <div class="panel-heading"> HEADING </div> <div class="panel-body"> <div class="chart"></div> <div> <p>HIGH <br/>144</p> </div> <div>MEDIUM <br/>2</div> <div>LOW <br/>3</div> </div> </div> 

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