首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CSS文本对齐中心关闭

CSS文本对齐中心关闭
EN

Stack Overflow用户
提问于 2021-04-30 19:33:18
回答 2查看 57关注 0票数 0

我有一个html页面,它使用中间对齐的文本。由于某种原因,顶部标题(#pageTitle id)关闭了一点,看起来它的左边有一些添加的空格。我试图在这里重现这个问题,但是在堆栈溢出编辑器中,格式完全不稳定。希望脚本能够在您的机器上运行。如果有人对为什么会发生这种事有任何洞察力,我将不胜感激。谢谢!

代码语言:javascript
运行
复制
body {
  padding: 0;
  margin: 0;
}


/* center aligned vertically and horizontally*/

.totalCenter {
  position: relative !important;
  top: 50% !important;
  left: 50% !important;
  transform: translate(-50%, -50%) !important;
}


/* centered horizontally */

.center {
  display: block;
  margin-left: auto;
  margin-right: auto;
}


/* div for main page content */

#mainDiv {
  width: 500px;
  max-width: 500px;
}

.title {
  padding: 0px;
  margin: 0px;
  margin-bottom: 15px;
}


/* login title */

#pageTitle {
  font-size: 65px;
  color: black;
  margin-left: 0px;
  padding-left: 0px;
}

#tagline {
  font-size: 30px;
  color: rgb(79, 79, 79);
  margin-bottom: 22px;
}


/* input for email/password */

.infoInput {
  text-align: center;
  background-color: white;
  border-radius: 8px;
  margin-bottom: 10px;
  outline-width: 0;
  padding: 5px;
  width: 100%;
  height: 50px;
  font-size: 22px;
}


/* login/register button */

.submitButton {
  color: black;
  background-color: rgb(255, 223, 0);
  border: none;
  border-radius: 4px;
  border-bottom: 4px solid rgb(218, 189, 0);
  cursor: pointer;
  outline: none;
  width: 100%;
  height: 55px;
  left: 10%;
  font-size: 25px;
  margin-top: 25px;
}

.submitButton:hover {
  background: rgb(235, 204, 0);
  transition: all 0.2s ease;
  cursor: pointer;
}

.submitButton:active {
  border: 0;
  transition: all 0.2s ease;
}

.topBarButton {
  border: none;
  outline: none;
  border-radius: 5px;
  cursor: pointer;
  width: 90px;
  height: 40px;
  margin: 10px;
  font-size: 20px;
  float: right;
}

#login {
  color: black;
  background-color: gold;
}

#register {
  background-color: black;
  color: gold;
}

#logo {
  margin: 10px;
  width: 40px;
  height: 40px;
  float: left;
}
代码语言:javascript
运行
复制
<head>
  <link href="/home/style/style.css" rel="stylesheet" />
</head>

<body>
  <div id="topbar">
    <img id="logo" src="/images/logo.png">

    <button class="topBarButton" id="register">
                Register
            </button>

    <button class="topBarButton" id="login">
                Login
            </button>
  </div>

  <div id="mainDiv" class="totalCenter">
    <h1 id="pageTitle" class="title">title</h1>
    <h2 id="tagline" class="title">page tagline</h2>

    <input class="infoInput center" placeholder="Your Email..." id="email" name="email" type="email" required />
    <button class="submitButton center">Next</button>
  </div>
</body>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-04-30 19:44:54

标题得到了填充,因为在使用lefttoptransform移动它之前,徽标图像(它具有浮点属性)位于左侧。如果从top lefttransform中删除mainDiv,您将看到以下内容:

这就是这个空格的来历。您应该在工具栏下向clear:both添加一个元素(请参阅下面的片段)

代码语言:javascript
运行
复制
body {
  padding: 0;
  margin: 0;
}
/* ADDED THIS*/
.clear{clear:both}
/* center aligned vertically and horizontally*/

.totalCenter {
  position: relative !important;
  top: 50% !important;
  left: 50% !important;
  transform: translate(-50%, 50%) !important;
}


/* centered horizontally */

.center {
  display: block;
  margin-left: auto;
  margin-right: auto;
}


/* div for main page content */

#mainDiv {
  width: 500px;
  max-width: 500px;
}

.title {
  padding: 0px;
  margin: 0px;
  margin-bottom: 15px;
}


/* login title */

#pageTitle {
  font-size: 65px;
  color: black;
  margin-left: 0px;
  padding-left: 0px;
}

#tagline {
  font-size: 30px;
  color: rgb(79, 79, 79);
  margin-bottom: 22px;
}


/* input for email/password */

.infoInput {
  text-align: center;
  background-color: white;
  border-radius: 8px;
  margin-bottom: 10px;
  outline-width: 0;
  padding: 5px;
  width: 100%;
  height: 50px;
  font-size: 22px;
  box-sizing:border-box;
}


/* login/register button */

.submitButton {
  color: black;
  background-color: rgb(255, 223, 0);
  border: none;
  border-radius: 4px;
  border-bottom: 4px solid rgb(218, 189, 0);
  cursor: pointer;
  outline: none;
  width: 100%;
  height: 55px;
  left: 10%;
  font-size: 25px;
  margin-top: 25px;
}

.submitButton:hover {
  background: rgb(235, 204, 0);
  transition: all 0.2s ease;
  cursor: pointer;
}

.submitButton:active {
  border: 0;
  transition: all 0.2s ease;
}

.topBarButton {
  border: none;
  outline: none;
  border-radius: 5px;
  cursor: pointer;
  width: 90px;
  height: 40px;
  margin: 10px;
  font-size: 20px;
  float: right;
}

#login {
  color: black;
  background-color: gold;
}

#register {
  background-color: black;
  color: gold;
}

#logo {
  margin: 10px;
  width: 40px;
  height: 40px;
  float: left;
}
代码语言:javascript
运行
复制
<head>
  <link href="/home/style/style.css" rel="stylesheet" />
</head>

<body>
  <div id="topbar">
    <img id="logo" src="/images/logo.png">

    <button class="topBarButton" id="register">
                Register
            </button>

    <button class="topBarButton" id="login">
                Login
            </button>
            <div class="clear"></div><!-- ADDED THIS -->
  </div>

  <div id="mainDiv" class="totalCenter">
    <h1 id="pageTitle" class="title">title</h1>
    <h2 id="tagline" class="title">page tagline</h2>

    <input class="infoInput center" placeholder="Your Email..." id="email" name="email" type="email" required />
    <button class="submitButton center">Next</button>
  </div>
</body>

票数 0
EN

Stack Overflow用户

发布于 2021-04-30 20:00:31

您可以使用flex对齐项,这样做更容易:

代码语言:javascript
运行
复制
body {
  padding: 0;
  margin: 0;
  display: flex;
  align-items: 'center'; 
  justify-content: 'center'; 
}

.totalCenter {
  display: flex;
  justify-content: 'center';
}

当将display属性设置为类的flex时,可以使用以下属性:

  • align-items:垂直对齐项目。
  • justify-content:alings的项目水平。

如果您无法了解更多的信息,那么Flex真的很酷,您可以查看这两篇文章:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67339520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档