Flexbox width of header aligned with outer part of content - html

I'm using flexbox to align my content blocks in the middle of the page, now i'm willing to align the header with the outer content blocks
In the image you can see what needs to happen
This is my current result:
And this is how it need's to look like
So when scaling the browser the content is going to the first line if there is space, the header needs to grow with this at that moment.
Here is a codepen with the flexbox in it
.flex {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
http://codepen.io/Dirkandries/pen/XmpGzw
Is this possible without using media queries and keeping the content boxes the same size?
*The padding and margin can be removed

Challenge #1
In looking at your code, you have a 10px margin applied to each box:
section {
background: red;
width: 300px;
height: 200px;
margin: 10px;
}
So one problem you will encounter in trying to align the header edges with the box edges is that there's an additional 10px of transparent space beyond the border of each box. But you're asking for the header to align with the border of the box. So we can either remove the margin from each box, or adjust the width of the header. I've gone with the latter in my solution below.
Challenge #2
You've specified a fixed width for each box (300px). This makes it difficult to match the header width with the row of boxes. What happens when the screen is 750px or 1150px wide? The boxes don't stretch to fill the width, a gap is created as a result, and the box row doesn't align with the header.
The box row width is 960px but the header width is 1150px.
One possible solution (or step in the right direction)
Align the flexbox in a column direction to vertically stack the header and the boxes (which are wrapped in a new container)
Use a nested flexbox to align the boxes in a row
Use calc for width values
Use media queries for screen adjustments
HTML
<article class="flex">
<header>
Header needs to be alignd with the container outer part
</header>
<div id="nested-inner-container"><!-- new container for nested flexbox -->
<section>content</section>
<section>content</section>
<section>content</section>
<section>content</section>
<section>content</section>
<section>content</section>
</div>
</article>
CSS
body { margin: 0; }
.flex {
display: flex;
flex-direction: column; /* main groups (header and div) in column direction */
align-items: center;
}
header {
height: 50px;
width: calc(100% - 20px); /* width accounts for left and right box margins */
background-color: blue;
}
#nested-inner-container {
display: flex; /* box group (flex item) becomes flex container, as well */
justify-content: center;
flex-wrap: wrap;
width: 100%; /* width equal to header width */
}
section {
flex: 1 1 calc(25% - 80px); /* flex basis equals four boxes per row less margins */
height: 200px;
margin: 10px;
background: red;
}
#media screen and (max-width: 1500px) {
section { flex-basis: calc(33.33% - 60px); } /* three boxes per row less margins */
}
#media screen and (max-width: 1250px) {
section { flex-basis: calc(50% - 40px); } /* two boxes per row less margins */
}
#media screen and (max-width: 1000px) {
section { flex-basis: calc(100% - 20px); } /* one box per row less margins */
}
DEMO: http://codepen.io/anon/pen/wKgVYb
NOTE: This answer may or may not be exactly what you're looking for. The question didn't address all the details (like "can the margins be adjusted?", "are the box widths adjustable?", "are media queries not an option or just something you're hoping to avoid?"). So my goal in this answer was to offer up some concepts that hopefully get you where you want to go. AT A MINIMUM, the header and the boxes align in all screen sizes :-)

As far as I know, there is no solution to your question using only CSS and the flex box model.
If what you want is just to align the background of the header, you can fake it.
You need another flex container, that will give you a background with the same rules as the real one. (And that will be used only for this... not really semantic)
.flex{
display: flex;
flex-wrap: wrap;
justify-content: center;
position: relative;
}
header{
width: 100%;
background: lightblue;
height: 50px;
margin: 0px 170px;
text-align: center;
}
section{
background: red;
width: 300px;
height: 200px;
margin: 10px;
}
.headerbkg {
position: absolute;
left: 0px;
right: 0px;
top: 0px;
height: 50px;
display: flex;
flex-wrap: wrap;
justify-content: center;
overflow: hidden;
z-index: -1;
}
.headerbkg div {
background: lightblue;
width: 300px;
height: 50px;
margin: 0px 10px;
}
<article class="flex">
<header>
Header needs to be alignd with the container outer part
</header>
<div class="headerbkg">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<section>content</section>
<section>content</section>
<section>content</section>
<section>content</section>
<section>content</section>
<section>content</section>
</article>
The real header is given a margin that will keep it always narrower that the background, so that won't spoli the effect.
To make this less evident, I have given it a centered text

The following are my adressment of the question.
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<meta name = "viewport"
content = "width = device-width, initial-scale = 1.0"
>
<link href = "CSS/Example.css"
rel = "stylesheet"
type = "text/css"
>
</head>
<body>
<header>
Header needs to be alignd with the container outer part
</header>
<div class="flex">
<section>content</section>
<section>content</section>
<section>content</section>
<section>content</section>
<section>content</section>
<section>content</section>
</div>
</body>
</html>
CSS
html,
*
{
border : 0;
box-sizing : border-box;
margin : 0;
padding : 0;
}
.flex
{
display : flex;
flex-wrap : wrap;
justify-content : center;
}
header
{
background : blue;
display : block;
height : 50px;
margin : auto;
width : 300px;
}
#media only screen and (min-width : 620px)
{
body header
{
width : 620px;
}
}
#media only screen and (min-width : 940px)
{
body header
{
width : 940px;
}
}
#media only screen and (min-width : 1260px)
{
body header
{
width : 1260px;
}
}
#media only screen and (min-width : 1580px)
{
body header
{
width : 1580px;
}
}
#media only screen and (min-width : 1900px)
{
body header
{
width : 1900px;
}
}
section
{
background : red;
display : block;
width : 300px;
height : 200px;
margin : 10px;
}
Firstly, in the CSS file there is the segment...
*
{
border : 0;
box-sizing : border-box;
margin : 0;
padding : 0;
}
By setting border, margin and padding to 0 for all elements you can eliminate any default settings for those properties that some or all browsers might display for an element. By adding box-sizing : border-box; any border or padding settings you make will be contained within the width of an element rather than without, making laying out a page much simpler.
I tend to include this snippet as a precaution.
As for setting the width of your header, it will need to be set to a fixed width that will need to be changed whenever the concentration of section's (themselves of a fixed width) to a line changes, which will occur whenever the viewport reaches certain widths.
The width of the header, and the width of the viewport at which it is changed, will equal the total width of the sections in one full line plus the total width of the margins between them, nnamely 300px, 620px, 940px, 1260px, 1580px, and 1900px. I have used media queries to perform the changeover at those points.
Note : Because of specificity issues caused when seeking to override a property using media queries I have placed body in front of the definition of header in each of the media queries.
Since the width of header isn't flexible and flex is mainly there to distribute the section's, I have placed header outside of flex.
If you have any questions or comments about my answer, then please feel free to reply.

It is much easier then it looks. Here is the CodePen: http://codepen.io/mikestratton/pen/PPmpKQ
Add the following two classes to your CSS:
.sleft {
background: red;
width: 50%;
height: 200px;
margin: 0;
float: left;
}
.sright{
background: red;
width: 50%;
height: 200px;
margin: 0;
float: left;
}
Then change your HTML to:
<article class="flex">
<header>
Header needs to be alignd with the container outer part
</header>
<section class="sleft">content</section>
<section class="sright">content</section>
<section class="sleft">content</section>
<section class="sright">content</section>
<section class="sleft">content</section>
<section class="sright">content</section>
</article>

Its Not the same as you want but i tried to do some little hacks & tweeks
http://codepen.io/anon/pen/GpEdKq
Check is this ok or you can use grids instead and it will be responsive too, Sorry if it doesn't help you Thanks
Html Code:
<article class="flex">
<div class="wrapper">
<header>
Header needs to be alignd with the container outer part
</header>
<section>content</section>
<section>content</section>
<section>content</section>
<section>content</section>
<section>content</section>
<section>content</section>
</div>
</article>
css code:
body{
margin: 0;
}
.wrapper{width:80%; margin-left:10%;margin-right:15%;}
.flex{
display: flex;
flex-wrap: wrap;
justify-content: center;
}
header{
width: 100%;
background: blue;
height: 50px;
}
section{
background: red;
width: 300px;
height: 200px;
margin: 10px;
float:left;
}

Related

Responsive HTML (Resize div when window becomes smaller than div)

I am curious how this type of responsive layout is achieved (see screenshots).
It looks like the form is in a div that does not start resizing or become responsive until the window has been resized smaller than the div.
I have tried a combination of divs with margins and flex, etc., and have added the <meta name="viewport" content="width=device-width, initial-scale=1.0"> tag to the head as well, but I only manage to make the page responsive to all window resizing.
Any direction here is much appreciated.
There are multiple methods of doing this:
#media queries:
with media queries, you can define style changes for specific breakpoints. In this case it is used to size a column to 100% width instead of 50% at screen at 480px or lower:
.container {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.container > div {
width: calc(50% - 20px);
}
#media only screen
and (max-width: 480px) {
.container > div {
width: 100%;
}
}
input {
display: block;
width: 100%;
box-sizing: border-box;
}
<div class="container">
<div>
<input>
<label>Firstname</label>
</div>
<div>
<input>
<label>Lasttname</label>
</div>
</div>
flex-grow:
This Flexbox approach defines the max-width of a column to 480px. If an element is smaller than 480px it will span the entire given width. flex-wrap: wrap is used to push the 2nd column to the next row. For screens larger than 960 px (2 columns), flex-grow will overwrite the max-width and allow the element to occupy the remaining available space.
.container {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.container > div {
width: 480px;
flex-grow: 1;
}
input {
display: block;
width: 100%;
box-sizing: border-box;
}
<div class="container">
<div>
<input>
<label>Firstname</label>
</div>
<div>
<input>
<label>Lasttname</label>
</div>
</div>
CSS-Grid + auto-fit:
This Grid solution uses minmax and auto-fit to create columns dynamically. It will fit as many columns as possible but ensures that no column is less then 480px (unless the screen is smaller) and resize the columns to occupy remaining space.
.container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(480px, 1fr));
gap: 20px;
}
input {
display: block;
box-sizing: border-box;
width: 100%;
}
<div class="container">
<div>
<input>
<label>Firstname</label>
</div>
<div>
<input>
<label>Lasttname</label>
</div>
</div>
by set width: 100%; and max-width to any value like max-width: 400px; you can do this
.my-div {
width: 100%;
max-width: 400px;
/* center */
margin-inline: auto;
/* just for demo */
background: #808080;
height: 50em;
}
<div class="my-div"></div>

Filling content background till it reaches footer

I have similar problem which can be found here. But i couldn't make it work or i didnt understand it fully.
My problem which im trying to resolve is - I want my content background to reach footer even if there isnt enough content to be displayed. I created a simple fiddle which can be found here. As you can see there isnt enough content to reach footer and there is this "blue" space between content and footer. I would like to make that space grey.
HTML :
<div class=blue>header here</div>
<p>LOGO here</p>
<div class="blue">navigation bar here</div>
<div class="content">
No content.
</div>
<div class="footer">footer is here</div>
CSS:
.blue {
color: #ffffff;
background-color: #294a70;
display: block;
float: none;
width: 400px;
margin: 0 auto;
text-align: center;
}
p {
text-align: center;
color: #ffffff;
}
.content {
background-color: #e6e6e6;
display: block;
float: none;
margin: 0 auto;
overflow:hidden;
width:400px;
margin-bottom:30px;
}
.footer {
color: #ffffff;
background-color: #294a70;
display: block;
float: none;
width: 400px;
margin: 0 auto;
text-align: center;
position: absolute;
bottom: 0;
left: 0;
right: 0;
height:30px;
}
body {
margin:0;
padding:0;
font-family: 'Open Sans', sans-serif;
line-height: 1.5;
font-size: 14px;
overflow-x:hidden;
background-image:url('http://www.planwallpaper.com/static/images/Alien_Ink_2560X1600_Abstract_Background_1.jpg');
min-height: 100%;
}
html {
position:relative;
min-height: 100%;
}
All help will be appreciated!
Use CSS3 calc() function
the trick is, if you know the height of header & footer, you can use this function with vh units, 100vh gives you screen height, just substract the height of hearder & footer from it.
E.g.
If header is 80px & Footer is 40px, i.e. total 120px, then use
.content{
min-height: calc(100vh - 120px);
}
The purpose of using min-height is if content is not present then atleast this height is applied, but if there is more content than screen then div is expanded to fit accordingly.
Updated JSFiddle:
https://jsfiddle.net/vj07e8g1/5/
You could try a flexbox layout instead:
HTML
<body>
<header></header>
<main class="content"></main>
<footer></footer>
</body>
CSS
body {
display: flex;
min-height: 100vh;
flex-direction: column;
}
main.content {
flex: 1;
}
Check out this codepen example: http://codepen.io/StefanBobrowski/pen/zZXXWy
You can add this to your content style:
min-height:400px;
It'll push the footer a little, but it'll do the work.
Hope this is what you're looking for.
The easiest contemporary way, depending on your browser-support requirements, would be to use CSS grids, which allows you to define rows and columns and assign certain content to be in specific places (placed by grid-row and grid-column), like follows:
html,
body {
height: 100%;
}
/* to force all elements to be sized including
their padding and border-widths */
body,
::before,
::after {
box-sizing: border-content;
}
body {
/* To use CSS grid, forcing the child elements of
the <body> element to adopt 'display: grid-item': */
display: grid;
/* defining the three columns of the grid, the first and
third being equal fractions of the space left over after
the second (middle) column's width of 400px is calculated */
grid-template-columns: 1fr 400px 1fr;
/* reducing the first three rows to the minimum height needed
to fully display their content, setting the fourth row
to take up the remaining unoccupied space once the other
heights are calcuated and setting the final row's height to
30px: */
grid-template-rows: min-content min-content min-content 1fr 30px;
height: 100vh;
background-image: url(https://i.stack.imgur.com/2oC8H.jpg);
}
body>* {
/* setting all the child elements of the <body> to be placed
in grid-column 2 (the central 400px-wide column): */
grid-column: 2;
}
/* Setting the default shared styles of the .blue elements: */
.blue {
color: #ffffff;
background-color: #294a70;
}
.blue.header {
/* positioning this element in the first (one-based counting)
row: */
grid-row: 1
}
body>p {
grid-row: 2;
}
.blue.navigation {
grid-row: 3;
}
div.content {
grid-row: 4;
/* background-color purely to show that the space of the
div.content element occupies the full space available: */
background-color: #ffa;
}
div.footer {
grid-row: 5;
}
<div class="header blue">header here</div>
<p>LOGO here</p>
<div class="blue navigation">navigation bar here</div>
<div class="content">
No content.
</div>
<div class="footer">footer is here</div>
JS Fiddle.
Please note that I did add a class-name to both the .blue elements in order to more-easily distinguish them according to their roles in the document, and from each other when placing them in the document.

column flex with height 0 padding trick

I have 2 divs that are placed on top of each other. For purposes of alignment, I am using display: flex and flex: column on the div containing these two divs. However, the first div uses the "height 0 padding" trick for videos. The problem I'm having, is that when using flex: column and change the width of the screen, the height doesn't change (and I want the height to change so that it matches the ratio for the video). What ends up happening is that the div stays the same, and the video shrinks within it and it looks ugly because there is extra background.
Plunker: https://plnkr.co/edit/TaeF5f8VufJWPU3GRZPr?p=preview
(in short, I want it such that when I change the width of the browser, the red div's height gets smaller)
CSS
/* Styles go here */
body {
height: 100%;
}
.container {
height: 80vh;
display: flex;
flex-flow: column;
}
.video {
flex: none;
height: 0;
padding-bottom: 30%;
background-color: red;
width: 80%;
}
.next-content {
flex: 1 0 auto;
width: 80%;
background-color: blue;
}
HTML:
<body>
<h1>Hello Plunker!</h1>
<div class="container">
<div class="video"></div>
<div class="next-content"></div>
</div>

Container height no longer than screen-header

I asked a question today about good and bad practises in CSS/HTML/jQuery and when it is appropriate to use jQuery to set container dimensions. I got some good answers
So, understanding that jQuery is not the best option, I decided to ask maybe some of you can give some input about this "problem"
So, I have a page put together with php. I have one header for all of my pages and content is being changed with php (I am saying this only to let you guys know that wrapping header and div in one container is not an option):
include ("header.php");
include ("$lang/$section.php");
include ("footer.php");
I have a header with fixed hight (100px + 100px margin-bottom) and after that I have a div which on screens smaller than 768px(height) I want to be no longer than the remaining space. If the screen is larger, I want my div to be
max-height: 420px;
with
padding: 100px 0;
Inside of this div I have 3 floated columns. I need them to fill the space in the parent div.
What I would usually do is- use jQuery and calculate screen height and subtract header height and all the margins and paddings. But as I've learned today, that is not a good practise.
So, to wrap it up: I NEED THE DIV TO FILL THE SPACE BETWEEN HEADER AND BOTTOM OF THE SCREEN FOR VIEWPORT HEIGHT SMALLER THAN 768px. MAX-HEIGHT FOR THIS DIV IS 420px. With jQuery it is super easy but I can't figure out the clean css way.
Maybe some of you have an idea?
Here is my fiddle, so you guys don't have to type out all of the code.
Thank you in advance!
You can use calc() and vh (viewport height).
calc() browser support: http://caniuse.com/#search=calc
vh browser support: http://caniuse.com/#search=vh
So we use calc(100vh - 200px) being 100vh the height of the viewport and 200px the height of the header.
Also, we add a media query so that when the screen is bigger than 768px height we limit the height to 420px.
Try this:
header { height: 100px; background: #ccc; margin-bottom: 100px; box-sizing: border-box; }
section { width: 100%; height: calc(100vh - 200px); padding: 50px 0; background: yellow; box-sizing: border-box; }
.col1, .col2, .col3 { float: left; width: 33%; }
.colPadding { padding: 25px; background: blue; }
.cb { width: 100%; height: 1px; clear: both; }
body {
margin: 0;
}
#media screen and (min-height: 768px) {
section {
max-height: 420px;
}
}
<header>
This is my header with 100px bottom margin
</header>
<section>
<div class="col1">
<div class="colPadding">
section with padding: 50px 0; and max-height: 420px;
</div>
</div>
<div class="col2">
<div class="colPadding">
Column 2
</div>
</div>
<div class="col3">
<div class="colPadding">
Column 3
</div>
</div>
<div class="cb"></div>
</section>
Gave it a shot with CSS3 flex-box model and screen media queries. Here is my fiddle.
I used 300px instead of 764px for the fiddle. (you can change it if you want, I just used 300px so that it's easier to test)
Applied CSS
* { box-sizing: border-box; } /* force sizing based on border */
body {
display: flex; /* flex for body since wrapping header and section is not allowed */
flex-flow: column wrap;
}
header {
height: 100px;
background: #ccc;
margin-bottom: 100px;
flex: 0 0 auto; /* make header size fixed */
}
section {
width: 100%;
max-height: 420px;
padding: 50px 0;
background: yellow;
/* to occupy remaining space */
flex: 1 1 auto;
/* for columns inside to occupy full width */
display: flex;
flex-flow: row wrap;
/* for immediate children to stretch to max height possible */
align-items: stretch;
}
.col1, .col2, .col3 {
float: left;
/* to occupy remaining width */
flex: 1 0 auto;
}
.colPadding {
padding: 25px;
background: blue;
}
.cb {
width: 100%;
height: 1px;
clear: both;
}
/* Custom CSS */
/* style to apply when the screen is less than or equal to 300px (you can change this to 768px) */
#media screen and ( max-height: 300px ){
body {
height: 100vh; /* for body to have a size of the full screen */
}
header {
margin: 0px; /* remove margin bottom */
}
section {
padding: 0px; /* remove margin bottom and top/bottom padding */
margin: 0px;
}
}
More on CSS3 flex-box here.

Div width 100% minus fixed amount of pixels

How can I achieve the following structure without using tables or JavaScript? The white borders represent edges of divs and aren't relevant to the question.
The size of the area in the middle is going to vary, but it will have exact pixel values and the whole structure should scale according to those values. To simplify it, I'd need a way to set "100% - n px" width to the top-middle and bottom-middle divs.
I'd appreciate a clean cross-browser solution, but in case it's not possible, CSS hacks will do.
Here's a bonus. Another structure I've been struggling with and end up using tables or JavaScript. It's slightly different, but introduces new problems. I've been mainly using it in jQuery-based windowing system, but I'd like to keep the layout out of the script and only control the size of one element (the middle one).
New way I've just stumbled upon: css calc():
.calculated-width {
width: -webkit-calc(100% - 100px);
width: -moz-calc(100% - 100px);
width: calc(100% - 100px);
}​
Source: css width 100% minus 100px
You can use nested elements and padding to get a left and right edge on the toolbar. The default width of a div element is auto, which means that it uses the available width. You can then add padding to the element and it still keeps within the available width.
Here is an example that you can use for putting images as left and right rounded corners, and a center image that repeats between them.
The HTML:
<div class="Header">
<div>
<div>This is the dynamic center area</div>
</div>
</div>
The CSS:
.Header {
background: url(left.gif) no-repeat;
padding-left: 30px;
}
.Header div {
background: url(right.gif) top right no-repeat;
padding-right: 30px;
}
.Header div div {
background: url(center.gif) repeat-x;
padding: 0;
height: 30px;
}
While Guffa's answer works in many situations, in some cases you may not want the left and/or right pieces of padding to be the parent of the center div. In these cases, you can use a block formatting context on the center and float the padding divs left and right. Here's the code
The HTML:
<div class="container">
<div class="left"></div>
<div class="right"></div>
<div class="center"></div>
</div>
The CSS:
.container {
width: 100px;
height: 20px;
}
.left, .right {
width: 20px;
height: 100%;
float: left;
background: black;
}
.right {
float: right;
}
.center {
overflow: auto;
height: 100%;
background: blue;
}
I feel that this element hierarchy is more natural when compared to nested nested divs, and better represents what's on the page. Because of this, borders, padding, and margin can be applied normally to all elements (ie: this 'naturality' goes beyond style and has ramifications).
Note that this only works on divs and other elements that share its 'fill 100% of the width by default' property. Inputs, tables, and possibly others will require you to wrap them in a container div and add a little more css to restore this quality. If you're unlucky enough to be in that situation, contact me and I'll dig up the css.
jsfiddle here: jsfiddle.net/RgdeQ
Enjoy!
You can make use of Flexbox layout. You need to set flex: 1 on the element that needs to have dynamic width or height for flex-direction: row and column respectively.
Dynamic width:
HTML
<div class="container">
<div class="fixed-width">
1
</div>
<div class="flexible-width">
2
</div>
<div class="fixed-width">
3
</div>
</div>
CSS
.container {
display: flex;
}
.fixed-width {
width: 200px; /* Fixed width or flex-basis: 200px */
}
.flexible-width {
flex: 1; /* Stretch to occupy remaining width i.e. flex-grow: 1 and flex-shrink: 1*/
}
Output:
.container {
display: flex;
width: 100%;
color: #fff;
font-family: Roboto;
}
.fixed-width {
background: #9BCB3C;
width: 200px; /* Fixed width */
text-align: center;
}
.flexible-width {
background: #88BEF5;
flex: 1; /* Stretch to occupy remaining width */
text-align: center;
}
<div class="container">
<div class="fixed-width">
1
</div>
<div class="flexible-width">
2
</div>
<div class="fixed-width">
3
</div>
</div>
Dynamic height:
HTML
<div class="container">
<div class="fixed-height">
1
</div>
<div class="flexible-height">
2
</div>
<div class="fixed-height">
3
</div>
</div>
CSS
.container {
display: flex;
}
.fixed-height {
height: 200px; /* Fixed height or flex-basis: 200px */
}
.flexible-height {
flex: 1; /* Stretch to occupy remaining height i.e. flex-grow: 1 and flex-shrink: 1*/
}
Output:
.container {
display: flex;
flex-direction: column;
height: 100vh;
color: #fff;
font-family: Roboto;
}
.fixed-height {
background: #9BCB3C;
height: 50px; /* Fixed height or flex-basis: 100px */
text-align: center;
display: flex;
flex-direction: column;
justify-content: center;
}
.flexible-height {
background: #88BEF5;
flex: 1; /* Stretch to occupy remaining width */
text-align: center;
display: flex;
flex-direction: column;
justify-content: center;
}
<div class="container">
<div class="fixed-height">
1
</div>
<div class="flexible-height">
2
</div>
<div class="fixed-height">
3
</div>
</div>
The usual way to do it is as outlined by Guffa, nested elements. It's a bit sad having to add extra markup to get the hooks you need for this, but in practice a wrapper div here or there isn't going to hurt anyone.
If you must do it without extra elements (eg. when you don't have control of the page markup), you can use box-sizing, which has pretty decent but not complete or simple browser support. Likely more fun than having to rely on scripting though.
Maybe I'm being dumb, but isn't table the obvious solution here?
<div class="parent">
<div class="fixed">
<div class="stretchToFit">
</div>
.parent{ display: table; width 100%; }
.fixed { display: table-cell; width: 150px; }
.stretchToFit{ display: table-cell; vertical-align: top}
Another way that I've figured out in chrome is even simpler, but man is it a hack!
.fixed{
float: left
}
.stretchToFit{
display: table-cell;
width: 1%;
}
This alone should fill the rest of the line horizontally, as table-cells do. However, you get some strange issues with it going over 100% of its parent, setting the width to a percent value fixes it though.
We can achieve this using flex-box very easily.
If we have three elements like Header, MiddleContainer and Footer. And we want to give some fixed height to Header and Footer. then we can write like this:
For React/RN(defaults are 'display' as flex and 'flexDirection' as column), in web css we'll have to specify the body container or container containing these as display: 'flex', flex-direction: 'column' like below:
container-containing-these-elements: {
display: flex,
flex-direction: column
}
header: {
height: 40,
},
middle-container: {
flex: 1, // this will take the rest of the space available.
},
footer: {
height: 100,
}
what if your wrapping div was 100% and you used padding for a pixel amount, then if the padding # needs to be dynamic, you can easily use jQuery to modify your padding amount when your events fire.
I had a similar issue where I wanted a banner across the top of the screen that had one image on the left and a repeating image on the right to the edge of the screen. I ended up resolving it like so:
CSS:
.banner_left {
position: absolute;
top: 0px;
left: 0px;
width: 131px;
height: 150px;
background-image: url("left_image.jpg");
background-repeat: no-repeat;
}
.banner_right {
position: absolute;
top: 0px;
left: 131px;
right: 0px;
height: 150px;
background-image: url("right_repeating_image.jpg");
background-repeat: repeat-x;
background-position: top left;
}
The key was the right tag. I'm basically specifying that I want it to repeat from 131px in from the left to 0px from the right.
In some contexts, you can leverage margin settings to effectively specify "100% width minus N pixels". See the accepted answer to this question.