gpt4 book ai didi

html - 如何使用 CSS flexbox 正确对齐按钮

转载 作者:太空宇宙 更新时间:2023-11-04 15:45:50 24 4
gpt4 key购买 nike

我知道之前有人在这里问过类似的问题,我确实读过它们,但无论我尝试什么,我似乎都做不好。抱歉,大约一周前开始研究 HTML/CSS,所以我是新手。

所以我有一个 flexbox 作为顶部导航栏,并试图在一行中均匀地隔开栏中的按钮。然而,无论我尝试什么,它都会将每个按钮堆叠在一起,导致每个按钮 1 行 --> 3 行。

代码如下,感谢您的帮助或建议!

* {
box-sizing: border-box;
}

body {
font-family: Arial;
padding: 10px;
background: #f1f1f1;
}

/* Header/Blog Title */
.header {
padding: 30px;
text-align: center;
background: white;
}

.header h1 {
font-size: 35px;
}

/* Style the top navigation bar */
.topnav {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row;
-moz-flex-direction: row;
flex: 2;
flex-direction: row;
justify-content: space-between;
align-items: stretch;
flex-wrap: wrap;
position: relative;
background-color: #333;
}

/* Style the topnav buttons */
.dropbtn {
background-color: #333;
color: white;
padding: 16px;
font-size: 16px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.0);
}

/*Dropdown content (hidden by default)*/
.dropdown-content {
display: none;
position: absolute;
background-color: #333;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content a {
color: white;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {background-color: inherit;}
.topnav:hover .dropdown-content {display: block;}
.topnav:hover .dropbtn {background-color: inherit;}
/* Change color on hover */
<body>
<div class="header">
<h1>Step 1: Header</h1>
<p>Subheading</p>
</div>

<div class="topnav">
<button class="dropbtn">Professional</button>
<div class="dropdown-content">
<a href="first.php" >Professional</a>
</div>
</div>

<div class="topnav">
<button class="dropbtn">Hobbies</button>
<div class="dropdown-content">
<a href="second.php" >Test</a>
</div>
</div>

<div class="topnav">
<button class="dropbtn">Tutorials</button>
<div class="dropdown-content">
<a href="third.php" >Test3</a>
</div>
</div>
</body>

最佳答案

这是你想要的吗?

基本上,我将所有 .topnav 包装在一个 divparent

父类有如下CSS

.parent {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row;
-moz-flex-direction: row;
flex: 2;
flex-direction: row;
justify-content: space-between;
align-items: stretch;
flex-wrap: wrap;
background-color: #333;
}

* {
box-sizing: border-box;
}

body {
font-family: Arial;
padding: 10px;
background: #f1f1f1;
}


/* Header/Blog Title */

.header {
padding: 30px;
text-align: center;
background: white;
}

.header h1 {
font-size: 35px;
}

.parent {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row;
-moz-flex-direction: row;
flex: 2;
flex-direction: row;
justify-content: space-between;
align-items: stretch;
flex-wrap: wrap;
background-color: #333;
}


/* Style the top navigation bar */

.topnav {
position: relative;
}


/* Style the topnav buttons */

.dropbtn {
background-color: #333;
color: white;
padding: 16px;
font-size: 16px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.0);
}


/*Dropdown content (hidden by default)*/

.dropdown-content {
display: none;
position: absolute;
background-color: #333;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}


/* Links inside the dropdown */

.dropdown-content a {
color: white;
padding: 12px 16px;
text-decoration: none;
display: block;
}

.dropdown-content a:hover {
background-color: inherit;
}

.topnav:hover .dropdown-content {
display: block;
}

.topnav:hover .dropbtn {
background-color: inherit;
}
<div class="header">
<h1>Step 1: Confucius</h1>
<p>He who copies the master, honours him</p>
</div>

<div class="parent">
<div class="topnav">
<button class="dropbtn">Professional</button>
<div class="dropdown-content">
<a href="first.php">Professional</a>
</div>
</div>

<div class="topnav">
<button class="dropbtn">Hobbies</button>
<div class="dropdown-content">
<a href="second.php">Test</a>
</div>
</div>

<div class="topnav">
<button class="dropbtn">Tutorials</button>
<div class="dropdown-content">
<a href="third.php">Test3</a>
</div>
</div>
</div>

关于html - 如何使用 CSS flexbox 正确对齐按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55989426/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com