为什么text-align属性在按钮元素中不起作用?

时间:2018-06-28 10:50:48

标签: html css button text-align

button {
    text-align: center;
    font-size:50px;
    color:black;
    border:2px solid white;
    border-style:dotted;
    border-color:red;
}

当我运行此代码时,除文本对齐方式外,所有属性均有效。我不明白为什么按钮内的文本对齐方式不起作用?

注意:是的,我知道有几种方法可以使按钮在中心对齐,我不是在问如何对齐按钮,而是在为什么text-align不能像其他所有属性一样在按钮元素内工作在工作中?

3 个答案:

答案 0 :(得分:1)

它不能像这样工作。将text-align:center属性分配给按钮本身不会使其居中对齐。您应该将此属性分配给它的父级。或者您可以为按钮分配宽度,然后添加margin: 0 auto;作为替代方式。

这是您的问题的解决方案。 添加此CSS

.btnwrap {
  text-align: center;
}

然后,用<p>

包裹您的按钮
<p class="btnwrap">
  <button>button 2</button>
</p>

答案 1 :(得分:0)

尝试自动添加保证金

Function Get-NewVHDPath {
    [cmdletbinding()]
    Param(
        [Parameter(Position = 0, ValueFromPipelineByPropertyName,
            HelpMessage = "Please enter the name of the VM to create.")]
        [string]$VMName,

        [switch]$Passthru)
    $Hypervisors = "Server1", "Server2", "Server3"
    Log "The hypervisors to check are: $Hypervisors"
    Log "Now querying the servers to determine how much space each has..."
    $Space = Try {
        Get-WmiObject Win32_LogicalDisk -ComputerName $Hypervisors | Where {$_.DeviceID -ne "C:"} | Select-Object PSComputerName, DeviceID,
        @{Name = "NSize"; Expression = {[math]::Round(($_.Size / 1GB), 2)}},
        @{Name = "NFreespace"; Expression = {[math]::Round(($_.Freespace / 1GB), 2)}}
    }
    Catch {
        Return $_
    }
    If ($Space) {
        Log "Query succeeded!"
        Log "Now calculating space on $Hypervisor..."
        # Determine the variables for the max drive size, the disk letter, and the server for the new VM
        $Max = ($Space | Measure-Object -Property NFreespace -Maximum).Maximum
        $LocalDrive = ($Space | Where {$_.NFreeSpace -eq $Max}).DeviceID
        $RemoteDrive = $LocalDrive.Replace(":", "$")
        $Server = ($Space | Where {$_.NFreeSpace -eq $Max}).PSComputerName
        $RemotePath = "\\$Server\$RemoteDrive\Virtual Machines"
        $RemoteVHDPath = "$Path\$VMName.vhdx"
        $LocalVHDPath = "$LocalDrive\Virtual Machines\$VMName.vhdx"

        # Output a custom object
        $Properties = @{'VMName' = $VMName;
            'Server' = $Server;
            'RemotePath' = $RemotePath;
            'LocalVHDPath' = $LocalVHDPath;
            'RemoteVHDPath' = $RemoteVHDPath;
            'FreeSpace' = $Max
        }

        $Object = New-Object -TypeName psobject -Property $Properties | Select-Object Server, VMName, RemotePath, LocalVHDPath, RemoteVHDPath, FreeSpace
        Write-Output $Object
    }
    Else {
        Write-Output $Space
        Log "Cannot determine free space on the Hypervisors. Exiting..."
        Break
    }
}

或将text-align: center; margin: auto; 赋予!important,也许还有另一个影响您的按钮的CSS代码

答案 2 :(得分:0)