【问题标题】:CSS margins don't work properly with percentages?CSS边距不能与百分比一起正常工作?
【发布时间】:2014-09-08 03:00:31
【问题描述】:

我目前有一个博客需要在一行中显示 4 个配置文件(代理风格)列表,我已经设计了网站,以便在其响应时,并且我希望这些配置文件能够响应地工作。此时它可以工作,但是当使用 50% 没有边距时,但 id 像它们之间的 10px 空格时,当这些边距用下面的代码添加时,它们不对齐,请参见 jfiddle。

http://jsfiddle.net/b0rrz8bg/

CSS

.new-agency-clients{
position: relative; 
border-radius:6px; 
background:#fcfbfc !important; 
background:-webkit-gradient(linear,left top,left bottom,from(#fcfbfc),to(#f7f6f7)) !important; 
background:-webkit-linear-gradient(#fcfbfc,#f7f6f7) !important;
background:-moz-linear-gradient(center top,#fcfbfc 0,#f7f6f7 100%) !important; 
background:-moz-gradient(center top,#fcfbfc 0,#f7f6f7 100%) !important; 
width:940px; 
height: 100%; 
box-shadow:0 0 3px #444; 
-moz-box-shadow:0 0 3px #444; 
-webkit-box-shadow:0 0 3px #444; 
padding-top: 10px; 
margin-top:10px; 
margin-bottom:10px; 
}

.new-agency-clients-box {
overflow:hidden;
}

.new-agency-clients-box .entry-profile {
bottom:0;
top:0;
left:0;
right:0;
margin-left:10px; 
margin-bottom: 10px;
float:left; 
position:relative; 
width: 225px; 
height: 270px; 
background: #ffffff; 
border-radius: 6px; 
overflow:hidden; 
box-shadow:0 0 3px #444; 
-moz-box-shadow:0 0 3px #444; 
-webkit-box-shadow:0 0 3px #444; 
}

.new-agency-clients-box .entry-profile .profilename { 
margin-top: -35px; 
line-height: 26px; 
height: 28px;
width: 100%; 
background:#cccccc; 
background:-webkit-gradient(linear,left top,left bottom,from(#eeeeee),to(#cccccc)); background:-webkit-linear-gradient(#eeeeee,#cccccc); 
background:-moz-linear-gradient(center top,#eeeeee 0,#cccccc 100%); 
background:-moz-gradient(center top,#eeeeee 0,#cccccc 100%); 
color: #444; 
display: block; 
font-size: 14px; 
font-weight: bold; 
position: relative; 
border-radius: 0px !important; 
}

.new-agency-clients-box .entry-profile .profile-box-image{ 
background: #0e0e0e !important; 
width: 225px; 
height: 270px; 
margin-bottom: 10px; 
cursor: pointer; 
position: relative; 
}

.new-agency-clients-box .entry-profile .profile-box-image:hover{ 
filter: alpha(opacity=60,Style=0); 
opacity: 0.6; 
cursor: pointer; 
}

.new-agency-clients-box .entry-profile .profile-box-image a img{ 
width: 225px; 
height: 270px; 
position: absolute;
margin: auto; 
top: 0; 
left: 0; 
right: 0; 
bottom: 0; 
}

.new-agency-clients-box .entry-profile .profile-box-image a img.default{
width: 225px; 
height: auto; 
position: absolute; 
margin: auto; 
top: 0; 
left: 0; 
right: 0; 
bottom: 0; 
}

@media only screen and (max-width: 750px) {

.wrap {width: auto;} 
.new-agency-clients {width: 100%;}
.new-agency-clients-box .entry-profile {width: 50%; height: 50%;}

}

HTML

<div class="new-agency-clients">
<div class="new-agency-clients-box">

<div class="entry-profile">

<div class="profile-box-image">
<a href="" rel="bookmark">
<img width="700" height="875" src="" class="attachment-post-thumbnail wp-post-image" alt="" />  
</a> 
</div>
<center>
    <div class="profilename">
        <h6><a href="" rel="bookmark" title="">Profile</a></h6>
    </div>
</center>
</div>

<div class="entry-profile">

<div class="profile-box-image">
<a href="" rel="bookmark">
<img width="700" height="875" src="" class="attachment-post-thumbnail wp-post-image" alt="" />  
</a> 
</div>
<center>
    <div class="profilename">
        <h6><a href="" rel="bookmark" title="">Profile</a></h6>
    </div>
</center>
</div>    

<div class="entry-profile">

<div class="profile-box-image">
<a href="" rel="bookmark">
<img width="700" height="875" src="" class="attachment-post-thumbnail wp-post-image" alt="" />  
</a> 
</div>
<center>
    <div class="profilename">
        <h6><a href="" rel="bookmark" title="">Profile</a></h6>
    </div>
</center>
</div>    

<div class="entry-profile">

<div class="profile-box-image">
<a href="" rel="bookmark">
<img width="700" height="875" src="" class="attachment-post-thumbnail wp-post-image" alt="" />  
</a> 
</div>
<center>
    <div class="profilename">
        <h6><a href="" rel="bookmark" title="">Profile</a></h6>
    </div>
</center>
</div>    

</div>
</div>

【问题讨论】:

  • 请将其缩减为重现问题所需的最少代码集。我假设您在尝试追踪它的过程中已经做了什么?
  • @torazaburo,他确实添加了准确的最小代码来描述他的问题
  • @Fabio 我相信你的话,对我来说似乎有很多多余的东西。

标签: html css margin percentage


【解决方案1】:

虽然您的响应式布局方法有很多需要讨论的地方,但在您的具体情况下,只需这样做:

@media only screen and (max-width: 750px) {

.wrap {width: auto;} 
.new-agency-clients {width: 100%;}
.new-agency-clients-box .entry-profile {width: calc( 50% - 10px ) ; height: auto}

}

CSS 边距工作正常,但如果您有 50%+10px,那么很明显元素将占用 50%+,从而换行到下一行。因此,我们使用calc 将这些元素设置为 50% MINUS 您想要的边距,瞧,它就像魔术一样工作。我有forked your fiddle,所以你可以到处玩

编辑:根据评论包含新代码

更改此 HTML

<img width="700" height="875" src="" class="attachment-post-thumbnail wp-post-image" alt="" />

对此(我们正在从标记中删除宽度和高度,无论如何,你不应该使用):

<img src="" class="attachment-post-thumbnail wp-post-image" alt="" />

现在,在您的 CSS 中,就这么简单:

.entry-profile .wp-post-image{width:100%; height:auto; max-width:700px;}

看到我们也可以使用 width:auto; height:100%; max-height:875px 并像这样限制最小高度或宽度 width:auto; height:100%; max-height:875px; min-height:200px /* or restrict the width if you want */ 。所有方法虽然相对相似,但在调整大小时具有不同的行为,但它们都是响应式的,因此请使用您的代码对其进行测试,看看您更喜欢哪个项目适合您的项目。

使用一个并调整大小,看看元素的行为,你会明白它是多么容易,这不过是,例如“图像将占包含元素的 100%,自动调整到最大宽度 700px最小宽度为 200px"

说了这么多,这是基本理论,在大多数情况下你可能更喜欢将最大和最小宽度应用于包含元素,所以你的 CSS 会稍微改变,一切都会变得更容易

.entry-profile{width: calc( 25% - 10px ) /* 4 columns! */ ; max-width:700px; min-width:200px}
.entry-profile .wp-post-image{width:100%; height:auto; /* responsive scalable images no matter the size of container */}

【讨论】:

  • 非常感谢法比奥 :)
  • 我想知道您是否可以对上面的代码提供帮助,因为我还有另一个问题,百分比不是我的事,但我试图让图像显示全宽和全高.entry-profile 是,图像总是相同的大小,不会调整到 .entry-profile div
  • 这就是为什么我说“你对响应式布局的方法有很多要讨论的地方”;)幸运的是,这并不难,只要记住你需要使用相对度量而不是固定度量(您可以将其用于最小和最大度量,除非设计绝对需要,否则不固定)。在您的情况下,就像在答案中进行编辑一样简单(我很容易包含代码)
猜你喜欢
  • 2016-10-19
  • 1970-01-01
  • 1970-01-01
  • 2015-08-12
  • 2022-12-12
  • 1970-01-01
  • 2017-06-21
  • 1970-01-01
  • 2021-08-15
相关资源
最近更新 更多