html - 如果文本太长,为什么图像不居中? (使用显示 : table; margin: 0 auto; to horizontally center)

标签 html css

问题

发生了什么 enter image description here

应该发生什么 enter image description here

出于某种原因,如果标题太长,它会导致图像及其下方的标题文本移动到最左侧。我该如何解决这个问题?

如果有人能给我提供使 imgh3h4 水平居中的 css,将不胜感激!

代码

html

<div class="form-screen form-photo">
    <button class="close close-form"><span class="icon-x"></span></button>
      <div class="form-wrapper">
        <h1>Title</h1>
        <h2>Date created</h2>
        <img src="images/Beauty Gurus/Karen Yeung.jpg" alt="Karen Yeung" id="2">
       <!--if the caption is too long the image does not center-->
        <h3>EDIT: VIDEO IS LIVE!!!! Ok Night ? Owls Or Early ? Birds?! Are You Awake? My Jet Lag Is Screwing My Up And I</h3>
        <h4>Credit</h4>
      </div>
</div>

CSS

/*Style form photo*/
.form-photo .form-wrapper{
  display: table;
  margin: 0 auto;
}
.form-photo h3, .form-photo h4{
  font-weight: 300;
  font-size: 2em;
  text-transform: capitalize;
  vertical-align: middle;
}

.form-photo h2{
  margin-bottom: 20px;
}

.form-photo h3{
  font-size: 2em;
}

.form-photo h4{
  font-size: 1.5em;
}

.form-photo img{
  width: 500px;
}

最佳答案

您需要限制文本的宽度以匹配图像的宽度,或者您可以只限制父级的宽度,并将图像设置为 width: 100%

.form-photo .form-wrapper{
  display: table;
  margin: 0 auto;
  width: 500px;
  table-layout: fixed;
}
.form-photo h3, .form-photo h4{
  font-weight: 300;
  font-size: 2em;
  text-transform: capitalize;
  vertical-align: middle;
}

.form-photo h2{
  margin-bottom: 20px;
}

.form-photo h3{
  font-size: 2em;
}

.form-photo h4{
  font-size: 1.5em;
}

.form-photo {
  width: 100%;
}
<div class="form-screen form-photo">
    <button class="close close-form"><span class="icon-x"></span></button>
      <div class="form-wrapper">
        <h1>Title</h1>
        <h2>Date created</h2>
        <img src="http://kenwheeler.github.io/slick/img/fonz1.png" alt="Karen Yeung" id="2">
        <h3>EDIT: VIDEO IS LIVE!!!! Ok Night ? Owls Or Early ? Birds?! Are You Awake? My Jet Lag Is Screwing My Up And I</h3>
        <h4>Credit</h4>
      </div>
</div>

关于html - 如果文本太长,为什么图像不居中? (使用显示 : table; margin: 0 auto; to horizontally center),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43060653/

相关文章:

css - 当我们在 angularjs 中使用标题时,如何避免标题顶部出现一行?

html - 控制高度并删除旋转 div 之间的空格

javascript - Coin-Slider 无法正确显示按钮(Chrome 除外)

HTML CSS 调整大小问题 - 按钮和复选框向下一行

python - 如何使用 BeautifulSoup 打开 parent 的包装4

html - 类的属性选择器以开头和结尾

css - Firefox、IE9+ 问题,div 高度在 td 内为 100%(Chrome 上的工作示例)

php - 创建自定义模板 Wordpress - 无响应

php - 术语突出显示算法 (HTML)

html - z-index 在位置固定元素内不起作用