【问题标题】:How to align div content in specific position using Css?如何使用 Css 在特定位置对齐 div 内容?
【发布时间】:2022-01-13 15:18:45
【问题描述】:

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: "\25BC";
  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 像素,如下所示。

代码:- https://jsfiddle.net/se8pa7m3/2/

【问题讨论】:

标签: html css media-queries


【解决方案1】:

我已经包装了“选择书籍格式”、“关于”&lt;div class="topnav-inner-wrap"&gt; 并在 .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: "\25BC";
  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>

【讨论】:

  • 你是对的,但是,我正在尝试实现响应式导航栏。当我应用你的样式时。当屏幕达到 1200 像素时。下拉菜单不在导航图标内。导航图标内部的设计也发生了变化。 (在 nav-bar 内)图标它应该垂直显示项目。我的完整代码与功能codesandbox.io/s/condescending-leftpad-cc7cm?file=/src/…
  • 它应该表现得像,当 1200px 时。像上面提到的格式一样对齐。当它达到 1200px 以下时。然后会显示导航图标。所以点击它,相同的元素需要一个一个垂直显示
  • 我已经更新了 CSS 和演示链接。我希望现在你的问题得到解决。请检查。
【解决方案2】:

检查this fiddle。对于这种情况,您可以使用 flexbox。将这些属性添加到您的 topnav

display: flex;
align-items: center;
justify-content: space-evenly;

align-items 用于垂直对齐项目,justify-content 用于等间距。如果您将display 设置为flex,则只能使用这两个属性。

更多关于flexboxhere

【讨论】:

    【解决方案3】:

    在父类中使用justify-content:space-between;

    【讨论】:

      猜你喜欢
      • 2022-01-25
      • 1970-01-01
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-09
      相关资源
      最近更新 更多