如何将文本的特定部分居中

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

How to center the specific part of a text

问题

Sure, here's the translated code portion you requested:

<div class="text-start">
    <p><strong>Email</strong><span class="value">example@example.com</span></p>
</div>

Please note that the code you provided is in HTML and includes some Bootstrap classes and custom CSS styles for formatting. If you have any specific questions or need further assistance with this code, please feel free to ask.

英文:

I'm currently learning bootstrap to design a website. And I'm now creating a profile page of a user. There will be fields (e. g. name, email etc.) and values against them (on one line, if the screen size allows). So I want the field to be at the left, and the value at the center, but I can't find out how to implement it using bootstrap classes and css, if needed. So here is the code:

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

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

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;script src=&quot;https://kit.fontawesome.com/2e9370a952.js&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js&quot; integrity=&quot;sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.min.js&quot;&gt;&lt;/script&gt;
    &lt;title&gt;Profile&lt;/title&gt;
    &lt;style&gt;
        body {
            background: linear-gradient(to right,  #7A96E9, #BFAEE0);
            text-align: center;
        }
        .profile-info {
            background: white;
            border-radius: 4px;
            box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.3);
        }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div class=&quot;container d-flex flex-column justify-content-center&quot; style=&quot;height: 100vh&quot;&gt;
        &lt;div class=&quot;row&quot;&gt;
        &lt;div class=&quot;col-lg-7 col-md-8 col-sm-10 mx-auto&quot;&gt;
            &lt;div class=&quot;profile-info p-4&quot;&gt;
                &lt;h1 class=&quot;mb-4&quot;&gt;Profile&lt;/h1&gt; 
                &lt;div class=&quot;text-start&quot;&gt;
                    &lt;p&gt;&lt;strong&gt;Email&lt;/strong&gt;&lt;span class=&quot;value&quot;&gt;example@example.com&lt;/span&gt;&lt;/p&gt;
                &lt;/div&gt;
               &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;    
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js&quot; integrity=&quot;sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

So here I want "Email" (in the tag "strong") to be at the left of the parent "profile-info" and the span (with class "value") to be centered in that parent.

答案1

得分: 1

将整个父元素的文本对齐方式设置为居中 (Text-Align),然后使用 position: absolutevalue 类的元素同时设置 left: 0right: 0,并将 strong 元素浮动到左侧 (float: left),如下所示:

.text-start p {
  text-align: center;
}

.text-start strong {
  float: left;
}

.value {
  position: absolute;
  left: 0;
  right: 0;
  margin: auto;
}

请注意,这是一段CSS代码,用于样式化HTML中的元素。

英文:

Text-Align the entire parent to center, and then position absolutely with the combination of left:0 and right:0 the value class, and float the strong to the left, Like so:

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

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

.text-start p {
  text-align: center
}

.text-start strong {
  float: left;
}

.value {
  position: absolute;
  left: 0;
  right: 0;
  margin: auto;
}

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

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  &lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC&quot; crossorigin=&quot;anonymous&quot;&gt;
  &lt;script src=&quot;https://kit.fontawesome.com/2e9370a952.js&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js&quot; integrity=&quot;sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.min.js&quot;&gt;&lt;/script&gt;
  &lt;title&gt;Profile&lt;/title&gt;
  &lt;style&gt;
    body {
      background: linear-gradient(to right, #7A96E9, #BFAEE0);
      text-align: center;
    }
    
    .profile-info {
      background: white;
      border-radius: 4px;
      box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.3);
    }
  &lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;div class=&quot;container d-flex flex-column justify-content-center&quot; style=&quot;height: 100vh&quot;&gt;
    &lt;div class=&quot;row&quot;&gt;
      &lt;div class=&quot;col-lg-7 col-md-8 col-sm-10 mx-auto&quot;&gt;
        &lt;div class=&quot;profile-info p-4&quot;&gt;
          &lt;h1 class=&quot;mb-4&quot;&gt;Profile&lt;/h1&gt;
          &lt;div class=&quot;text-start&quot;&gt;
            &lt;p&gt;&lt;strong&gt;Email&lt;/strong&gt;&lt;span class=&quot;value&quot;&gt;example@example.com&lt;/span&gt;&lt;/p&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js&quot; integrity=&quot;sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月6日 19:47:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188700.html
匿名

发表评论

匿名网友

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

确定