[英]aligning elements center in div, not centering
I have the following 我有以下
I need to get the team names and points centered, under team jerseys. 我需要在队服下以队名和得分为中心。 I have been researching and trying, and tweaking but it just wont get centered. 我一直在研究和尝试,并进行调整,但它不会居中。
In my latest attempt I tried help in this post How to align an element always center in div without giving width to its parent div? 在我最近的尝试中,我尝试了这篇文章中的帮助。 如何在div始终居中对齐元素而不给其父div设置宽度? with display:inline block to no success: 与display:inline块一起没有成功:
.tNames {
margin-left:0px auto;
margin-right:0px auto;
float:left;
}
<div align="center" class="tNames">
<center>
<label class="blue"><input type="radio" name="picks['.$x.']" value="'.$row['team1'].'"><span>'.$team1.'</span></label><br />
<label class="green"><input type="radio" name="picks['.$x.']" value="'.$row['team2'].'"><span>'.$team2.'</span></label><br />
<label class="green"><input type="radio" name="picks['.$x.']" value="draw"><span>Draw</span></label><br />
</center>';
echo'BY';
echo'<select name="score[]" id="winScore">';
echo'<option value="1">1</option>';
echo'<option value="2">2</option>';
echo'<option value="3">3</option>';
echo'<option value="4">4</option>';
echo'<option value="5">5</option>';
echo'<option value="6">6</option>';
echo'</select>';
echo'POINTS';
echo'</div>';
WITHOUT FLOAT 没有浮标
.tNames{
text-align: center;
}
This is the type of CSS that I normally use for centering things: 这是我通常用于居中的CSS类型:
.center{
display: block;
margin: 0 auto;
width: 20%; /*this can be whatever the width of each team name*/
text-align: center;
}
Hope that helps. 希望能有所帮助。 The comments are correct, using float: left;
注释正确,使用float: left;
will of course make the content go to the left rather than be centered. 当然会使内容向左移动而不是居中。 Here's a rough version that I made by removing the PHP from the code you posted: 这是我通过从发布的代码中删除PHP制成的粗略版本:
.tNames>* { margin: 0 auto; display: block; width: 20%; text-align: center; }
<div class="tNames"> <label class="blue"><input type="radio" name="picks['.$x.']" value="'.$row['team1'].'"><span>'.$team1.'</span></label> <label class="green"><input type="radio" name="picks['.$x.']" value="'.$row['team2'].'"><span>'.$team2.'</span></label> <label class="green"><input type="radio" name="picks['.$x.']" value="draw"><span>Draw</span></label> <select name="score[]" id="winScore"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> </select> </div>
Your problem is caused by the float:left;
您的问题是由float:left;
引起的float:left;
which is forcing them to be aligned to the left of the div. 这迫使它们在div的左侧对齐。
To align them in center you could simply use display:table;
要使它们居中对齐,您可以简单地使用display:table;
with .tNames
and display:table-row;
与.tNames
和display:table-row;
with .tNames labels
which represent the team names, here's what you need: 具有代表团队名称的.tNames labels
,这是您需要的:
.tNames { margin-left: 0px auto; margin-right: 0px auto; dsiplay: table; } .tNames label { display: table-row; }
<div align="center" class="tNames"> <label class="blue"> <input type="radio" name="picks['.$x.']" value="'.$row['team1'].'"><span>'.$team1.'</span> </label> <br /> <label class="green"> <input type="radio" name="picks['.$x.']" value="'.$row['team2'].'"><span>'.$team2.'</span> </label> <br /> <label class="green"> <input type="radio" name="picks['.$x.']" value="draw"><span>Draw</span> </label> <br />By <select name="score[]" id="winScore"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> </select>points. </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.