[英]3 DIVs, 1 Container, Centrally aligned horizontally
I've got three DIV
s that I've put into a container DIV
. 我有三个DIV
,我已经放入容器DIV
。
What I want is as follows: 我想要的是如下:
Here's where I'm up to: 这是我要去的地方:
#light-table { background-color: #e2e2e2; width: 100%; padding-top: 15px; padding-bottom: 15px; padding-left: 40px; padding-right: 40px; text-align: left; margin-top: 30px; margin-bottom: 30px; } #leftdiv { float: left; padding: 0 20px; /*margin:20px 0;*/ position: relative; width: 25%; flex-basis: 25%; } #leftdivcontainer { vertical-align: middle; width: 100%; text-align: center; } #light-table-paragraph { font-family: 'Droid Serif'; font-size: 20px; color: #2e2e2e; text-align: left; line-height: 40px; }
<div id="light-table"> <h3 id="light-table-head-style">content.</h3> <div id="leftdivcontainer"> <div id="leftdiv"> <p id="light-table-paragraph">Left</p> </div> <div id="leftdiv"> <p id="light-table-paragraph">Middle</p> </div> <div id="leftdiv"> <p id="light-table-paragraph">Right</p> </div> </div> </div>
Please can someone help tell me where I'm going wrong? 请有人帮我告诉我哪里出错了?
Thanks! 谢谢! Scott 斯科特
set the div the contains the three small divs display:flex
and give it 75% width of the container, then set space around the content as follow: 设置div包含三个小div display:flex
并给它75%的容器宽度,然后在内容周围设置空格如下:
#leftdiv { /*float: left;*/ padding:0 20px; /*margin:20px 0;*/ position:relative; /* edits */ width:33.33%; flex-basis: 25%; } #leftdivcontainer { vertical-align:middle; text-align: center; /* edits */ width:75%; display: flex; margin: 0px auto; justify-content: space-around; } #light-table-paragraph { font-family: 'Droid Serif'; font-size: 20px; color: #2e2e2e; text-align: left; line-height:40px; } #light-table { background-color: #e2e2e2; width: 100%; padding-top: 15px; padding-bottom: 15px; padding-left: 40px; padding-right: 40px; text-align: left; margin-top:30px; margin-bottom: 30px; }
<div id="light-table"> <h3 id="light-table-head-style">content.</h3> <div id="leftdivcontainer"> <div id="leftdiv"><p id="light-table-paragraph">Left</p></div> <div id="leftdiv"><p id="light-table-paragraph">Middle</p></div> <div id="leftdiv"><p id="light-table-paragraph">Right</p></div> </div> </div>
Here's how I would do it. 这是我将如何做到这一点。
Give each .leftdiv (indeed this should be a class, id's are unique) 33% of total viewport width: 给每个.leftdiv(实际上这应该是一个类,id是唯一的)33%的总视口宽度:
.leftdiv {
float: left;
width: 33%;
}
and center each paragraph inside these divs, give it 75% width: 并将这些div中的每个段落居中,给它75%的宽度:
.leftdiv p {
display: block;
width: 75%;
margin: 0 auto !important; /* you won't need !important if your code is well structured */
}
This is a cleaner solution, as you'll notice there is no horizontal scroll present. 这是一个更清洁的解决方案,因为您会注意到没有水平滚动。
Here is a codepen. 这是一个codepen。
Also, you need to clear your parent div #leftdivcontainer (did that as well). 此外,你需要清除你的父div #leftdivcontainer(也做到了这一点)。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.