CSS问题涉及到`div`和内联组件。

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

CSS problem with divs and inline components

问题

想象一下这样的代码:

<div class="main">
  <div>
    <div class="div"></div>
    <div class="div1"></div>
  </div>

  <div class="div2"></div>
</div>

它会呈现出类似以下的效果:

CSS问题涉及到`div`和内联组件。

我希望蓝色的 div 出现在红色的 div 右侧并保持在那里。假设我不能改变 div 的当前位置,所以我需要在 CSS 中实现。如何可以做到呢?

英文:

Imagine a code like this:

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

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

.div {
  width: 100%;
  height: 100px;
  background-color: green;
}

.div1 {
  width: 100px;
  height: 100px;
  background-color: red;
}

.div2 {
  width: 100px;
  height: 100px;
  background-color: blue;
}

.main {
  background-color: yellow;
}

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

&lt;div class=&quot;main&quot;&gt;
  &lt;div&gt;
    &lt;div class=&quot;div&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;div1&quot;&gt;&lt;/div&gt;
  &lt;/div&gt;

  &lt;div class=&quot;div2&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

It will render something like this:

CSS问题涉及到`div`和内联组件。

I want that the blue div comes up and stay on the right of the red div. Imagine that I can´t change the divs from where they are, so I need to do it in css. How can I do it?

答案1

得分: 1

不更改标记的情况下,如果您将float: left设置为红色&lt;div&gt;,则可以将蓝色&lt;div&gt;放在其右侧。

英文:

Without changing the markup, if you set float: left to the red &lt;div&gt; then you could put the blue &lt;div&gt; to its right side

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

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

.div {
  width: 100%;
  height: 100px;
  background-color: green;
}

.div1 {
  width: 100px;
  height: 100px;
  background-color: red;
  float: left;
}

.div2 {
  width: 100px;
  height: 100px;
  background-color: blue;
  display: inline-block;
  vertical-align: top;
}

.main {
  background-color: yellow;
  
}

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

&lt;div class=&quot;main&quot;&gt;
  &lt;div&gt;
    &lt;div class=&quot;div&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;div1&quot;&gt;&lt;/div&gt;
  &lt;/div&gt;

  &lt;div class=&quot;div2&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

使用float在红色div上的先前解决方案效果很好,但这里还有另一种可能的解决方案:

position: relative;应用于蓝色div(以便能够相对于其默认位置移动它),并添加top: -100px; left: 100px;以将其移动到红色div旁边:

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

<!-- language: lang-css -->
.div {
  width: 100%;
  height: 100px;
  background-color: green;
}

.div1 {
  width: 100px;
  height: 100px;
  background-color: red;
}

.div2 {
  width: 100px;
  height: 100px;
  background-color: blue;
  position: relative;
  top: -100px;
  left: 100px;
}

.main {
  background-color: yellow;
}

<!-- language: lang-html -->
<div class="main">
  <div>
    <div class="div"></div>
    <div class="div1"></div>
  </div>

  <div class="div2"></div>
</div>

<!-- end snippet -->
英文:

The previous solution which uses float on the red div works well, but here is another possible solution:

Apply position: relative; to the blue div (to be able to move it in relation to its default position) and add top: -100px; left: 100px; to move it up next to the red div:

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

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

.div {
  width: 100%;
  height: 100px;
  background-color: green;
}

.div1 {
  width: 100px;
  height: 100px;
  background-color: red;
}

.div2 {
  width: 100px;
  height: 100px;
  background-color: blue;
  position: relative;
  top: -100px;
  left: 100px;
}

.main {
  background-color: yellow;
}

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

&lt;div class=&quot;main&quot;&gt;
  &lt;div&gt;
    &lt;div class=&quot;div&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;div1&quot;&gt;&lt;/div&gt;
  &lt;/div&gt;

  &lt;div class=&quot;div2&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: 0

这也可以通过grid CSS来实现。这里我使用了一个命名模板box,然后在"chatty verbose" CSS中为每个"block"放置了位置相关的样式。我为了清晰度添加了一些颜色和其他元素,但将"position relate"部分保留在单独的CSS块中。

.main {
  font-size: 2rem;
  display: grid;
  grid-template: "box";
  background-color: yellow;
}

.main *,
.main::before {
  grid-area: box;
}

.green-block {
  place-self: start;
}

.red-block {
  width: 50%;
  place-self: end start;
}

.blue-block {
  width: 50%;
  place-self: end end;
}

.green-block {
  height: 3rem;
  background-color: green;
}

.red-block {
  height: 3rem;
  background-color: red;
}

.blue-block {
  background-color: blue;
}

.blue-block,
.green-block,
.red-block {
  /* 为了清晰度和在块中使文本垂直居中 */
  display: grid;
  color: cyan;
  font-family: sans-serif;
  text-transform: uppercase;
  place-items: center;
}
<div class="main">
  <div>
    <div class="div green-block">green</div>
    <div class="div1 red-block">red</div>
  </div>
  <div class="div2 blue-block">blue</div>
</div>
英文:

This can also be done with the grid CSS. Here I used a named template box and then in the "chatty verbose" CSS I put the positional related for each "block". I added classes to the CSS just for clarity but you could update to your classes.

I added some color and things just for clarity and visual references but kept the "position relate" in separate CSS chunks.

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

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

.main {
  font-size: 2rem;
  display: grid;
  grid-template: &quot;box&quot;;
  background-color: yellow;
}

.main *,
.main::before {
  grid-area: box;
}

.green-block {
  place-self: start;
}

.red-block {
  width: 50%;
  place-self: end start;
}

.blue-block {
  width: 50%;
  place-self: end end;
}

.green-block {
  height: 3rem;
  background-color: green;
}

.red-block {
  height: 3rem;
  background-color: red;
}

.blue-block {
  background-color: blue;
}

.blue-block,
.green-block,
.red-block {
  /* color for clarity and just to super center the text in the blocks */
  display: grid;
  color: cyan;
  font-family: sans-serif;
  text-transform: uppercase;
  place-items: center;
}

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

&lt;div class=&quot;main&quot;&gt;
  &lt;div&gt;
    &lt;div class=&quot;div green-block&quot;&gt;green&lt;/div&gt;
    &lt;div class=&quot;div1 red-block&quot;&gt;red&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;div2 blue-block&quot;&gt;blue&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月7日 00:42:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364170.html
匿名

发表评论

匿名网友

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

确定