7

i have already tried many thing to center a span Element (which contains couple of DIVs) in a wrapper DIV (in this case the soccerField DIV)...

HTML structure looks like this:

<div id="soccerField">
    <span id="defendLine">
        <div>player 1</div>
        <div>player 2</div>
        <div>player 3</div>
        <div>player 4</div>
    </span>
<div>

CSS looks like this:

#field{
padding: 0%;
min-width: 250px;
min-height: 1000px;
max-width: 800px;
position: relative;
text-align: center
}

#defendLine{
 display: inline-block;  
}

at the moment it looks like: enter image description here

at the end it should looks like: enter image description here

but this result i only get when I am using this code:

#defendLine{
  position: absolute;
  left:59px
}

but this is not the right way I think becasue when I have only 3 players or 5 players at my DefendLine, i also have to give some "left: X" values. But i want to make it dynamically. >> doesnt matter how much players, always center the span element, independentley how much players my includes.

I have already check these articles which helps other, but in this case. I couldnt help myself :(

CSS How to center a div horizontally How do you easily horizontally center a <div> using CSS?

I would be really happy if anyone could give me a hint!

Edit:

I have created a fiddle, i tried all solutions out. I think something else is the reason for my problem :(

https://jsfiddle.net/rztad75y/2/

Community
  • 1
  • 1
nbg15
  • 129
  • 1
  • 3
  • 11
  • on a separate consideration.. you may benefit from embedding resources.. jump over to https://www.base64-image.de/ .. drag and drop your images.. get the css declaration that contains the image/base64 string representation.. – Brett Caswell Nov 20 '16 at 08:53
  • you have a lot more style declarations going on there.. seems you're inheriting `margin:0` from a style declaration toward the bottom.. it should be `margin:0 auto` on `#defendLine`.. as @Eli indicated in his answer. and really..that's the only consideration here. `text-align:center;` on parent and `margin-right: auto;margin-left: auto` on child – Brett Caswell Nov 20 '16 at 09:04
  • ok i wil try it again now, hold on pls – nbg15 Nov 20 '16 at 09:09
  • @BrettCaswell: i did exactly what you said ;) look here please at the CSS section i have placed the relevant code: https://jsfiddle.net/rztad75y/7/ but like you see in the fiddle, it doesn't work... – nbg15 Nov 20 '16 at 09:17
  • yeah.. I did..the issue is you're assign `position:absolute; left; top;` on `.def` https://jsfiddle.net/rztad75y/9/ – Brett Caswell Nov 20 '16 at 09:35
  • 1
    check this please: https://jsfiddle.net/UsulPro/rztad75y/8/ is it what you need? – Oleg Pro Nov 20 '16 at 09:40
  • 1
    @OlegPro WOW OLEG! THANKS THATS REALLY WHAT I NEED! – nbg15 Nov 20 '16 at 10:12
  • Possible duplicate of [Center span with text in div](http://stackoverflow.com/questions/32176703/center-span-with-text-in-div) – Rob Nov 20 '16 at 15:05

5 Answers5

11

Here is a JSFiddle with the solution and I'll explain some keys of how to achieve it:

1) I use flexbox

#field {
  /* I skip properties that do not change */
  display: flex;
  justify-content: center; /* here we just put the only child div to the center */
}

I changed #defendLine to .defendLineSE just to make sure that it's not overriden elsewhere. (that's why your code could not work - just change that back)

.defendLineSE {
  width: 400px;
  display: flex;
  justify-content: space-between;
}

2) Note that we need to have width. This width is a sum of #defendie-s width and spaces between them (justify-content: space-between;)

3) I removed all position: absolute in your #defendie because they destroy our flexbox (you don't need to use left: 387px; as well)

tip1: I believe you will find usefull this flexbox-cheatsheet

tip2: Since we use justify-content: space-between; you may not need such classes like .firstColumn and others

tip3: change #defendLine to .anyLine, remove width: 400px;, add margin to your .defendie-s - you'll have universal solution for any line with any number of players

Oleg Pro
  • 2,323
  • 1
  • 15
  • 23
  • hey Oleg, for some reasons bothe of your solutions aer not working... I think something else is wrong... I have added a fiddle at my 1st post (sorry for the huge css part)... maybe its easier to see where the problem is... thank you also to you for your help! ;) – nbg15 Nov 20 '16 at 08:56
  • Could you checked: https://jsfiddle.net/UsulPro/rztad75y/8/ Is it what you are looking for? It's in the middle. I was need to remove some `absolute` props in CSS. If you like it, I can give you some details about solution – Oleg Pro Nov 20 '16 at 09:36
  • thank you so much man! yes pleas! could you tell me what i have to change? – nbg15 Nov 20 '16 at 10:13
  • can i copy the whole css part back into my own code? EDIT: no it doesnt work that easy hehe... I tried it without success – nbg15 Nov 20 '16 at 10:15
  • @nbg15 that depends on your application and configuration.. it looks like there is possible bundling going on there.. and potential for transpiling.. – Brett Caswell Nov 20 '16 at 10:17
6

using a <span> tag is a bad practice. instead, change it to a <div> like this:

HTML

<div id="soccerField">
    <div id="defendLine">
        <div>player 1</div>
        <div>player 2</div>
        <div>player 3</div>
        <div>player 4</div>
    </div>
<div>

CSS

#defendLine{
 width: 90%;
 margin: 0 auto;  
}

Edit if you'd like to center the content of the div itself, set text-align:center or simply use flexbox:

display:flex;
justify-content: center;
align-items: center;

more info could be found here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

elicohenator
  • 747
  • 6
  • 17
  • Hey Eli! Thanks for your help! your first comment doesnt work. and about your edited answer I have one question! Should I add these 3 lines to my wrapper div or to my child div (old span)??? Thank u so much! greetings – nbg15 Nov 20 '16 at 08:24
  • if you'll check the guide you'll see that flexbox is always added to the container of your elements :) anyway, s are for text elements so stick to divs. and check the link i've gave you – elicohenator Nov 20 '16 at 08:27
  • please accept the answer if it's correct. thank you @nbg15 – elicohenator Nov 20 '16 at 08:58
  • Thanks for your help again Eli, but your code doenst helped me out :( MAybe you could check my fiddle at my 1st post? I really dont know what other css is the reason why your code doenst work for me :( – nbg15 Nov 20 '16 at 08:59
  • 1
    they wont change because you gave them `position: absolute`. if you're not familiar with grid solutions, try using some framework for it (i recommend Zurb's Foundation) and learn to work with columns @nbg15 – elicohenator Nov 20 '16 at 09:14
  • I put a quick fix in the handle his code in https://jsfiddle.net/rztad75y/9/ .. but you are absolutely correct. – Brett Caswell Nov 20 '16 at 09:37
  • Using a `` is NOT bad practice unless you are saying it's bad practice in this case but that would be subject to opinion. I don't see how using a `
    ` changes anything.
    – Rob Nov 20 '16 at 15:07
  • @Rob using `span` as a container for other elements is a bad practice.. the same goes for `p`.. those tags are only supposed to contain text.. it also breaks accessibility readers/interpreters. – Brett Caswell Nov 21 '16 at 03:28
  • @BrettCaswell Yes. I was basing my comment on what he said and his suggested solution but didn't relate it to the question. – Rob Nov 21 '16 at 04:04
1

You could use transform: translateX() like this..

position: absolute;
left: 50%;
transform: translateX(-50%);

#field{
padding: 0;
min-width: 250px;
min-height: 1000px;
max-width: 800px;
position: relative;
text-align: center
}

#defendLine{
 display: flex;
  position: absolute;
  top: 0%; left: 50%;
  transform: translateX(-50%);

}
#defendLine div {
 display: flex;
 flex-basis: 20%;
}
<div id="soccerField">
    <span id="defendLine">
        <div>player 1</div>
        <div>player 2</div>
        <div>player 3</div>
        <div>player 4</div>
    </span>
<div>

It works vertically as well..

  top: 50%; left: 50%;
  transform: translateY(-50%) translateX(-50%);
Hastig Zusammenstellen
  • 4,286
  • 3
  • 32
  • 45
  • Hey Hastig Zusammenstellen! Thanks for your help! Your code always center it. Thats good ;) but out of my player field! (center it depends on the whole width size of my page if have the feelings)... i cant add images to this post here I think! – nbg15 Nov 20 '16 at 08:29
  • If you can recreate it in a fiddle we can help you with that. You may need to put `width: 100%;` on your `img` so that it's responsive relative to its container. – Hastig Zusammenstellen Nov 20 '16 at 08:33
1

Use display: flex for the parent div, and justify-content: center to center items inside on the horizontal axis, and align-items: center to center them vertically. Flex boxes are a real helper in cases like this.

Babao
  • 1,273
  • 1
  • 8
  • 3
0

You were on the right track. You can center elements using text-align: center

#field{
padding: 0%;
min-width: 250px;
min-height: 1000px;
max-width: 800px;
position: relative;
/*text-align: center*/
}

#defendLine{
 text-align: center;
 display: block;/* This is a span, you have to make it a block*/
}

#defendLine div{
 display: inline-block;  
}
<div id="soccerField">
    <span id="defendLine">
        <div>player 1</div>
        <div>player 2</div>
        <div>player 3</div>
        <div>player 4</div>
    </span>
<div>
Gildas.Tambo
  • 22,173
  • 7
  • 50
  • 78