无法使用最大宽度设置居中复杂内容

时间:2016-12-01 17:44:28

标签: html css

我的中心内容有max-width元素集我有问题。左右的间距不一样。 This is a pen to my project。问题似乎源于形式 side-post, span_1_of_3,因为右边的可变边距。

我试过

 .inner {
        display: table;
        margin: 0 auto;
    }

 .outer2{
    margin: 0 auto;
    display: table;
    text-align:center;
    }

我对这个问题的解决方案有点困惑。任何想法如何解决?

此图像显示了我面临的障碍:

enter image description here

3 个答案:

答案 0 :(得分:1)

删除宽度:15em;来自.side-post和间距将相等

答案 1 :(得分:0)

你可以使用display:flex on .outer2和justify-content:center和align-items:center,它应该集中你所有的内容



@charset "UTF-8";
/* CSS Document */

/* Base Styles */

body{
	margin: 0;
	padding: 0;
	font-size: 16px;
	line-height: 1.4em;
	color: #222;
	background: red;
	font-family: 'Lato', sans-serif;
}

img{
	max-width: 100%;
	height: auto;
	border-radius: 3px;
}

.outer2{
  justify-content:center;
  align-items:center;
  display: flex;

}

a{
	text-decoration: none;
	color: inherit;
}



/* Grid */

.section {
	clear: both;
	padding: 0;
	margin: 0;
}

.col {
	display: block;
	float:left;
	margin: 1% 0 1% 1.6%;
}
.col:first-child { margin-left: 0; }


.span_2_of_3 {
	width: 67.1%;
}
.span_1_of_3 {
	width: 31.2%;
}


/* Header & Navigation */





/* Blog Posts */

.container{
	max-width: 1420px;
	margin: 0 auto;
	padding: 20px;
}



.side-post{
	background: #fff;
	margin: 0 auto 1.5em auto;
	width: 15em;
	padding: 20px;
}



.side-content{
	font-size: .9em;
	margin-top: 8px;
	margin-bottom: 0;
}



.date{
	
	text-decoration-color: currentColor;
}



/* Button */

.button{
	display: block;
	padding: 15px 20px;
	margin-top: .8em;
	background: #009ACD;
	text-align: center;
	color: #fff;
	font-size: 1.4em;
	border-radius: 3px;
	transition: all .4s ease-in;
}

.button:hover{
	background: #005c7b;
}

/*Media Queries*/

@media only screen and (min-width: 1200px) {
.span_2_of_3 {
	width: 75.1%;
}
.span_1_of_3 {
	width: 17.2%;
}
}

/* @900px*/
@media only screen and (max-width: 56.25em) {
	.col{ margin: 1% 0 1% 0%;}
	.span_2_of_3, .span_1_of_3{ width: 100%; }
	.side-post{ width: 42%;
			   float: left;
			   margin: -2% 0 1% 1.6%;}
}

/* @685px*/
@media only screen and (max-width: 42.813em) {
	.side-post{ width: auto;
				padding: 15px;
			    margin: -2% auto 8% auto; }
	.date{ font-size: .9em; }		
}

/* @480px */

/* @380px*/
@media only screen and (max-width: 23.75em) {
	.side-content{ font-size: .8em; }
	.button{ padding: 10px 15px; }
}




/*CONTENT OF LEFT SIDE*/
@charset "UTF-8";
/* CSS Document */

/*PEN STYLES*/
@import url(https://fonts.googleapis.com/css?family=Open+Sans:400,700|Roboto:400,700);

.blog-card {
	transition: height 0.3s ease;
	-webkit-transition: height 0.3s ease;
	background: #fff;
	border-radius: 3px;
	box-shadow: 0 3px 7px -3px rgba(0, 0, 0, 0.3);
	margin: 0 auto 1.6%;
	overflow: hidden;
	position: relative;
	font-size: 14px;
	line-height: 1.45em;
	height:460px;
	-webkit-font-smoothing: antialiased;
	-moz-osx-font-smoothing: grayscale;
}


.blog-card:hover .details {
	left: 0;
}

.blog-card:hover.alt .details {
	right: 0;
}

.blog-card.alt .details {
	right: -100%;
	left: inherit;
}

.blog-card .photo {
	height: 200px;
	position: relative;
}

.blog-card .photo.photo1 {
	background: url("http://i62.tinypic.com/34oq4o0.jpg") center no-repeat;
	background-size: cover;
}

.blog-card .photo.photo2 {
	background: url("http://i60.tinypic.com/xeiv79.jpg") center no-repeat;
	background-size: cover;
}

.blog-card .details {
	transition: all 0.3s ease;
	-webkit-transition: all 0.3s ease;
	background: rgba(0, 0, 0, 0.6);
	box-sizing: border-box;
	color: #fff;
	font-family: "Open Sans";
	list-style: none;
	margin: 0;
	padding: 10px 15px;
	height: 300px;
    /*POSITION*/
	position: absolute;
	top: 0;
	left: -100%;
}

.blog-card .details > li {
	padding: 3px 0;
}

.blog-card .details li:before, .blog-card .details .tags ul:before {
	font-family: FontAwesome;
	margin-right: 10px;
	vertical-align: middle;
}

.blog-card .details .author:before {
	content: "\f007";
}

.blog-card .details .date:before {
	content: "\f133";
}

.blog-card .details .tags ul {
	list-style: none;
	margin: 0;
	padding: 0;
}

.blog-card .details .tags ul:before {
	content: "\f02b";
}

.blog-card .details .tags li {
	display: inline-block;
	margin-right: 3px;
}

.blog-card .details a {
	color: inherit;
	border-bottom: 1px dotted;
}

.blog-card .details a:hover {
	color: #75D13B;
}

.blog-card .description {
	padding: 10px;
	box-sizing: border-box;
	position: relative;
}

.blog-card .description h1 {
	font-family: "Roboto";
	line-height: 1em;
	margin: 0 0 10px 0;
}

.blog-card .description h2 {
	color: #9b9b9b;
	font-family: "Open Sans";
	line-height: 1.2em;
	text-transform: uppercase;
	font-size: 1em;
	font-weight: 400;
	margin: 1.2% 0;
}

.blog-card .description p {
	position: relative;
	margin: 0;
	padding-top: 20px;
}

.blog-card .description p:after {
	content: "";
	background: #75D13B;
	height: 6px;
	width: 40px;
        /*POSITION*/
	position: absolute;
	top: 6px;
	left: 0;
}

.blog-card .description a {
	color: #75D13B;
	margin-bottom: 10px;
	float: right;
}

.blog-card .description a:after {
	transition: all 0.3s ease;
	-webkit-transition: all 0.3s ease;
	content: "\f061";
	font-family: FontAwesome;
	margin-left: -10px;
	opacity: 0;
	vertical-align: middle;
}

.blog-card .description a:hover:after {
	margin-left: 5px;
	opacity: 1;
}

@media screen and (min-width: 700px) {
	.blog-card {
		height: 300px;
		max-width: 900px;
	}

	.blog-card:hover .photo {
		transform: rotate(5deg) scale(1.3);
	}

	.blog-card:hover.alt .photo {
		transform: rotate(-5deg) scale(1.3);
	}

	.blog-card.alt .details {
		padding-left: 30px;
	}

	.blog-card.alt .description {
		float: right;
	}

	.blog-card.alt .description:before {
		transform: skewX(5deg);
		right: -15px;
		left: inherit;
	}

	.blog-card.alt .photo {
		float: right;
	}

	.blog-card .photo {
		transition: all 0.5s ease;
		-webkit-transition: all 0.5s ease;
		float: left;
		height: 100%;
		width: 40%;
	}

	.blog-card .details {
		width: 40%;
	}

	.blog-card .description {
		float: left;
		width: 60%;
		z-index: 0;
	}

	.blog-card .description:before {
		transform: skewX(-5deg);
		content: "";
		background: #fff;
		width: 100%;
		z-index: -1;
        /*POSITION*/
		position: absolute;
		left: -15px;
		top: 0;
		bottom: 0;
	height:300px;
	}
}

<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>

	<link rel="stylesheet" href="css/blog.css">
	<link rel="stylesheet" href="css/blog2.css">

</head>

<body>
	<div class="outer2">
	<div class="container inner">
		  <div class="section">
			    <div class="col span_2_of_3">
      	
<!--LEFT SIDE-->
<div class="blog-card">
	<div class="photo photo1"></div>
	<ul class="details">
		<li class="author"><a href="#">John Doe</a></li>
		<li class="date">Aug. 24, 2015</li>
		<li class="tags">
			<ul>
				<li><a href="#">Learn</a></li>
				<li><a href="#">Code</a></li>
				<li><a href="#">HTML</a></li>
				<li><a href="#">CSS</a></li>
			</ul>
		</li>
	</ul>
	<div class="description">
		<h1>Learning to Code</h1>
		<h2>Opening a door to the future</h2>
		<p class="summary">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad eum dolorum architecto obcaecati enim dicta praesentium, quam nobis! Neque ad aliquam facilis numquam. Veritatis, sit. Loren ipsum h Holalala araba aranbd the good dog now. <br> <br> Loren ipsum h Holalala araba aranbd the good dog now.  Loren ipsum h Holalala araba aranbd the good dog now.  Loren ipsum h Holalala araba aranbd the good dog now.  Loren ipsum h Holalala araba aranbd the good dog now. </p>
		<a href="#">Read More</a>
	</div>
</div>
<div class="blog-card alt">
	<div class="photo photo2"></div>
	<ul class="details">
		<li class="author"><a href="#">Jane Doe</a></li>
		<li class="date">July. 15, 2015</li>
		<li class="tags">
			<ul>
				<li><a href="#">Learn</a></li>
				<li><a href="#">Code</a></li>
				<li><a href="#">JavaScript</a></li>
			</ul>
		</li>
	</ul>
	<div class="description">
		<h1>Mastering the Language</h1>
		<h2>Java is not the same as JavaScript</h2>
		<p class="summary">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad eum dolorum architecto obcaecati enim dicta praesentium, quam nobis! Neque ad aliquam facilis numquam. Veritatis, sit. Loren ipsum h Holalala araba aranbd the good dog now. <br> <br> Loren ipsum h Holalala araba aranbd the good dog now.  Loren ipsum h Holalala araba aranbd the good dog now.  Loren ipsum h Holalala araba aranbd the good dog now.  Loren ipsum h Holalala araba aranbd the good dog now. </p>
		<a href="#">Read More</a>
	</div>
</div>

			    </div>
				
			    <aside class="col span_1_of_3">
				      <div class="side-post">
					        <img src="http://placehold.it/800x400&text=img" alt="img">
            <p class="side-content">Ut noster tractavissent, summis hic eiusmod te quem. Doctrina velit litteris eu eu fore ingeniis philosophari ne quid o ingeniis ne anim, illum ea iudicem. Pariatur duis dolor hic dolor ad vidisse amet elit ita summis, quo duis te malis.</p>
				      </div>
				
				      <div class="side-post">
					        <p class="side-content">Ultricies lacus et, non orci gravida purus augue integer, sed turpis. Erat dapibus. 						Eleifend massa scelerisque, aenean penatibus laoreet ut in.</p>
        					<a href="#" class="button">Subscribe!</a>
				      </div>
			     </aside>
		  </div>
	</div>
</div>

</body>
</html>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

更改边帖的CSS。您的边距设置正确但side_post具有固定的宽度。它应该是100%。

旧CSS

.side-post {
    background: #fff;
    margin: 0 auto 1.5em auto;
    width: 15em;
    padding: 20px;
}

新CSS

.side-post {
    background: #fff;
    /* margin: 0 auto 1.5em auto; */
    width: 100%;
    padding: 20px;
    box-sizing: border-box;
}