如何使这个
在另一个
中呈现?

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

How can i get this div to render inside another div?

问题

需要通过CSS将go-button div 放在initial-bar div 内,可以使用display: flex;align-items: center; justify-content: center; 来实现。以下是你的代码的修改部分:

.initial-bar {
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #1a1a1a;
    width: 1440px;
    height: 90px;
}

请将上述CSS代码添加到你的样式表中,这样go-button 就会在 initial-bar 内居中显示。

英文:

I need to get a certain div inside another div, by using HTML and CSS. I placed the div go-button in the initial-bar, but won't work. Anyways, here's my code.

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

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

body {
    background: #2a2a2a;
    color: white;
    font-family: sans-serif;
}
.p-title-bar {
    color: #FFFFFF;
    font-size:  32px;
    line-height: 18px;
    font-style: oblique;
}
.go-button {
    background-color: #207000;
    border-radius: 10px;
    width: 100px;
    height: 50px;
}

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

&lt;div class=&quot;initial-bar&quot; style=&quot;background-color: #1a1a1a; width: 1440px; height: 90px;&quot;&gt;
    &lt;div class=&quot;text-title-spacing&quot;&gt;
        &amp;nbsp;
    &lt;/div&gt;
        &lt;h1 class=&quot;p-title-bar&quot;&gt;&amp;nbsp;&amp;nbsp;.HUS&lt;/p&gt;
    &lt;div class=&quot;go-button&quot;&gt;

    &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

It shows the div go-button out of the div initial-bar, How can i get the div go-button in the div initial-bar.

I tried to get it with CSS text-align in center, and align in center also. I expected the div go-button to be inside the div initial-bar. And it resulted on a green 100x50 pixels square with a border-radius of 10 pixels.

答案1

得分: 2

你的HTML结构有问题,因为你在h1标签前用了一个错误的p标签闭合。以下是已更新的代码:

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

<!-- language: lang-css -->
body {
    background: #2a2a2a;
    color: white;
    font-family: sans-serif;
}
.p-title-bar {
    color: #FFFFFF;
    font-size: 32px;
    line-height: 18px;
    font-style: oblique;
}
.go-button {
    background-color: #207000;
    border-radius: 10px;
    width: 100px;
    height: 50px;
}

<!-- language: lang-html -->
<div class="initial-bar" style="background-color: #1a1a1a; width: 1440px; height: 90px;">
    <div class="text-title-spacing">
        &nbsp;
    </div>
    <h1 class="p-title-bar">&nbsp;&nbsp;.HUS</h1>
    <div class="go-button">

    </div>
</div>

<!-- end snippet -->

希望这对你有帮助。

英文:

Your html structure is disturbing because you are have closing p tag with for h1 tag. Here is the updated code:-

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

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

  body {
            background: #2a2a2a;
            color: white;
            font-family: sans-serif;
        }
        .p-title-bar {
            color: #FFFFFF;
            font-size:  32px;
            line-height: 18px;
            font-style: oblique;
        }
        .go-button {
            background-color: #207000;
            border-radius: 10px;
            width: 100px;
            height: 50px;
        }

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

&lt;div class=&quot;initial-bar&quot; style=&quot;background-color: #1a1a1a; width: 1440px; height: 90px;&quot;&gt;
        &lt;div class=&quot;text-title-spacing&quot;&gt;
            &amp;nbsp;
        &lt;/div&gt;
        &lt;h1 class=&quot;p-title-bar&quot;&gt;&amp;nbsp;&amp;nbsp;.HUS&lt;/h1&gt;
        &lt;div class=&quot;go-button&quot;&gt;

        &lt;/div&gt;
    &lt;/div&gt;

<!-- end snippet -->

答案2

得分: 1

你的代码中有一个错误,因为你打开了一个&lt;h1&gt;标签,然后用&lt;p&gt;关闭它

这导致创建了一个&lt;p&gt;元素,将&lt;div&gt;移至其下方

我不确定这是否解决了你的问题,因为我不太了解你想要实现什么结果

英文:

In your code there is an error because you open an &lt;h1&gt; tag and close it with &lt;p&gt;

which generates that a &lt;p&gt; element is created and this moves the &lt;div&gt; below

I don't know if this solves your problem because I don't quite understand what result you want to achieve

答案3

得分: 0

如注意到,你的 h1 标签在闭合时使用了 &lt;/p>,已经在下方修复。通常 h1 标签是块级元素,将其更改为内联元素或内联块级元素。然后将“go”按钮的显示方式更改为内联块级元素。

即便如此,这将解决你的问题,但在我看来,更好的方式是使用弹性盒模型(flexbox)。

body {
  background: pink;
  color: white;
  font-family: sans-serif;
}

.p-title-bar {
  color: red;
  font-size: 32px;
  line-height: 18px;
  font-style: oblique;
}

.go-button {
  background-color: #207000;
  border-radius: 10px;
  width: 100px;
  height: 50px;
}

.initial-bar{
  display:flex;
  justify-content:space-between;
  align-items:center;
}
<div class="initial-bar" style="background-color: lightgreen; height: 90px;">
  <div class="text-title-spacing">
    &nbsp;
  </div>
  <h1 class="p-title-bar">&nbsp;&nbsp;.HUS</h1>
  <div class="go-button">

  </div>
</div>

请注意,以上是你的代码的翻译部分。

英文:

As noted, your h1 is being closed with a &lt;/p> which is fixed below. h1's are typically block elements, change this one to inline or inline-block. Then change the display on the go button to inline-block.

That said, will solve your problem but a better way (IMO) is to use flexbox

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

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

body {
    background: pink;
    color: white;
    font-family: sans-serif;
}
.p-title-bar {
    color: red;
    font-size:  32px;
    line-height: 18px;
    font-style: oblique;    
    display:inline-block;
}
.go-button {
    background-color: #207000;
    border-radius: 10px;
    width: 100px;
    height: 50px;
    display:inline-block;
}

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

&lt;div class=&quot;initial-bar&quot; style=&quot;background-color: lightgreen; width: 1440px; height: 90px;&quot;&gt;
    &lt;div class=&quot;text-title-spacing&quot;&gt;
        &amp;nbsp 
    &lt;/div&gt;
        &lt;h1 class=&quot;p-title-bar&quot;&gt;&amp;nbsp;&amp;nbsp;.HUS&lt;/h1&gt;
    &lt;div class=&quot;go-button&quot;&gt;

    &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

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

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

body {
  background: pink;
  color: white;
  font-family: sans-serif;
  
}

.p-title-bar {
  color: red;
  font-size: 32px;
  line-height: 18px;
  font-style: oblique;
  
}

.go-button {
  background-color: #207000;
  border-radius: 10px;
  width: 100px;
  height: 50px;
  
}

.initial-bar{
display:flex;
justify-content:space-between;
align-items:center;}

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

&lt;div class=&quot;initial-bar&quot; style=&quot;background-color: lightgreen;  height: 90px;&quot;&gt;
  
  &lt;div class=&quot;text-title-spacing&quot;&gt;
    &amp;nbsp
  &lt;/div&gt;
  
  &lt;h1 class=&quot;p-title-bar&quot;&gt;&amp;nbsp;&amp;nbsp;.HUS&lt;/h1&gt;
  
  &lt;div class=&quot;go-button&quot;&gt;

  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定