css - 用 div 包裹后图像无法居中?网页格式

标签 css html center

我需要让图片水平居中,应该很简单,我用margin:auto就可以了。它有效,正如您在下面看到的那样。

<html xmlns="http://www.w3.org/1999/xhtml">


<head>


<!-- Latest compiled and minified CSS -->
<link rel="stylesheet"
	href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"
	integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7"
	crossorigin="anonymous">

<style>
.center_image {
	margin: auto;
	margin-left: auto;
	margin-right: auto;
}
</style>


<style>
</style>

</head>
<body>

	<!-- carousel section -->
	<div class="container carosel_section" id="carosel_section">

		<div id="myCarousel" class="carousel slide">
			<!-- Indicators -->
			<ol class="carousel-indicators">
				<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
				<li data-target="#myCarousel" data-slide-to="1"></li>
				<li data-target="#myCarousel" data-slide-to="2"></li>
			</ol>

			<!-- Wrapper for slides -->
			<div class="carousel-inner" role="listbox">
				<div class="item active" style="height: 100%;">

					<img style="height: 100%"
						class="img-thumbnail img-responsive center_image"
						src="https://s-media-cache-ak0.pinimg.com/236x/05/46/63/0546638b58d2d396f97ad69177f104fa.jpg">



				</div>

			</div>


		</div>
	</div>


</body>
</html>

但是当我用 div 包装图像时,它没有放在中心。正如你在这里看到的。

<html xmlns="http://www.w3.org/1999/xhtml">


<head>


<!-- Latest compiled and minified CSS -->
<link rel="stylesheet"
	href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"
	integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7"
	crossorigin="anonymous">

<style>
.center_image {
	margin: auto;
	margin-left: auto;
	margin-right: auto;
}
</style>


<style>
</style>

</head>
<body>

	<!-- carousel section -->
	<div class="container carosel_section" id="carosel_section">

		<div id="myCarousel" class="carousel slide">
			<!-- Indicators -->
			<ol class="carousel-indicators">
				<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
				<li data-target="#myCarousel" data-slide-to="1"></li>
				<li data-target="#myCarousel" data-slide-to="2"></li>
			</ol>

			<!-- Wrapper for slides -->
			<div class="carousel-inner" role="listbox">
				<div class="item active" style="height: 100%;">
					<div class="carousel_item_grow_image"
						style="height: 100%; width: 100%; background-color: blue;">
						<img style="height: 100%"
							class="img-thumbnail img-responsive center_image"
							src="https://s-media-cache-ak0.pinimg.com/236x/05/46/63/0546638b58d2d396f97ad69177f104fa.jpg">

					</div>


				</div>

			</div>


		</div>
	</div>


</body>
</html>

最佳答案

.img-thumbnail 类中删除 display:inline-block; 或将其更改为 display:block;

.img-thumbnail {
  display:block;
}

关于css - 用 div 包裹后图像无法居中?网页格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37225823/

相关文章:

CSS DIV高度背景图片重叠

css - 这段 HTML 代码有什么问题?

javascript - Bootstrap 3 允许列溢出容器

python - html 表单中多个同名输入

javascript - 单击时, Accordion 标题会刷新页面

html - 如何防止文本在居中和范围 slider 值发生变化时移动?

html - 如何水平居中对齐内容并固定在左侧?

html - Chrome 中模糊的缩小图像

html - 将引用 SVG 图像嵌入 HTML 文件

html - 如何在一个 div 中垂直居中多个内联 block div?