Align span right inside div

Asked

Viewed 4,549 times

6

I’m doubtful how to align a text within span as an example:

This is my HTML

inserir a descrição da imagem aqui

I need to leave it like this, left text aligned on the right and left text aligned on the right, as an example:

inserir a descrição da imagem aqui

HTML:

<div className="viewInstrument-output">
    <div className="outputs">
        <div>
            <span className="viewInstrument-output-title viewInstrument-font">PROG. DEGELOS</span>
        </div>
        <div>
            <label className="viewInstrument-output-status-activated viewInstrument-output-status">Ativo</label>
        </div>
    </div>
    <div className="outputs">
        <div>
            <span className="viewInstrument-output-title viewInstrument-font">SAÍDAS</span>
        </div>
        <div>
            <label className="viewInstrument-output-status-activated viewInstrument-output-status">Refrigeração</label>
            <label className="viewInstrument-output-status-activated viewInstrument-output-status">Defrost</label>
            <label className="viewInstrument-output-status-deactivated viewInstrument-output-status">Fans</label>
        </div>
    </div>
    <div className="outputs">
        <div>
            <span className="viewInstrument-output-title viewInstrument-font">TEMP. DE CONTROLE</span>
         </div>
        <div>
            <label>-5.0<sup>°c</sup></label>
        </div>
    </div>
    <div className="outputs">
        <div>
            <span className="viewInstrument-output-title viewInstrument-font">DIFERENCIAL</span>
        </div>
        <div>
            <label>3.0<sup>°c</sup></label>
        </div>
    </div>
</div>
  • Can you post the CSS code that formats this excerpt as well? And what would be the attribute className elements in HTML? This attribute does not exist by default in HTML. It would not just be class?

  • It’s because I’m using reactJS.

3 answers

5

It is possible using display: table, and defining rows and columns to be tabulated, and then aligning the texts of each column.

Example

#tabela {
  display: table;
  background-color: gray;
  color: #fff;
}

.linha {
  display: table-row;
}

.coluna1 {
  display: table-cell;
  padding-right: 40px;
  padding-bottom: 10px;
  text-align: right;
}

.coluna2 {
  display: table-cell;
  padding-bottom: 10px;
  text-align: left;
  border: 1px solid #000;
  border-radius: 20px;
  border-color: #fff;
  background-color: #ccc;
  color: #fff;
  font-weight: bold;
}
<div id="tabela">
  <div class="linha">
    <div class="coluna1">Prog. Degelos</div>
    <div class="coluna2">Ativo</div>
  </div>
  <div class="linha">
    <div class="coluna1">Saídas</div>
    <div class="coluna2">Refrigeração - Defrost</div>
  </div>
  <div class="linha">
    <div class="coluna1">Temp. de controle</div>
    <div class="coluna2"> -5.0</div>
  </div>
</div>

  • use the flow-root is a bit skittish, I hope the AP does not copy this part of your CSS ;D

3


One way to do it would be by reorganizing your HTML in this way:

body {
  background: #333;
  color: #fff;
  font-family: sans-serif;
}

#labels {
  text-transform: uppercase;
  text-align: right;
  margin-right: 20px;
}

button {
  text-transform: uppercase;
  margin: 0 5px;
  border: none;
  background: #075E56;
  padding: 0 10px;
  border-radius: 10px;
  color: #fff;
}

.data {
  float: left;
}

.data p {
  line-height: 1rem;
  min-height: 21px;
}
<div id="labels" class="data">
  <p>Prog. Degelos</p>
  <p>Saídas</p>
  <p>Temp. de Controle</p>
  <p>Diferencial</p>
</div>

<div id="button" class="data">
  <p><button>Ativo</button></p>
  <p><button>Refrigeração</button><button>Defrost</button><button>Fans</button></p>
  <p>-5.0ºC</p>
  <p>3.0ºC</p>
</div>

  • 1

    +1 Use two columns and apply line-height: 1rem; in the content is a much better solution than my.

  • @Tobiasmesquita Your solution is very good too, but I only use tables as last chance. Haha...

  • I wanted to enjoy the HTML of him ;D already looked like grid even.

  • @Tobiasmesquita Opa. I confused your solution with the first one.You have many answers. Yours I saw now. Yes, yours has the advantage of keeping his organization.

  • I decided to take Leon’s to leave more organized the Abels and the Buttons, anyway the two were excellent, thank you.

  • @Tobiasmesquita your tbm worked out! Thank you

  • @Marquinferreira I do not blame you, I would do the same. hehe.

  • I have this question tbm, if I can help... https://answall.com/questions/204982/withdrawar-o-focus-outline-do-input-css

Show 3 more comments

3

the first thing you need to do is set a width for the div with the titles and for the with the tags. Then you can use the text-align css.

heml, body {
  position: relative;
  width: 100%;
  height: 100%;
  margin: 0px;
  padding: 0px;
  background-color: DarkSlateGray;
}

.linha:after {
  visibility: hidden;
  display: block;
  content: "";
  clear: both;
  height: 0;
}

.linha .celula {
  float: left;
  height: 40px;
  line-height: 40px;
}

.conteudo {
  width: calc(100% - 150px);
}

.titulo {  
  width: 150px;  
  text-align: right;  
  font-size: 12px;
}

.info {
  font-size: 25px;
  line-height: 30px;
}

.info, .titulo {
  box-sizing: border-box;
  font-weight: bold;
  padding: 0px 5px;
  color: white;
}

.tag {
  box-sizing: border-box;
  height: 28px;
  width: 100px;
  margin: 6px 5px;
  padding: 5px;
  line-height: 20px;
  float: left;
  border: 1px solid white;
  border-radius: 10px;
  background-color: teal;
  color: white;
  text-align: center;
}
<div>
    <div class="linha">
        <div class="celula titulo">
            <span>PROG. DEGELOS</span>
        </div>
        <div class="celula conteudo">
            <div class="tag">Ativo</div>
        </div>
    </div>
    <div class="linha">
        <div class="celula titulo">
            <span>SAÍDAS</span>
        </div>
        <div class="celula conteudo">
            <div class="tag">Refrigeração</div>
            <div class="tag">Defrost</div>
            <div class="tag">Fans</div>
        </div>
    </div>
    <div class="linha">
        <div class="celula titulo">
            <span>TEMP. DE CONTROLE</span>
        </div>
        <div class="celula conteudo">
            <div class="info">-5.0<sup>°c</sup></div>
        </div>
    </div>
    <div class="linha">
        <div class="celula titulo">
            <span>DIFERENCIAL</span>
        </div>
        <div class="celula conteudo">
            <div class="info">3.0<sup>°c</sup></div>
        </div>
    </div>
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.