Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create span on new line?

Tags:

html

css

flexbox

I have multiple spans inside of a div but all are at same line. I want to write every span on new line. Here is the html code:

.divContent {
  position: absolute;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  width: 100%;
  bottom: 5%;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  z-index: 2;
  opacity: .8
}

.divContent span {
  font-size: 0.875rem;
  color: #4D575D;
  margin: 0 3px;
}
<div id="content" class="divContent">
  <span>Span 1</span>
  <span>Span 2</span>
  <span>Span 3</span>
</div>

Now the result of this code is: Span 1 Span 2 Span 3

But I want

Span 1

Span 2

Span 3

like image 398
Ask Avatar asked Mar 05 '23 10:03

Ask


2 Answers

Change the direction to column:

.divContent {
  position: absolute;
  display: flex;
  flex-direction: column; /*added this */
  width: 100%;
  bottom: 5%;
  justify-content: center;
  align-items:center; /*added this if you want centring*/
  z-index: 2;
  opacity: .8
}

.divContent span {
  font-size: 0.875rem;
  color: #4D575D;
  margin: 0 3px
}
<div id="content" class="divContent">
  <span>Span 1</span>
  <span>Span 2</span>
  <span>Span 3</span>
</div>
like image 174
Temani Afif Avatar answered Mar 16 '23 18:03

Temani Afif


You can achieve the desired effect using the display propery that you've defined in .divContent class in your stylesheet.

.divContent {
  position: absolute;
  display: grid; /* changed here */
  /* YOU CAN ALSO USE inline-grid HERE */
  width: 100%;
  bottom: 5%;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  z-index: 2;
  opacity: .8
}
.divContent span {
  font-size: 0.875rem;
  color: #4D575D;
  margin: 0 3px
}
<div id="content" class="divContent">
        <span>Span 1</span>
        <span>Span 2</span>
        <span>Span 3</span>
</div>
like image 25
retr0 Avatar answered Mar 16 '23 18:03

retr0