Flexbox内嵌盒子

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

Flex box inside a box

问题

  1. 将内部框与外部框的中心右侧对齐是我的问题之一。
  2. 使用 flexbox 将内部框变成一个“行”,并在较小的显示屏上移到外部框的底部是我的问题之二。

大屏幕结果

小屏幕结果

<p class="inline-block-buttons">
  文本1
  <a>文本2</a>
</p>
p {
  border: 1px solid black;
  margin: 2rem 0;
  padding: 0.25rem;
}

a {
  padding: 1rem 2rem;
  border: 1px solid black;
  text-decoration: none;
  margin: 1rem;
  border-radius: 2px;
}

.inline-block-buttons a {
  display: inline-block;
  align-self: right;
}

body {
  margin: 2rem;
}

@media (max-width: 500px) {
  .inline-block-buttons {
    flex-direction: column;
  }
}
英文:

I'm trying to fit a box inside a box, where the layout changes depending on the screen size e.g. if under 400px.

I'm having trouble at two points:

  1. Aligning the inner box to the centre right of the outer box and
  2. Using flexboxes to make the inner box into a 'row' and fall to the bottom of the outer box on smaller displays

Large display results

Small display results

&lt;p class=&quot;inline-block-buttons&quot;&gt;
  Text 1
  &lt;a&gt;Text 2&lt;/a&gt;
&lt;/p&gt;
p {
  border: 1px solid black;
  margin: 2rem 0;
  padding: 0.25rem;
}

a {
  padding: 1rem 2rem;
  border: 1px solid black;
  text-decoration: none;
  margin: 1rem;
  border-radius: 2px;
}

.inline-block-buttons a {
  display: inline-block;
  align-self: right;
}
body {
  margin: 2rem;
}
  @media (max-width: 500px) {
  .inline-block-buttons {
    flex-direction: column;
  }

答案1

得分: 0

以下是您要翻译的代码部分:

p {
  border: 1px solid black;
  margin: 2rem 0;
  padding: 1rem 2rem;
  display: flex;
  align-items: center;
  border-radius: 10px;
}

a {
  padding: 1rem 2rem;
  border: 1px solid black;
  text-decoration: none;
  margin: 1rem;
  border-radius: 10px;
}

.inline-block-buttons a {
  margin-left: auto;
}

body {
  margin: 2rem;
  flex-direction: column;
  font-weight: bold;
}

@media (max-width: 500px) {
  .inline-block-buttons {
    flex-direction: column;
  }
  .inline-block-buttons a {
    margin: 1rem;
  }
}
<body>
  <p class="inline-block-buttons">
    Text 1
    <a>Text 2</a>
  </p>
</body>
英文:

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

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

p {
  border: 1px solid black;
  margin: 2rem 0;
  padding: 1rem 2rem;
  display:flex;
  align-items: center;
  border-radius: 10px;
}

a {
  padding: 1rem 2rem;
  border: 1px solid black;
  text-decoration: none;
  margin: 1rem;
  border-radius: 10px;
}

.inline-block-buttons a {
  margin-left: auto;
}

body {
  margin: 2rem;
  flex-direction: column;
  font-weight: bold;
}
@media (max-width: 500px) {
  .inline-block-buttons {
    flex-direction: column;
  }
  .inline-block-buttons a{
    margin: 1rem;
  }
}

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

&lt;body&gt;
&lt;p class=&quot;inline-block-buttons&quot;&gt;
  Text 1
  &lt;a&gt;Text 2&lt;/a&gt;
&lt;/p&gt;
&lt;/body&gt;

<!-- end snippet -->

答案2

得分: 0

以下是您要翻译的内容:

尝试这个:

<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-css -->
div.inline-block-buttons {
    display: flex;
    justify-content: space-between;
    padding: 30px;
    flex-wrap: wrap;
}

.inline-block-buttons {
    border: 1px solid black;
    border-radius: 10px;
    padding: 15px 100px;
}

@media (max-width: 500px) {
  .inline-block-buttons {
    flex-direction: column;
    text-align: center;
  }
  .inline-block-buttons p {
    margin-bottom: 50px;
  }
}

<!-- 语言:lang-html -->
 <div class="inline-block-buttons">
        <p>Text 1</p>
        <a class="inline-block-buttons">Text 2</a>
 </div>

<!-- 结束代码片段 -->

请注意,我已经保留了代码部分的原始标记,以便您可以在需要时使用它们。

英文:

Try this :

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

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

div.inline-block-buttons {
    display: flex;
    justify-content: space-between;
    padding: 30px;
    flex-wrap: wrap;
}

.inline-block-buttons {
    border: 1px solid black;
    border-radius: 10px;
    padding: 15px 100px;
}


@media (max-width: 500px) {
  .inline-block-buttons {
    flex-direction: column;
    text-align: center;
  }
  .inline-block-buttons p {
    margin-bottom: 50px;
  }
}

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

 &lt;div class=&quot;inline-block-buttons&quot;&gt;
        &lt;p&gt;Text 1&lt;/p&gt;
        &lt;a class=&quot;inline-block-buttons&quot;&gt;Text 2&lt;/a&gt;
 &lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月19日 20:43:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76506744.html
匿名

发表评论

匿名网友

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

确定