如何使图像完全出血但填充内容

时间:2015-03-19 10:29:13

标签: css

我的HTML标记主要是段落,uls&标题。我无法编辑标记,但我喜欢将内容填充为40像素,但要使图像全宽,所以-40px左边和-40px右边。

没有jQuery,最好的方法是什么?

这是HTML

    <p>But how? Genius in the Sperm Whale? Has the Sperm Whale ever written a book, spoken a speech?</p>

<p>Champollion deciphered the wrinkled granite hieroglyphics. But there is no Champollion to decipher the Egypt of every man's and every being's face.</p>
<p><img src="http://lorempixel.com/400/200/sports" /></p>
<h2>If the Sperm Whale be physiognomically a Sphinx</h2>

<p>In the full-grown creature the skull will measure at least twenty feet in length. Unhinge the lower jaw, and the side view of this skull is as the side of a moderately inclined plane resting throughout on a level base.</p>

<p>It is plain, then, that phrenologically the head of this Leviathan, in the creature's living intact state, is an entire delusion.</p>

和CSS

p,h2{padding:0 40px;}
img{margin-left:-40px;width:100%;}

https://jsfiddle.net/xea3m64q/1/

布局很流畅,所以如果我能提供帮助,我真的不想修改宽度。

2 个答案:

答案 0 :(得分:1)

您只需要修改CSS即可:

p, h2 {
    padding:0 40px 0 40px; /*Just so that we can see why I added the 80px. (top, right, bottom, left)*/
}

img {
    margin-left: -40px; /*-40px to make up for the parent's padding to the left*/
    width: calc(100% + 80px); /*You need to change to this.*/
}

答案 1 :(得分:0)

如果您不知道填充量,这里有更通用的解决方案。你有多少填充并不重要。

img {
  width: 100vw;
  margin-left: calc(50% - 50vw);
}

p,
h2 {
  padding: 0 40px;
}

img {
  width: 100vw;
  margin-left: calc(50% - 50vw);
}
<p>But how? Genius in the Sperm Whale? Has the Sperm Whale ever written a book, spoken a speech?</p>

<p>Champollion deciphered the wrinkled granite hieroglyphics. But there is no Champollion to decipher the Egypt of every man's and every being's face.</p>
<p><img src="https://i.picsum.photos/id/163/536/354.jpg?hmac=n2PYO9vvS48HLf1RW7MB27ZvPxK5nhLT3sTTcVIqDtw" /></p>
<h2>If the Sperm Whale be physiognomically a Sphinx</h2>

<p>In the full-grown creature the skull will measure at least twenty feet in length. Unhinge the lower jaw, and the side view of this skull is as the side of a moderately inclined plane resting throughout on a level base.</p>

<p>It is plain, then, that phrenologically the head of this Leviathan, in the creature's living intact state, is an entire delusion.</p>

也适用于 max-width 容器。

p,
h2 {
  padding: 0 40px;
}

p {
  max-width: 300px;
  margin: 0 auto;
}

img {
  width: 100vw;
  margin-left: calc(50% - 50vw);
}
<p>But how? Genius in the Sperm Whale? Has the Sperm Whale ever written a book, spoken a speech?</p>

<p>Champollion deciphered the wrinkled granite hieroglyphics. But there is no Champollion to decipher the Egypt of every man's and every being's face.</p>
<p>
<img src="https://i.picsum.photos/id/163/536/354.jpg?hmac=n2PYO9vvS48HLf1RW7MB27ZvPxK5nhLT3sTTcVIqDtw" /></p>
<h2>If the Sperm Whale be physiognomically a Sphinx</h2>

<p>In the full-grown creature the skull will measure at least twenty feet in length. Unhinge the lower jaw, and the side view of this skull is as the side of a moderately inclined plane resting throughout on a level base.</p>

<p>It is plain, then, that phrenologically the head of this Leviathan, in the creature's living intact state, is an entire delusion.</p>