如何使用 Css 对齐特定位置的 div 内容?
How to align div content in specific position using Css?
body {
margin: 0;
font - family: Arial,
Helvetica,
sans - serif;
}
.topnav {
overflow: hidden;
background - color: #333;
}
.topnav a {
float: left;
display: block;
color: # f2f2f2;
text - align: center;
padding: 14 px 16 px;
text - decoration: none;
font - size: 17 px;
}
.topnav a: hover {
background - color: #ddd;
color: black;
}
.topnav a.active {
background - color: #04aa6d;
color: white;
}
.topnav .icon {
display: none;
}
@media screen and (max-width: 600px) {
.topnav a:not(:first-child) {
display: none;
}
.topnav a.icon {
float: right;
display: block;
}
}
@media screen and (max-width: 600px) {
.topnav.responsive {
position: relative;
}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
}
/* select dropdown css code */
select {
-webkit-appearance: none;
-moz-appearance: none;
-ms-appearance: none;
appearance: none;
outline: 0;
box-shadow: none;
border: 0 !important;
background: # 5 c6664;
background - image: none;
flex: 1;
padding: 0 0.5e m;
color: #fff;
cursor: pointer;
font - size: 1e m;
font - family: "Open Sans", sans - serif;
}
select::-ms - expand {
display: none;
}
.select {
position: relative;
display: flex;
width: 20e m;
height: 3e m;
line - height: 3;
background: #5c6664;
overflow: hidden;
border-radius: 0.25em;
}
.select::after {
content: "BC";
position: absolute;
top: 0;
right: 0;
padding: 0 1em;
background: # 2 b2e2e;
cursor: pointer;
pointer - events: none;
transition: 0.25 s all ease;
}
.select: hover::after {
color: #23b499;
}
<div class="topnav" id="myTopnav">
<a href="#home" class="active">Red FMTuning</a>
<a href="#news" class="aligntune">Choose a tune from ur favourite</a>
<a href="#contact">
<div class="select">
<select name="format" id="format">
<option selected>Choose a book format</option>
<option value="pdf">PDF</option>
<option value="txt">txt</option>
<option value="epub">ePub</option>
<option value="fb2">fb2</option>
<option value="mobi">mobi</option>
</select>
</div>
</a>
<a href="#about" class="alignout">About</a>
<a href="javascript:void(0);" class="icon" @click="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
如上图,你可以看到,所有元素,如“调音”、“从你最喜欢的曲目中选择一首曲子”、“选择一种书籍格式”、“关于”。所有内容都不是形状正确。主要是下拉菜单(“选择书籍格式”)未根据其他内容对齐。当单击下拉菜单时也如此。它与内容重叠。
我需要对齐所有元素,直到达到如下所示的 1200 像素。
勾选this fiddle。对于这种情况,您可以使用 flexbox
。将这些属性添加到您的 topnav
:
display: flex;
align-items: center;
justify-content: space-evenly;
align-items
用于垂直对齐,justify-content
用于space等分。如果将 display
设置为 flex
.
,则只能使用这两个属性
更多关于 flexbox
here。
我包装了“选择书籍格式”、“关于”<div class="topnav-inner-wrap">
并在 .topnav
和 .topnav-inner-wrap
上应用了 flexbox css
工作演示: https://jsfiddle.net/k2beLnrv/
function myFunction(){
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
.topnav {
overflow: hidden;
background-color: #333;
display: flex;
align-items: center;
justify-content: space-between;
}
.topnav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #04aa6d;
color: white;
}
.topnav .icon {
display: none;
}
@media screen and (max-width: 600px) {
.topnav > a:not(:first-child) {
display: none;
}
.topnav a.icon {
float: right;
display: block;
}
}
@media screen and (max-width: 600px) {
.topnav.responsive {
position: relative;
display: block;
}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a,
.topnav.responsive .topnav-inner-wrap {
float: none;
display: block;
text-align: left;
}
}
@media screen and (max-width: 1200px) {
.topnav > a:not(:first-child), .topnav .topnav-inner-wrap {
display: none;
}
.topnav a.icon {
float: right;
display: block;
}
}
@media screen and (max-width: 1200px) {
.topnav.responsive {
position: relative;
display: block;
}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a,
.topnav.responsive .topnav-inner-wrap {
float: none;
display: block !important;
text-align: left;
}
}
/* select dropdown css code */
select {
-webkit-appearance: none;
-moz-appearance: none;
-ms-appearance: none;
appearance: none;
outline: 0;
box-shadow: none;
border: 0 !important;
background: #5c6664;
background-image: none;
flex: 1;
padding: 0 0.5em;
color: #fff;
cursor: pointer;
font-size: 1em;
font-family: "Open Sans", sans-serif;
}
select::-ms-expand {
display: none;
}
.select {
position: relative;
display: flex;
width: 20em;
height: 3em;
line-height: 3;
background: #5c6664;
overflow: hidden;
border-radius: 0.25em;
}
.select::after {
content: "BC";
position: absolute;
top: 0;
right: 0;
padding: 0 1em;
background: #2b2e2e;
cursor: pointer;
pointer-events: none;
transition: 0.25s all ease;
}
.select:hover::after {
color: #23b499;
}
.topnav-inner-wrap {
display: flex;
align-items: center;
}
<div class="topnav" id="myTopnav">
<a href="#home" class="active">Red FMTuning</a>
<a href="#news" class="aligntune">Choose a tune from ur favourite</a>
<div class="topnav-inner-wrap">
<a href="#contact">
<div class="select">
<select name="format" id="format">
<option selected>Choose a book format</option>
<option value="pdf">PDF</option>
<option value="txt">txt</option>
<option value="epub">ePub</option>
<option value="fb2">fb2</option>
<option value="mobi">mobi</option>
</select>
</div></a
>
<a href="#about" class="alignout">About</a>
</div>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
在父class
中使用justify-content:space-between;
body {
margin: 0;
font - family: Arial,
Helvetica,
sans - serif;
}
.topnav {
overflow: hidden;
background - color: #333;
}
.topnav a {
float: left;
display: block;
color: # f2f2f2;
text - align: center;
padding: 14 px 16 px;
text - decoration: none;
font - size: 17 px;
}
.topnav a: hover {
background - color: #ddd;
color: black;
}
.topnav a.active {
background - color: #04aa6d;
color: white;
}
.topnav .icon {
display: none;
}
@media screen and (max-width: 600px) {
.topnav a:not(:first-child) {
display: none;
}
.topnav a.icon {
float: right;
display: block;
}
}
@media screen and (max-width: 600px) {
.topnav.responsive {
position: relative;
}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
}
/* select dropdown css code */
select {
-webkit-appearance: none;
-moz-appearance: none;
-ms-appearance: none;
appearance: none;
outline: 0;
box-shadow: none;
border: 0 !important;
background: # 5 c6664;
background - image: none;
flex: 1;
padding: 0 0.5e m;
color: #fff;
cursor: pointer;
font - size: 1e m;
font - family: "Open Sans", sans - serif;
}
select::-ms - expand {
display: none;
}
.select {
position: relative;
display: flex;
width: 20e m;
height: 3e m;
line - height: 3;
background: #5c6664;
overflow: hidden;
border-radius: 0.25em;
}
.select::after {
content: "BC";
position: absolute;
top: 0;
right: 0;
padding: 0 1em;
background: # 2 b2e2e;
cursor: pointer;
pointer - events: none;
transition: 0.25 s all ease;
}
.select: hover::after {
color: #23b499;
}
<div class="topnav" id="myTopnav">
<a href="#home" class="active">Red FMTuning</a>
<a href="#news" class="aligntune">Choose a tune from ur favourite</a>
<a href="#contact">
<div class="select">
<select name="format" id="format">
<option selected>Choose a book format</option>
<option value="pdf">PDF</option>
<option value="txt">txt</option>
<option value="epub">ePub</option>
<option value="fb2">fb2</option>
<option value="mobi">mobi</option>
</select>
</div>
</a>
<a href="#about" class="alignout">About</a>
<a href="javascript:void(0);" class="icon" @click="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
如上图,你可以看到,所有元素,如“调音”、“从你最喜欢的曲目中选择一首曲子”、“选择一种书籍格式”、“关于”。所有内容都不是形状正确。主要是下拉菜单(“选择书籍格式”)未根据其他内容对齐。当单击下拉菜单时也如此。它与内容重叠。
我需要对齐所有元素,直到达到如下所示的 1200 像素。
勾选this fiddle。对于这种情况,您可以使用 flexbox
。将这些属性添加到您的 topnav
:
display: flex;
align-items: center;
justify-content: space-evenly;
align-items
用于垂直对齐,justify-content
用于space等分。如果将 display
设置为 flex
.
更多关于 flexbox
here。
我包装了“选择书籍格式”、“关于”<div class="topnav-inner-wrap">
并在 .topnav
和 .topnav-inner-wrap
工作演示: https://jsfiddle.net/k2beLnrv/
function myFunction(){
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
.topnav {
overflow: hidden;
background-color: #333;
display: flex;
align-items: center;
justify-content: space-between;
}
.topnav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #04aa6d;
color: white;
}
.topnav .icon {
display: none;
}
@media screen and (max-width: 600px) {
.topnav > a:not(:first-child) {
display: none;
}
.topnav a.icon {
float: right;
display: block;
}
}
@media screen and (max-width: 600px) {
.topnav.responsive {
position: relative;
display: block;
}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a,
.topnav.responsive .topnav-inner-wrap {
float: none;
display: block;
text-align: left;
}
}
@media screen and (max-width: 1200px) {
.topnav > a:not(:first-child), .topnav .topnav-inner-wrap {
display: none;
}
.topnav a.icon {
float: right;
display: block;
}
}
@media screen and (max-width: 1200px) {
.topnav.responsive {
position: relative;
display: block;
}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a,
.topnav.responsive .topnav-inner-wrap {
float: none;
display: block !important;
text-align: left;
}
}
/* select dropdown css code */
select {
-webkit-appearance: none;
-moz-appearance: none;
-ms-appearance: none;
appearance: none;
outline: 0;
box-shadow: none;
border: 0 !important;
background: #5c6664;
background-image: none;
flex: 1;
padding: 0 0.5em;
color: #fff;
cursor: pointer;
font-size: 1em;
font-family: "Open Sans", sans-serif;
}
select::-ms-expand {
display: none;
}
.select {
position: relative;
display: flex;
width: 20em;
height: 3em;
line-height: 3;
background: #5c6664;
overflow: hidden;
border-radius: 0.25em;
}
.select::after {
content: "BC";
position: absolute;
top: 0;
right: 0;
padding: 0 1em;
background: #2b2e2e;
cursor: pointer;
pointer-events: none;
transition: 0.25s all ease;
}
.select:hover::after {
color: #23b499;
}
.topnav-inner-wrap {
display: flex;
align-items: center;
}
<div class="topnav" id="myTopnav">
<a href="#home" class="active">Red FMTuning</a>
<a href="#news" class="aligntune">Choose a tune from ur favourite</a>
<div class="topnav-inner-wrap">
<a href="#contact">
<div class="select">
<select name="format" id="format">
<option selected>Choose a book format</option>
<option value="pdf">PDF</option>
<option value="txt">txt</option>
<option value="epub">ePub</option>
<option value="fb2">fb2</option>
<option value="mobi">mobi</option>
</select>
</div></a
>
<a href="#about" class="alignout">About</a>
</div>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
在父class
中使用justify-content:space-between;