Html 使对齐的文本块在div内居中
我这里有一个非常简单的问题。标题不言自明。我知道这是一个常见问题,我知道在线解决方案。但是我的情况就是不起作用 我想要一个对齐的文本块…位于div的正中间 这就是它现在的样子: 我希望它看起来像这样: 我试过:Html 使对齐的文本块在div内居中,html,css,Html,Css,我这里有一个非常简单的问题。标题不言自明。我知道这是一个常见问题,我知道在线解决方案。但是我的情况就是不起作用 我想要一个对齐的文本块…位于div的正中间 这就是它现在的样子: 我希望它看起来像这样: 我试过: margin: 0px, auto; 并且div的宽度已经定义 这是幻灯片2的css /*slide 2 */ #slide-2 .bcg { position: relative; background-color: #1C1C1C; padding:200px; } slid
margin: 0px, auto;
并且div的宽度已经定义
这是幻灯片2的css
/*slide 2 */
#slide-2 .bcg {
position: relative;
background-color: #1C1C1C;
padding:200px;
}
slide-2 .hsContent {
position: relative;
}
slide-2 .hscontainer {
width: 100%;
height: 80%;
overflow: hidden;
position:relative;
margin: 0px, auto;
}
#slide-2 h2 {
bottom: 10px;
color: #696d6d;
font-size: 16px;
line-height: 150%;
position: absolute;
line-spacing: 1px;
text-align: justify;
width: 700px;
}
和html文件的一部分
<section id="slide-2" class="homeSlide">
//background style
<div class="bcg">
//container style
<div class="hsContainer">
<h2>
Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
Aenean dictum ligula et tellus tempus,
id tincidunt sapien ultricies.
</h2>
</div>
</div>
</section>
//背景风格
//容器样式
Lorem ipsum dolor sit amet,
献祭精英。
埃涅阿斯的名言《舌苔》和《时令》,
我是一个天才。
感谢将h2元素居中,只需使用此
#slide-2 h2 {
color: #696d6d;
font-size: 16px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
line-spacing: 1px;
text-align: justify;
}
为了将
放在这个答案的评论中指出的的中心,我将css调整为以下内容:
#slide-2 .bcg {
position: relative;
background-color: #1C1C1C;
padding:200px;
}
#slide-2 h2 {
color: #696d6d;
font-size: 16px;
position:absolute;
top: 50%;
left: 50%
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
line-spacing: 1px;
text-align: justify;
}
-webkit转换
和-ms转换
是浏览器前缀。如果不添加它们,h2将不会在Firefox或IE中居中
在这方面有一篇很好的博客文章
是一个jsfiddle。您需要给h2一个宽度,以便边距:0 auto
工作……等等,有很多问题。做一把小提琴…没有它们它会居中。这取决于你使用的IE或Firefox的版本。是的,我通常只包括前缀,以确保我不会遇到问题。还有一件事是,你的答案是正确的。但看起来他想把h2放在中心,而不是整个容器。只是提醒一下。是的,我现在正在看。@Donte'Trumble我根据你的评论做了一些调整。虽然对于正常大小的窗口,我的代码是正确的,但如果调整了窗口的大小,.hsContainer将向上推送内容。你的CSS解释了这个变化,我调整了我的答案,使之与设置一致;h2至位置:绝对值;我给了你一个加分,因为你的回答激发了我的改变。