为什么我的文本字段不出现在中央?

huangapple go评论61阅读模式
英文:

Why does my text field does net appear in the centre?

问题

I am building a website for a project at college but somehow I do not get the paragraph aligned in the centre on the top of the webpage.

我正在为大学项目建立网站,但不知何故,我无法使顶部段落居中对齐。

I tried inline, but that did not work, then I tried CSS, but it still does not work the way I want it to:

我尝试了内联方式,但没有成功,然后我尝试了CSS,但它仍然没有按我想要的方式工作:

body {
    font-family: 'Roboto', sans-serif;
    background-color: black;
    color: white;
}

*{
    margin: 0;
    padding: 0;
    list-style: none;
    text-decoration: none;
}

.sidebar {
    position: fixed;
    left: -250;
    width: 250px;
    height: 100%;
    background: #042331;
}

.sidebar header {
    font-size: 22px;
    color: white;
    text-align: center;
    line-height: 70px;
    background: #063146;
    user-select: none;
}

.sidebar ul a{
    display: block;
    height: 100%;
    width: 100%;
    line-height: 65px;
    font-size: 20px;
    color: white;
    padding-left: 40px;
    box-sizing: border-box;
    border-top: 1px solid rgba(255, 255, 255, .1);
    border-bottom: 1px solid black;
    transition: .4s;
}

ul li:hover a{
    padding-left: 50px;
}

.sidebar ul a i{
    margin-right: 16px;
}

.welcome1{
    text-align: center;
    color: white; 
    background-color: #063146;
}

This is the html markup:

这是HTML标记:

<!doctype html>
<html lang="eng" dir="ltr">
    <head>
        <meta charset="utf-8">
        <title>side-menu bar</title>
        <link rel="stylesheet" href="style.css">
        <script src="https://kit.fontawesome.com/55138bdcbe.js" crossorigin="anonymous"></script>
    </head>
    <body>
        <img src="sidebar1.jpg" style="height: 100px; width : 250px" alt="">
        <div class="sidebar">
            <header>Traning-App Navigator</header>
            <ul>
                <li><a hrf="homepage.html"><i class="fas fa-home"></i>  Home</a></li>
                <li><a hrf="project.html"><i class="fas fa-brush"></i> Project</a></li>
                <li><a hrf="aboutus.html"><i class="far fa-question-circle"></i> About us</a></li>
                <li><a hrf="contact.html"><i class="fas fa-envelope"></i> Contact</a></li>
            </ul>
        </div>
        <div class="welcome1">  <p>
            here you will find everything you will need for your Training such as nutri&#235;nts, schematics, advice on training exercises and alike!
            so if you are ready to start your training journey... then let us guide you through it!
            </p></div>
    </body> 
</html>

But when I save it and try to use it the code just does not seem to do what it should. Do you have any solutions to this problem?

但是当我保存并尝试使用它时,代码似乎没有按预期工作。您有解决这个问题的办法吗?

英文:

I am building a website for a project at college but somehow I do not get the paragraph aligned in the centre on the top of the webpage.

I tried inline, but that did not work, then I tried CSS, but it still does not work the way I want it to:

body {
    font-family: &#39;Roboto&#39;, sans-serif;
    background-color: black;
    color: white;
}

*{
    margin: 0;
    padding: 0;
    list-style: none;
    text-decoration: none;
}

.sidebar {
    position: fixed;
    left: -250;
    width: 250px;
    height: 100%;
    background: #042331;
}

.sidebar header {
    font-size: 22px;
    color: white;
    text-align: center;
    line-height: 70px;
    background: #063146;
    user-select: none;
}

.sidebar ul a{
    display: block;
    height: 100%;
    width: 100%;
    line-height: 65px;
    font-size: 20px;
    color: white;
    padding-left: 40px;
    box-sizing: border-box;
    border-top: 1px solid rgba(255, 255, 255, .1);
    border-bottom: 1px solid black;
    transition: .4s;
}

ul li:hover a{
    padding-left: 50px;
}

.sidebar ul a i{
    margin-right: 16px;
}

.welcome1{
    text-align: center;
    color: white; 
    background-color: #063146;
}

This is the html markup:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.sidebar {
        position: fixed;
        left: -250;
        width: 250px;
        height: 100%;
        background: #042331;
    }
.sidebar header {
    font-size: 22px;
    color: white;
    text-align: center;
    line-height: 70px;
    background: #063146;
    user-select: none;
}
.sidebar ul a{
    display: block;
    height: 100%;
    width: 100%;
    line-height: 65px;
    font-size: 20px;
    color: white;
    padding-left: 40px;
    box-sizing: border-box;
    border-top: 1px solid rgba(255, 255, 255, .1);
    border-bottom: 1px solid black;
    transition: .4s;
}
ul li:hover a{
    padding-left: 50px;
}
.sidebar ul a i{
    margin-right: 16px;
}

<!-- language: lang-html -->

&lt;!doctype html&gt;
&lt;html lang=&quot;eng&quot; dir=&quot;ltr&quot;&gt;
    &lt;head&gt;
        &lt;meta charset=&quot;utf-8&quot;&gt;
        &lt;title&gt;side-menu bar&lt;/title&gt;
        &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
        &lt;script src=&quot;https://kit.fontawesome.com/55138bdcbe.js&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;img src=&quot;sidebar1.jpg&quot; style=&quot;height: 100px; width : 250px&quot; alt=&quot;&quot;&gt;
        &lt;div class=&quot;sidebar&quot;&gt;
            &lt;header&gt;Traning-App Navigator&lt;/header&gt;
            &lt;ul&gt;
                &lt;li&gt;&lt;a hrf=&quot;homepage.html&quot;&gt;&lt;i class=&quot;fas fa-home&quot;&gt;&lt;/i&gt;  Home&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a hrf=&quot;project.html&quot;&gt;&lt;i class=&quot;fas fa-brush&quot;&gt;&lt;/i&gt; Project&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a hrf=&quot;aboutus.html&quot;&gt;&lt;i class=&quot;far fa-question-circle&quot;&gt;&lt;/i&gt; About us&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a hrf=&quot;contact.html&quot;&gt;&lt;i class=&quot;fas fa-envelope&quot;&gt;&lt;/i&gt; Contact&lt;/a&gt;&lt;/li&gt;
                
            &lt;/ul&gt;
        &lt;/div&gt;
        &lt;div class=&quot;welcome1&quot;&gt;  &lt;p&gt;
            here you will find everything you will need for your Training such as nutri&#235;nts, schematics, advice on training exercises and alike!
            so if you are ready to start your training journey... then let us guide you through it!
            &lt;/p&gt;&lt;/div&gt;
    &lt;/body&gt; 
&lt;/html&gt;

<!-- end snippet -->

But when I save it and try to use it the code just does not seem to do what it should. Do you have any solutions to this problem?

答案1

得分: 1

Sure, here is the translated content:

&lt;strike&gt;If welcome1 is your paragraph you have 2 choices.
```css
display:flex;
justify-content:center;
align-items:center;

Or

display:grid;
place-items:center;

Without seeing the html, this is the best recommendation that can be given
</strike>

After review of HTML:
It can be noted that the major problem is that your side bar is set to fixed and this makes it no longer inline with the welcome div therefor the welcome div is returning to take up the whole space.
an idea fix is listed below

.sidebar{
position: fixed;
left: 0;
}

.welcome1{
width: 100%;
margin-left: 250px;
}

This minor change should give room for your div to scroll down and for text to be centered within that div and will take up 100% of the left over space post applying the margin-left.


<details>
<summary>英文:</summary>

&lt;strike&gt;If welcome1 is your paragraph you have 2 choices.
```css
display:flex;
justify-content:center;
align-items:center;

Or

display:grid;
place-items:center;

Without seeing the html, this is the best recommendation that can be given
</strike>

After review of HTML:
It can be noted that the major problem is that your side bar is set to fixed and this makes it no longer inline with the welcome div therefor the welcome div is returning to take up the whole space.
an idea fix is listed below

.sidebar{
position: fixed;
left: 0;
}

.welcome1{
width: 100%;
margin-left: 250px;
}

This minor change should give room for your div to scroll down and for text to be centered within that div and will take up 100% of the left over space post applying the margin-left.

答案2

得分: 0

I moved the <div> with the paragraph to the top of the code. That fixes the display order. I also added:

p {
  text-align: center;
}

If you don't want the text to take up the entire width of the page though, then you can use:

p {
  max-width: 500px;
  margin: auto;
  text-align: center;
}
英文:

I moved the &lt;div&gt; with the paragraph to the top of the code. That fixes the display order. I also added:

p {
  text-align: center;
}

I guess you meant that by centering.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.sidebar {
  position: fixed;
  left: -250;
  width: 250px;
  height: 100%;
  background: #042331;
}

.sidebar header {
  font-size: 22px;
  color: white;
  text-align: center;
  line-height: 70px;
  background: #063146;
  user-select: none;
}

.sidebar ul a {
  display: block;
  height: 100%;
  width: 100%;
  line-height: 65px;
  font-size: 20px;
  color: white;
  padding-left: 40px;
  box-sizing: border-box;
  border-top: 1px solid rgba(255, 255, 255, .1);
  border-bottom: 1px solid black;
  transition: .4s;
}

ul li:hover a {
  padding-left: 50px;
}

.sidebar ul a i {
  margin-right: 16px;
}

p {
  max-width: 500px;
  margin: auto;
  text-align: center;
}

<!-- language: lang-html -->

&lt;!doctype html&gt;
&lt;html lang=&quot;eng&quot; dir=&quot;ltr&quot;&gt;

&lt;head&gt;
  &lt;meta charset=&quot;utf-8&quot;&gt;
  &lt;title&gt;side-menu bar&lt;/title&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
  &lt;script src=&quot;https://kit.fontawesome.com/55138bdcbe.js&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;img src=&quot;sidebar1.jpg&quot; style=&quot;height: 100px; width : 250px&quot; alt=&quot;&quot;&gt;
  &lt;div class=&quot;welcome1&quot;&gt;
    &lt;p&gt;
      here you will find everything you will need for your Training such as nutri&#235;nts, schematics, advice on training exercises and alike! so if you are ready to start your training journey... then let us guide you through it!
    &lt;/p&gt;
  &lt;/div&gt;
  &lt;div class=&quot;sidebar&quot;&gt;
    &lt;header&gt;Traning-App Navigator&lt;/header&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a hrf=&quot;homepage.html&quot;&gt;&lt;i class=&quot;fas fa-home&quot;&gt;&lt;/i&gt;  Home&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a hrf=&quot;project.html&quot;&gt;&lt;i class=&quot;fas fa-brush&quot;&gt;&lt;/i&gt; Project&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a hrf=&quot;aboutus.html&quot;&gt;&lt;i class=&quot;far fa-question-circle&quot;&gt;&lt;/i&gt; About us&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a hrf=&quot;contact.html&quot;&gt;&lt;i class=&quot;fas fa-envelope&quot;&gt;&lt;/i&gt; Contact&lt;/a&gt;&lt;/li&gt;

    &lt;/ul&gt;
  &lt;/div&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

I you don't want the text to take up the entire width of the page though, then you can use:

    p {
      max-width: 500px; /* The maximal width for your text. 
      Don&#39;t use normal width, because if the viewport is smaller than the width you set, there will be overflow. */
      margin: auto;
      text-align: center;
    }

huangapple
  • 本文由 发表于 2023年6月29日 18:34:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580224.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定