【发布时间】:2021-10-23 04:45:06
【问题描述】:
如何在同一行的 1 个 div 内添加多个 div,它们显示在不同的行上。我只使用 HTML 和 CSS。
HTML(Div 在 Buttons 注释下):
<!DOCTYPE html>
<html>
<head>
<title>Cook Book</title>
<link rel="stylesheet" href="../CSS/StyleSheet.css">
</head>
<body>
<!-- Page Header -->
<header class="Header">
<div class="HeaderDiv">
<h1 class="HeaderText"> Welcome To Cook Book!</h1>
</div>
</header>
<!--Buttons-->
<div class="navbar">
<a href="Home.html" style="width: 20%">Home</a>
<div class="RecipesDropDown">
<button class="RecipesButton" style="width: 20%">Recipes</button>
<div class="DropDown-Content-Recipes">
<a href="ApplePie.html">Apple Pie</a>
<a href="BananaPie.html">Banana Cream Pie</a>
</div>
</div>
<div class="TermsDropDown">
<button class="TermsButton" style="width: 20%">Cooking Terms</button>
<div class="DropDown-Content-Terms">
<a href="Terms.html">Pie Cooking Terms</a>
</div>
</div>
<div class="DietDropDown">
<button class="DietButton" style="width: 20%">Cooking Diets</button>
<div class="DropDown-Content-Diet">
<a href="HealthyRecipes.html">Practical & Healthy Recipes</a>
<div class="WeeklyDropDown">
<button class="WeeklyButton" style="width: 20%">Recipes</button>
<div class="DropDown-Content-Weekly">
<a href="Week1.html">Week 1</a>
<a href="Week2.html">Week 2</a>
<a href="Week3.html">Week 3</a>
<a href="Week4.html">Week 4</a>
</div>
</div>
</div>
</div>
<a href="Contact.html" style="width: 20%">Contact Us</a>
</div>
<!-- Middle Text -->
<div class="MiddleDiv">
<p class="MiddleText"> <b>Your Favorite Foods, <br> Your Weight Loss Goals, <br> Your Kind Of Diet!</b></p>
<input type="email" class="EmailText" placeholder="Enter Your Email" size="35">
<button type="submit" class="EmailButton"> Subscribe </button>
</div>
<!--Bottom Watermark-->
<div>
<div class="BottomDiv">
<p> Delicious no-frill meals, all custom selected for your specific calorie goal, delivered to your inbox weekly! </p>
</div>
<div class="Watermark">
<p> © Souheil Tawil - 2021 </p>
</div>
</div>
</body>
</html>
CSS(Div 样式在第一个注释块下):
body {
background-image: url("../Images/bg.jpg");
background-repeat: no-repeat;
background-attachment: fixed;
background-size: 100% 100%;
margin: 0;
}
.Header{
background-image: url("../Images/woodbg.jpg");
background-repeat: no-repeat;
background-attachment: fixed;
background-size: 100% 100%;
}
.HeaderDiv{
color: white;
font-size: 25px;
text-align: center;
text-shadow:
-2px -2px 0 #000,
2px -2px 0 #000,
-2px 2px 0 #000,
2px 2px 0 #000;
}
.HeaderText{
margin-top: 0px;
margin-bottom: 0px;
padding: 30px;
}
.Watermark{
color: #000000;
background-color: white;
text-align: center;
position: absolute;
bottom: 0;
width: 100%;
}
.BottomDiv{
color: #ADD8E6;
background-color: red;
font-size: 19px;
text-align: center;
position: absolute;
width: 100%;
height: 100px;
bottom: 0;
}
.MiddleDiv{
text-align: center;
text-align: left;
}
.MiddleText{
margin-top: 330px;
margin-left: 150px;
font-size: 42px;
color: white;
text-shadow:
-2px -2px 0 #000,
2px -2px 0 #000,
-2px 2px 0 #000,
2px 2px 0 #000;
}
.EmailText{
margin-left: 150px;
line-height: 30px;
}
.EmailButton{
color: white;
background-color: red;
padding: 6px 30px;
}
/* ---------------------------- */
/* Drop Down Navigation Bar */
/* ---------------------------- */
.navbar {
overflow: hidden;
background-color: #668e45;
}
.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
/* ---------------------------- */
/* Drop Down Recipes Button */
/* ---------------------------- */
.RecipesDropDown {
overflow: hidden;
}
.RecipesDropDown .RecipesButton {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
margin: 0;
border: 2px solid #FFFFFF;
}
.navbar a:hover, .RecipesDropDown:hover .RecipesButton {
background-color: #03fc77;
}
.DropDown-Content-Recipes {
display: none;
position: absolute;
background-color: #03fc77;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.DropDown-Content-Recipes a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.DropDown-Content-Recipes a:hover {
background-color: #ddd;
}
.RecipesDropDown:hover .DropDown-Content-Recipes {
display: block;
}
/* ---------------------------- */
/* Drop Down Terms Button */
/* ---------------------------- */
.TermsDropDown {
overflow: hidden;
}
.TermsDropDown .TermsButton {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
margin: 0;
border: 2px solid #FFFFFF;
}
.navbar a:hover, .TermsDropDown:hover .TermsButton {
background-color: #03fc77;
}
.DropDown-Content-Terms {
display: none;
position: absolute;
background-color: #03fc77;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.DropDown-Content-Terms a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.DropDown-Content-Terms a:hover {
background-color: #ddd;
}
.TermsDropDown:hover .DropDown-Content-Terms {
display: block;
}
/* ---------------------------- */
/* Drop Down Diet Button */
/* ---------------------------- */
.DietDropDown {
overflow: hidden;
}
.DietDropDown .DietButton {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
margin: 0;
border: 2px solid #FFFFFF;
}
.navbar a:hover, .DietDropDown:hover .DietButton {
background-color: #03fc77;
}
.DropDown-Content-Diet {
display: none;
position: absolute;
background-color: #03fc77;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.DropDown-Content-Diet a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.DropDown-Content-Diet a:hover {
background-color: #ddd;
}
.DietDropDown:hover .DropDown-Content-Diet {
display: block;
}
/* ---------------------------- */
/* Drop Down Diet Button */
/* ---------------------------- */
.WeeklyDropDown {
overflow: hidden;
}
.WeeklyDropDown .WeeklyButton {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
margin: 0;
border: 2px solid #FFFFFF;
}
.navbar a:hover, .WeeklyDropDown:hover .WeeklyButton {
background-color: #03fc77;
}
.DropDown-Content-Weekly {
display: none;
position: absolute;
background-color: #03fc77;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.DropDown-Content-Weekly a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.DropDown-Content-Weekly a:hover {
background-color: #ddd;
}
.WeeklyDropDown:hover .DropDown-Content-Weekly {
display: block;
我在 .nav 中尝试了 flex 和 block inline,但没有成功,白色边框用于显示按钮大小。
编辑:添加后
.navbar {
overflow: hidden;
background-color: #668e45;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
并将宽度从 20% 修改为适合内容的按钮现在可以工作,边框围绕按钮文本而不是围绕整个绿色 div。如果有更好的解决方法,请告诉我。
【问题讨论】:
-
给父 div “display: flex”,对于内部 div-s 给他们“display: inline-block”。