【发布时间】: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>
实际上只是 </body> 之后和我的结束 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