【问题标题】:Using Bootstrap to position a span under a div使用 Bootstrap 将跨度定位在 div 下
【发布时间】:2019-10-29 16:26:21
【问题描述】:

我正在尝试在我正在处理的项目中实现更多的 Bootstrap 元素。我有一个圆形 div,下面有一些文字“转到主页”。我希望跨度在 div 下方居中并在一行上 - 我如何使用 Bootstrap 来完成此操作?

.avatar {
	margin: 10px;
	border-radius: 50%;
	height: 90px;
	text-align: center;
	width: 90px;
	float: right;
	border: 5px solid #fafafa;
  background-color: blue;
}

.initials {
	font-size: 2rem;/* 50% of parent */
	position: relative;
	top: 1.2rem; /* 25% of parent */
	color: #fafafa;
	font-weight: 700;
	}
  
  .update-homepage-link {
	float: right;
	color: #fafafa;
	font-weight: bold;
  color: pink;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="col-6 col-xl-2 order-xl-3">
				<div class="avatar">
					<span class="initials"> WW </span>
				<a class="update-homepage-link" href="">Go to Homepage</a>
			</div>

以前,我使用以下方法将其置于圆形 div 下方的中心,但我被要求在 Bootstrap 中完成该项目。我一直在尝试间距课程,但我还没有找到正确的组合。谁能给点建议?

【问题讨论】:

    标签: html css twitter-bootstrap position margin


    【解决方案1】:

    跟bootstrap无关,请看修改后的CSS。如果要使内容居中,请将完整的 div 包装到另一个具有 'text-center' 类的 div 中。

    .avatar {
    	margin: 10px;
    	border-radius: 50%;
    	height: 90px;
    	text-align: center;
    	width: 90px;
    	border: 5px solid #fafafa;
      background-color: blue;
    }
    
    .initials {
    	font-size: 2rem;/* 50% of parent */
    	position: relative;
    	top: 1.2rem; /* 25% of parent */
    	color: #fafafa;
    	font-weight: 700;
    	}
      
      .update-homepage-link {
    	color: #fafafa;
    	font-weight: bold;
      color: pink;
    }
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
    <div class="col-6 col-xl-2 order-xl-3">
    				<div class="avatar">
    					<span class="initials"> WW </span>
    			    </div>
                    <a class="update-homepage-link" href="">Go to Homepage</a>

    【讨论】:

      【解决方案2】:

      我重新创建了您的 sn-p,将解决方案放在旧 HTML 下。

      我使用了一个Bootstrap Card 组件来解决这个问题和一些其他的自定义类。

      .avatar {
        margin: 10px;
        border-radius: 50%;
        height: 90px;
        text-align: center;
        width: 90px;
        float: right;
        border: 5px solid #fafafa;
        background-color: blue;
      }
      
      .initials {
        font-size: 2rem;
        /* 50% of parent */
        position: relative;
        top: 1.2rem;
        /* 25% of parent */
        color: #fafafa;
        font-weight: 700;
      }
      
      .update-homepage-link {
        float: right;
        color: #fafafa;
        font-weight: bold;
        color: pink;
      }
      
      
      /* Classes for the solution */
      
      .card.avatar-card {
        border: none;
        border-radius: 0;
      }
      
      .card-img-top.avatar-bs {
        width: 90px;
        height: 90px;
        border-radius: 50%;
        border: 5px solid #fafafa;
        background-color: blue;
        justify-content: center;
        align-self: center;
      }
      
      .initials-bs {
        align-self: center;
        font-size: 2rem;
        color: #fafafa;
        font-weight: 700;
      }
      
      .update-homepage-link-bs {
        color: #fafafa;
        font-weight: bold;
        color: pink;
      }
      <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
      <div class="container">
        <div class="row">
          <div class="col-6 col-xl-2 order-xl-3">
            <div class="avatar">
              <span class="initials">WW</span>
              <a class="update-homepage-link" href="">Go to Homepage</a>
            </div>
          </div>
        </div>
        <!-- SOLUTION -->
        <div class="row">
          <div class="col-6 col-xl-2 order-xl-3">
            <div class="card avatar-card" style="width: 18rem">
              <div class="card-img-top avatar-bs d-flex">
                <span class="initials-bs d-flex"> WW </span>
              </div>
              <div class="card-body text-center">
                <a class="card-title update-homepage-link-bs" href="">Go to Homepage</a>
              </div>
            </div>
          </div>
        </div>
      </div>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-08
        • 1970-01-01
        • 2012-05-06
        相关资源
        最近更新 更多