2

Нужно сделать справа div заданной ширины, слева на всю оставшуюся ширину

div

Как будет правильнее?

3 ответа 3

3

Можно с помощью FlexBox


Про FlexBox: на русском и английском

Поддержка FlexBox caniuse.com

Поддержка FlexBox

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
html,
body {
  width: 100%;
  height: 100%;
}
.content {
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-flex-flow: row wrap;
      -ms-flex-flow: row wrap;
          flex-flow: row wrap;
  width: 100%;
  height: 100%;
}
.content .block1 {
  -webkit-box-flex: 1;
  -webkit-flex: 1 auto;
      -ms-flex: 1 auto;
          flex: 1 auto;
  height: 100%;
  background-color: #008000;
}
.content .block2 {
  -webkit-box-flex: 0;
  -webkit-flex: 0 200px;
      -ms-flex: 0 200px;
          flex: 0 200px;
  height: 100%;
  background-color: #ffc0cb;
}
<div class="content">
  <div class="block1"></div>
  <div class="block2"></div>
</div>

Второй вариант с помощью display: table; и display: table-cell;

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
html,
body {
  width: 100%;
  height: 100%;
}
.content {
  display: table;
  width: 100%;
  height: 100%;
}
.content .block1 {
  display: table-cell;
  width: auto;
  height: 100%;
  background-color: #008000;
}
.content .block2 {
  display: table-cell;
  width: 200px;
  height: 100%;
  background-color: #ffc0cb;
}
<div class="content">
  <div class="block1"></div>
  <div class="block2"></div>
</div>

1

Можно решить данную задачу с использованием css функции calc().

.container {
  display: block;
  width: 800px;
  height: 1000px;
  border: 1px solid #000;
}

article {
  display: block;
  width: calc(100% - 200px);
  height: 1000px;
  float: left;
  background-color: #ffc4c4;  
}

aside {
  display: block;
  width: 200px;
  height: 1000px;
  float: right;
  background-color: #666;
}
<div class="container">
  <article>Content</article>
  <aside>Sidebar</aside>
</div>

1
  • calc это же есть плохо, есть друге варианты?
    – Rufex
    9 фев 2016 в 13:36
0

Можно решить через float и overflow: hidden. Также добавляем контейнеру clearfix:

.clearfix:after {
  content: "";
  display: table;
  clear: both;
}

.left-stretch {
  overflow: hidden;
}

.right-fixed {
  float: right;
  width: 200px;
}
<div class="container clearfix">
  <div class="right-fixed">
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  </div>
  <div class="left-stretch">
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  </div>
</div>

Почему overflow: hidden работает можно почитать в статье на хабре.

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими пользовательским соглашением, политикой конфиденциальности и политикой о куки

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.