【问题标题】:How do you center a container inside of a column?如何在列中居中放置容器?
【发布时间】:2017-08-25 04:43:04
【问题描述】:

我不知道如何将我的图像容器放在我的列中。另外由于某种原因,即使所有设置都与其他两个完全一样,中间的图片也不会显示,是否有某种原因会发生这种情况?

此外,无论出于何种原因,我的列在使用 google chrome 时格式不正确。我认为 -webkit- 应该可以解决这个问题?

.columns {
	-webkit-column-count: 3;
	-moz-column-count: 3;
	column-count: 3;
	column-gap: 20px;
}

.columns-format {
	display: inline-block;
	width: auto;
}

.image-container {
	display: inline-block;
	border: 1px solid #ddd;
    border-radius: 4px;
    padding: 5px;
    transition: 0.3s;
	box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
	margin: auto;
}

.image-container hover {
	box-shadow: 0 0 2px 1px rgba(0, 140, 186, 0.5);
<div class="columns">
<div class="columns-format">
<h4>Professionals</h4>
<p>As Northeast Ohio Landscapers, Landscaping Company has been caring for customers and their properties throughout Cuyahoga County since 1981.</p>

<p>Our core values:
<ul>
	<li>Bring a positive attitude with you each and every day</li>
	<li>Work hard, pitch in, be helpful and productive</li>
	<li>Be fair and respectful with all people in all encounters</li>
</ul>
</p>
<div class="image-container">
	<a target="_blank" href="images/stairwell.jpg">
		<img src="images/stairwell.jpg" width="250px" height="200px">
		</a>
</div>
</div>

<div class="columns-format">
<h4>Services</h4>
<p>Whether you are new to Cuyahoga County, switching landscape service providers, or need to add a service to your existing account, Landscaping Company makes it easy for you.</p>
<p>Experienced in every facet of the landscape industry, we fulfil our one goal - total care philosophy by meeting your every need in the following areas:</p>
<p>Residential Landscape</p>
<p>Maintenance Landscape</p>
<p>Design/Installation</p>
<p>Plant Health Care</p>
<div class="image-container">
	<a target="_blank" href="images/flowers.jpg">
		<img src="images/flowers.jpg" width="250px" height="200px">
		</a>
</div>
</div>

<div class="columns-format">
<h4>Mission</h4>
<p>When it comes to Residential Landscape Maintenance in Northeast Ohio, we have a pretty simple mission.</p>
<p>Our mission is to unite people in positive relationships to improve lives.</p>
<p>This includes not only our great customers but also our fantastic employees and vendors that we work with each and every day.</p>
<p>If we are not improving your life then we are not living up to our mission.</p>
</div>
<div class="image-container">
	<a target="_blank" href="images/lighting.jpg">
		<img src="images/lighting.jpg" width="250px" height="200px">
		</a>
</div>
</div>

【问题讨论】:

  • 准备小提琴? sn-ps 很难处理
  • @Siddharth jsfiddle.net/babws8k3 我注意到 sn-ps 很糟糕,除非您将其扩展到全屏。无论出于何种原因,它在 Google Chrome 中看起来也很奇怪,因此如果您使用 IE 或 FireFox,它看起来会更好。我不知道如何解决它。
  • 知道了,加了答案,看看

标签: html css css-multicolumn-layout


【解决方案1】:

我建议您使用flexbox 而不是columns

columns 更多地意味着使文本和图像动态分布在一定数量的列上,并且您的示例看起来更像是 3 列,其内容应该包含在每列中,columns 不会这样做。

要使您的图像/列不重叠,您可以为 columns-format 提供最小宽度(在下面的示例中),将图像大小设置为百分比,换行等。

.columns {
  display: flex;
  justify-content: space-between;
}
.columns-format {
  display: inline-block;
  width: 30%;
  min-width: 300px;
}
.image-container {
  text-align: center;
}
.image-container a {
  display: inline-block;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 5px;
  transition: box-shadow 0.3s;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
.image-container a:hover {
  box-shadow: 0 0 2px 1px rgba(0, 140, 186, 0.5);
}
<div class="columns">
  <div class="columns-format">
    <h4>Professionals</h4>
    <p>As Northeast Ohio Landscapers, Landscaping Company has been caring for customers and their properties throughout Cuyahoga County since 1981.</p>

    <p>Our core values:</p>
    <ul>
      <li>Bring a positive attitude with you each and every day</li>
      <li>Work hard, pitch in, be helpful and productive</li>
      <li>Be fair and respectful with all people in all encounters</li>
    </ul>
    <div class="image-container">
      <a target="_blank" href="images/stairwell.jpg">
        <img src="images/stairwell.jpg" width="250px" height="200px">
      </a>
    </div>
  </div>

  <div class="columns-format">
    <h4>Services</h4>
    <p>Whether you are new to Cuyahoga County, switching landscape service providers, or need to add a service to your existing account, Landscaping Company makes it easy for you.</p>
    <p>Experienced in every facet of the landscape industry, we fulfil our one goal - total care philosophy by meeting your every need in the following areas:</p>
    <p>Residential Landscape</p>
    <p>Maintenance Landscape</p>
    <p>Design/Installation</p>
    <p>Plant Health Care</p>
    <div class="image-container">
      <a target="_blank" href="images/flowers.jpg">
        <img src="images/flowers.jpg" width="250px" height="200px">
      </a>
    </div>
  </div>

  <div class="columns-format">
    <h4>Mission</h4>
    <p>When it comes to Residential Landscape Maintenance in Northeast Ohio, we have a pretty simple mission.</p>
    <p>Our mission is to unite people in positive relationships to improve lives.</p>
    <p>This includes not only our great customers but also our fantastic employees and vendors that we work with each and every day.</p>
    <p>If we are not improving your life then we are not living up to our mission.</p>
    <div class="image-container">
      <a target="_blank" href="images/lighting.jpg">
        <img src="images/lighting.jpg" width="250px" height="200px">
      </a>
    </div>
  </div>
</div>

【讨论】:

  • 这很好用,除了它保持格式化,使列的底部对齐,在第一列和第三列留下一堆空白。不知道如何解决这个问题。 imgur.com/a/71tQi 看起来它只出现在 IE 中,适用于 Google Chrome,但是当我在 Google Chrome 中调整它的大小时,它会与容器重叠而不是响应。 imgur.com/a/ySHZI
  • @ChaCol 在您的屏幕转储中,column 看起来像 align-items: flex-end;,应该是 align-items: flex-start;。为了确定这是做什么的,我需要查看代码
  • jsfiddle.net/kjtqybuu 我刚刚使用了你给我的代码。 justify-content: space-between 有什么作用?它似乎没有在重新调整它。
  • @ChaCol 更新了小提琴jsfiddle.net/kjtqybuu/1,其中container1 得到了min-width,因此列不会重叠。对于 IE 10,您需要为 flex 属性添加前缀,IE 11 对我来说很好
  • @ChaCol space-between 使列之间有 10% 的间隙(3 乘以 30% 列 = 90%,剩下 10%)
【解决方案2】:

在部分中添加另一个类-

<div class="columns-format">
<div class='text-container'>
<h4>Professionals</h4>
<p>As Northeast Ohio Landscapers, Landscaping Company has been caring for customers and their properties throughout Cuyahoga County since 1981.</p>

<p>Our core values:
<ul>
    <li>Bring a positive attitude with you each and every day</li>
    <li>Work hard, pitch in, be helpful and productive</li>
    <li>Be fair and respectful with all people in all encounters</li>
</ul>
</p>
</div>
<div class="image-container">
    <a target="_blank" href="images/stairwell.jpg">
        <img src="images/stairwell.jpg" width="250px" height="200px">
        </a>
</div>
</div>

并放入css

.text-container{text-align: left;}

在css的列格式中添加两行

.columns-format {
    display: inline-block;
    width: auto;
    float: left;
    text-align: center;
}

【讨论】:

  • 谢谢,这在大多数情况下都有效。出于某种原因,它将我的部分格式化为不再位于单独的列中。你知道我该如何解决吗? imgur.com/a/S2Rxm
  • 您可以使用 或者为了获得更好的输出,我建议使用引导行和列,这也适用于响应式网站。
【解决方案3】:

这是你期待的输出吗?

检查Link to Jsbin中的输出

.columns {
	-webkit-column-count: 3;
	-moz-column-count: 3;
	column-count: 3;
	column-gap: 20px;
}

.columns-format {
   text-align:center;
  margin:auto;
	
	
}

.image-container {
	display: inline-block;
	border: 1px solid #ddd;
    border-radius: 4px;
    padding: 5px;
    transition: 0.3s;
	box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
	margin: auto;
    text-align:center;
}

.image-container hover {
	box-shadow: 0 0 2px 1px rgba(0, 140, 186, 0.5);
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<div class="columns">
<div class="columns-format">
<h4>Professionals</h4>
<p>As Northeast Ohio Landscapers, Landscaping Company has been caring for customers and their properties throughout Cuyahoga County since 1981.</p>

<p>Our core values:
<ul>
	<li>Bring a positive attitude with you each and every day</li>
	<li>Work hard, pitch in, be helpful and productive</li>
	<li>Be fair and respectful with all people in all encounters</li>
</ul>
</p>
<div class="image-container">
	<a target="_blank" href="images/stairwell.jpg">
		<img src="images/stairwell.jpg" width="250px" height="200px">
		</a>
</div>
</div>

<div class="columns-format">
<h4>Services</h4>
<p>Whether you are new to Cuyahoga County, switching landscape service providers, or need to add a service to your existing account, Landscaping Company makes it easy for you.</p>
<p>Experienced in every facet of the landscape industry, we fulfil our one goal - total care philosophy by meeting your every need in the following areas:</p>
<p>Residential Landscape</p>
<p>Maintenance Landscape</p>
<p>Design/Installation</p>
<p>Plant Health Care</p>
<div class="image-container">
	<a target="_blank" href="images/flowers.jpg">
		<img src="images/flowers.jpg" width="250px" height="200px">
		</a>
</div>
</div>

<div class="columns-format">
<h4>Mission</h4>
<p>When it comes to Residential Landscape Maintenance in Northeast Ohio, we have a pretty simple mission.</p>
<p>Our mission is to unite people in positive relationships to improve lives.</p>
<p>This includes not only our great customers but also our fantastic employees and vendors that we work with each and every day.</p>
<p>If we are not improving your life then we are not living up to our mission.</p>
</div>
<div class="image-container">
	<a target="_blank" href="images/lighting.jpg">
		<img src="images/lighting.jpg" width="250px" height="200px">
		</a>
</div>
</div>
</body>
</html>

【讨论】:

  • 只有底部的图像容器,而不是文本。
【解决方案4】:

在你的内联块图像容器之前,你需要一个环绕的 div。像添加类的东西

.centered{ text-align:center }

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
  .columns {
    -webkit-column-count: 3;
    -moz-column-count: 3;
    column-count: 3;
    column-gap: 20px;
  }

  .columns-format {
    display: inline-block;
    width: auto;
  }

  .image-container {
    display: inline-block;
    border: 1px solid #ddd;
      border-radius: 4px;
      padding: 5px;
      transition: 0.3s;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
    margin: 0 auto;
  }

  .image-container hover {
    box-shadow: 0 0 2px 1px rgba(0, 140, 186, 0.5);
  }
  .centered{
    text-align:center;
  }
</style>
</head>
<body>
  <div class="columns">
<div class="columns-format">
<h4>Professionals</h4>
<p>As Northeast Ohio Landscapers, Landscaping Company has been caring for customers and their properties throughout Cuyahoga County since 1981.</p>

<p>Our core values:
<ul>
    <li>Bring a positive attitude with you each and every day</li>
    <li>Work hard, pitch in, be helpful and productive</li>
    <li>Be fair and respectful with all people in all encounters</li>
</ul>
</p>
<div class = "centered">

<div class="image-container">
    <a target="_blank" href="images/stairwell.jpg">
        <img src="http://placehold.it/200x200" width="250px" height="200px">
        </a>
</div>
</div>
</div>

<div class="columns-format">
<h4>Services</h4>
<p>Whether you are new to Cuyahoga County, switching landscape service providers, or need to add a service to your existing account, Landscaping Company makes it easy for you.</p>
<p>Experienced in every facet of the landscape industry, we fulfil our one goal - total care philosophy by meeting your every need in the following areas:</p>
<p>Residential Landscape</p>
<p>Maintenance Landscape</p>
<p>Design/Installation</p>
<p>Plant Health Care</p>

<div class = "centered">
<div class="image-container">
    <a target="_blank" href="images/flowers.jpg">
        <img src="http://placehold.it/200x200" width="250px" height="200px">
        </a>
</div>
</div>
</div>

<div class="columns-format">
<h4>Mission</h4>
<p>When it comes to Residential Landscape Maintenance in Northeast Ohio, we have a pretty simple mission.</p>
<p>Our mission is to unite people in positive relationships to improve lives.</p>
<p>This includes not only our great customers but also our fantastic employees and vendors that we work with each and every day.</p>
<p>If we are not improving your life then we are not living up to our mission.</p>
</div>

<div class = "centered">

<div class="image-container">
    <a target="_blank" href="images/lighting.jpg">
        <img src="http://placehold.it/200x200" width="250px" height="200px">
        </a>
</div>
</div>
</div>

</body>
</html>

【讨论】:

  • 这也可以。您知道为什么在 Google Chrome 中它会扭曲列,就好像只有 2 而不是 3? imgur.com/a/I5PUE
【解决方案5】:

只需将display: flex 添加到.column-formats 并给flex-direction column 就可以了

.columns {
	-webkit-column-count: 3;
	-moz-column-count: 3;
	column-count: 3;
	column-gap: 20px;
}

.columns-format {
	display: flex;
	width: auto;
    flex-direction:column
}

.image-container {
	display: inline-block;
	border: 1px solid #ddd;
    border-radius: 4px;
    padding: 5px;
    transition: 0.3s;
	box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
	margin: auto;
}

.image-container hover {
	box-shadow: 0 0 2px 1px rgba(0, 140, 186, 0.5);
<div class="columns">
<div class="columns-format">
<h4>Professionals</h4>
<p>As Northeast Ohio Landscapers, Landscaping Company has been caring for customers and their properties throughout Cuyahoga County since 1981.</p>

<p>Our core values:
<ul>
	<li>Bring a positive attitude with you each and every day</li>
	<li>Work hard, pitch in, be helpful and productive</li>
	<li>Be fair and respectful with all people in all encounters</li>
</ul>
</p>
<div class="image-container">
	<a target="_blank" href="images/stairwell.jpg">
		<img src="images/stairwell.jpg" width="250px" height="200px">
		</a>
</div>
</div>

<div class="columns-format">
<h4>Services</h4>
<p>Whether you are new to Cuyahoga County, switching landscape service providers, or need to add a service to your existing account, Landscaping Company makes it easy for you.</p>
<p>Experienced in every facet of the landscape industry, we fulfil our one goal - total care philosophy by meeting your every need in the following areas:</p>
<p>Residential Landscape</p>
<p>Maintenance Landscape</p>
<p>Design/Installation</p>
<p>Plant Health Care</p>
<div class="image-container">
	<a target="_blank" href="images/flowers.jpg">
		<img src="images/flowers.jpg" width="250px" height="200px">
		</a>
</div>
</div>

<div class="columns-format">
<h4>Mission</h4>
<p>When it comes to Residential Landscape Maintenance in Northeast Ohio, we have a pretty simple mission.</p>
<p>Our mission is to unite people in positive relationships to improve lives.</p>
<p>This includes not only our great customers but also our fantastic employees and vendors that we work with each and every day.</p>
<p>If we are not improving your life then we are not living up to our mission.</p>
<div class="image-container">
	<a target="_blank" href="images/lighting.jpg">
		<img src="images/lighting.jpg" width="250px" height="200px">
		</a>
</div>
</div>

</div>

【讨论】:

    猜你喜欢
    • 2022-11-17
    • 2015-05-30
    • 1970-01-01
    • 2014-04-06
    • 2021-03-03
    • 2016-05-11
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    相关资源
    最近更新 更多