How to center a span in a div - html

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:
at the end it should looks like:
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/

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

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/

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%);

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>

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.

Related

Mini-navbar with left, middle and right in HTML and CSS

I want to make a very simple navbar with HTML and CSS (so simple I prefer to do it without Bootstrap), made of just three short texts, situated on the leftmost, center, and rightmost part of one single line.
My idea is that I cut the line in two halves, put the left & middle part in the first half, and the rightmost part in the second half. So I tried the following :
.div_left {
float: left;
position: absolute;
}
.div_right {
float: right;
text-align: right;
position: absolute;
}
.container_for_mininavbar {
width: 100%;
position: absolute;
}
.mininavbar_left_half {
width: 50%;
float: left;
position: absolute;
}
.mininavbar_right_half {
width: 50%;
float: right;
position: absolute;
}
<div class="container_for_mininavbar">
<div class="mininavbar_left_half">
<div class="div_left">Left Text</div>
<div class="div_right">Center Text</div>
</div>
<div class="mininavbar_right_half">
<div class="div_right">Right Text</div>
</div>
</div>
But that doesn't work, all the texts are on top of each other.
What is the correct way to do this?
Just remove position absolute.
I'll suggest to use flexbox to do this and don't use float anymore
.div_left {
float: left;
}
.div_right {
float: right;
text-align: right;
}
.container_for_mininavbar {
width: 100%;
}
.mininavbar_left_half {
width: 50%;
float: left;
}
.mininavbar_right_half {
width: 50%;
float: right;
}
<div class="container_for_mininavbar">
<div class="mininavbar_left_half">
<div class="div_left">Left Text</div>
<div class="div_right">Center Text</div>
</div>
<div class="mininavbar_right_half">
<div class="div_right">Right Text</div>
</div>
</div>
And this is a little example with flexbox
.container_for_mininavbar {
width: 100%;
border: 1px;
display: flex;
}
.container_for_mininavbar div {
flex: 0 1 33.33%;
border: 1px solid #000;
text-align: center;
}
<div class="container_for_mininavbar">
<div>Left Text</div>
<div>Center Text</div>
<div>Right Text</div>
</div>
So, you want some links to the left, some to the center and the rest to the right?
The easiest and most effective way (by me) is to use Flexbox.
So, you need a container div, named "navigation" (or however you want) which contains another 2 divs, one for the left side, and one for the right side.
Now, assign to the navigation div, the following:
display: flex; /* is going to display the div flex */
justify-content: space-between; /* this is where magic happens, it will push the items from the nav div, which are the other 2 divs to the left and right side*/
flex-flow: row nowrap;
The first property is for it to be displayed in a row, you can set it to column too, and the nowrap is not going to let the content to deform in some sort of way, if you set that to wrap, of course, it will wrap under, but I suggest letting that nowrap, but I don't think flex-flow is 100% neccesary in this situation
Now, the flexbox works for the other 2 divs as well, maybe you want the links in the left-side div to be "justify-content: space-between;" or space-evenly, or center, space-around, etc.
I recommend you to learn Flexbox, it's very useful and simple to use.
I hope this answer will help you. :)
And to center the links in each div, use align-items: center; , it will center the links on the Y scale. (which is top-bottom)
EDIT: If you want center links too, it's the same thing, just make another div between the left-side div and the right div. And the justify-content: space-betweeen; it's going to have the same effect. And if you don't link how it scales, you can always use the margins in the div.

Justify content does not seem to affect my flexbox children. Is it because of my height/width properties for one of my elements?

What I have set up is a flexbox that contains 3 children: the previous button, a div that contains the slideshow, and the next button. I have the flexbox set to justify-content: center, but the arrows stick to the far sides of the flexbox instead of right next to the slide div on either side. There is a gap between each arrow and the slide that won't go away unless I make the browser window narrower so the flexbox doesn't have any extra room. Setting justify-content to center should be keeping them all together.
I'm having the same issue with the slide container as well. It is also a flexbox and contains 3 children: a title, the image, and numbers at the bottom. When I shrink the window, the slide image shrinks to fit, but the title and numbers both stay stuck to the top and bottom of the flexbox instead of clustered in the middle with the image.
I would like this to be dynamic so I won't have to adjust the CSS as much with media queries for a number of different breaking points.
Things I have tried:
setting the margins to 0px for all children
setting the padding to 0px for all children
putting the buttons inside the slideshow container instead of the
current parent and setting the slideshow container to flex
every other option for justify-content (center, flex-start, flex-end, etc); none of them seemed to have any effect on the children
removing the width attribute from my slideshow container and setting it to auto (this made the slides disappear)
removing the height attribute from my slide container (this did fix the issue, but it also caused the slide to extend past the boundaries of its container)
I am also using some javascript that probably doesn't need to be included, but it sets each new slide's display to block and each previous slide's display to none. In order for this slideshow to work, I am under the impression that the slides need to their position set to absolute, and their parent container set to relative.
Here is a fiddle with the exact code I am using. I initially tried to simplify it to ask this question by removing container1 and the description on the left, but then I couldn't replicate the problem I was having. I am wondering if it has something to do with the height or width attributes of one of elements.
https://jsfiddle.net/kj7wzr5a/
.container1 {
width: 85%;
height: 650px;
display: flex;
justify-content: space-around;
align-items: center;
margin: 0 auto;
text-align: center;
}
.left {
width: 45%;
text-align: center;
margin-left: 30px;
margin-right: 20px;
}
.descrip1 {
margin-top: 20px;
}
.right {
height: 100%;
width: 45%;
display: flex;
justify-content: center;
align-items: center;
}
.flex {
height: 100%;
display: flex;
flex-direction: column;
align-content: center;
}
.slideshow-container {
height: 100%;
width: 360px;
margin-top: 25px;
position: relative;
}
.slide {
height: 90%;
width: 100%;
display: none;
position: absolute;
}
.slide:first-child {display: block}
.slide {
-webkit-transition: 0.5s ease;
transition: 0.5s ease;
}
.slide img {
height: 100%;
width: 100%;
margin: 0px;
object-fit: contain;
}
/* Next & previous buttons */
.buttons {
height: auto;
}
<div class="container1 bodytext">
<div class="left">
<div class="descrip1">
<p>Blah blah text</p>
</div>
</div>
<div class="right">
<div class="buttons">
<a class="slider_nav prev"><</a>
</div>
<div class="slideshow-container">
<div class="slide">
<div class="flex">
<div class="text">New Character</div>
<img src="1.png" style="width:100%">
<div class="numbertext">1 / 10</div>
</div>
</div>
<div class="slide">
<div class="flex">
<div class="text">New Character Stats</div>
<img src="2.png" style="width:100%">
</div>
</div>
</div>
<div class="buttons">
<a class="slider_nav next">></a>
</div>
</div>
</div>
I am fairly new to coding and haven't had any formal training.

Can I create Background and foreground using Flexbox?

I want to position one div in the background and the other div in the foreground. The foground div should be centered automatically vertically and horizontally according to the size of the div in the background, or the size of the container div.
I wonder if there is an elegant way to do it using Flexbox. Sometimes, before I start arranging the flexbox elements I see the different children overlap. So far I have failed to duplicate this. The advantage is that if we can get the elements to overlap using flexbox, we can center the foreground both vertically and horizontally easily using the margin:auto;.
A different way to do this is to use position relative and absolute. For example:
<div class="container" style="position:relative; height:400px; width:100%;">
<div class="backgorund" style="height:100%; width:100%; background-color:green;">
<div class ="child" style="position:absolute; left:50%;">
<div class="foreground" style="position:relative; right:50%">
<p>I'm centered horizontally, but not vertically</p>
</div>
</div>
</div>
but then I'll have to add flexbox inside the foreground div to center the foreground vertically.
I’m not sure if I got you right. But you would just need display: flex; for releasing the magic, align-items: center; for horizontal, and justify-content: center; for vertical aligning nested element(s).
html, body {
height: 100%;
}
.container {
height: 100%;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
background-color: pink;
}
.item {
padding: 2em;
color: white;
background-color: hotpink;
}
<div class="container">
<div class="item">
<p>Lorem ipsum dolor sit amet</p>
<p>Consectetur adipisci velit</p>
</div>
</div>
The answer would be no. You can use flexbox and you can center things - and you can stack those things on top of each other, but there isn't currently a special flexbox way of doing that. You'll have to use another positioning.
I use this technique often. Note that you'll need a relatively positioned parent
#mixin absolute-translate-center() {
float: none; /* just in case */
position: absolute;
margin-left: 50%;
margin-right: 50%;
transform: translate(-50%, -50%);
}
.thing {
#include absolute-translate-center();
}
or
absolute-translate-center()
float: none
position: absolute;
margin-left: 50%
margin-right: 50%
transform: translate(-50%, -50%)
.thing
absolute-translate-center()

Put and align text with the first div of three (flexbox)

I'm trying to make this layout (I've made a picture to explain what i want to do):
So I've 4 divs where I'm going to put some text inside. I've used flexbox and justify content to align them center, but i want to put a text "Latest News" that is aligned with the first div (in this case Element 1).
I'm not able to think about an elegant solution to my problem, so I'm here to ask for help.
.wrapper{
display: flex;
justify-content: center;
}
.box{
height: 200px;
background-color: red;
margin: 10px;
width: 200px;
}
<p class="section-title">Latest News</p>
<div class="wrapper">
<div class="box">Element 1</div>
<div class="box">Element 2</div>
<div class="box">Element 3</div>
<div class="box">Element 4</div>
</div>
There are a few ways you can do it, and it depends how dynamic your box elements are going to be.
One simple solution that works for n boxes is to include the section title to the first box and give it position: absolute whilst adding margin-top to the wrapper to make space for the title.
https://codepen.io/anon/pen/MJpOrM
.wrapper {
display: flex;
justify-content: center;
margin-top: 40px;
}
.box {
height: 200px;
background-color: red;
margin: 10px;
width: 300px;
}
.section-title {
position: absolute;
top: 0px;
}
<div class="wrapper">
<div class="box">
<p class="section-title">Latest News</p>
Element 1
</div>
<div class="box">Element 2</div>
<div class="box">Element 3</div>
<div class="box">Element 4</div>
</div>
Considering that you have a fixed width for your boxes, the easiest solution is to make the section-title a fixed width too:
.section-title {
width: 1260px; //This is merely 300 * 4 + the margin
margin: auto;
}
https://codepen.io/anon/pen/BpWmJV

How to reduce the indentation between first and second line inside circle?

This issue really was causing me troubles.
I have a circle. That circle is a square with border-radius: 50%. It's also flex.
The first line contains an icon, the second line has text.
My problem is that they have large indentation between, I'd like them to be closer to each other. I can't come up with an idea how to fix this.
JSFiddle
HTML
<ion-content has-header="false">
<div class="dashboard-grey-menu">
<div class="flex row no-padding">
<div class="col">
<div class="circle" ui-sref='clients'>
<div class="ion-ionic"></div>
<div>Second line</div>
</div>
</div>
</div>
</div>
</ion-content>
CSS
.dashboard-grey-menu {
height: 23vh;
background-color: #959595;
}
.circle {
border-radius: 50%;
width: 18vw;
height: 18vh;
background-color: #D0D0D0;
font-size: 1em;
font-weight: 900;
margin: auto;
display: flex;
align-items: center;
justify-content: space-around;
flex-flow: column;
}
How would you solve this? Thanks in advance!
Solution 1: add style="padding-bottom:8vh to your second div
Solution 2: delete justify-content: space-around; from .circle in css because main reason for this space is that one. U should do it with using padding or margin css commands with using vh to not lose responsivilty