内联 div 之间的自动间距
如何在流体宽度父 div 内将 div 之间的间距设置为“自动”?仅使用 CSS 是否可以实现? 更具体: 我有一些父 div 宽度 width: 90%
。我里面有 3 个内联元素。我希望第一个元素接触父 div 的左侧,第二个元素位于中间,最后一个元素接触父 div 的右侧。
@编辑: 我正在添加一些代码:
#news-pane {
margin: 50px auto;
width: 90%;
}
#news {
color: #333;
width: 300px;;
vertical-align: top;
display: inline-block;
}
#news img {
max-width: 300px;
height: auto;
}
HTML:
<ul id="news-pane">
<li id="news">
<div id="news-header">
<h1>Title</h1><span>Date</span>
</div>
<p><News content</p>
</li>
<li id="news">
<div id="news-header">
<h1>Title</h1><span>Date</span>
</div>
<p><News content</p>
</li>
<li id="news">
<div id="news-header">
<h1>Title</h1><span>Date</span>
</div>
<p><News content</p>
</li>
</ul>
How can I set spacing between divs to "auto" inside fluid width parent div? Is it even possible with CSS only?
More specific:
I have some parent div width width: 90%
. I have 3 inline elements inside it. I want first element to touch left side of my parent div, second element to be in middle, and last element to touch right side of the parent div.
@edit:
I'm adding some code:
#news-pane {
margin: 50px auto;
width: 90%;
}
#news {
color: #333;
width: 300px;;
vertical-align: top;
display: inline-block;
}
#news img {
max-width: 300px;
height: auto;
}
HTML:
<ul id="news-pane">
<li id="news">
<div id="news-header">
<h1>Title</h1><span>Date</span>
</div>
<p><News content</p>
</li>
<li id="news">
<div id="news-header">
<h1>Title</h1><span>Date</span>
</div>
<p><News content</p>
</li>
<li id="news">
<div id="news-header">
<h1>Title</h1><span>Date</span>
</div>
<p><News content</p>
</li>
</ul>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将第一个元素设置为
float:left
,第二个元素设置为自动边距,第三个元素设置为float:right
。 AFAIK 元素必须被阻止才能起作用。演示: http://jsfiddle.net/2CmSK/
看到代码后编辑:主要问题是你多个元素使用相同的 id。您必须使用唯一的 id 或使用类。 HTML 目前无效,并且在不同的浏览器中可能会出现不可预测的行为。
第二个问题是外部 div 的宽度以百分比给出,而新闻项的宽度以像素给出。如果用户的浏览器窗口宽度小于 1000 像素,则外部 div 宽度将小于 900 像素,并且新闻项将无法容纳在内部。
在我读到的字里行间,您正在使用某种封闭/标准工具来生成新闻项目,因此您无法修改代码,并且它们都必须具有相同的结构。如果是这种情况,那么不可以,如果它们在结构上相同而不使用 JavaScript,那么您不能让它们表现不同。
You can make the first element
float:left
, the second element with auto margins and the third elementfloat:right
. AFAIK the elements have to be block for this to work.demo: http://jsfiddle.net/2CmSK/
EDIT after seeing the code: The main problem is that you're using the same id for multiple elements. You have to use unique ids or use classes instead. The HTML is invalid right now and might behave unpredictably in different browsers.
The second problem is that the outer div's width is given in percentages and the news items' width is given in pixels. If the user's browser window is less than 1000px wide the outer div will be less than 900px wide and the news items won't fit inside.
And between the lines I'm reading that you're using some kind of closed/standard tool to generate the news items so you can't modify the code and they all have to have the same structure. If this is the case then no, you can't have them behave differently if they are structurally identical without using JavaScript.