将属性应用于类但不应用于:before和:after

时间:2017-10-10 08:56:45

标签: css css3

我试图以箭头的形式创建一个步进器(主要使用angular / css)。让我们从这个codepen开始吧。我现在要做的是将状态名称置于箭头的中间

我的直接想法是围绕这些行做一些事情来应用text-align center属性。

.block-head {
   /* rest of the class css */
   text-align: center;
}

.block-head:before {
   /* rest of the class css */
   text-align: left;
}

.block-head:after {
   /* rest of the class css */
   text-align: left;
}

问题是正在工作。我尝试将文本对齐属性设置为块文本类的中心,但它也不起作用...

如何将text-align:center属性应用于我的课程,而不是应用于以下课程:之前& :之后?如果不可能,我应该如何更改代码以保持当前设计并使文本居中?

4 个答案:

答案 0 :(得分:2)

您可以使用flexbox属性。

display: flex;justify-content: center添加到block-text

codepen

答案 1 :(得分:0)

有多种方法可以获得它。试试这样吧。通过提供.block-text margin-left:50%;

,这是一种快速解决方法



function ctrl($scope){
  
  // those variable are passed as param to the directive
  $scope.selectedKey = 2
  $scope.elements =
            [
              {
                name: "Status 1",
                key: 1,
                description: "description status 1"
              },
              {
                name: "Status 2",
                key: 2,
                description: "description status 2"
              },
              {
                name: "Status 3",
                key: 3,
                description: "description status 3"
              },
              {
                name: "Status 4",
                key: 4,
                description: "description status 4"
              }
            ]

            $scope.colors =
            {
                current: "lightblue",
                success: "lightgreen",
                disable: "lightgrey"
            }
  
  
  
  
   $scope.widthCalc = ($scope.elements.length - 1) * 26
   $scope.getColor = function($index) {
            if ($scope.elements[$index].key === $scope.selectedKey)
              return $scope.colors['current']
            if ($scope.elements[$index].key < $scope.selectedKey)
              return $scope.colors['success']
            if ($scope.elements[$index].key > $scope.selectedKey)
              return $scope.colors['disable']
          }
  
}
&#13;
.block-container {
	width: auto;
	padding-top: 4px;
	padding-right: 8px;
	padding-left: 8px;
  margin-bottom: -10px;
}

.block-head {
  background-color: #4D81BF;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  position: relative;
	width:22px;
  margin-right:4px;
	margin-bottom: -5px;
}

.block-text {
  color: black;
  font-size: 14px;
	margin-left: 50%;
}

.block-head:before {
  color: #FAFAFA;
  border-left: 10px solid;
  border-top: 10px solid transparent;
  border-bottom: 10px solid transparent;
  display: inline-block;
  content: '';
	z-index:1;
  position: absolute;
  top:0;
}

.block-head:after {
  color: inherit;
	z-index:2;
  border-left: 10px solid;
  border-top: 10px solid transparent;
  border-bottom: 10px solid transparent;
  display: inline-block;
  content: '';
  position: absolute;
  right: -10px;
  top:0;
}

.block-head-first {
  margin-left:0px !important;
}

.block-head-last {
  margin-right:0px;
}

.block-head-first:before {
  color: #FAFAFA;
  border-left: 0;
  border-top: 0;
  border-bottom: 0;
  display: inline-block;
  content: '';
	z-index:1;
  position: absolute;
  top:0;
}

.block-head-last:after {
  color: #4D81BF;
  border-left: 0;
  border-top: 0;
  border-bottom: 0;
  display: inline-block;
  content: '';
	z-index:1;
  position: absolute;
  top:0;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html ng-app>
<head>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.5/angular.min.js"></script>
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
<meta charset=utf-8 />
<title>Angular JS Demo</title>
</head>
<body ng-controller="ctrl">
  <div class="block-container">
  <div ng-repeat="element in elements"
  class="block-head"
  ng-class="{'block-head-first': $first, 'block-head-last': $last}"
  ng-style="{'width' : selectedKey === element.key ? 'calc(100% - ' + widthCalc + 'px)' : '',
             'background-color': getColor($index),
             'color': getColor($index)}">
    <span class="block-text" ng-if="selectedKey === element.key">{{element.name}}</span>
  </div>
</div>

</body>
</html>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

.block-head {
   text-align: center;
}

.block-head:before {
   left: 0px;
}

答案 3 :(得分:0)

查看我的codepen:

修复相对简单。 您试图更改外部元素的text-align,并将其更改为内部元素:

block-text {
  color: black;
  font-size: 14px;
  padding-left: 24px;
  display: inline-block;
  width: 100%;
  text-align: center;
}

我也从margin-left更改为padding-left以正确处理width: 100%

相关问题