尝试在另一个 div 中使用内联 CSS 居中 div。

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

trying to center divs within another div using inline css

问题

以下是翻译好的部分:

"I have an assignment for class (I know inline CSS isn't standard practice) in which he wants us to specifically use inline CSS in order to have a div box containing three other boxes that are centred horizontally. When I try to do it, no matter what I try, the formatting ends up odd, and I can't make them show up horizontally next to each other."

英文:

I have an assignment for class (I know inline CSS isn't standard practice) in which he wants us to specifically use inline css in order to have a div box containing three other boxes that are centred horizontally. When I try to do it, no matter what I try, the formatting ends up odd, and I can't make them show up horizontally next to each other.

Here's what I tried:

Here's what it looks like, sorry for the badly scribbled out personal information.

答案1

得分: 0

我不确定我是否很好地理解了这个任务,但我想到了这个:

Float示例中,我使用width: calc((100% - 40px) / 3),而不是width: 33%,以消除子div的margin-right20px + 20px + 0 = 40px)。如果这有意义...
我还在容器div上使用了display: flow-root,这是创建块级格式上下文的最简单和安全的方式。

希望这有所帮助。

英文:

Not sure if I understand the assignment very well, but I came up this:

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

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

  &lt;h2&gt;Using Float&lt;/h2&gt;
  &lt;div style=&quot;background-color: pink; padding: 20px; text-align: center; display: flow-root&quot;&gt;
    &lt;div style=&quot;background-color: aquamarine; margin-right: 20px; width: calc((100% - 40px) / 3); height: auto; float: left;&quot;&gt;&lt;p&gt;Lorem, ipsum dolor.&lt;/p&gt;&lt;/div&gt;
    &lt;div style=&quot;background-color: aquamarine; margin-right: 20px; width: calc((100% - 40px) / 3); height: auto; float: left;&quot;&gt;&lt;p&gt;Lorem, ipsum dolor.&lt;/p&gt;&lt;/div&gt;
    &lt;div style=&quot;background-color: aquamarine; margin-right: 0px; width: calc((100% - 40px) / 3); height: auto; float: left;&quot;&gt;&lt;p&gt;Lorem, ipsum dolor.&lt;/p&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;h2&gt;Using Flex&lt;/h2&gt;
  &lt;div style=&quot;background-color: pink; padding: 20px; text-align: center; display: flex; gap: 20px;&quot;&gt;
    &lt;div style=&quot;background-color: aquamarine; flex-grow: 1;&quot;&gt;&lt;p&gt;Lorem, ipsum dolor.&lt;/p&gt;&lt;/div&gt;
    &lt;div style=&quot;background-color: aquamarine; flex-grow: 1;&quot;&gt;&lt;p&gt;Lorem, ipsum dolor.&lt;/p&gt;&lt;/div&gt;
    &lt;div style=&quot;background-color: aquamarine; flex-grow: 1;&quot;&gt;&lt;p&gt;Lorem, ipsum dolor.&lt;/p&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;h2&gt;Using Grid&lt;/h2&gt;
  &lt;div style=&quot;background-color: pink; padding: 20px; text-align: center; display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 20px;&quot;&gt;
    &lt;div style=&quot;background-color: aquamarine;&quot;&gt;&lt;p&gt;Lorem, ipsum dolor.&lt;/p&gt;&lt;/div&gt;
    &lt;div style=&quot;background-color: aquamarine;&quot;&gt;&lt;p&gt;Lorem, ipsum dolor.&lt;/p&gt;&lt;/div&gt;
    &lt;div style=&quot;background-color: aquamarine;&quot;&gt;&lt;p&gt;Lorem, ipsum dolor.&lt;/p&gt;&lt;/div&gt;
  &lt;/div&gt;

<!-- end snippet -->

In the Float example I use width: calc((100% - 40px) / 3) instead of width: 33% in order to eliminate the margin-right of the child divs (20px + 20px + 0 = 40px). If that makes sense...<br>
I also use display: flow-root on the container div, which is the easiest and safest way to create a block formatting context.<br><br>
Hope it helps.

huangapple
  • 本文由 发表于 2023年2月24日 05:48:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550665.html
匿名

发表评论

匿名网友

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

确定