Css stretch div vertically

CSS Vertically Stretch List Items

The following tutorial shows you how to use CSS to do «CSS Vertically Stretch List Items».

CSS Style

The CSS style to do «CSS Vertically Stretch List Items» is

body, html < height:100%; > ul < height:100%; display:table; > li < height:25%; display:table-row; > li div < height:25%; display:table-cell; vertical-align:middle; >

HTML Body

body> ul> li> div> One!-- w w w. d e m o 2 s . c o m -->   li> div> Two  li> div> Three  li> div> Three br>a Half    

The following iframe shows the result. You can view the full source code and open it in another tab.

html> head> meta name="viewport" content="width=device-width, initial-scale=1"> style id="compiled-css" type="text/css"> body, html < height: 100%; > ul !-- w w w . d e m o 2s . co m--> height: 100%; display: table; > li < height: 25%; display: table-row; > li div< height: 25%; display: table-cell; vertical-align: middle; >  body> ul> li>div>One  li>div>Two  li>div>Three  li>div>Three br>a Half     

demo2s.com | Email: | Demo Source and Support. All rights reserved.

Источник

CSS solution to stretch div vertically

Aside from some cross-browser issues, flex is the best solution for this if your project allows for it.

  1. we don’t know the height of the parent div
  2. the height of first and third divs (let’s call them ‘top’ and ‘bottom’) are known in advance
  3. the height of the second div (let’s call it ‘middle’) is not known: it should take up all the space between the ‘top’ and ‘bottom’ divs (and only that space), regardless of how much or how little content it may have.

Solution 1: Use ‘absolute’ positioning with both ‘top’ and ‘bottom’

If the ‘middle’ div uses absolute positioning, and you don’t set its height, but do set both its ‘top’ and ‘bottom’, then it will automatically stretch to fill. I have not tested it, but the markup will look something like this:

 
Heading at the TOP!
Lorem ipsum dolor sit amet, consectetuer.
)" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this answer" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter devto" data-se-share-sheet-location="2" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f3.0%2f" data-se-share-sheet-license-name="CC BY-SA 3.0" data-s-popover-placement="bottom-start">Share
)" title="">Improve this answer
)">edited Apr 6, 2016 at 9:10
answered Apr 6, 2016 at 8:50
Add a comment |
0

One solution is use calc.

#second

Источник

How to stretch a div vertically and horizontally to occupy all the space?

@Arbel I tried to post a link to jsFiddle but SO complains about some code. And I was too quick on the submit button.

@Cam I'm sorry - I don't follow. Are you saying that I need to copy the code that is at the link to jsFiddle in SO as well? What did you edit?

You have to include your code in your question in order to add a jsfiddle. Spacing, if you dont add 4 spaces after your code it wont register your code as code.

4 Answers 4

I'm not sure if I completely understand your goals. I assumed:

body < margin: 0; padding: 0; width: 100%; >div.faqticleList < background: #ffdd00; width: 200px; height: 100%; float: left; >div.faqticlePreview < background: #bb88ff; width: 100%; height: 100%; margin-left: -203px; padding-left: 203px; >div.container < border: solid 1px #007700; margin: 0px; height: 100px; //overflow: hidden; //overflow: auto; >div.faqticleList div, div.faqticlePreview div
document.getElementById("faqticleList").innerHTML = "
faqticleList
"; document.getElementById("faqticlePreview").innerHTML = "
faqticlePreview
";

Godlike! Coorect assumption and correct answer. It's depressing, actually. I've used to be the dude to go to when it came to CSS. Then I did something else for almost a year and poof, the savvyness is gone. Now, that I see your solution, I remember the trick with float and negative margin.

Float the left column, and make the right column a regular block element with overflow: hidden . That might be the simplest way to do it.

div.faqticleList < /* display: inline-block; */ float: left; . >div.faqticlePreview < /* display: inline-block; */ /* width: auto; */ overflow: hidden; . >

This will do what you want, but I would recommend you set your height to fixed, or it wont work,

div.faqticleList < background: #ffdd00; display: inline-block; width: 30%; height: 150px; >div.faqticlePreview < background: #bb88ff; display: inline-block; width: 69%; height: 100%; clear: both; >div.container < border: solid 1px #007700; margin: 0px; height: 100%; //overflow: hidden; //overflow: auto; display: block; clear: both; >

Not quite. The width is not 30% on the yellow. It's supposed to be fixed. But the purple one is supposed to fill out the rest of the remaining width. Also, the height of 100% is not in effect.

If you are trying to fix width this, thats easy, I was assuming you were setting this with the idea to create a responsive website.

The width of the left one is supposed to be fixed. The width of the right one varies with the page width. The same with the height, but in this case, it's always 100%. I don't get it to work, though.

You can use jquery to dynamically find the width.

JS: document.getElementById("faqticleList").innerHTML = "faqticleList"; document.getElementById("faqticlePreview").innerHTML = "faqticlePreview"; var difWidth = $('.container').width() - 212; $('#faqticlePreview').css( "width", difWidth ) 

Then, in your CSS, remove the width from faqticlePreview and float the other div left:

div.faqticleList < background: #ffdd00; display: inline-block; padding: 3px; width: 200px; height: 150px; float: left; >div.faqticlePreview

Note: The width you are subtracting needs to be 212. 200px width from the first div, plus 3px of padding on each side of both divs 200+(3x4)=212

Источник

Stretching a div vertically to fit content

How can I make the .welcome-content-wrap class stretch to fit its content and still keep the transparent style I've given it? I understand that you wouldn't usually add background images like I have but there's a reason why I have done it like I have

.welcome-section < position: relative; >.welcome-background < max-width: 100%; display: block; height: auto; >.welcome-content-wrap < position: absolute; top:10%; left: 0; right: 0; bottom: 10%; margin: 0 auto; max-width: 800px; background: rgb(0, 0, 0); background: rgba(0, 0, 0, 0.7); text-align: center; >.welcome-content-wrap img < display: block; width: 50%; margin: 50px auto; >.welcome-content-wrap p < color: #fff; margin: 25px; >.text-style
 

WELCOME to Popsie's of Bramhope a modern, local
Yorkshire fish and chip shop. At Popsie's all our food is hand
prepared by Popsie's Dad in the shop's kitchen every day.

All our fish is sourced from sustainable sources.

We cook fish to order so that it isn't left to dry out under lights,
like in too many chip shops.

Our potatoes are from local Yorkshire & Lincolnshire farms and
we pride ourselves on using the best and freshest ingredients.

Источник

How to vertically align and stretch content using css flexbox?

Css: how to vertically align and stretch content using CSS flexbox?

Flexbox is a powerful layout tool in CSS that makes it easy to align and distribute elements on a web page. Here are a few methods to vertically align and stretch content using flexbox:

Method 1: Using align-items and justify-content

  • Step 1 - Set the container to be a flex container by setting the display property to "flex" on the parent element.

The value of "center" will align the content to the center of the container.

.container  display: flex; align-items: center; >
  • Step 3 - Use the justify-content property to horizontally align the content within the flex container

The value of "center" will align the content to the center of the container.

.container  display: flex; align-items: center; justify-content: center; >
  • Step 4 - If you want to stretch the flex items to fill the entire container, you can use the flex property to set the flex-grow value to 1.
.container  display: flex; align-items: center; justify-content: center; > .item  flex-grow: 1; >

Method 2: Using flex-wrap

  • Step 1 - As before set the container to be a flex container by setting the display property to "flex" on the parent element.

The value of "stretch" will stretch the flex lines to fill the container

.container  display: flex; align-content: stretch; >

Method 3: Using align-self and flex-grow

  • Step 1 - As before set the container to be a flex container by setting the display property to "flex" on the parent element.
  • Step 2 - Use the align-self property to vertically align a single flex item within the flex container

The value of "center" will align the item to the center of the container.

.container  display: flex; > .item  align-self: center; >
  • Step 3 - If you want to stretch the flex item to fill the entire container, you can use the flex property to set the flex-grow value to 1.
.container  display: flex; > .item  align-self: center; flex-grow: 1; >

It is important to note that these properties and values are not exclusive to one another and can be combined in different ways to achieve the desired layout.

use case and depending on the layout you're trying to achieve, one method may be more appropriate than the others.

In addition to the properties I've already discussed, there are several other flexbox properties that can be used to further control the layout of elements within a flex container. Here are a few more that are worth mentioning:

  • flex-basis: sets the initial size of a flex item before any free space is distributed.
  • flex-shrink: sets how much the flex item will shrink relative to other flex items when there isn't enough space in the container.
  • order: sets the order of a flex item relative to other flex items.
  • flex-wrap: sets whether flex items are forced onto a single line or can wrap onto multiple lines.

It's worth noting that when working with flexbox, it's important to have a good understanding of the layout of your HTML elements. Flexbox is most effective when used to layout immediate children of a flex container. As you gain more experience with flexbox, you'll be able to use it more effectively to create more complex layouts. I hope you find this information helpful and good luck with your development journey!

Conclusion

In conclusion, flexbox is a powerful layout tool in CSS that makes it easy to align and distribute elements on a web page. To vertically align and stretch content using flexbox, you can use properties such as align-items , align-content , align-self , justify-content and set the flex-grow value to 1. Each of these properties have their own specific use cases and can be combined in different ways to achieve the desired layout. Additionally, there are other flexbox properties such as flex-basis , flex-shrink , order and flex-wrap that can be used to further control the layout of elements within a flex container. Flexbox is most effective when used to layout immediate children of a flex container and with more experience one can effectively create complex layouts.

Источник

Читайте также:  Java click to play
Оцените статью