【问题标题】:Centering DIV, but with other CSS That won't allow it居中 DIV,但与其他 CSS 不允许它
【发布时间】:2017-08-20 06:21:45
【问题描述】:

我想在<body> 中居中一个 div,但我就是不知道怎么做。我已经尝试过 flexboxes 和旧的 margin: 0 auto; 技巧。这是我的代码:

        <link href="https://fonts.googleapis.com/css?family=Comfortaa|Cutive+Mono|Oswald" rel="stylesheet">
    <link href="../css/ham.css" rel="stylesheet">
    <link rel="stylesheet" href="../css/main.css" type="text/css" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script
      src="https://code.jquery.com/jquery-3.2.1.min.js"
      integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
      crossorigin="anonymous"></script>
  <script type="text/javascript">

  //Unselctable Function

    $.fn.extend({ 
        disableSelection: function() { 
            this.each(function() { 
                if (typeof this.onselectstart != 'undefined') {
                    this.onselectstart = function() { return false; };
                } else if (typeof this.style.MozUserSelect != 'undefined') {
                    this.style.MozUserSelect = 'none';
                } else {
                    this.onmousedown = function() { return false; };
                }
            }); 
        } 
    });

    $(document).ready(function() {

        //NAVBAR HIDE

        $('nav').hide();

        //UNSELECTABLE

        $('.unselectable').disableSelection();

        //HAMBURGERS

        var $hamburger = $(".hamburger");
        $hamburger.on("click", function(e) {
        $hamburger.toggleClass("is-active");
        $('nav').slideToggle();
    });
    });
  </script>
</head>
<body>
    <nav>
        <ul>
            <li><a href="../">home</a></li>
            <li><a href="../work">work</a></li>
            <li><a href="../contact">contact</a></li>
        </ul>
    </nav>
    <button class="hamburger hamburger--spin" type="button">
        <span class="hamburger-box">
            <span class="hamburger-inner"></span>
        </span>
    </button>
    <div>center this</div>

实际上只是 &lt;/body&gt; 之后和我的结束 html - 之后没有脚本。

这是我的 CSS:

/* FONTS

font-family: 'Comfortaa', cursive;
font-family: 'Oswald', sans-serif;
font-family: 'Cutive Mono', monospace;

*/

/*RESET*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}

a {
    text-decoration: none;
    color: white;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}
body {
    line-height: 1;
}
ol, ul {
    list-style: none;
}
blockquote, q {
    quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
    content: '';
    content: none;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}

/*STYLES*/

.unselectable {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

body {
    background-color: black;
    background-image: url(../images/bg.jpg);
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-position: center; 
}

ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    width: inherit;
    background-color: #212121;
}

li {
    float: left;
    color: #fff;
    cursor: pointer;
}

li a {
    display: block;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    font-family: 'Comfortaa', cursive;
    /*transition*/
    -webkit-transition: color 0.2s; /* Safari */
    transition: color 0.2s;
}

li a:hover {
    color: #BDBDBD;
}

.hamburger {
    position: absolute;
}

button:focus {
    outline: none;
}

@media only screen and (max-width: 768px) {
    /* For mobile phones: */
    * {
        width: 100%;
    }

    li a {
        padding: 0;
        padding-top: 14px;
        text-align: center;
    }

    li:last-child {
        padding-bottom: 14px;
    }

}

.content {
     margin: 0 auto;
     width: 100px;
}

^ .content 是我想要在页面中垂直和水平居中的内容。 我应该说,我也在使用https://jonsuh.com/hamburgers/ - 顺便说一句,这是一个很棒的库。

对不起,如果这个问题之前已经回答过,但我在任何地方都找不到。

谢谢 阿利斯泰尔

【问题讨论】:

  • 你试过body { display: flex; align-items: center; justify-content: center; }吗?请注意,您的 CSS 定义了 .content 的规则,尽管您的 HTML 中没有该类的元素。

标签: javascript jquery html css flexbox


【解决方案1】:

您可以通过 css 将任何 div 居中,给它一个固定的宽度和边距(标准方式)。

.any{
  width:300px;/*  use your required wid and height */
  height:300px;
  background:red; /*  Just for example */
  margin :0px auto;
}

你甚至可以看看这个垃圾箱: https://jsbin.com/rewayuwoso/edit?html,css,output

【讨论】:

  • 这将始终保持 div 居中我希望它给你一些想法:)
  • 谢谢,但我需要 div 具有响应性和流畅性,所以我不确定尺寸,虽然我很确定 100% 宽度
  • 在指定在这种情况下对您有帮助的尺寸时使用 %
  • 这是使 div 居中的标准方法,您可以完美地对齐居中的任何 div
  • 这适用于您使用的每个 div...这取决于您如何使用它们
【解决方案2】:

您已经忽略了将内容类放在您的 div html 中。

应该是&lt;div class="content"&gt;center this&lt;/div&gt;

那么它将居中。希望这会有所帮助。

但是,使用 margin:0 auto 只会将其水平居中。

【讨论】:

  • 单独使用 auto 或使用百分比,然后 auto 使其水平和垂直居中,例如45% 自动(50 可能太多了)
  • jsfiddle.net/c27ncc93 这是 36% 自动(透明背景)的样子
猜你喜欢
  • 1970-01-01
  • 2014-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多