Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Css 如何将内容放在屏幕中央_Css_Html - Fatal编程技术网

Css 如何将内容放在屏幕中央

Css 如何将内容放在屏幕中央,css,html,Css,Html,我试着把我的东西放在屏幕中央的标签上,但它不起作用 如何使用HTML和CSS将其放在屏幕中央? 我在哪里- 代码- 结果-如果不使用HTML5,可以尝试使用HTML标记 前- 此文本将居中对齐。 如果您使用的是HTML5,那么您需要使用css进行修改 <element>.<class> { margin-left: auto; margin-right: auto; width: 6em } 。{ 左边距:自动; 右边距:自动; 宽度:6em

我试着把我的东西放在屏幕中央的标签上,但它不起作用

如何使用HTML和CSS将其放在屏幕中央? 我在哪里-

代码-


结果-

如果不使用HTML5,可以尝试使用HTML标记

前-

此文本将居中对齐。
如果您使用的是HTML5,那么您需要使用css进行修改

<element>.<class> {
    margin-left: auto;
    margin-right: auto;
    width: 6em
}
。{
左边距:自动;
右边距:自动;
宽度:6em
}

如果您提供了一些您已经编写的代码,这会容易得多,但是如果您想将标记居中,您可以这样做:

CSS

#parentTag{
   position: relative;
}

#myTagIWantToBeCentered{
    position: absoulte;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}
HTML:

<div id="parentTag">
    <div id="myTagIWantToBeCentered">
       <!--stuff here-->
    </div>
</div>

如果需要将内容水平和垂直居中,请使用以下方法:

.parent {
  position: relative;
}
.child {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

假设有一个.child元素要在.parent元素中居中,则有2个选项:

1) Flexbox-您可以使用以下css:

.parent {
  display: flex;
  flex-direction: 
  flex-wrap: nowrap;
  justify-content: center;
  align-content: center;
  align-items: center;
}

.child {
  flex: 0 1 auto;
  align-self: auto;
  text-align: center; /*optional*/
}
2) 绝对定位:

.parent {
  position:relative;
}

.child{
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center; /*optional*/
}
.box\u文本{
颜色:#ffffff;
字体大小:20px;
字体大小:粗体;
位置:绝对位置;
最高:50%;
左:50%;
转换:翻译(-50%,-50%);
-webkit转换:翻译(-50%,-50%);
}
.盒子{
宽度:100px;
高度:100px;
背景色:红色;
保证金:3倍;
位置:相对位置;
}


0
如果在引导上运行,则可以轻松添加类
文本中心
。通常,(内联)块元素可以用css居中

.center { margin: 0 auto; }

如果您能提供您的代码,那么我们可以在此基础上提出建议,这将是最好的。

您可以尝试使用CSS Flexbox。如果你加上

.class {
    display: flex;
    align-items: center;     //Aligns the content vertically
    justify-content: center; //Aligns the content horizontally
}
添加到父html元素


例如,看看这个js提琴。

向我们展示您的尝试,这样我们就不会建议您已经做过的事情。。同时查看
margin
-可以像
margin:0 auto
那样使用它居中things@ThisGuyHasTwoThumbs我刚刚更新了它,我应该放边距吗?太好了-你的img 404s顺便说一句哈哈,看看我的答案,看看它是否有用:)
已被弃用,建议使用CSS对齐。此外,<代码> <代码>将内容水平放置在父div引用的中间:也可以将余量优化为<代码>空白:0自动;<代码>谢谢你的回答谢谢你的回答谢谢你的回答谢谢你的回答
.class {
    display: flex;
    align-items: center;     //Aligns the content vertically
    justify-content: center; //Aligns the content horizontally
}