I want to make a div which:
Stretches to 100% width and height of browser window,
Makes all content inside centered vertically and horizontally,
Has min-height = all content + 10% of top&bottom padding.
I've made some code:
html {
height: 100%;
}
body {
height: 100%;
}
.blah {
display: flex;
flex-direction: column;
width: 100%;
height: 100%;
justify-content: center;
text-align: center;
padding: 10% 0 10% 0;
background: #ffb3b3;
}
<div class="blah">
<p>Here goes some content</p>
</div>
The same on jsfiddle
As you can see, it works fine, except point 3 - when scaling down, the content overflows the div around it:
screen
I've tried to set for .blah:
height: auto;
min-height: 100% !important;
position: relative;
but then it doesn't work on bigger resolutions - div is bigger than the browser height.
This solution doesn't work.
I will be extremely grateful for any ideas.
you just need to use box-sizing:border-box
html,
body {
height: 100%;
margin: 0
}
.blah {
display: flex;
flex-direction: column;
justify-content: center;
text-align: center;
background: #ffb3b3;
min-height: 100%;
padding: 10% 0;
box-sizing: border-box;
}
<div class="blah">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed maximus rhoncus erat sit amet ullamcorper. Cras quis vulputate ex, ut sollicitudin massa. Vivamus vitae ipsum posuere, eleifend quam quis, pulvinar tellus. Cras semper, lectus sit amet molestie
</div>
Related
For a homepage designed in flexbox, I am encountering a problem which I cannot solve in CSS, unfortunately. I also wonder whether it is doable at all in CSS only. If not, then I will need to find a solution in JS.
The website is mobile first. In the flex container there are an H2 element, 3 divs: .intro, .image and .text. In .text there are paragraphs and a button.
In the mobile and tablet queries all works fine, except for the desktop version where the .text div should go underneath the .intro div, which both go to the right of the .image div. Left and right should be 50% both.
This is the code:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row wrap;
flex-flow: row wrap;
font-weight: bold;
text-align: center;
}
.flex-container > * {
padding: 10px;
flex: 1 100%;
}
.text {
text-align: left;
background: cornflowerblue;
}
.intro {
background: yellow;
}
.image {
background: moccasin;
height: 200px;
}
#media all and (min-width: 600px) {
.image { flex: 1 0px; }
.text { flex: 1 0px; }
.image { order: 1; }
.text { order: 2; }
}
#media all and (min-width: 769px) {
.flex-container :not(.image){
-webkit-flex-flow: column nowrap;
flex-flow: column nowrap;
}
.intro { flex: 1 50%; }
.image { flex: 12 0px; }
.text { flex: 1 50%; }
.image { order: 1; }
.intro { order: 2; }
.text { order: 3; }
}
</style>
</head>
<body>
<h2>Title</h2>
<div class="flex-container">
<div class="intro">
<p>Intro: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sed ex turpis.</p>
</div>
<div class="image">Image</div>
<div class="text">
<p>Text: Cras luctus nibh lectus, in ullamcorper ex tempor eleifend. Nulla bibendum, eros a consequat vestibulum, orci massa fermentum quam, sed commodo nunc ex vitae nisl.</p>
<button>click me</button>
</div>
</div>
</body>
</html>
Here's how it should look like:
how the desktop media query should look like
if you have nothing about grid, you may use it for the desktop layout :
.flex-container {
font-weight: bold;
text-align: center;
}
.flex-container>* {
padding: 10px;
flex: 1 100%;
}
.text {
text-align: left;
background: cornflowerblue;
}
.intro {
background: yellow;
}
.image {
background: moccasin;
height: 200px;
}
#media all and (min-width: 600px) {
.flex-container {
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row wrap;
flex-flow: row wrap;
}
.image {
flex: 1 0px;
}
.text {
flex: 1 0px;
}
.image {
order: 1;
}
.text {
order: 2;
}
}
#media all and (min-width: 769px) {
.flex-container {
display: grid;
grid-template-columns: 1fr 1fr;
}
.intro,
.text {
grid-column: 2;
}
.image {
grid-row:1 / span 2
}
}
<h2>Title</h2>
<div class="flex-container">
<div class="intro">
<p>Intro: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sed ex turpis.</p>
</div>
<div class="image">Image</div>
<div class="text">
<p>Text: Cras luctus nibh lectus, in ullamcorper ex tempor eleifend. Nulla bibendum, eros a consequat vestibulum, orci massa fermentum quam, sed commodo nunc ex vitae nisl.</p>
<button>click me</button>
</div>
</div>
usefull link : https://css-tricks.com/snippets/css/complete-guide-grid/
I'm trying to get even space between the font awesome icon and the paragraph of text to it's right, which is separated by a divider (in this the case, the right-border of the icon).
How can I make the space between the icon and it's border even, the same as the space between icons border and paragraph of text? I'm using flex's space-between at the moment, as well as some padding, but the space isn't evenly distributed, and it gets worse as the screen resizes.
body {
height: 100vh;
width: 100%;
}
#container {
height: 90%;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
#display {
height: 76%;
width: 100%;
background-color: #ECECEC;
overflow: hidden;
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
.content {
display: flex;
justify-content: space-evenly;
align-items: center;
flex-direction: row;
height: 100%;
width: 95%;
}
.content i {
width: 25%;
display: flex;
justify-content: center;
align-items: center;
border-right: 1px solid black;
}
.text {
width: 50%;
justify-content: center;
align-items: center;
padding: 0 6%;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
<div id="container">
<div id="display">
<div class="content">
<i class="fas fa-balance-scale fa-7x"></i>
<div class="text">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut scelerisque volutpat libero, at venenatis dolor rutrum vel. Donec fermentum eleifend tortor, at sollicitudin est rutrum nec. Fusce eget vehicula ex. Vestibulum semper gravida nulla, in aliquam ipsum dignissim nec.</p>
</div>
</div>
</div>
</div>
Try this.
.content i {
padding-right: 35px;
}
I'm using css grid and in this case i have made a div that fit in a grid (the green box)(at the right the color is green not yellow) and inside this div i have made another grid (2 columns) and in one at the left there is the image and at the right there is another div (the red one) with a paragraph.
As you can see i have used and height of 61vh otherwise the image doesn't fit well and than i had to set also an height for the red box otherwise it goes outside of the green box.
I think this is not ther right way of doing it, how can i do it without setting an exact height?
.GreenBox{
margin-bottom: 10px;
grid-column: 2/4;
grid-row: 6/7;
width: 100%;
height: 61vh;
display: grid;
margin-left: -200px;
margin-right: -10px;
grid-template-columns: 50% 50%;
grid-template-rows: auto;
border: 2px solid greenyellow;
}
.RedBox{
grid-column: 2;
display: grid;
grid-template-columns: 1fr;
grid-template-rows: repeat(3, 1fr);
border: 1px solid red;
height: 60.6vh;
padding-left: 10px;
}
.PurpleBox{
grid-row: 2;
border: 1px solid blueviolet;
}
.Image{
width: 100%;
height: auto ;
box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.4);
}
<div className="GreenBox">
<img className="Image" src={foto2}/>
<div className="RedBox">
<p className="Paragraph">ILorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas a tellus eget velit cursus feugiat. Proin quis condimentum velit, a pellentesque erat. Maecenas consectetur eros et nibh condimentum pharetra. </p>
</div>
</div>
PS: the problem is that if that if i resize the page there is always a bottom gap, i would like to remove that and fit well the content in the greenbox.
I am trying to achieve this layout The black thin line shows the border of the outer div. Inside, there are two divs (red and blue). I would like to position them next to each other with a little space in between. Additionally, the top/bottom of the red div and the top/bottom of the blue div should be equal. The left and right should also be equal. This should be equal no matter the size of the browser.
I've tried playing around with the margins but I can't do it so that its exactly equal. Here's is the link for the full code of my attempt.
Here is a snippet of my code:
#about {
background-color: #D1C9BE;
border-style: solid;
border-color: black;
position: relative;
}
#aboutImage {
border-style: dotted;
border-color: white;
background-color: red;
height: 150px;
width: 150px;
margin-top: 200px;
}
#aboutInfo {
border-style: dotted;
border-color: white;
background-color: blue;
width: 300px;
height: 400px;
font-size: 35px;
text-align: right;
margin-left: 20px;
}
Also is there a way to automatically size a div based on how much text is in it? I've seen solutions for two divs of equal size just positioned side by side but how would I do so with two divs, different sizes?
Use flex-box. Plus don't mix flex box and traditional positioning styles.
You can accomplish what you need with display: flex and justify-content: space-evenly; and align-items: center;
body {
margin: 0;
}
html, body {
height:100%;
}
/* FULLPAGE */
.section {
height: 100vh;
display: flex;
justify-content: space-evenly;
align-items: center;
}
/* ABOUT */
#about {
background-color: #D1C9BE;
}
#aboutImage {
border-color: white;
background-color: red;
height: 150px;
width: 150px;
}
#aboutInfo {
border-color: white;
background-color: blue;
font-size: 35px;
}
#aboutInfo p {
font-size: 15px;
}
<html>
<body>
<section id="about" class="section">
<!-- Picture -->
<div id="aboutImage"></div>
<!-- Description -->
<div id = "aboutInfo">
Lorem Ipsum.
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. <br> Suspendisse malesuada lacus commodo enim varius, <br> non gravida ipsum faucibus. Vivamus pretium pulvinar <br> elementum. In vehicula ut elit vitae dapibus. Cras ipsum <br> neque, finibus id mattis vehicula, rhoncus in mauris. In <br> hendrerit vitae velit vel consequat. Duis eleifend dui vel <br> tempor maximus. Aliquam rutrum id dolor vel ullamcorper. <br> Nunc cursus sapien a ex porta dictum.
</p>
</div>
</section>
</body>
<html>
You can use flex.
For the parent container, type it
.container {
display: flex;
flex-direction:row;
align-items: center;
}
You can use flexbox for that. It will help you horizontally (justify-content) and vertically (align-items) center your elements with equal space around them (justify-content: space-evenly). In this case your child elements don't need any extra styling.
#about {
display: flex;
align-items: center;
justify-content: space-evenly;
padding: 20px;
background-color: #D1C9BE;
border-style: solid;
border-color: black;
}
#aboutImage {
border-style: dotted;
border-color: white;
background-color: red;
height: 150px;
width: 150px;
}
#aboutInfo {
border-style: dotted;
border-color: white;
background-color: blue;
width: 300px;
height: 400px;
font-size: 35px;
text-align: right;
}
As for your last question re automatically sized divs, this is actually the default if you omit the height property. The div will then be just as tall as your number of lines of text (assuming you keep the width set).
I am experimenting with flexboxes and the tutorial from codebin and I want the layout to be a full screen flexbox layout.
The Jfiddle can't emulate the 100% stretch thing for some reason but the screenshot seems to make the issue clear.
But I want the header to have a predefined height without it stretching and there is a lot of empty space in the elements that don't seem to wrap itself to it's content.
This goes for all the items really. I would want a layout where I can define the height of items without creating non wrapped gaps in the process. % doesn't seem to be working.
http://jsfiddle.net/rhkahtaa/
<html>
<style>
body {
display:flex;
}
.wrapper {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row wrap;
flex-flow: row wrap;
font-weight: bold;
text-align: center;
align-items:stretch;
}
.wrapper > * {
padding: 10px;
flex: 1 100%;
flex-basis: 1 auto;
}
.header {
height:20%;
flex-shrink:1;
background: red;
}
.footer {
background: green;
}
.main {
width:80%;
text-align: left;
background: blue;
}
.aside-1 {
width:10%;
background: gold;
}
.aside-2 {
width:10%;
background: pink;
}
#media all and (min-width: 600px) {
.aside { flex: 1 auto; }
}
#media all and (min-width: 800px) {
.main { flex: 2 0px; }
.aside-1 { order: 1; }
.main { order: 2; }
.aside-2 { order: 3; }
.footer { order: 4; }
}
</style>
<body>
<div class="wrapper">
<header class="header">Header</header>
<article class="main">
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>
</article>
<aside class="aside aside-1">Aside 1</aside>
<aside class="aside aside-2">Aside 2</aside>
<footer class="footer">Footer</footer>
</div>
</body>