[英]How can I get my content centered in this responsive div?
I have 4 columns in a row that become 1 column on mobile. 我连续有4列,成为移动设备上的1列。 The way this looks on mobile is fine, but I want to center the content in divs 2 and 4 in each row. 在移动设备上的外观很好,但是我想将内容集中在每行的div 2和div 4中。 What would be the best way to do this? 最好的方法是什么?
Apparently my edit was deleted or the site messed up, but I had said I want to do this vertical and horizontal. 显然我的编辑已删除或网站混乱,但我曾说过我想垂直和水平进行此操作。 However, it was my fault I forgot to link to the page. 但是,这是我的错,我忘了链接到该页面。 Sorry, guys. 对不起大家。 - http://www.dismantledesign.com/testsite2/clients.html -http://www.dismantledesign.com/testsite2/clients.html
Edit: I appreciate all the answers, but I don't think anyone understands that this div has no set height. 编辑:我很感谢所有的答案,但我认为没有人知道该div没有设置高度。 I just want that text and icons to stay centered as the screen moves. 我只希望文本和图标在屏幕移动时保持居中。 The flex didn't seem to work because it required setting a height to the div. flex似乎没有用,因为它需要为div设置一个高度。
I also simplified my HTML 我还简化了HTML
Code 码
.clientsdetail { text-align: center; margin: 0px; padding: 0px; overflow-x: hidden; } .clientinfo h3 { padding-top: 10px;; } .clientinfo p { padding: 0px 30px; font-size: 15px; }
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <div class="row"> <div class="col-md-3 nopadding"> <img class="img-responsive" src="img/clients/ivey.jpg"> </div> <div class="col-md-3 nopadding cinfo"> <div class="clientinfo text-center"> <h3>IVEY</h3> <p>Clarksville, TN</p> <div class="social-icons-clients"> <a href="#"><span class="icon-sprite sprite-ig"> </span></a> <a href="#"><span class="icon-sprite sprite-fb"> </span></a> <a href="#"><span class="icon-sprite sprite-gp"> </span></a> <a href="#"><span class="icon-sprite sprite-sc"> </span></a> <a href="https://twitter.com/contracoda" target="_blank"><span class="icon-sprite sprite-tw"> </span></a> </div> </div> </div> <div class="col-md-3 nopadding cinfo"> <img class="img-responsive" src="img/clients/rufus.jpg"> </div> <div class="col-md-3 nopadding"> <div class="clientinfo text-center"> <h3>RUFUS DAWKINS</h3> <p>Clarksville, TN</p> <div class="social-icons-clients"> <a href="#"><span class="icon-sprite sprite-ig"> </span></a> <a href="#"><span class="icon-sprite sprite-fb"> </span></a> <a href="#"><span class="icon-sprite sprite-gp"> </span></a> <a href="#"><span class="icon-sprite sprite-sc"> </span></a> <a href="https://twitter.com/contracoda" target="_blank"><span class="icon-sprite sprite-tw"> </span></a> </div> </div> </div> </div>
(UPDATED) You can use the Pseudoclass
to select the 2 and 4 div. (更新)您可以使用Pseudoclass
选择2和4 div。
.row div:nth-child(2n) {
/* Your CSS Style */
}
If the content inside the 2 and 4 div are inline/inline-block
, just use text-align: center;
如果2和4 div inline/inline-block
的内容是inline/inline-block
,则只需使用text-align: center;
. 。
Demo 演示版
Here is the simple demo for the pseudoclass
. 这是pseudoclass
的简单演示。
.row > div { padding: 10px; } .row > div:nth-child(2n) { color: #fff; background: red; text-align: center; height: 50px; display: flex; align-items: center; justify-content: center; }
<div class="row"> <div>One</div> <div>Two</div> <div>Three</div> <div>Four</div> </div>
Hope this is helpful for you. 希望这对您有帮助。 Thanks. 谢谢。
You can use flex properties to make your content to center. 您可以使用flex属性使内容居中。
for more details, you can check 有关更多详细信息,您可以检查
Try a different approach. 尝试不同的方法。 Bootstrap's floating grid system will not get you where you want. Bootstrap的浮动网格系统无法将您带到您想要的地方。
First, remove row
and col-
classes so you have something like this: 首先,删除row
和col-
类,所以你有这样的事情:
<div class="my-row">
<div></div>
<div></div>
<div></div>
</div>
Then use CSS to achieve the same result as before, but with the content aligned center: 然后使用CSS达到与以前相同的结果,但内容对齐中心:
@media (min-width: 992px) {
.my-row {
display: flex;
align-items: stretch;
}
.my-row > div {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
}
}
If you are using SASS, then replace hardcoded 992px with the @screen-md-min
variable. 如果使用的是SASS,则将硬编码的992px替换为@screen-md-min
变量。
Here's a JsFiddle of your simplified example: 这是简化示例的JsFiddle:
https://jsfiddle.net/mpnz9b30/1/ https://jsfiddle.net/mpnz9b30/1/
<div class="col-md-6 nopadding cinfo">
<div class="clientinfo">
<h3>IVEY</h3>
<p>Clarksville, TN</p>
<div class="social-icons-clients">
<a href="#"><span class="icon-sprite sprite-ig"> </span></a>
<a href="#"><span class="icon-sprite sprite-fb"> </span></a>
<a href="#"><span class="icon-sprite sprite-gp"> </span></a>
<a href="#"><span class="icon-sprite sprite-sc"> </span></a>
<a href="https://twitter.com/contracoda" target="_blank">
<span class="icon-sprite sprite-tw"> </span>
</a>
</div>
</div>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.