首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用CSS将元素转换为给定的填充

问如何使用CSS将元素转换为给定的填充
EN

Stack Overflow用户
提问于 2011-04-19 13:17:19
回答 3查看 3K关注 0票数 3

在下图中。我给container设置了10px填充,因为大多数时候我需要这个填充。只有标题(蓝色)我不需要填充。如何在填充上扩展标题?

http://i.stack.imgur.com/DeSHZ.jpg

我不想给容器内的每个元素单独填充。

​

​

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-04-19 13:18:30

标题的边距为负值。

在jsFiddle上查看:

CSS

代码语言:javascript
复制
div {
  background: red;  
  padding:10px;
  width:200px; 
}
p {
  background: green;   
}
h2 {
  margin: 0 -10px;   
  background: blue;
}

HTML

代码语言:javascript
复制
<div>
    <p>This is the paragraph. This is the paragraph.</p>
    <h2>This is a heading</h2>    
    <p>This is the paragraph. This is the paragraph.</p>
    <h2>This is a heading</h2>
    <p>This is the paragraph. This is the paragraph.</p>
</div>
票数 8
EN

Stack Overflow用户

发布于 2011-04-19 15:38:32

使用与jessegavin相同的代码。我使用了相对和绝对定位

在jsFiddle here上查看

CSS

代码语言:javascript
复制
div {
 background: red;  
 padding:10px;
 width:200px;
 position:relative;
}
p {
 background: green;   
}
h2 {
 background: blue;
 position:absolute;
 left:0;
 width:100%;
}

HTML

代码语言:javascript
复制
<div>
    <p>This is the paragraph. This is the paragraph. This is the paragraph. This is the paragraph. </p>
    <h2>This is a heading</h2>    
    <p>This is the paragraph. This is the paragraph. This is the paragraph. This is the paragraph. </p>
    <h2>This is a heading</h2>
    <p>This is the paragraph. This is the paragraph. This is the paragraph. This is the paragraph. </p>
</div>
票数 1
EN

Stack Overflow用户

发布于 2011-04-19 13:22:01

正如jessegavin所说,使用负边距。

负的利润率是时髦的,但它们工作正常。您必须在标题的width中包含两次10px填充(一次用于左填充,另一次用于右填充),然后执行负margin-left

代码语言:javascript
复制
margin-left: -10px;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5711930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档