垂直对齐移动视图的div

时间:2017-06-27 07:48:41

标签: html css

我在桌面视图中并排显示5个div。 我用过" display:block-inline"实现这一目标。

对于我的移动视图,我使用了媒体查询并将显示设置为" display:block"这样它们就可以在移动设备中垂直对齐。

但是,当我切换到移动视图时,div会一个接一个地垂直对齐,但我正在丢失桌面视图中的样式。喜欢背景颜色和文字颜色。

我无法理解为什么会这样。

任何帮助都将不胜感激。

PS:我有义务不使用Bootstrap



ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #222222;
}

li {
  float: left;
}

li:last-child {
  border-right: none;
}

li a {
  display: block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

li a:hover:not(.active) {
  background-color: #111;
}

.active {
  background-color: #F98900;
}

nav[role="sub"] ul {
  background-color: #525252;
}

nav[role="sub"] li a:hover {
  text-decoration: underline;
  background: #525252;
  color: #F98900;
}

nav[role="main"] ul {
  background-color: #222222;
}

.block {
  display: inline-block;
}

.mml
/*manual margin to left */

{
  margin-left: 2%;
}

.dailystatus {
  color: #ABABA7;
  background-color: #343434;
}

.mpl {
  padding-left: 4em;
}

@media(max-width:500px) {
  .mpl {
    padding-left: 1em;
  }
  .mmt {
    margin-top: 1em;
  }
  .block2 {
    display: block;
  }
}

.width {
  width: 15%;
}

<div class="mml">
  <p> Your task view all</p>
  <div class="width block dailystatus block2">
    <p>Follow Up 05:30pm</p>
    <p>Ashwin Kumar</p>
    <p>Brigade Group</p>
    <p>Contact</p>
    <p>barbara.coolier@chance.name</p>
    <p>+91 8965238745</p>
    <p>City</p>
    <p>Bangalore</p>
    <p>Lorem ipsum dolor sit amet, no sea altera primis blandit, id nam cibo labitur. In iuvaret bonorum argumentum eum
    </p>
    <p></p>
  </div>
  <div class="width block mml dailystatus block2">
    <p>Follow Up 05:30pm</p>
    <p>Ashwin Kumar</p>
    <p>Brigade Group</p>
    <p>Contact</p>
    <p>barbara.coolier@chance.name</p>
    <p>+91 8965238745</p>
    <p>City</p>
    <p>Bangalore</p>
    <p>Lorem ipsum dolor sit amet, no sea altera primis blandit, id nam cibo labitur. In iuvaret bonorum argumentum eum
    </p>
    <p></p>
  </div>
  <div class="width block mml dailystatus block2">
    <p>Follow Up 05:30pm</p>
    <p>Ashwin Kumar</p>
    <p>Brigade Group</p>
    <p>Contact</p>
    <p>barbara.coolier@chance.name</p>
    <p>+91 8965238745</p>
    <p>City</p>
    <p>Bangalore</p>
    <p>Lorem ipsum dolor sit amet, no sea altera primis blandit, id nam cibo labitur. In iuvaret bonorum argumentum eum
    </p>
    <p></p>
  </div>
  <div class="width block mml dailystatus block2">
    <p>Follow Up 05:30pm</p>
    <p>Ashwin Kumar</p>
    <p>Brigade Group</p>
    <p>Contact</p>
    <p>barbara.coolier@chance.name</p>
    <p>+91 8965238745</p>
    <p>City</p>
    <p>Bangalore</p>
    <p>Lorem ipsum dolor sit amet, no sea altera primis blandit, id nam cibo labitur. In iuvaret bonorum argumentum eum
    </p>
    <p></p>
  </div>
  <div class="width block mml dailystatus block2">
    <p>Follow Up 05:30pm</p>
    <p>Ashwin Kumar</p>
    <p>Brigade Group</p>
    <p>Contact</p>
    <p>barbara.coolier@chance.name</p>
    <p>+91 8965238745</p>
    <p>City</p>
    <p>Bangalore</p>
    <p>Lorem ipsum dolor sit amet, no sea altera primis blandit, id nam cibo labitur. In iuvaret bonorum argumentum eum
    </p>
    <p></p>
  </div>
</div>
&#13;
&#13;
&#13;

如何控制div的宽度,以便它们在移动视图中应用设备的整个宽度

2 个答案:

答案 0 :(得分:1)

如果您已发布完整代码,则您的CSS代码已完全关闭,您尚未使用任何列表项目。这些样式仅适用于列表项。话虽如此,我认为你已经发布了一半的代码,无论如何;

我做了一些编辑,看到了这段代码。调整大小时,每个人都堆叠在顶部。

body {
  font-family: sans-serif;
  font-size: .9rem;
  background-color: #343434;
}

p {
  color: white;
}

a:hover:not(.active) {
  background-color: #111;
}

a:hover {
  text-decoration: underline;
  background: #525252;
  color: #F98900;
}

h1 {
  text-align: center;
}


/* 1 column: 320px */

.autowide {
  margin: 0 auto;
  width: 98%;
}

.autowide img {
  float: left;
  margin: 0 .75rem 0 0;
}

.autowide .module {
  background-color: #4c4b4b;
  border-radius: .25rem;
  margin-bottom: 1rem;
}

.autowide .module p {
  padding: .25rem .75rem;
}


/* 2 columns: 600px */

@media only screen and (min-width: 600px) {
  .autowide .module {
    float: left;
    margin-right: 2.564102564102564%;
    width: 48.717948717948715%;
  }
  .autowide .module:nth-child(2n+0) {
    margin-right: 0;
  }
}


/* 3 columns: 768px */

@media only screen and (min-width: 768px) {
  .autowide .module {
    width: 31.623931623931625%;
  }
  .autowide .module:nth-child(2n+0) {
    margin-right: 2.564102564102564%;
  }
  .autowide .module:nth-child(3n+0) {
    margin-right: 0;
  }
}


/* 4 columns: 992px and up */

@media only screen and (min-width: 992px) {
  .autowide .module {
    width: 23.076923076923077%;
  }
  .autowide .module:nth-child(3n+0) {
    margin-right: 2.564102564102564%;
  }
  .autowide .module:nth-child(4n+0) {
    margin-right: 0;
  }
}
<div class="autowide">
  <div class="module">
    <p>Follow Up 05:30pm</p>
    <p>Ashwin Kumar</p>
    <p>Brigade Group</p>
    <p>Contact</p>
    <p><a>barbara.coolier@chance.name</a></p>
    <p>+91 8965238745</p>
    <p>City</p>
    <p>Bangalore</p>
    <p>Lorem ipsum dolor sit amet, no sea altera primis blandit, id nam cibo labitur. In iuvaret bonorum argumentum eum
    </p>
  </div>
  <div class="module">
    <p>Follow Up 05:30pm</p>
    <p>Ashwin Kumar</p>
    <p>Brigade Group</p>
    <p>Contact</p>
    <p><a>barbara.coolier@chance.name</a></p>
    <p>+91 8965238745</p>
    <p>City</p>
    <p>Bangalore</p>
    <p>Lorem ipsum dolor sit amet, no sea altera primis blandit, id nam cibo labitur. In iuvaret bonorum argumentum eum
    </p>
  </div>
  <div class="module">
    <p>Follow Up 05:30pm</p>
    <p>Ashwin Kumar</p>
    <p>Brigade Group</p>
    <p>Contact</p>
    <p><a>barbara.coolier@chance.name</a></p>
    <p>+91 8965238745</p>
    <p>City</p>
    <p>Bangalore</p>
    <p>Lorem ipsum dolor sit amet, no sea altera primis blandit, id nam cibo labitur. In iuvaret bonorum argumentum eum
    </p>
  </div>
  <div class="module">
    <p>Follow Up 05:30pm</p>
    <p>Ashwin Kumar</p>
    <p>Brigade Group</p>
    <p>Contact</p>
    <p><a>barbara.coolier@chance.name</a></p>
    <p>+91 8965238745</p>
    <p>City</p>
    <p>Bangalore</p>
    <p>Lorem ipsum dolor sit amet, no sea altera primis blandit, id nam cibo labitur. In iuvaret bonorum argumentum eum
    </p>
  </div>
  <div class="module">
    <p>Follow Up 05:30pm</p>
    <p>Ashwin Kumar</p>
    <p>Brigade Group</p>
    <p>Contact</p>
    <p><a>barbara.coolier@chance.name</a></p>
    <p>+91 8965238745</p>
    <p>City</p>
    <p>Bangalore</p>
    <p>Lorem ipsum dolor sit amet, no sea altera primis blandit, id nam cibo labitur. In iuvaret bonorum argumentum eum
    </p>
  </div>
</div>

答案 1 :(得分:0)

我简化了CSS并使用了flexbox。在媒体查询中,宽度被重新定义为100%。

.container {
  display: flex;
  justify-content: space-between;
}

.dailystatus {
  color: #ABABA7;
  background-color: #343434;
}

.width {
  width: 15%;
}

@media screen and (max-width: 500px) {
  .width {
    width: 100%;
  }
}
<p> Your task view all</p>
<div class="container">
  <div class="width dailystatus">
    <p>Follow Up 05:30pm</p>
    <p>Ashwin Kumar</p>
    <p>Brigade Group</p>
    <p>Contact</p>
    <p>barbara.coolier@chance.name</p>
    <p>+91 8965238745</p>
    <p>City</p>
    <p>Bangalore</p>
    <p>Lorem ipsum dolor sit amet, no sea altera primis blandit, id nam cibo labitur. In iuvaret bonorum argumentum eum
    </p>
    <p></p>
  </div>
  <div class="width dailystatus">
    <p>Follow Up 05:30pm</p>
    <p>Ashwin Kumar</p>
    <p>Brigade Group</p>
    <p>Contact</p>
    <p>barbara.coolier@chance.name</p>
    <p>+91 8965238745</p>
    <p>City</p>
    <p>Bangalore</p>
    <p>Lorem ipsum dolor sit amet, no sea altera primis blandit, id nam cibo labitur. In iuvaret bonorum argumentum eum
    </p>
    <p></p>
  </div>
  <div class="width dailystatus">
    <p>Follow Up 05:30pm</p>
    <p>Ashwin Kumar</p>
    <p>Brigade Group</p>
    <p>Contact</p>
    <p>barbara.coolier@chance.name</p>
    <p>+91 8965238745</p>
    <p>City</p>
    <p>Bangalore</p>
    <p>Lorem ipsum dolor sit amet, no sea altera primis blandit, id nam cibo labitur. In iuvaret bonorum argumentum eum
    </p>
    <p></p>
  </div>
  <div class="width dailystatus">
    <p>Follow Up 05:30pm</p>
    <p>Ashwin Kumar</p>
    <p>Brigade Group</p>
    <p>Contact</p>
    <p>barbara.coolier@chance.name</p>
    <p>+91 8965238745</p>
    <p>City</p>
    <p>Bangalore</p>
    <p>Lorem ipsum dolor sit amet, no sea altera primis blandit, id nam cibo labitur. In iuvaret bonorum argumentum eum
    </p>
    <p></p>
  </div>
  <div class="width dailystatus">
    <p>Follow Up 05:30pm</p>
    <p>Ashwin Kumar</p>
    <p>Brigade Group</p>
    <p>Contact</p>
    <p>barbara.coolier@chance.name</p>
    <p>+91 8965238745</p>
    <p>City</p>
    <p>Bangalore</p>
    <p>Lorem ipsum dolor sit amet, no sea altera primis blandit, id nam cibo labitur. In iuvaret bonorum argumentum eum
    </p>
    <p></p>
  </div>
</div>