html - div 不水平和垂直居中

标签 html css

我想让蓝绿色和绿色 div 位于红色 div 的中心。我试过 margin:0 自动、 float 、文本对齐,所有这些都不起作用。

turquoise div = header-logo-wrap
green div = header-text-wrap

如何使这些 div 水平和垂直居中?

非常感谢任何帮助,谢谢

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

body{
	background-color:#F6F7C1;	
	padding:0px;
	margin:0 auto; /* aligns content to touch the edge; gets rid of default spacing/margin between edge and content */
}

/* Header */
#header-wrap{
	background:#D6ECFF;
	width:100%;
	height:auto;
	border-bottom: 3px solid #CCC;
	/* margin:0 auto; needed? */
}
#header-top{
	/* contains logo & title text */
	background:#F00;
	width:960px;
	height:200px;
	margin:0 auto; /* aligns centrally */
}
.header-text-wrap{
	width:452px;
	height:auto;
	background:#0F0;
	text-align:justify;
	float:left;
}
.header-logo-wrap{
	width:100px;
	height:100px;
	background:#0CC;
	text-align:justify;
	float:left;
}
#header-bottom{
	/* contains navigation menu */
	background:#00F;
	width:960px;
	height:50px;
	margin:0 auto; /* aligns centrally */
	border-top: 3px solid #CCC;
}

/* Fonts */
header{
	font-family: Arial, sans-serif, tahoma, Arial, Cambria;
	-webkit-font-smoothing: antialiased; /* subtley makes fonts smoother */
	font-size:45px;
	text-transform:uppercase;
	line-height:40px;
}
slogan{
	font-family: courier new, tahoma, Arial, Cambria, serif;
	-webkit-font-smoothing: antialiased; /* subtley makes fonts smoother */
	font-size:20px;
	text-transform:uppercase;
	word-spacing:10px;
	letter-spacing:5px;
}
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="style.css"/>
<!-- Meta Tags Below -->
<meta name="description" content="" />
<meta name="keywords" content="" />
<title>___________________________</title>
<!-- Google Analytics Code Below -->
<!-- _____________________________ -->
</head>

<body>
<div id="header-wrap">

<div id="header-top">
<div class="header-logo-wrap"></div>
<div class="header-text-wrap">
<header>text goes here &amp; more here
</header>
<slogan>more text goes here</slogan>
</div>
</div>
<div id="header-bottom"></div>

</div>
</body>
</html>

最佳答案

使用flex定位。

@charset "UTF-8";

/* CSS Document */

body {
  background-color: #F6F7C1;
  padding: 0px;
  margin: 0 auto;
  /* aligns content to touch the edge; gets rid of default spacing/margin between edge and content */
}
/* Header */

#header-wrap {
  background: #D6ECFF;
  width: 100%;
  height: auto;
  border-bottom: 3px solid #CCC;
  /* margin:0 auto; needed? */
}
#header-top {
  /* contains logo & title text */
  background: #F00;
  width: 100%;
  height: 200px;
  margin: 0 auto;
  /* aligns centrally */
  display: flex;
  justify-content: center;
  align-items: center;
}
.header-text-wrap {
  width: 452px;
  height: auto;
  background: #0F0;
  text-align: justify;
  float: left;
}
.header-logo-wrap {
  width: 100px;
  height: 100px;
  background: #0CC;
  text-align: justify;
  float: left;
}
#header-bottom {
  /* contains navigation menu */
  background: #00F;
  width: 100%;
  height: 50px;
  margin: 0 auto;
  /* aligns centrally */
  border-top: 3px solid #CCC;
}
/* Fonts */

header {
  font-family: Arial, sans-serif, tahoma, Arial, Cambria;
  -webkit-font-smoothing: antialiased;
  /* subtley makes fonts smoother */
  font-size: 45px;
  text-transform: uppercase;
  line-height: 40px;
}
slogan {
  font-family: courier new, tahoma, Arial, Cambria, serif;
  -webkit-font-smoothing: antialiased;
  /* subtley makes fonts smoother */
  font-size: 20px;
  text-transform: uppercase;
  word-spacing: 10px;
  letter-spacing: 5px;
}
<div id="header-wrap">

  <div id="header-top">
    <div class="header-logo-wrap"></div>

    <div class="header-text-wrap">
      <header>text goes here &amp; more here
      </header>
      <slogan>more text goes here</slogan>
    </div>

  </div>
  <div id="header-bottom"></div>

关于html - div 不水平和垂直居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41435090/

相关文章:

javascript - NativeScript - 如何防止 CSS 缩小?

html - 样式 Bootstrap 事件选项卡

html - 如何使用 CSS 制作多选框,但标签显示在顶部而不是左侧?

html - 在 CSS 网格布局中使用 *ngFor 在一列中不希望显示所有内容

javascript - 简单的 HTML 表单添加两个数字

css - 字形-颜色变化问题

html - 根据元素的类设置标签标签样式它是标签 "for"

javascript - 当我遇到重新启动的障碍时,我将如何添加一个事件

javascript - 使用 JQuery 进行弹出窗口/对话框的最佳方式?

javascript - 本地存储未设置