Страницы

Поиск по вопросам

воскресенье, 29 декабря 2019 г.

Div. Занять оставшееся место

#html #css


Возникла проблема с блочной вёрсткой. Допустим, у нас есть 3 блока:


Ширина sidebar-left и sidebar-right равна 300px и 250px соответственно. sidebar-left находится слева, sidebar-right – справа, content – между ними. Как сделать так, чтобы ширина content занимала всё оставшееся место?


Ответы

Ответ 1



Вводим дополнительный блок wrapper и работаем с дивами как с таблицей. И резиновое и вашим требованиям соответствует. .wrapper { display: table; width: 100%; } .sidebar-left { width: 300px; display: table-cell; height: 200px; background: red; } .content { display: table-cell; height: 200px; background: green; } .sidebar-right { width: 250px; display: table-cell; height: 200px; background: blue; }
http://jsfiddle.net/en7dv0jw/ Извиняюсь за дублирование css свойств.

Ответ 2



Современный способ. Flexbox .container { display: flex; } .sidebar-left { flex: 0 0 300px; } .sidebar-right { flex: 0 0 250px; }
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.
Современный способ. Grid .container { display: grid; grid-template-columns: 300px 1fr 250px; }
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.
Для того, чтобы это работало также в IE применяем старый синтаксис и явно задаём положение каждого элемента: .container { display: -ms-grid; display: grid; -ms-grid-columns: 300px 1fr 250px; grid-template-columns: 300px 1fr 250px; } .content { -ms-grid-column: 2; } .sidebar-left { -ms-grid-column: 3; }
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.
Классика. float и overflow: hidden. .clearfix:after { content: ""; display: table; clear: both; } .content { overflow: hidden; } .sidebar-left { float: left; width: 300px; } .sidebar-right { float: right; width: 250px; }
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.
Почему overflow: hidden работает можно почитать в статье на хабре.

Комментариев нет:

Отправить комментарий