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

  • 本文关键字:应用于 after before 属性 css
  • 更新时间 :
  • 英文 :


我正在尝试以箭头的形式创建一个步进器(主要使用角度/css)。让我们从这个代码笔开始。我现在要做的是将状态名称居中放在箭头中间

我的直接想法是围绕这些行做一些事情来应用文本对齐中心属性。

.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 属性应用于我的类,而不是它的 :before & :after?如果不可能,我应该如何更改我的代码以保持当前设计并使文本居中?

您可以为此使用 flexbox 属性。

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

代码笔

有多种方法可以获取它。尝试这样。这是一个快速解决方案,通过给你.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']
          }
  
}
.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;
}
<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>

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

看看我的代码笔:。

修复相对简单。您正在尝试更改外部元素的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%

最新更新