边框半径样式不直接应用于按钮,但通过特定类工作

  • 本文关键字:工作 按钮 样式 应用于 边框 css
  • 更新时间 :
  • 英文 :


在我的HTML中,表格单元格内有按钮。所有这些按钮都应该通过应用边框半径样式转换为圆形。在我的css中,我做了以下事情

button{
border-radius: 50%;
width: 100px;
height: 100px;
background-color: gray;
margin: 1px;
border: 4px solid black;
}

所有其他样式都应用于按钮,但不应用于边框半径。我能让它工作的唯一方法是为 HTML 表分配一个类,然后在 css 中使用它 - 像这样

.board button{
border-radius: 50%;
width: 100px;
height: 100px;
background-color: gray;
margin: 1px;
border: 4px solid black;
}

相关的 HTML 是

<div>
<table class='board'>
<tr>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
</tr>
</table>
</div>

完整的HTML和CSS如下

.HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Connect Four Game</title>
<link rel="stylesheet" href="practice.css">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
<h1>Welcome to Connect Four!</h1>
<h2>The object of this game is to connect four of your chips in a row!</h2>
<h3>it is your turn, please pick a column to drop your chip</h3>
</div>
<div class="">
<table class='board'>
<tr>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
</tr>
</table>
</div>
<script src="practice.js">
</script>
</body>
</html>

.css

h1, h2, h3{
text-align: center;
}
h1{
padding-top: 50px;
}
h3{
padding-bottom: 50px;
}
table{
border: none;
width: 30%;
margin-left: auto;
margin-right: auto;
}
button{
border-radius: 50%;
width: 100px;
height: 100px;
background-color: gray;
margin: 1px;
border: 4px solid black;
}

结果是

由于您发布了显示您正在使用 Bootstrap 的其他代码,我已经确定了您的问题。

引导程序使用重置代码

button {
border-radius: 0;
}

您有几个选择。

大锤方法

button {border-radius: 50%;!important}

这种方法虽然有效,但任何时候都可能产生不必要的副作用,只要你想覆盖,你还必须使用一个可以使维护成为PITA的!important(将来你会喜欢不使用!important)

在引导后加载自定义 CSS

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> 
<link rel="stylesheet" href="practice.css">

在您的"按钮"上使用自定义类我的偏好

该类可以应用于父项,就像在 OP 中所做的那样 或按钮本身,如下所示

使用类是使CSS世界运转的原因。不要害怕使用它们,它们是您的朋友:) 当你不使用类时,你说每个使用此样式表的按钮将始终具有这些规范。但是,当您稍后使用提交按钮添加表单并且不添加类时会发生什么 - 您将获得一个 100px 的圆形按钮

.btn-custom{
border-radius: 50%;
width: 100px;
height: 100px;
background-color: gray;
margin: 1px;
border: 4px solid black;
}
<div>
<table>
<tr>
<td><button type="button" class="btn-custom"></button></td>
<td><button type="button" class="btn-custom"></button></td>
<td><button type="button" class="btn-custom"></button></td>
<td><button type="button" class="btn-custom"></button></td>
<td><button type="button" class="btn-custom"></button></td>
<td><button type="button" class="btn-custom"></button></td>
<td><button type="button" class="btn-custom"></button></td>
</tr>
</table>
</div>

旁注:对于旧版浏览器的支持,您可能需要回退

-webkit-border-radius: 50%;
-moz-border-radius: 50%;
border-radius: 50%;

我认为这是一个问题,当您在按钮上应用边框半径时,它不会应用于按钮,因为没有按钮的父级 但是,当您使用父类名称应用边界半径时,它将对父类的相应值取 % 值 您可以通过将边框半径从 50% 转换为 50px 来亲自尝试。它将完美地工作。

您的引导程序样式之一是覆盖半径样式。在半径后添加"!important",它就可以工作了。

边框半径: 50% !重要

最新更新