【问题标题】:How to center an image between two columns in CSS?如何在CSS中的两列之间居中图像?
【发布时间】:2015-10-07 18:03:47
【问题描述】:

我有这个 html:

<div>
    <img class="image">
    <p class="text"></p>
</div>

我希望我的文本动态分为两列,所以我使用column-count 属性:

p.text
{
    -webkit-column-count: 2;
    -moz-column-count: 2;
    column-count: 2;
}

我还想将图像在两列之间居中,在宽度和高度上,以便获得这种效果:

你将如何完成这项任务?
是否可以仅使用 css 完成工作?
如果没有,有没有办法用 javascript 来做到这一点,保留 column-count 属性?

【问题讨论】:

  • 兼容 IE9 或只是现代浏览器好吗?
  • 仅适用于现代浏览器的解决方案仍然可以。
  • 不可能使用 CSS 进行 AFAIK,但 CSS Shapes/Regions/Flow 除外,这些仍处于试验阶段。
  • 这可能有点用处。我不知道它有多坚固。 css-tricks.com/float-center
  • 有没有办法用 javascript 做到这一点,保留 column-count 属性?

标签: html css


【解决方案1】:

没有简单的方法可以做到这一点。但是,您可以使用伪元素“伪造”环绕效果。

* {
  margin: 0px;
  padding: 0px;
}

.text {
  width: 49%;
}

#text-l {
  float: left;
}

#text-r {
  float: right;
}

#text-l:before, #text-r:before {
  content: ""; 
  width: 125px; 
  height: 250px;
}

#text-l:before {
  float: right;
}

#text-r:before {
  float: left;
}

.image {
  height: 250px;
  width: 250px;
  position: absolute;
  left: 50%;
  margin-left: -125px;
  background-color: yellow;
}
<div>
    <img class="image" src="http://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png">
    <p class="text" id="text-l">
      How to create a Minimal, Complete, and Verifiable example

When asking a question about a problem caused by your code, you will get much better answers if you provide code people can use to reproduce the problem. That code should be…

…Minimal – Use as little code as possible that still produces the same problem
…Complete – Provide all parts needed to reproduce the problem
…Verifiable - Test the code you're about to provide to make sure it reproduces the problem
Minimal
    </p>
    <p class="text" id="text-r">
      The more code there is to go through, the less likely people can find your problem. Streamline your example in one of two ways:

Restart from scratch. Create a new program, adding in only what is needed to see the problem. This can be faster for vast systems where you think you already know the source of the problem. Also useful if you can't post the original code publicly for legal or ethical reasons.
Divide and conquer. When you have a small amount of code, but the source of the problem is entirely unclear, start removing code a bit at a time until the problem disappears – then add the last part back.
Minimal and readable

Minimal does not mean terse - don't sacrifice communication to brevity. Use consistent naming and indentation, and include comments if needed to explain portions of the code. Most code editors have a shortcut for formatting code - find it, and use it! Also, don't use tabs - they may look good in your editor, but they'll just make a mess on Stack Overflow.
    </p>
</div>

【讨论】:

  • 谢谢。所以我想在保留列计数属性的同时没有办法做到这一点?
  • @pr0gma 我不知道有什么办法。
猜你喜欢
  • 2011-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-30
  • 2021-06-25
  • 1970-01-01
  • 1970-01-01
  • 2018-10-05
相关资源
最近更新 更多