[英]Why does the order of my divs change when I use display: inline-block;?
I created a 3 column layout for big screens (min 1200px ). 我为大屏幕(最小1200px)创建了3列布局。 I'm trying to align the columns using display: inline-block.
我正在尝试使用display:inline-block对齐列。
The problem is that the order of the columns changes (to secondary - tertiary - primary) even though in the HTML the order is different (secondary - primary - tertiary). 问题是,即使HTML中的顺序不同(第二-主-第三),列的顺序也会更改(变为第二-第三-主)。 What seems to be the problem?
似乎是什么问题?
You can find the complete code example here: https://codepen.io/Cilvako/pen/brqeVN 您可以在此处找到完整的代码示例: https : //codepen.io/Cilvako/pen/brqeVN
<div class="container clearfix">
<div class="secondary col">
<h2>Welcome!</h2>
<p>...</p>
</div>
<div class="primary col">
<h2>Great food</h2>
<p>...</p>
</div>
<div class="tertiary col">
<h2>How to get here</h2>
<p>...</p>
<p>...</p>
<p>...</p>
</div>
</div>
The CSS looks like this: CSS看起来像这样:
.primary {
width: 40%;
}
.secondary,
.tertiary {
width: 30%;
}
.col {
display: inline-block;
vertical-align: top;
padding: 1rem;
margin: 0;
}
Because you're also float
ing those elements left and right. 因为您还在左右
float
这些元素。 Remove the floats, then remove the white space between the elements so the 30%, 40%, 30%
elements will all be on the same row and be 100%
total. 删除浮子,然后删除元素之间的空白,以便
30%, 40%, 30%
元素都在同一行上,总计为100%
。
/* ================================= Base Element Styles ==================================== */ * { box-sizing: border-box; } body { font-family: 'Varela Round', sans-serif; line-height: 1.6; color: #3a3a3a; } p { font-size: .95em; margin-bottom: 1.8em; } h2, h3, a { color: #093a58; } h2, h3 { margin-top: 0; } a { text-decoration: none; } img { max-width: 100%; } /* ================================= Base Layout Styles ==================================== */ /* ---- Navigation ---- */ .name { font-size: 1.25em; } .name a, .main-nav a { text-align: center; } .main-nav a { font-size: .95em; color: #3acec2; text-transform: uppercase; } .main-nav a:hover { color: #093a58; } .main-nav li { margin-bottom: 1rem; } .main-header, .banner, .main-footer { text-align: center; } /* ---- Layout Containers ---- */ .banner { color: #fff; background: #3acec2; padding: 40px; } .main-footer { background: #d9e4ea; padding: 2em 0; margin-top: 30px; } .container { margin: auto; max-width: 90%; margin-top: 3rem; } /* ---- Page Elements ---- */ .logo { width: 190px; } .headline { margin-bottom: -.2rem; } /* ================================= Media Queries ==================================== */ @media (min-width: 769px) { /* ---- Header ---- */ .name { float: left; margin: 1.5rem 0 0 1.7rem; } .main-nav { float: right; } .main-nav li { margin-top: 1.3rem; padding: .5rem 1.7rem; display: inline-block; } /* ---- Page content ---- */ .primary { width: 47.5%; margin-left: 2.5%; } .secondary { width: 45%; } .tertiary { clear: both; } /* ---- Float clearfix ---- */ .clearfix::after { content: " "; display: table; clear: both; } } @media (min-width: 1200px) { /* ---- Page content ---- */ .container { width: 80%; max-width: 1150px; } .primary { width: 40%; } .secondary, .tertiary { width: 30%; } .col { display: inline-block; vertical-align: top; padding: 1rem; margin: 0; } /* ---- Float clearfix ---- */ .clearfix::after { content: " "; display: table; clear: both; } }
<header class="main-header clearfix"> <h1 class="name"><a href="#">Best City Guide</a></h1> <ul class="main-nav"> <li><a href="#">ice cream</a></li> <li><a href="#">donuts</a></li> <li><a href="#">tea</a></li> <li><a href="#">coffee</a></li> </ul> </header><!--/.main-header--> <div class="banner"> <h1 class="headline">The Best City</h1> <span class="tagline">The best drinks and eats in the best city ever.</span> </div><!--/.banner--> <div class="container clearfix"> <div class="secondary col"> <h2>Welcome!</h2> <p>Everything in this city is worth waiting in line for.</p> <p>Cupcake ipsum dolor sit. Amet chocolate cake gummies jelly beans candy bonbon brownie candy. Gingerbread powder muffin. Icing cotton candy. Croissant icing pie ice cream brownie I love cheesecake cookie. Pastry chocolate pastry jelly croissant.</p> <p>Cake sesame snaps sweet tart candy canes tiramisu I love oat cake chocolate bar. Jelly beans pastry brownie sugar plum pastry bear claw tiramisu tootsie roll. Tootsie roll wafer I love chocolate donuts.</p> </div><div class="primary col"> <h2>Great food</h2> <p>Croissant macaroon pie brownie. Cookie marshmallow liquorice gingerbread caramels toffee I love chocolate. Wafer lollipop dessert. Bonbon jelly beans pudding dessert sugar plum. Marzipan toffee dragée chocolate bar candy toffee pudding I love. Gummi bears pie gingerbread lollipop.</p> </div><div class="tertiary col"> <h2>How to get here</h2> <p><strong>Plane: </strong>Tiramisu caramels gummies chupa chups lollipop muffin. Jujubes chocolate caramels cheesecake brownie lollipop dragée cheesecake.</p> <p><strong>Train: </strong>Pie apple pie pudding I love wafer toffee liquorice sesame snaps lemon drops. Lollipop gummi bears dessert muffin I love fruitcake toffee pie.</p> <p><strong>Car: </strong>Jelly cotton candy bonbon jelly-o jelly-o I love. I love sugar plum chocolate cake pie I love pastry liquorice.</p> </div><!--/.tertiary--> </div> <footer class="main-footer"> <span>©2017 Residents of The Best City Ever.</span> </footer>
The problem solved when I add float: left;
当我添加
float: left;
时,问题解决了float: left;
to the primary
class under @media (min-width: 1200px)
到
@media (min-width: 1200px)
下的primary
类别@media (min-width: 1200px)
https://codepen.io/anon/pen/VzpjMR https://codepen.io/anon/pen/VzpjMR
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.