如何将中间的 div 向下包裹

huangapple go评论48阅读模式
英文:

How to wrap the middle div down

问题

以下是代码的部分翻译:

<!DOCTYPE html>
<html>
<head>
	<style>
		#mainCont
		{
			background-color: lightblue;
			display: flex;
			justify-content: space-between;
		}
		#middleCont
		{
			background-color: lavender;
		}
		#middleCont a
		{
			padding: 0 80px;
		}
	</style>
</head>
<body>
	<div id="mainCont">
		<div>Word</div>
		<div id="middleCont">
			<a href="#">link 1</a>
			<a href="#">link 2</a>
			<a href="#">link 3</a>
			<a href="#">link 4</a>
			<a href="#">link 5</a>
		</div>
		<div>Word</div>
	</div>
</body>
</html>

图片中解释的预期结果。

英文:

The code below have three divs that expected to be responsive based on how the user resize the window.

when the window is too small, i want the middle div to wrap down like the picture explain.

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
	&lt;style&gt;
		#mainCont
		{
			background-color: lightblue;
			display: flex;
			justify-content: space-between;
		}#middleCont
		{
			background-color: lavender;
		}#middleCont a
		{
			padding: 0 80px;
		}
	&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;div id=&quot;mainCont&quot;&gt;
		&lt;div&gt;Word&lt;/div&gt;
		&lt;div id=&quot;middleCont&quot;&gt;
			&lt;a href=&quot;#&quot;&gt;link 1&lt;/a&gt;
			&lt;a href=&quot;#&quot;&gt;link 2&lt;/a&gt;
			&lt;a href=&quot;#&quot;&gt;link 3&lt;/a&gt;
			&lt;a href=&quot;#&quot;&gt;link 4&lt;/a&gt;
			&lt;a href=&quot;#&quot;&gt;link 5&lt;/a&gt;
		&lt;/div&gt;
		&lt;div&gt;Word&lt;/div&gt;
	&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

(https://i.stack.imgur.com/VN02X.jpg)

Thanks

The expected result explained in the picture

答案1

得分: 1

> 使用 order CSS 属性,我们可以根据屏幕大小更改元素的顺序,并使用 flex-wrap 来创建另一行
DOC
>
> 最佳做法是使用类名选择器而不是 ID 选择器

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.mainCont
        {
            background-color: lightblue;
            display: flex;
            justify-content: space-between;
        }
        .middleCont
        {
            background-color: lavender;
        }
        .middleCont a
        {
            padding: 0 80px;
        }

@media (max-width: 575.98px){
  .mainCont{
    flex-wrap:wrap;
  }
  .middleCont{
    order:3
  }
}

<!-- language: lang-html -->

&lt;div class=&quot;mainCont&quot;&gt;
        &lt;div&gt;Word&lt;/div&gt;
        &lt;div class=&quot;middleCont&quot;&gt;
            &lt;a href=&quot;#&quot;&gt;link 1&lt;/a&gt;
            &lt;a href=&quot;#&quot;&gt;link 2&lt;/a&gt;
            &lt;a href=&quot;#&quot;&gt;link 3&lt;/a&gt;
            &lt;a href=&quot;#&quot;&gt;link 4&lt;/a&gt;
            &lt;a href=&quot;#&quot;&gt;link 5&lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;Word&lt;/div&gt;
    &lt;/div&gt;

<!-- end snippet -->

英文:

> Using order css property we can change the order of elements based on screen size and use flex-wrap to make another row
DOC
>
> It is best practice to use class name selector instead of id selector

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.mainCont
        {
            background-color: lightblue;
            display: flex;
            justify-content: space-between;
        }
        .middleCont
        {
            background-color: lavender;
        }
        .middleCont a
        {
            padding: 0 80px;
        }

@media (max-width: 575.98px){
  .mainCont{
    flex-wrap:wrap;
  }
  .middleCont{
    order:3
  }
}

<!-- language: lang-html -->

&lt;div class=&quot;mainCont&quot;&gt;
        &lt;div&gt;Word&lt;/div&gt;
        &lt;div class=&quot;middleCont&quot;&gt;
            &lt;a href=&quot;#&quot;&gt;link 1&lt;/a&gt;
            &lt;a href=&quot;#&quot;&gt;link 2&lt;/a&gt;
            &lt;a href=&quot;#&quot;&gt;link 3&lt;/a&gt;
            &lt;a href=&quot;#&quot;&gt;link 4&lt;/a&gt;
            &lt;a href=&quot;#&quot;&gt;link 5&lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;Word&lt;/div&gt;
    &lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月11日 11:47:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982254.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定