如何使一个人使用下来
How to make one bring using down?
我要显示以下内容,所以在第一行:One Two,第二行:Three Four,第三行:Five。
我怎么能这样做,我一定会接受答案。
<div>
<div>Four</li>
<div>Five</li>
</div>
一个相对简单的解决方案是使用flex-box方法:
/* Setting up the basic 'default' display
options: */
ul,
li {
list-style-type: none;
margin: 0;
padding: 0;
box-sizing: border-box;
}
ul {
/* display: flex sets up the <ul> element
to contain flex-items: */
display: flex;
/* this establishes whether the model should
be row-based or column-based: */
flex-direction: row;
/* stretches the flex-items (the <li>) to
fill the space: */
align-items: stretch;
/* defines whether the children may, or may
not, wrap to a new line: */
flex-wrap: nowrap;
}
li {
display: flex-item;
/* 0: 'flex-grow',
the amount by which the particular
element may grow, relative to its
siblings.
1: 'flex-shrink',
the amount by which the element
may shrink relative to its
siblings.
20%: 'flex-basis',
the initial width of the particular
element relative to its container.
*/
flex: 0 1 20%;
border: 2px solid #000;
border-radius: 0.5em;
line-height: 2em;
text-align: center;
}
@media screen and (max-width: 600px) {
ul {
/* we want the elements to wrap to
new lines at this point, so we
change 'nowrap' to 'wrap': */
flex-wrap: wrap;
}
li {
/* here, the flex-grow is set to 2,
meaning that an element can grow
to twice the size of a sibling,
flex-shrink remains the same, and
flex-basis is now 50%, because we
want either one or two (at most)
elements per line: */
flex: 2 1 50%;
}
}
@media screen and (max-width: 200px) {
ul {
flex-wrap: wrap;
}
li {
/* while you didn't specify an alternate
view rule for smaller screen widths, I
added this to show how flex-boxes can
be used to switch to a one-element per
row alternative at tiny widths, simply
by setting the flex-basis to 100%: */
flex: 0 1 100%;
}
}
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>
JS Fiddle demo.
当然,上面假设您希望<li>
元素单独在其自己的行上具有该行的完整宽度;如果不是这种情况,那么您可以简单地将flex: 2 1 50%
行替换为flex: 0 1 50%
,这将防止一行上的项目相对于其兄弟姊妹增长。
ul,
li {
list-style-type: none;
margin: 0;
padding: 0;
box-sizing: border-box;
}
ul {
display: flex;
flex-direction: row;
align-items: stretch;
flex-wrap: nowrap;
}
li {
display: flex-item;
flex: 0 1 20%;
border: 2px solid #000;
border-radius: 0.5em;
line-height: 2em;
text-align: center;
}
@media screen and (max-width: 600px) {
ul {
flex-wrap: wrap;
}
li {
flex: 0 1 50%;
}
}
@media screen and (max-width: 200px) {
ul {
flex-wrap: wrap;
}
li {
flex: 0 1 100%;
}
}
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>
JS Fiddle demo.
引用:
- "Using CSS flexible boxes" (MDN).
您需要的媒体查询如下所示。同时,这里有一个快速测试的小提琴。只要展开和收缩(调整大小)窗口,看看你得到什么。
@media screen and (max-width: 600px) {
ul li{
display: inline-block;
width:50%;
padding:15px;
box-sizing: border-box;
margin:0;
float:left;
}
}
的例子:
ul,li{
list-style:none;
}
@media screen and (max-width: 600px) {
ul li{
display: inline-block;
width:50%;
padding:15px;
box-sizing: border-box;
margin:0;
float:left;
}
}
<html>
<head>
<title>test</title>
</head>
<body>
<ul style={{listStyleType: 'none'}}>
<li style={{display: 'inline'}}>One</li>
<li style={{display: 'inline'}}>Two</li>
<li style={{display: 'inline'}}>Three</li>
<li style={{display: 'inline'}}>Four</li>
<li style={{display: 'inline'}}>Five</li>
</ul>
</body>
</html>
@media screen and (max-width: 600px)
{
ul > li:nth-child(odd)
{
clear: both;
}
}
相关文章:
- 使一组对象与 jQuery 一起拖动
- 如何获取一个人的Google+ ID
- Javascript-经理是一个人,但不是经理本身?功能/原型继承
- 使一组元素透明,但一个除外
- 如何在 Javascript 中使一组函数在另一组函数之后出现
- 我如何使用 php 或 JavaScript 使一个人被重定向到我的移动网站(如果他们在移动设备上)
- 如何在给定日期和时间的情况下使一天倒退24小时
- Trello API访问仅适用于一个人
- 使一个数组与另一个数组完全相等
- 图像获胜'Don’一个人淡出后再淡出
- 自定义功能在谷歌电子表格:一个人之间的分歧和其他组
- 如何使一个数组的html内容
- 如何使一段Javascript工作,只有当用户是在不同的选项卡
- 如何使一个人使用下来
- 如何使一页打印到打印机只一次
- CSS复选框,如何使一项仅在另一项被选中时才被选中
- 如何检测一个人是否进入了谷歌地图上的某个区域
- 创建一个人-设置和更改姓和名
- 我如何使一个数组与唯一的元素(即删除重复项)
- 如何使一段JavaScript代码先于另一段运行