当我使用display时,为什么我的div的顺序会改变:inline-block;?

时间:2017-08-07 21:59:54

标签: html css alignment

我为大屏幕创建了一个3列布局(最小1200px)。我正在尝试使用display:inline-block对齐列。

问题是列的顺序发生了变化(到二级 - 三级 - 即使在HTML中,顺序也不同(次要 - 主要 - 第三级)。什么似乎是问题?

您可以在此处找到完整的代码示例: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>

CSS看起来像这样:

.primary {
   width: 40%;
  }

.secondary,
.tertiary {
  width: 30%;
  }

.col {
  display: inline-block;
  vertical-align: top;
  padding: 1rem;
  margin: 0;
}

2 个答案:

答案 0 :(得分:2)

因为您还左右{{}}了这些元素。删除浮动,然后删除元素之间的空白区域,以使float元素全部位于同一行,并且总共30%, 40%, 30%

&#13;
&#13;
100%
&#13;
/* ================================= 
  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;
	}

}
&#13;
&#13;
&#13;

答案 1 :(得分:2)

当我将float: left;添加到primary

下的@media (min-width: 1200px)类时,问题就解决了

https://codepen.io/anon/pen/VzpjMR