如何在框内对齐文本?

时间:2018-12-25 12:00:58

标签: html css html5

我创建了一个文本并在其周围划了一个边界。后来,我根据需要指定了盒子的尺寸。当我增加文字大小时,它延伸到了框外。我什至写了'text-align:center;'在它的CSS部分。仍然没有任何结果。

我已经尝试过text-align:center;

.cards {
  display: grid;
  grid-template-row: 1fr 2fr 1fr;
  margin-right: 50px;
  margin-left: 50px;
  grid-row-gap: 20px;
}

.main {
  grid-row: 2/3;
  display: grid;
  grid-template-row: 1fr 1fr;
  grid-row-gap: 50px;
  margin-top: 80px;
}

.upper {
  grid-row: 1/2;
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  height: 150px;
  grid-column-gap: 10px;
}

.name {
  color: white;
  border: solid 10px #00CED1;
  border-radius: 15px;
  font-size: 50px;
  height: 130px;
  padding: 5px;
  margin-left: 100px;
  grid-column: 1/3;
  text-align: center;
}
<div class="cards">
  <div class="main">
    <div class="upper">
      <div class="name">
        <h1>
          <f style="font-family:'Abril Fatface'">BITS</f>
          <g style="font-family:'Antic Didone'">Hack</g>
        </h1>
      </div>
      <div class="year">
        <h1 style="font-family:'Asap Condensed'">2019</h1>
      </div>
    </div>
  </div>
</div>

我希望BITSHack这个名字在边界之内,但它正在扩展它。

3 个答案:

答案 0 :(得分:1)

请勿使用高度:130像素;内部名称类。尝试以下代码:

.name{
   height: auto;
}

答案 1 :(得分:0)

--------  -----
| GREY |  | 4 |
--------  -----
| BLUE |  | 3 |
--------  -----
| BLUE |  | 5 |
--------  -----
| GREY |  | 1 |
--------  -----
    .cards
    {
	    display:grid;
	    grid-template-row: 1fr 2fr 1fr;
	    margin-right:50px;
	    margin-left:50px;
	    grid-row-gap:20px;
    }
    .main
    {
	    grid-row:2/3;
	    display:grid;
	    grid-template-row:1fr 1fr;
	    grid-row-gap:50px;
	    margin-top:80px;
    }
    .upper
    {
	    grid-row:1/2;
	    display:grid;
	    grid-template-columns:1fr 2fr 1fr;
	    height:150px;
        //height: auto;
	    grid-column-gap:10px;
    }
    .name
    {
	    color:black;
	    border: solid 10px #00CED1;
	    border-radius:15px;
	    font-size:30px;
        height:auto;
	    padding:5px;
	    margin-left:50px;
	    grid-column:1/3;
	    text-align:center;
    }

现在尝试执行它,您的预期结果将会出现。

答案 2 :(得分:0)

完全不使用height,默认设置为自动。另外,请勿使用fg标签,而应使用span。 Yuo可以将两个跨距都用div包裹,然后将div对准中心(如果要给容器的话(在您的情况下为.name

display: flex;
align-items: center;
justify-content: center;

.cards {
  display: grid;
  grid-template-row: 1fr 2fr 1fr;
  margin-right: 50px;
  margin-left: 50px;
  grid-row-gap: 20px;
}

.main {
  grid-row: 2/3;
  display: grid;
  grid-template-row: 1fr 1fr;
  grid-row-gap: 50px;
  margin-top: 80px;
}

.upper {
  grid-row: 1/2;
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  height: 150px;
  grid-column-gap: 10px;
}

.name {
  color: green;
  border: solid 10px #00CED1;
  border-radius: 15px;
  font-size: 50px;
  padding: 5px;
  margin-left: 100px;
  grid-column: 1/3;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
}
<div class="cards">
  <div class="main">
    <div class="upper">
      <div class="name">
        <div>
          <span style="font-family:'Abril Fatface'">BITS</span>
          <span style="font-family:'Antic Didone'">Hack</span>
        </div>
      </div>
      <div class="year">
        <h1 style="font-family:'Asap Condensed'">2019</h1>
      </div>
    </div>
  </div>
</div>