Center span in div using flexbox

Asked

Viewed 104 times

1

I’m using positions to use the top and the left but it still doesn’t look cool.

CSS:

span.vertical {
    writing-mode: vertical-lr;
    transform: rotate(180deg);
    height: 200px;
}
span.comum {
    width: 100%;    
}
div.cabecalhoVertical{
    display: flex;
    flex-direction: row;
    width: 100%;
    height: 200px;
    flex-wrap: nowrap;
    border: .1px rgb(0,0,0) solid;
}
div.cabecalhoVertical div {
    border-right: .1px rgb(0,0,0) solid;
    height: 100%;
    flex-grow: 1;
}
div.cabecalhoVertical div span {
    display: block;
    text-align: center; 
    font-weight: bolder;
}

HTML:

<div class='cabecalhoVertical'>
    <div style='width:100px;'><span class='comum'   >Nome do Gcéu </span></div>
    <div style='width:100px;'><span class='vertical'>Supervisor</span></div>
    <div style='width:100px;'><span class='comum'   >Líder</span></div>
    <div style='width:050px;'><span class='vertical'>Houve Supervisão?</span></div>
    <div style='width:050px;'><span class='vertical'>Houve dia de Jejum?</span></div>
    <div style='width:050px;'><span class='vertical'>Houve dia de Evangedismo?</span></div>
    <div style='width:050px;'><span class='vertical'>Membros Compromissados</span></div>
    <div style='width:050px;'><span class='vertical'>Visitantes</span></div>
    <div style='width:050px;'><span class='vertical'>Crianças de 0 à 12 anos</span></div>
    <div style='width:050px;'><span class='vertical'>Total de presentes</span></div>
    <div style='width:050px;'><span class='vertical'>Ofertas</span></div>                    
    <div style='width:050px;'><span class='vertical'>Disicpulados</span></div>                    
    <div style='width:050px;'><span class='vertical'>Número de decisões</span></div>                    
</div>    

It was right but I couldn’t centralize the text (span) in div in one of the positions.

Upshot: inserir a descrição da imagem aqui I tried with the CSS below using the postion but it was not good,

span.vertical {
    writing-mode: vertical-lr;
    transform: rotate(180deg);
    height: 200px;
    left: 50%;
    margin-left: -25%;
}
span.comum {
    width: 100%;    
    top: 50%;
    margin-top: -100px;
}
div.cabecalhoVertical{
    display: flex;
    flex-direction: row;
    width: 100%;
    height: 200px;
    flex-wrap: nowrap;
    border: .1px rgb(0,0,0) solid;
}
div.cabecalhoVertical div {
    position: relative;
    border-right: .1px rgb(0,0,0) solid;
    height: 100%;
    flex-grow: 1;
}
div.cabecalhoVertical div span {
    position: absolute;
    display: block;
    text-align: center;    
    font-weight: bolder;
}

The goal is centralize the span both in the vertical how much in the horizontal, Both of us spans who are as upset as those who are not. Upshot:

inserir a descrição da imagem aqui

2 answers

1


On the items that are horizontal, just vc remove the margin-top: -100 that you put on and already resolve.

On those standing upright take the margin-left: -25% and put translateX(50%) along with the rotate. Or if you want to make a small optical adjustment in case I thought you were not 100% aligned then you can use the margin-left same, but with 3 or 4px adjustment just beyond the translateX.

inserir a descrição da imagem aqui

See the result

span.vertical {
    writing-mode: vertical-lr;
    transform: rotate(180deg) translateX(50%);
    height: 200px;
    left: 50%;
    /* margin-left: -25%; */
}
span.comum {
    width: 100%;    
    top: 50%;
    /* margin-top: -100px; */
}
div.cabecalhoVertical{
    display: flex;
    flex-direction: row;
    width: 100%;
    height: 200px;
    flex-wrap: nowrap;
    border: .1px rgb(0,0,0) solid;
}
div.cabecalhoVertical div {
    position: relative;
    border-right: .1px rgb(0,0,0) solid;
    height: 100%;
    flex-grow: 1;
}
div.cabecalhoVertical div span {
    position: absolute;
    display: block;
    text-align: center;    
    font-weight: bolder;
}
    
 
<div class='cabecalhoVertical'>
    <div style='width:100px;'><span class='comum'   >Nome do Gcéu </span></div>
    <div style='width:100px;'><span class='vertical'>Supervisor</span></div>
    <div style='width:100px;'><span class='comum'   >Líder</span></div>
    <div style='width:050px;'><span class='vertical'>Houve Supervisão?</span></div>
    <div style='width:050px;'><span class='vertical'>Houve dia de Jejum?</span></div>
    <div style='width:050px;'><span class='vertical'>Houve dia de Evangedismo?</span></div>
    <div style='width:050px;'><span class='vertical'>Membros Compromissados</span></div>
    <div style='width:050px;'><span class='vertical'>Visitantes</span></div>
    <div style='width:050px;'><span class='vertical'>Crianças de 0 à 12 anos</span></div>
    <div style='width:050px;'><span class='vertical'>Total de presentes</span></div>
    <div style='width:050px;'><span class='vertical'>Ofertas</span></div>                    
    <div style='width:050px;'><span class='vertical'>Disicpulados</span></div>                    
    <div style='width:050px;'><span class='vertical'>Número de decisões</span></div>                    
</div>

  • Okay. But there’s still a problem with this approach: Look, Gcéu’s Name wasn’t exactly centered. And, as would be the case that name was for example: Name of Gcéu where the ...: Well, that wouldn’t fit in a single line, which would be a problem. You know? For verticals, translateX solved it. I tried Translatey horizontal but realized that not everything is flowers. KKK

  • @Carlosrocha I think that for all the questions and answers that you have had here on the site these types of fine tuning you could do alone in my view, not all are flowers, in fact, but I know that if you try you solve. The role of the answers here is to help you grow, not to do 100% of your job, or to foresee out of the blue the problems you have there and didn’t mention when you first asked the question. With everything that Sam and I have already responded to, it’s not possible that you can’t make that kind of adjustment on your own. You have to do your part ;)

  • Got it. So according to your perception I haven’t tried it here? I’ve tried it with align-items and it didn’t work. N]ao am the type that doesn’t try. But even so, I appreciate your effort already fetus and I will accept your answer as you accept! It’s just that here, there’s no proper space to show the attempts I’ve made other than the question. I’m following by this video https://www.youtube.com/watch?v=dbVQqcV9iEc, but when I try to use Alig-items it just doesn’t work

  • @Carlosrocha what I’m talking about is that with the techniques covered in the other answers you could already solve this, the align items only align the child if the father tbm has flex display. If you think the answer has solved it is cool, but if you think that this still not the way you would like to not have to worry about accepting, can appear someone else willing to help you with something closer to what you need, we do not answer only the points, and open questions not accepted disrupt the site. The intention here is to help the community, points are consequence. Abs

  • Thanks, but I think I’m a little lost here. I have a better Back experience and I get a lot of css. I’m guessing that position: abasoluto is being the villain. I’ll try it another way

  • @Carlosrocha I’m in the street now but I’ll see if I can improve something for you

  • Man, thank you so much for the support. I’ll wait

Show 2 more comments

1

I got:

span.vertical {
	writing-mode: vertical-lr;
	transform: rotate(180deg);
}
span.comum {		

}
div.cabecalhoVertical{
	display: flex;
	height: 200px;
	border: .1px rgb(0,0,0) solid;

}
div.cabecalhoVertical div {
	display: flex;
	flex-grow: 1;
	border-right: .1px rgb(0,0,0) solid;
	align-items: center;
	justify-content: center;
}
div.cabecalhoVertical div span {
	text-align: center;
	font-weight: bolder;
}
<div class='cabecalhoVertical'>
	<div style='width:215px;'><span class='comum'   >Nome do Gcéu </span></div>
	<div style='width:215px;'><span class='vertical'>Supervisor</span></div>
	<div style='width:215px;'><span class='comum'   >Líder</span></div>
	<div style='width:035px;'><span class='vertical'>Houve Supervisão?</span></div>
	<div style='width:035px;'><span class='vertical'>Houve dia de Jejum?</span></div>
	<div style='width:035px;'><span class='vertical'>Houve dia de Evangedismo?</span></div>
	<div style='width:035px;'><span class='vertical'>Membros Compromissados</span></div>
	<div style='width:035px;'><span class='vertical'>Visitantes</span></div>
	<div style='width:035px;'><span class='vertical'>Crianças de 0 à 12 anos</span></div>
	<div style='width:035px;'><span class='vertical'>Total de presentes</span></div>
	<div style='width:035px;'><span class='vertical'>Ofertas</span></div>					
	<div style='width:035px;'><span class='vertical'>Disicpulados</span></div>					
	<div style='width:035px;'><span class='vertical'>Número de decisões</span></div>			
</div>	

Now is to try with UL

  • Strange! With UL instead of DIV appears a kind of extra cell ocuta in the corner by pushing the "Name of Gceu" to the right. I wonder what it is?

  • Dude, I’m not gonna be able to tell you why you’re acting so cool!

  • is, the goal is to make with the least amount of lines

  • 1

    to solve the problem with UL, simply remove the standard padding before the first LI qe is 40px by padding:0

Browser other questions tagged

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