3

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.

enter image description here

.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>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Ron Buenavida
  • 59
  • 1
  • 2
  • 8
  • 2
    I see no `

    ` tags within MEDIUM nor LOW.. However, there is one within HIGH and seems to be doing what you're after..

    – Option Jul 20 '17 at 15:21

2 Answers2

0

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.

ajc2000
  • 651
  • 5
  • 20
0

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>
Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90