如何使用html和css对齐按钮?

时间:2017-01-26 10:21:26

标签: html css

我写了一个包含一些按钮的小html页面。我想做的是让它们看起来像这样:

enter image description here 我在css上一点也不擅长,我尝试了一些组合,但我得到的是将它们放在蓝线下面#34;像这样:

enter image description here 这是我的HTML代码:

  <div class="module form-module">
    <div class="flex-container">
      <article class="article">
        <table>
          <tr>
            <td>
              <button>Configurations</button>
            </td>
            <td>
              <button>Create User </button>
            </td>
            <td>
              <button>Update User</button>
            </td>
            <td>
              <button>Create Group</button>
            </td>
            <td>
              <button>Update Group</button>
            </td>
          </tr>
        </table>
      </article>
    </div>
  </div>

你可以在那个plunker中找到我的css代码:https://plnkr.co/edit/sCcBBFfWRiCwGz8hs8oR?p=catalogue

你能帮我解决一下html中的这个小问题吗?

2 个答案:

答案 0 :(得分:0)

CSS更改:

.form-module {
    border-bottom: 5px solid #33b5e5;
    position: relative;
    background: #ffffff;

    width: 100%;
    box-shadow: 0 0 3px rgba(0, 0, 0, 0.1);
    margin: 0 auto;
    align: right;
}

.flex-container > * {
    padding: 15px;
    -webkit-flex: 1 100%;
    flex: 1 100%;
}

table {
  border-spacing:0px;
}

td {
  padding:0px;
}

更改已过期

蓝线是边框,设置在按钮的父容器上。当你希望它在底部时,它被设置为顶部,因此这是第一次改变。

然后你有填充,它将按钮与容器的边缘分开,这是第二次更改,将其设置为0px。

最后,表格和每个按钮都有一个1px的边框,可以将它与容器的边缘分开,第三个更改是将这些边框设置为0px。

小建议:

如果您不了解:使用浏览器检查器更好地了解CSS的用途真的很有帮助。另外,如果你不想从零开始制作所有东西,我建议你看看Bootstrap,这很容易,可能会节省你很多时间。

祝你好运。

如果它有用,请点击完整的CSS

body {
  background: #e9e9e9;
  color: #666666;
  font-family: 'RobotoDraft', 'Roboto', sans-serif;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

table {
  border-spacing:0px;
}

td {
  padding:0px;
}

/* Pen Title */
.pen-title {
  padding: 50px 0;
  text-align: center;
  letter-spacing: 2px;
}
.pen-title h1 {
  margin: 0 0 20px;
  font-size: 48px;
  font-weight: 300;
}
.pen-title span {
  font-size: 12px;
}
.pen-title span .fa {
  color: #33b5e5;
}
.pen-title span a {
  color: #33b5e5;
  font-weight: 600;
  text-decoration: none;
}



/* Form Module */
.form-module {
  position: relative;
  background: #ffffff;

  width: 100%;
  border-bottom: 5px solid #33b5e5;
  box-shadow: 0 0 3px rgba(0, 0, 0, 0.1);
  margin: 0 auto;
  align: right;
}
.form-module .toggle {
  cursor: pointer;
  position: absolute;
  top: -0;
  right: -0;
  background: #33b5e5;
  width: 30px;
  height: 30px;
  margin: -5px 0 0;
  color: #ffffff;
  font-size: 12px;
  line-height: 30px;
  text-align: center;
}
.form-module .toggle .tooltip {
  position: absolute;
  top: 5px;
  right: -65px;
  display: block;
  background: rgba(0, 0, 0, 0.6);
  width: auto;
  padding: 5px;
  font-size: 10px;
  line-height: 1;
  text-transform: uppercase;
}
.form-module .toggle .tooltip:before {
  content: '';
  position: absolute;
  top: 5px;
  left: -5px;
  display: block;
  border-top: 5px solid transparent;
  border-bottom: 5px solid transparent;
  border-right: 5px solid rgba(0, 0, 0, 0.6);
}
.form-module .form {
  display: none;
  padding: 40px;
}
.form-module .form:nth-child(2) {
  display: block;
}
.form-module h2 {
  margin: 0 0 20px;
  color: #33b5e5;
  font-size: 18px;
  font-weight: 400;
  line-height: 1;
}

.form-module table {
  width: 100%
}





.form-module input {
  outline: none;

  width: 80%;
  border: 1px solid #d9d9d9;

    padding: 10px 15px;


  font-weight: 400;
  -webkit-transition: 0.3s ease;
  transition: 0.3s ease;
}
.form-module input:focus {
  border: 1px solid #33b5e5;
  color: #333333;
}
.form-module button {
  cursor: pointer;
  background: #33b5e5;
  width: 90%;

  border: 0;
  padding: 10px 15px;
  color: #ffffff;
  -webkit-transition: 0.3s ease;
  transition: 0.3s ease;
}
.form-module button:hover {
  background: #178ab4;
}

.form-module select {
  cursor: pointer;

  width: 85%;
  height:80%;


  padding: 10px 15px;

  -webkit-transition: 0.3s ease;
  transition: 0.3s ease;
}

.flag button {
  background: white;
  color: #178ab4;
}

.flag button:hover {
  background: white;
  border: 20px;
  border-color: #178ab4;
}

.flag  {
  cursor: pointer;
  background: white;
  width: 100%;
  border: 0;
  padding: 10px 15px;
  border-color: #178ab4;
  -webkit-transition: 0.3s ease;
  transition: 0.3s ease;

}

.flag a {
  cursor: pointer;
  background: white;
  width: 100%;
  border: 0;
  color: #33b5e5;
 border-color: #178ab4;
  -webkit-transition: 0.3s ease;
  transition: 0.3s ease;

}

.flag a :hover {
 background: white;

}

.form-module .cta {
  background: #f2f2f2;
  width: 100%;
  padding: 15px 40px;
  box-sizing: border-box;
  color: #666666;
  font-size: 12px;
  text-align: center;
}
.form-module .cta a {
  color: #333333;
  text-decoration: none;
}



.flex-container {
    display: -webkit-flex;
    display: flex;  
    -webkit-flex-flow: row wrap;
    flex-flow: row wrap;
    align: right;
}

.flex-container > * {
    padding: 0px;
    -webkit-flex: 1 100%;
    flex: 1 100%;
}


.article {
    text-align: left;
}

.detail button {
    width: 120px
}

.detail table {
       border-collapse: separate;   
      border-spacing: 5px 10px;
      width: 100%
}


.search table {

      border-collapse: separate;    
      border-spacing: 5px 10px;
      width: 70%
}

.search button {
    width: 120px
}

.search input {
  outline: none;

  width: 90%;
  border: 1px solid #d9d9d9;

    padding: 10px 15px;


  font-weight: 400;
  -webkit-transition: 0.3s ease;
  transition: 0.3s ease;
}

.search select {
  cursor: pointer;

  width: 90%;
  padding: 10px 15px;

  -webkit-transition: 0.3s ease;
  transition: 0.3s ease;
}

.resultSearch table {

      border-collapse: separate;    
      border-spacing: 5px 10px;
      width: 80%
}



.dropbtn {
      outline: none;
    border:1;
   width: 80%;
  border: 1px solid #d9d9d9;

    padding: 10px 15px;

    font-size: 16px;
     cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
 }
#myInput {
    border-box: box-sizing;
    background-image: url('searchicon.png');
    background-position: 14px 12px;
    background-repeat: no-repeat;
    font-size: 16px;

      outline: none;

   width: 100%;
  border: 1px solid #d9d9d9;

    padding: 10px 15px;

}

.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f6f6f6;
    min-width: 230px;
    overflow: auto;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown a:hover {background-color: #ddd}

.show {display:block;}

#searchbox
 {
    width: 35px;
 }  
@media all and (min-width: 768px) {
    .nav {text-align:left;-webkit-flex: 1 auto;flex:1 auto;-webkit-order:1;order:1;}
    .article {-webkit-flex:5 0px;flex:5 0px;-webkit-order:2;order:2;}
    footer {-webkit-order:3;order:3;}
}


.newUser button {
    width: 120px

}

.newUser table {
      border-collapse: separate;    
      border-spacing: 5px 10px;
      width: 90%
}

.return table{
     border-collapse: separate;     
      border-spacing: 5px 10px;
      width: 90%;


}

.returnCheckBox input {

    align: left;
      width:0%;

}
.invoiceListTable table {
       border-collapse: separate;   
      border-spacing: 5px 10px;
      width: 90%
}

.sessionInfo  table {
       border-collapse: separate;   
      border-spacing: 5px 10px;
      width: 100%;
        align: right;

}

.sessionInfo {
    display: -webkit-flex;
    display: flex;  
    -webkit-flex-flow: row wrap;
    flex-flow: row wrap;
    align: right;
}

.sessionInfo > * {
    padding: 15px;
    -webkit-flex: 1 100%;
    flex: 1 100%;
      align: right;

}


.sessionInfo {
  position: relative;
  background: #ffffff;

  width: 20%;
  border-top: 5px solid #33b5e5;
  box-shadow: 0 0 3px rgba(0, 0, 0, 0.1);
  margin: 0 auto;
  align: right;
}
.sessionInfo .toggle {
  cursor: pointer;
  position: absolute;
  top: -0;
  right: -0;
  background: #33b5e5;
  width: 30px;
  height: 30px;
  margin: -5px 0 0;
  color: #ffffff;
  font-size: 12px;
  line-height: 30px;
  text-align: center;
}
.sessionInfo .toggle .tooltip {
  position: absolute;
  top: 5px;
  right: -65px;
  display: block;
  background: rgba(0, 0, 0, 0.6);
  width: auto;
  padding: 5px;
  font-size: 10px;
  line-height: 1;
  text-transform: uppercase;
}
.sessionInfo .toggle .tooltip:before {
  content: '';
  position: absolute;
  top: 5px;
  left: -5px;
  display: block;
  border-top: 5px solid transparent;
  border-bottom: 5px solid transparent;
  border-right: 5px solid rgba(0, 0, 0, 0.6);
}
.form-module .form {
  display: none;
  padding: 40px;
  align:right; 
}

<强>更新 你提到按钮应该在表单模块之外和之上,所以html需要改变而不仅仅是css。我们删除了嵌套在form-module div中的flex-container div,并在关闭这个容器后放置它。由于按钮是从.form-module的样式获得布局属性,因此有必要创建一个新类&#34;按钮&#34;。基本上现在表单模块和按钮位于不同的容器中,并具有独立的样式属性。

要了解容器块的工作原理: http://www.w3schools.com/html/html_blocks.asp

<强> HTML:

<div class="flex-container buttons">
      <article class="article">
        <table>
          <tr>
            <td>
              <button>Configurations</button>
            </td>
            <td>
              <button>Create User </button>
            </td>
            <td>
              <button>Update User</button>
            </td>
            <td>
              <button>Create Group</button>
            </td>
            <td>
              <button>Update Group</button>
            </td>
          </tr>
        </table>
      </article>
    </div>
 <div class="module form-module">
 </div>

更改了css:

body {
  background: #e9e9e9;
  color: #666666;
  font-family: 'RobotoDraft', 'Roboto', sans-serif;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.buttons table {
  width: 100%;
  border-spacing:0px;
}

.buttons td {
  padding:0px;
}

.buttons input {
  outline: none;

  width: 80%;
  border: 1px solid #d9d9d9;

    padding: 10px 15px;


  font-weight: 400;
  -webkit-transition: 0.3s ease;
  transition: 0.3s ease;
}
.buttons input:focus {
  border: 1px solid #33b5e5;
  color: #333333;
}
.buttons button {
  cursor: pointer;
  background: #33b5e5;
  width: 90%;

  border: 0;
  padding: 10px 15px;
  color: #ffffff;
  -webkit-transition: 0.3s ease;
  transition: 0.3s ease;
}
.buttons button:hover {
  background: #178ab4;
}

.buttons select {
  cursor: pointer;

  width: 85%;
  height:80%;


  padding: 10px 15px;

  -webkit-transition: 0.3s ease;
  transition: 0.3s ease;
}

完成css:

body {
  background: #e9e9e9;
  color: #666666;
  font-family: 'RobotoDraft', 'Roboto', sans-serif;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.buttons table {
  width: 100%;
  border-spacing:0px;
}

.buttons td {
  padding:0px;
}

.buttons input {
  outline: none;

  width: 80%;
  border: 1px solid #d9d9d9;

    padding: 10px 15px;


  font-weight: 400;
  -webkit-transition: 0.3s ease;
  transition: 0.3s ease;
}
.buttons input:focus {
  border: 1px solid #33b5e5;
  color: #333333;
}
.buttons button {
  cursor: pointer;
  background: #33b5e5;
  width: 90%;

  border: 0;
  padding: 10px 15px;
  color: #ffffff;
  -webkit-transition: 0.3s ease;
  transition: 0.3s ease;
}
.buttons button:hover {
  background: #178ab4;
}

.buttons select {
  cursor: pointer;

  width: 85%;
  height:80%;


  padding: 10px 15px;

  -webkit-transition: 0.3s ease;
  transition: 0.3s ease;
}

/* Pen Title */
.pen-title {
  padding: 50px 0;
  text-align: center;
  letter-spacing: 2px;
}
.pen-title h1 {
  margin: 0 0 20px;
  font-size: 48px;
  font-weight: 300;
}
.pen-title span {
  font-size: 12px;
}
.pen-title span .fa {
  color: #33b5e5;
}
.pen-title span a {
  color: #33b5e5;
  font-weight: 600;
  text-decoration: none;
}



/* Form Module */
.form-module {
  position: relative;
  background: #ffffff;

  width: 100%;
  border-top: 5px solid #33b5e5;
  box-shadow: 0 0 3px rgba(0, 0, 0, 0.1);
  margin: 0 auto;
  align: right;
}

.form-module .toggle {
  cursor: pointer;
  position: absolute;
  top: -0;
  right: -0;
  background: #33b5e5;
  width: 30px;
  height: 30px;
  margin: -5px 0 0;
  color: #ffffff;
  font-size: 12px;
  line-height: 30px;
  text-align: center;
}
.form-module .toggle .tooltip {
  position: absolute;
  top: 5px;
  right: -65px;
  display: block;
  background: rgba(0, 0, 0, 0.6);
  width: auto;
  padding: 5px;
  font-size: 10px;
  line-height: 1;
  text-transform: uppercase;
}
.form-module .toggle .tooltip:before {
  content: '';
  position: absolute;
  top: 5px;
  left: -5px;
  display: block;
  border-top: 5px solid transparent;
  border-bottom: 5px solid transparent;
  border-right: 5px solid rgba(0, 0, 0, 0.6);
}
.form-module .form {
  display: none;
  padding: 40px;
}
.form-module .form:nth-child(2) {
  display: block;
}
.form-module h2 {
  margin: 0 0 20px;
  color: #33b5e5;
  font-size: 18px;
  font-weight: 400;
  line-height: 1;
}

.form-module table {
  width: 100%
}





.form-module input {
  outline: none;

  width: 80%;
  border: 1px solid #d9d9d9;

    padding: 10px 15px;


  font-weight: 400;
  -webkit-transition: 0.3s ease;
  transition: 0.3s ease;
}
.form-module input:focus {
  border: 1px solid #33b5e5;
  color: #333333;
}
.form-module button {
  cursor: pointer;
  background: #33b5e5;
  width: 90%;

  border: 0;
  padding: 10px 15px;
  color: #ffffff;
  -webkit-transition: 0.3s ease;
  transition: 0.3s ease;
}
.form-module button:hover {
  background: #178ab4;
}

.form-module select {
  cursor: pointer;

  width: 85%;
  height:80%;


  padding: 10px 15px;

  -webkit-transition: 0.3s ease;
  transition: 0.3s ease;
}

.flag button {
  background: white;
  color: #178ab4;
}

.flag button:hover {
  background: white;
  border: 20px;
  border-color: #178ab4;
}

.flag  {
  cursor: pointer;
  background: white;
  width: 100%;
  border: 0;
  padding: 10px 15px;
  border-color: #178ab4;
  -webkit-transition: 0.3s ease;
  transition: 0.3s ease;

}

.flag a {
  cursor: pointer;
  background: white;
  width: 100%;
  border: 0;
  color: #33b5e5;
 border-color: #178ab4;
  -webkit-transition: 0.3s ease;
  transition: 0.3s ease;

}

.flag a :hover {
 background: white;

}

.form-module .cta {
  background: #f2f2f2;
  width: 100%;
  padding: 15px 40px;
  box-sizing: border-box;
  color: #666666;
  font-size: 12px;
  text-align: center;
}
.form-module .cta a {
  color: #333333;
  text-decoration: none;
}



.flex-container {
    display: -webkit-flex;
    display: flex;  
    -webkit-flex-flow: row wrap;
    flex-flow: row wrap;
    align: right;
}

.flex-container > * {
    padding: 0px;
    -webkit-flex: 1 100%;
    flex: 1 100%;
}


.dropbtn {
      outline: none;
    border:1;
   width: 80%;
  border: 1px solid #d9d9d9;

    padding: 10px 15px;

    font-size: 16px;
     cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
 }


.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f6f6f6;
    min-width: 230px;
    overflow: auto;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown a:hover {background-color: #ddd}

.show {display:block;}

ul{
  list-style-type:none;
  width:500px;
}

li{
  width:33%;
  text-align:center;
  float:left;
  border-bottom:2px solid #33b5e5;
}

答案 1 :(得分:0)

也许不是表格&#39;使用&#39;列表&#39;像ul或li的html元素。

plunker

新HTML:

<div class="module form-module">
    <div class="flex-container">
      <article class="article">
        <ul>
            <li>
              <button>Configurations</button>
            </li>
            <li>
              <button>Create User </button>
            </li>
            <li>
              <button>Update User</button>
            </li>
         </ul>
      </article>
    </div>
  </div>

并添加以下CSS:

ul{
  list-style-type:none;
  width:500px;
}

li{
  width:33%;
  text-align:center;
  float:left;
  border-bottom:2px solid #33b5e5;
}