如何在Bootstrap表中垂直居中放置元素

时间:2020-03-01 18:34:18

标签: html css twitter-bootstrap bootstrap-4

我已经看过这个question并应用了样式,但是我表中的项目没有垂直对齐:

enter image description here

HTML看起来像这样:

.table > tbody > tr > td {
        vertical-align: center;
        background-color: lightgreen;
    }
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

<div class="table-responsive">  
      <table class="table table-sm table-hover">
          <thead>
              <tr class="d-flex">
                  <th class="col-2">Item 01</th>
                  <th class="col-2">Item 02</th>
                  <th class="text-center col-4">Buttons</th>
                  <th class="text-right col-2">Item 04</th>
              </tr>
          </thead>
          <tbody>
              <tr class="d-flex">
                  <td class="col-2">
                      <div style="background-image: url(//placehold.it/1026x600/CC1111/FFF);width: 80px;
                          height: 80px;
                          border-radius: 100%;
                          background-size: cover;">
                      </div>
                  </td>
                  <td class="col-2 text-center">Hey!</td>
                  <th class="col-4">
                      <div class="row no-gutters">
                          <div class="col-2">
                              <button class="btn btn-secondary btn-block">
                                  -
                              </button>
                          </div>
                          <div class="col-8">
                              7 in cart
                          </div>
                          <div class="col-2">
                            <button class="btn btn-secondary btn-block">
                                +
                            </button>
                          </div>
                      </div>
                  </th>
                  <th class="col-2 text-right">Item 04</th>
              </tr>
          </tbody>
      </table>
    </div>

我无法理解为什么在应用background-color时它没有垂直对齐。你能说一下,物品如何垂直对齐吗?

任何帮助将不胜感激。

This is a codeply example.

2 个答案:

答案 0 :(得分:1)

// Based on: https://www.electronics-lab.com/project/esp32-webserver-tutorial/

#include <WiFi.h>
// Replace with your network credentials 
const char* ssid = "MySSID"; 
const char* password = "MYPASSWRD"; 
// Set web server port number to 80 
WiFiServer server(80); 
// Variable to store the HTTP request String header;
String header;

// Declare the Serial2 pins & vars
#define RXD2  16
#define TXD2  17
char GrnXmt = 'U';  // char sent when top button
char RedXmt = 'A';  // char sent when bottom button
String Mychar = " ";   // The loop back character

// Declare the pins to which the LEDs are connected 
int greenled = 26;
int redled   = 27;   
String greenstate = "queued";// state of green LED
String redstate = "queued";// state of red LED

void setup() {
  Serial.begin(115200);
  Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2);
 // Set the pinmode of the pins to which the LEDs are connected and turn them low to prevent flunctuations
  pinMode(greenled, OUTPUT);
  pinMode(redled, OUTPUT);
  digitalWrite(greenled, LOW);
  digitalWrite(redled, LOW);
  //connect to access point
  WiFi.begin(ssid, password);
  Serial.print("Connecting to ");
  Serial.println(ssid);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());// this will display the Ip address which should be entered into your browser
  server.begin();
}

void loop(){
  WiFiClient client = server.available();   // Listen for incoming clients
  if (client) {                             // If a new client connects,
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();

            // turns the GPIOs sent and queued
            if (header.indexOf("GET /green/sent") >= 0) {
              Serial.println("green sent");
              greenstate = "sent";
              digitalWrite(greenled, HIGH);

            Serial.print("@ green Serial2.available() == ");
            Serial.println(Serial2.available());

              Serial2.write(GrnXmt);
            } else if (header.indexOf("GET /green/queued") >= 0) {
              Serial.println("green queued");
              greenstate = "queued";
              digitalWrite(greenled, LOW);
              Mychar = ' ';
            } else if (header.indexOf("GET /red/sent") >= 0) {
              Serial.println("red sent");
              redstate = "sent";
              digitalWrite(redled, HIGH);

            Serial.print("@ red Serial2.available() == ");
            Serial.println(Serial2.available());   

              Serial2.write(RedXmt);
            } else if (header.indexOf("GET /red/queued") >= 0) {
              Serial.println("red queued");
              redstate = "queued";
              digitalWrite(redled, LOW);
              Mychar = ' ';
            }

            Serial.print("Serial2.available() == ");
            Serial.println(Serial2.available());

 //              if(Serial2.available()){
               size_t len = Serial2.available();
               uint8_t sbuf[len];
               delay(100);
               Mychar = Serial2.read();
               Serial.print(" Mychar == ");
               Serial.println(Mychar);
//               }


            // Display the HTML web page
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            // CSS to style the on/queued buttons 
            // Feel free to change the background-color and font-size attributes to fit your preferences
            client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
            client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
            client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
            client.println(".button2 {background-color: #77878A;}</style></head>");

            // Web Page Heading
            client.println("<body><h1>My Web Server</h1>");

            // Display current state, and ON/OFF buttons for GPIO 26  
            client.println("<p>Send 'U' to Serial2 port - State -> " + greenstate + "</p>");
            // If the green LED is queued, it displays the ON button       
            if (greenstate == "queued") {
              client.println("<p><a href=\"/green/sent\"><button class=\"button\">SEND</button></a></p>");
            } else {
              client.println("<p><a href=\"/green/queued\"><button class=\"button button2\">RESET</button></a></p>");
            } 

            // Display current state, and ON/OFF buttons for GPIO 27 
            client.println("<p>Send 'A' to Serial2 port - State -> " + redstate + "</p>");
            // If the red LED is queued, it displays the ON button       
            if (redstate == "queued") {
              client.println("<p><a href=\"/red/sent\"><button class=\"button\">SEND</button></a></p>");
            } else {
              client.println("<p><a href=\"/red/queued\"><button class=\"button button2\">RESET</button></a></p>");
            }
            client.println("</body></html>");

            // Print out the responce
            client.println(); // blank line - astectically pleasing
            client.println("<p>You sent a ->  " + Mychar + "</p>");                

            // The HTTP response ends with another blank line
            client.println();
            // Break out of the while loop
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
  }
}

应该是(请注意添加到flex div中的.align-items-center类):

 <tbody>
          <tr class="d-flex">
              <td class="col-2">
                  <div style="background-image: url(//placehold.it/1026x600/CC1111/FFF);width: 80px;
                      height: 80px;
                      border-radius: 100%;
                      background-size: cover;">
                  </div>
              </td>

答案 1 :(得分:1)

我已经检查了您的HTML表格代码,并在浏览器中对其进行了检查,对您的th,td类进行了一些更改,还为样式表中的列元素编写了一些CSS,以使它们的列元素能够正确地垂直对齐

<tr class="d-flex">
                        <td class="col-2">
                            <div style="
                                background-image: url(//placehold.it/1026x600/CC1111/FFF);
                                width: 80px;
                                height: 80px;
                                border-radius: 100%;
                                background-size: cover;
                                ">
                            </div>
                        </td>
                        <td class="col-3 text-center coltd">Hey!</td>
                        <th class="col-4 colth">
                            <div class="row no-gutters">
                                <div class="col-2">
                                    <button class="btn btn-secondary btn-block">
                                        -
                                    </button>
                                </div>
                                <div class="col-8">
                                    7 in cart
                                </div>
                                <div class="col-2">
                                  <button class="btn btn-secondary btn-block">
                                      +
                                  </button>
                                </div>
                            </div>
                        </th>
                        <th class="col-2 text-right coltd">Item 04</th>
                    </tr>

您可以在样式表中添加此CSS代码

.colth {
    padding: 30px 0 !important;
    text-align: left !important;
}
.coltd {
    padding: 30px 0 !important;
    text-align: left !important;
}

您可以在代码中进行这些更改,这将对您非常有帮助 谢谢