简体   繁体   English

将 div 置于另一个 div 中的最佳方法是什么?

[英]What is the best way to center a div inside another div?

I have so far been trying to create a basic website layout shown in this picture.到目前为止,我一直在尝试创建这张图片中显示的基本网站布局。 https://imgur.com/a/JhURder https://imgur.com/a/JhURder

It includes a grey background, with a centered div inside with a white background.它包括一个灰色背景,里面有一个白色背景居中的 div。 With centered an image to the left and text etc to the right.将图像居中左侧,文本等居中右侧。

I am a large advocate for twitter bootstrap, so far I have implemented this.我是 twitter bootstrap 的大力倡导者,到目前为止我已经实现了这一点。 (React style) (反应风格)

            <div class="container-fluid" style={{
                'background-color' : 'rgb(224,225,226)',
                'height' : '40vh'
            }}>
                <div class="row">
                    <div class="col-1 text-center"> Test </div>

                    <div class="col-10 text-center" style={{
                        'height' : '30vh',
                        'background-color' : 'rgb(255,255,255)',
                        'margin-top' : '5vh'
                    }}>

                    centered

                    </div>

                    <div class="col-1 text-center">test</div>
                </div>
            </div>

So far it half ways.到目前为止,它已经进行了一半。 But honestly I kind of gave us because it's turned into a nested mess.但老实说,我有点给了我们,因为它变成了一个嵌套的烂摊子。 With website development I always know their is a bigger way, so I come here for people to roast my attempt so far and could give me in a proper way of reaching my goal.随着网站开发,我总是知道他们是一个更大的方式,所以我来这里是为了让人们对我的尝试进行迄今为止的尝试,并可以以适当的方式给我实现我的目标。

Here's some ideas of how you can achieve it.这里有一些关于如何实现它的想法。

https://codepen.io/Warisara-L/pen/wOMxwR https://codepen.io/Warisara-L/pen/wOMxwR

// HTML
<div class="wrapper" id="solution-1">
  <div class="inner">1</div>
</div>

// SCSS
#solution-1 {
  display: grid;
  .inner {
    margin: auto;
  }
}

#solution-2 {
  display:flex;
  justify-content: center;
  align-items: center;
}

#solution-3 {
  position: relative;
  .inner {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
}

#solution-4 {
  display: flex;
  .inner {
    margin: auto;
  }
}

#solution-5 {
  display: grid;
  justify-content: center;
  align-items: center;
}

#solution-6 {
  position: relative;
  .inner {
    position: absolute;
    margin: auto;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
  }
}

#solution-7 {
  height: 120px;
  line-height: 120px;
  text-align: center;
  .inner {
    display: inline-block;
    vertical-align: middle;
  }
}

#solution-8 {
  display: flex;
  .inner {
    align-self: center;
    margin: 0 auto;
  }
}

Have a good day sir :)祝先生有美好的一天:)

With FlexBox使用 FlexBox

With display: flex;display: flex; you can specify the properties justify-content and align-items to center in order to center child elements both horizontally and vertically.您可以指定属性justify-contentalign-items center ,以便在水平和垂直方向上居中子元素。

When a display: flex element is a row ( flex-direction: row; ) justify-content will handle horizontal positioning while align-items handles vertical positioning.display: flex元素是一行( flex-direction: row; )时, justify-content将处理水平定位,而align-items处理垂直定位。 When flex-direction is set to column , justify-content and align-items meanings are swapped.flex-direction设置为columnjustify-contentalign-items含义会交换。

Example:例子:

.parent-element {
  display: flex;
  /* flex-direction defaults to row */

  justify-content: center;
  align-items: center;
}

With CSS Grid使用 CSS 网格

You can utilize the property place-items to center the children of a grid.您可以利用属性place-items将网格的子place-items居中。 You should be aware, however, that place-items is not currently supported on all browsers.但是,您应该知道,当前并非所有浏览器都支持place-items

Example:例子:

.parent-element {
  display: grid;
  place-items: center;
}

Margin auto保证金自动

Sometimes you can utilize the auto value for the margin property to push an element towards the center.有时您可以利用margin属性的auto值将元素推向中心。

Be sure to check out this post that explains what is needed for margin: auto;请务必查看这篇文章,其中解释margin: auto;所需的内容margin: auto; to work . 工作

Example:例子:

.child-element {
  /* top right bottom left -- all set to auto */
  margin: auto;
}

Absolute positioning with a transform带变换的绝对定位

You can position an item in the center of the screen by setting the position property to absolute (this will take it out of the normal document flow).您可以通过将position属性设置为absolute (这将使其脱离正常文档流)将项目定位在屏幕中央。 Then, you can use the properties left and top to move the element down and to the right 50% .然后,您可以使用lefttop属性将元素向下和向右移动50%

Once it's pushed 50% we need to move it back half the width/height of the element, we can do that with a translate inside the property transform .一旦它被推到50%我们需要将它移回元素宽度/高度的一半,我们可以在属性transform中使用translate来做到这一点。

Example:例子:

.child-element {
  position: absolute;
  top: 50%;
  left: 50%;

  /* translate(<x value>, <y value>) */
  transform: translate(-50%, -50%);
}

Disclaimer:免责声明:

This is not advised for elements that are a part of your normal document flow as they will most likely overlap other elements.对于属于正常文档流一部分的元素,不建议这样做,因为它们很可能会与其他元素重叠。 It's great for modals though.不过这对模态来说很棒。

尝试在要居中的内部 div 中设置 css margin: 0 auto

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM