【问题标题】:Can't center a container无法居中容器
【发布时间】:2020-12-06 20:16:56
【问题描述】:

我正在尝试使容器居中,并且正在使用 twitter bootstrap 4

这是html

<div class="container">
            <div class="features">
                <h3 class="text-center mb-5">So many ways to connect with all your friends</h1>
            <div class="row">
        <div class="col-sm-4">
            <div class="card" style="width: 18rem;">
  <img class="card-img-top" src="49824.php" alt="A simple MacBook Air with a glass of water next to it">
  <div class="card-body">
    <h5 class="card-title">Mac users are lucky!</h5>
    <p class="card-text">Our application designed for Mac users allows them to have a more stable version of our interface as well as a more pleasant design and unbeatable speed.<br>
    More secure, easier, and more efficient.</p>
    <div class="text-center mt-4">
    <a href="#" class="btn btn-primary">Download</a>
    </div>
</div>
</div>
        </div>
        
        <div class="col-sm-4">
            <div class="card" style="width: 18rem;">
  <img class="card-img-top" src="1114478.php" alt="A brown-haired woman smiling while being on the phone">
  <div class="card-body">
    <h5 class="card-title">Broadcom Wi-Fi</h5>
    <p class="card-text">With our technology 'Broadcom Wi-Fi', calling your friends has never been so easy before!<br>You can call them outside of your house, even when there's no Wi-Fi around you.<br><br></p>
    <div class="text-center mt-4">
    <a href="#" class="btn btn-primary">Try</a>
    </div>
</div>
</div>
        </div>
        <div class="col-sm-4">
            <div class="card" style="width: 18rem;">
  <img class="card-img-top" src="59843.php" alt="A group of friends discussing in a classroom">
  <div class="card-body">
    <h5 class="card-title">Keep contact with your friends.</h5>
    <p class="card-text">A good reason for using Messenger is that you can stay in contact with anyone you meet in life.<br>Add your friends by adding their unique IDs.</p>
    <div class="text-center mt-4">
    <a href="#" class="btn btn-primary">Get an ID</a>
    </div>
</div>
</div>
        </div>
                </div>
                </div>
                </div>
        </div>

这是 css 的预览(我将发布我的所有样式表,因为我不知道它是如何工作的):

@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap');

* {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

h1,
h2,
h3,
h4,
h5,
h6 {
    font-weight:700 !important;
}

p {
    font-weight:300;
}

body {
    background:#fff !important;
    font-family:'Roboto', sans-serif !important;
}

select {
    padding:0 30px 0 10px !important;
    -webkit-padding-end: 30px !important;
    -webkit-padding-start: 10px !important;
}

span.navbar-text {
    font-size:9pt;
    color:rgba(0,0,0,.55);
    font-weight:400;
}

p.desc {
    color:rgba(0,0,0,.55);
}

.dropdown-menu {
    background:#fff !important;
    border-radius:0 !important;
    font-weight:300 !important;
    color:rgba(0,0,0,.55) !important;
    max-width:50%;
    position:absolute;
}

a.dropdown-item {
    font-weight:300 !important;
    color:rgba(0,0,0,.55) !important;
}

.navbar {
    border-bottom:1px solid rgba(0,0,0,0.04);
    background:#fff;
}

.jumbotron {
    background:rgba(255,255,255,0);
    color:#000;
    margin:0 auto;
    border-radius:0;
}

.px-11 {
    font-size:11pt;
}

.pd-20 {
    padding-top:20px;
}

img.brand {
    transition:transform 0.5s ease;
}

img.brand:hover {
    transform: scale(1.06);
}

ul.navbar-nav {
    margin-left:10px;
}

a {
    letter-spacing:0.1px;
    font-size:11pt;
    font-weight:300;
    color: rgba(0, 0, 0, .55);
}

li.active > a {
    font-weight:500;
}

li {
    padding-right:10px;
}

a:hover {
    color:#000;
    text-decoration:underline;
}

.brand-text {
    font-size:11pt;
    font-weight:300;
    color:#000;
    position:relative;
}

h1 {
    color:#000;
}

.form-control {
    border-radius:6px;
    background:#fff;
    color:rgba(0,0,0,.55);
    font-size:10pt;
    font-weight:300;
    border:1px solid rgba(0,0,0,0.04);
    width:50%;
    letter-spacing:-0.1px;
    padding:20px 15px;
}

.form-control:focus {
  border:1px solid rgba(0,0,0,0.08);
  box-shadow:none;
}

nav ul li a,
nav ul li a:after,
nav ul li a:before {
  transition: all 0.2s ease;
}

label {
    font-weight:300;
    font-size:11pt;
}

.btn {
    background:#0078FF;
    color:#fff;
    font-size:10pt;
    border-radius:3px;
    font-weight:500;
}

.btn:hover {
    color:#fff;
}

.smart-scroll{
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  z-index: 1030;
}
.scrolled-down{
   transform:translateY(-100%); transition: all 0.3s ease-in-out;
}
.scrolled-up{
   transform:translateY(0); transition: all 0.3s ease-in-out;
}

.card-title {
    font-size:16pt !important;
    font-weight:700 !important;
}

.card-text {
    font-size:9pt;
    font-weight:400;
    color:rgba(0,0,0,.55);
}

.card {
    border-radius:10px;
    border:0;
    box-shadow:0px 0px 20px rgba(0,0,0,.07) !important;
}

.features {
    margin-top:75px !important;
    margin-bottom:50px;
}

下面是我的问题的预览:

【问题讨论】:

    标签: html css twitter-bootstrap containers center


    【解决方案1】:

    从有类卡片的div中移除宽度

    <div class="card" style="width: 18rem;">
    

    到:

    <div class="card">
    

    为你的 col div 或你的 card div 使用 padding 和 margin => https://getbootstrap.com/docs/4.1/utilities/spacing/

    【讨论】:

    • 哦,是的!这行得通,非常感谢。你知道有没有办法让我的所有页面居中?我对它的另一部分有这个问题。 preview
    • 我猜你有一个这样的 div preview 你可以使用“float-right”并调整你的宽度 getbootstrap.com/docs/4.1/utilities/float
    • Ça ne risque pas de justement tout mettre sur la droite de la page?
    • 不行,你要好好利用,你要在我之前给的预览中定位div @AndrzejJesztar
    【解决方案2】:

    在这种情况下,只需使用

    <center>
    your code you want to center
    </center>
    

    95% 的时间都在工作

    【讨论】:

    • 好的,成功了,谢谢。但是,是否有另一种方法可以通过使用 CSS 来解决这个问题?因为我不喜欢这种类型的代码...如果没有其他可能,我将使用它作为替代方式。
    • 不,只是不。 &lt;center&gt; 元素已为 obsolete for decades,不应使用。改用 CSS
    • @j08691 是的,我就是这么想的。
    猜你喜欢
    • 2013-10-02
    • 2021-03-03
    • 1970-01-01
    • 2012-03-03
    • 2022-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多