简体   繁体   English

如何对齐文本以使该图像居中?

[英]How to align text to center this image?

I have this sample: 我有这个样本:

link 链接

CODE HTML: 代码HTML:

<div class="col-md-4 tab-bottom">
                    <div class="tab-bottom-img"><img width="380" height="380" src="http://dg-design.ch/bagel/wp-content/uploads/2016/02/1-380x380.png" class="attachment-news wp-post-image" alt="1"> </div>
                        <div class="tab-bottom-content">
                            SED PERSPICIATIS                            <p>Sed ut perspiciatis unde omnis iste natus error sit<br>
voluptatem accusantium doloremque laudantium,<br>
totam rem aperiam, eaque ipsa quae ab illo inventore<br>
veritatis et quasi architecto beatae vitae dicta sunt<br>
explicabo.</p>
                        </div>
                </div>

CODE CSS: 代码CSS:

.tab-bottom-content {
    position: absolute;
  text-align:center;
    top: 0px;
}

Image width and height will vary ... he should always be in the middle 图像的宽度和高度会有所不同...他应该始终在中间

I tried to align text center but not working... 我试图对齐文本中心,但无法正常工作...

Text must be aligned both horizontally and vertically. 文本必须水平和垂直对齐。

Can you help me to solve this problem please? 您能帮我解决这个问题吗?

Thanks in advance! 提前致谢!

Try the following CSS: 尝试以下CSS:

.tab-bottom-content {
    position: absolute;
    text-align:left;
    margin-top: -240px;
    margin-left: 20px;; 
}

Before, you had top: 0; 之前,您有top: 0; which means to show the div in the 0 pixels from main port. 这意味着在距主端口0 pixels处显示div。 If you want to change bring the text in the middle of it then change the value for top CSS property. 如果要更改,请在文本中间添加文字,然后更改top CSS属性的值。 If the Image and Div are not the first elements on your page, then I suggest to use margin instead of top property. 如果Image和Div不是页面上的头一个元素,则建议使用margin而不是top属性。

Try this code ( https://jsfiddle.net/g6r8ayq1/1/ ): 尝试以下代码( https://jsfiddle.net/g6r8ayq1/1/ ):

HTML HTML

<div class="col-md-4 tab-bottom">
                    <div class="tab-bottom-img"><img width="380" height="380" src="http://dg-design.ch/bagel/wp-content/uploads/2016/02/1-380x380.png" class="attachment-news wp-post-image" alt="1"> </div>
                        <div class="tab-bottom-content">
                            SED PERSPICIATIS                            <p>Sed ut perspiciatis unde omnis iste natus error sit<br>
voluptatem accusantium doloremque laudantium,<br>
totam rem aperiam, eaque ipsa quae ab illo inventore<br>
veritatis et quasi architecto beatae vitae dicta sunt<br>
explicabo.</p>
                        </div>
                </div>

CSS CSS

* {
 margin: 0;
 padding: 0;

 }
 .tab-bottom {
  position: relative;
  width: 100%;
  height: 400px;
  background-color: red;

 }
.tab-bottom-img {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);

 }
.tab-bottom-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

Source: https://css-tricks.com/centering-css-complete-guide/ 资料来源: https : //css-tricks.com/centering-css-complete-guide/

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

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