Best way of styling CSS - html

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.

Related

How do I create 3 columns flex grid with first and last columns have 2 rows while the middle has rowspan?

As the title says, here's my example
I've already tried doing this using flex with position absolute but the problem is the responsiveness. How do I make this better? Is there a way that I could do this with just flexbox and not use position absolute while making the first row of the first and last column have equal height?
Here's my initial code:
<div class="block uk-width-1-1">
<div class="content-wrapper">
<div class="content uk-position-relative">
<div>
<div class="uk-grid uk-child-width-1-4 uk-flex-between">
<div class="card--wrapper">
<div class="card">
<h3>Lorem Ipsum</h3>
<p class="description">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam quis tortor ut ante rhoncus placerat. Nam at placerat tellus, a accumsan nisi.</p>
</div>
</div>
<div class="card--wrapper">
<div class="card">
<h3>Lorem Ipsum</h3>
<p class="description">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam quis tortor ut ante rhoncus placerat. Nam at placerat tellus, a accumsan nisi.</p>
</div>
</div>
</div>
<div class="uk-grid uk-child-width-1-4 uk-flex-between">
<div class="card--wrapper">
<div class="card">
<h3>Lorem Ipsum</h3>
<p class="description">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam quis tortor ut ante rhoncus placerat. Nam at placerat tellus, a accumsan nisi.</p>
</div>
</div>
<div class="card--wrapper">
<div class="card">
<h3>Lorem Ipsum</h3>
<p class="description">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam quis tortor ut ante rhoncus placerat. Nam at placerat tellus, a accumsan nisi.</p>
</div>
</div>
</div>
</div>
<div class="image--wrapper uk-position-absolute uk-width-1-3">
<div class="image">
<!-- Image here (middle column)-->
<img src="/../images/hero-sample.png" alt="image">
</div>
</div>
</div>
</div>
</div>
CSS
.image--wrapper {
position: absolute;
left: 50%;
transform: translateX(-50%);
top: 0;
}
.block {
background: green;
}
h3, p.description {
color: white;
font-family: 'Poppins';
}
p.description {
font-weight: 400;
font-size: 14px;
line-height: 21px;
color: rgba(255,255,255,0.7);
margin-bottom: 0;
}
h3 {
font-weight: 600;
font-size: 16px;
line-height: 28px;
}
.card {
max-width: 240px;
padding: 20px;
box-sizing: border-box;
border: 1px solid rgba(255,255,255, 0.3);
}
.child-width-1-4 > div {
width: 25%;
}
.width-1-3 {
width: 33%;
}
.grid {
display: flex;
flex-wrap: wrap;
margin: 18.12px 0 0 0;
padding: 0;
}
.flex-between {
justify-content: space-between;
}
.position-absolute {
position: absolute !important;
}
.position-relative {
position: relative !important;
}
#media screen and (max-width: 1200px) {
.content-wrapper {
padding: 60px 25px
}
}
In this solution, the outer container is a grid with 12 columns. The middle column (.col2) takes up twice the space of col1 and col3. The -1 in grid-column: 10 / -1 means to span to the end of the grid, wherever it is.
Inside, I make the first and last columns into flexboxes, so that their children can take up an even amount of space in their respective containers. All spacing between columns and rows is accomplished using gap.
.container {
display: grid;
grid-template-columns: repeat(12, 1fr);
min-height: 100vh;
min-height: 100dvh;
gap: 1rem;
padding: 1rem;
}
/* 3 columns */
.col1 {
grid-column: 1 / 4;
}
/* 6 columns */
.col2 {
grid-column: 4 / 10;
}
/* 3 columns */
.col3 {
grid-column: 10 / -1;
}
.col1,
.col3 {
display: flex;
flex-direction: column;
gap: 1rem;
}
.col1 > *,
.col3 > * {
flex: 1;
}
body { margin: 0; }
* { box-sizing: border-box; }
.col2, .container > * > * { border: 1px solid; }
<div class="container">
<div class="col1">
<div></div>
<div></div>
</div>
<div class="col2"></div>
<div class="col3">
<div></div>
<div></div>
</div>
</div>
jsFiddle
there is more than one way to do that. I personally prefer using flex for such situations because it's more flexible for adjustment.
so basically, I used three containers in a row flex-direction then in the containers on the sides, I put 2 divs in each with column flex-direction. that's it.
you can control max and min width as you wish.
body {
background: lightgrey;
display: flex;
justify-content: center;
}
#container {
/* background: lightcoral; */
width: 100%;
height: 400px;
display: flex;
flex-direction: row;
}
.middle {
width: 60%;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
.left, .right{
width: 20%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.left div,
.right div {
border: 1px solid;
width: 100%;
height: 50%;
margin: 10px;
}
.middle div {
border: 1px solid;
width: 95%;
height: 100%;
margin: 10px;
}
<body>
<div class="container" id="container">
<div class="left">
<div></div>
<div></div>
</div>
<div class="middle">
<div></div>
</div>
<div class="right">
<div></div>
<div></div>
</div>
</div>
</body>
In your problem its better to display grid instead of flex
div1: middle
div2: left-top
div3: left-bottom
div4: right-top
div5: right-bottom
.parent {
display: grid;
grid-template-columns: repeat(5, 1fr);
grid-template-rows: repeat(2, 1fr);
grid-column-gap: 0px;
grid-row-gap: 0px;
}
.div1 { grid-area: 1 / 2 / 3 / 5; }
.div2 { grid-area: 1 / 1 / 2 / 2; }
.div3 { grid-area: 2 / 1 / 3 / 2; }
.div4 { grid-area: 1 / 5 / 2 / 6; }
.div5 { grid-area: 2 / 5 / 3 / 6; }

Columns side by side with css grid

I am having a problem understanding how CSS grid works.
I want 3 columns side by side with a specific width and height, but there's a gap between each columns.
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr;
grid-column-gap: 0px;
}
.left {
grid-column: 1;
border: 1px solid red;
height: 500px;
width: 300px;
}
.middle {
grid-column: 2;
border: 1px solid red;
height: 500px;
width: 300px;
}
.right {
grid-column: 3;
border: 1px solid red;
height: 500px;
width: 300px;
}
<div class="grid-container">
<div class="left">1</div>
<div class="middle">2</div>
<div class="right">3</div>
</div>
In which way I can put away this gap? And if anyone can explain me how to reduce the line of code in css I would be grateful, thanks!
EDIT
I solved in this way
.wrapper{
display: grid;
place-items: center;
min-height: 100vh;
}
.grid-container{
margin: 0 auto;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr;
grid-column-gap: 0px;
width: 60vw;
height: 450px;
}
.left{
background-color: hsl(31, 77%, 52%);
grid-column: 1;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
}
.middle{
background-color: hsl(184, 100%, 22%);
grid-column: 2;
}
.right{
background-color: hsl(179, 100%, 13%);
grid-column: 3;
border-top-right-radius: 10px;
border-bottom-right-radius: 10px;
}
<div class="wrapper">
<div class="grid-container">
<div class="left">
<img src="images/icon-sedans.svg" alt="sedans" class="logo">
<h2 class="text-title">Sedans</h2>
<div class="main-text">
Choose a sedan for its affordability and excellent fuel economy. Ideal for cruising in the city
or on your next road trip.
</div>
<button class="btn">Learn More</button>
</div>
<div class="middle">
<img src="images/icon-suvs.svg" alt="sedans" class="logo">
<h2 class="text-title">Suvs</h2>
<div class="main-text">
Take an SUV for its spacious interior, power, and versatility. Perfect for your next family vacation
and off-road adventures.
</div>
<button class="btn">Learn More</button>
</div>
<div class="right">
<img src="images/icon-luxury.svg" alt="sedans" class="logo">
<h2 class="text-title">Luxury</h2>
<div class="main-text">
Cruise in the best car brands without the bloated prices. Enjoy the enhanced comfort of a luxury
rental and arrive in style.
</div>
<button class="btn">Learn More</button>
</div>
</div>
</div>
But now what I want to do is to make all of this to be responsive
Specify your width on your grid parent, remove any static width on your child elements. With the fraction set in grid-template-columns: 1fr 1fr 1fr; each column will take up a third of the parents width. So if your parent is set to say 80 view width => 80% of the view port, then your columns will spread out over a third of that width each.
If you have had 4 items each set to 1fr, then they would take up 25%, 5 would take up 20%, basically => number of children elements/parents width.
.grid-container {
margin: auto;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr;
grid-column-gap: 0;
width: 80vw;
}
.left>ul {
height: 80%;
background: pink;
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: space-between;
}
.left {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
border: 1px solid red;
grid-column: 1;
}
.middle {
padding: 5px;
grid-column: 2;
border: red solid 1px;
}
.middle span {
display: flex;
flex-direction: column;
justify-content: start;
padding: 5px;
grid-column: 2;
background: lightblue;
border-bottom: 1px solid black;
}
.right {
grid-column: 3;
border: 1px solid red;
padding: .2rem;
display: flex;
flex-direction: column;
justify-content: space-around;
}
.row {
padding: 0 1rem;
display: flex;
justify-content: space-between;
align-items: start;
background: #EEE;
}
.right div:nth-of-type(2) {
color: #a2a2a2;
text-align: center;
}
<div class="grid-container">
<div class="left">Here is some content for column 1 with list items
<ul>
<li>
display: flex
</li>
<li>
flex-direction: column
</li>
<li>
justify-content: space-around
</li>
<li>
align-items: flex-start
</li>
</ul>
</div>
<div class="middle">
<span>This is inside a span tag</span>
<span>Parent has flex direction of column</span>
<span>Justify Content set at start</span>
<span>this is inside a span tag</span> More content for column 2. it is a bit longer than the first textual content. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum viverra pulvinar tincidunt. Nam consequat metus et cursus auctor.
Suspendisse posuere sem ut tortor lacinia, nec tempor turpis ultrices. Sed vitae gravida orci.</div>
<div class="right">
<div class="row">
<h3>Row</h3>
<h3>Space</h3>
<h3>Between</h3>
</div>
<span>Parents flex <span style="background: pink;">flex-direction </span> and <span style="background: pink;">display-flex</span> <b>flex</b> and <b>column</b></span></span>
<span>Parents flex <span style="background: pink;">justify-content</span> is set to <b>space-around</b></span>
<div>Good ole text-align: center... Vestibulum viverra pulvinar tincidunt. Nam consequat metus et cursus auctor. Suspendisse posuere sem ut tortor lacinia, nec tempor turpis ultrices.
</div>
</div>
</div>
Example of vertically centering text with flex-direction set to column.
body {
margin: 0;
padding: 0;
}
.parent {
text-align: center;
display: flex;
flex-direction: column;
justify-content: center;
height: 100vh;
}
.child {
margin: 0 auto;
height: 200px;
width: calc(200px - 1rem);
background: lightblue;
display: flex;
flex-direction: column;
justify-content: center;
padding: .5rem;
}
<div class="parent">
<div class="child">Vertically centering using flex.
<br><br> Justify content when parents flex-direction is set to column, will center content vertically.</div>
</div>
You can simply specify the widths that you need in the grid-template-columns property. There is no gap visible between columns.
If you want the red borders you can specify a common class for those divs and just do it once.
.grid-container{
display: grid;
grid-template-columns: 300px 300px 300px;
grid-template-rows: 1fr;
column-gap: 0;
height: 500px;
}
.left{
border: 1px solid red;
}
.middle{
border: 1px solid red;
}
.right{
border: 1px solid red;
}
<div class="grid-container">
<div class="left">1</div>
<div class="middle">2</div>
<div class="right">3</div>
</div>

testing css layout in firefox vs safari on ios

I would really appreciate it if someone can enlighten me with my css layout issue. On my localhost, I am using Firefox's responsive design mode tool to check my layout in iPhoneX and everything seems to be going according to plan. However, when I deploy it to Heroku, and open the page, everything goes haywire. Here is what it looks like:
*EDIT: The one on the left is from Firefox's Responsive Design Mode, the one on the right is the from iOs' Safari.
First of all, forgive my naivete but I was thinking that the simulation in Firefox should match the actual output in iOs Safari otherwise it would be difficult to test layouts. :) So the code snippet can verify this when you run the snippet on mobile it replicates my exact issue.
Secondly, I can't for the life of me, see what actually I am doing wrong with my CSS. (facepalm). Again, any help is appreciated.
#splash-bg {
height: 150px;
width: 100vw;
background: url("https://images.unsplash.com/photo-1425326452142-67c31f601d2f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2982&q=80") no-repeat center 50px fixed;
background-size: contain;
border: 1px solid black;
}
#brand-info {
width: 100%;
display: flex;
flex-direction: column-reverse;
}
#brand-info #desc {
padding: 20px;
background-color: #ffd400;
}
#brand-info #desc img {
width: 100%;
max-height: 50px;
margin: 20px 0 10px 0;
border: 1px solid black;
}
#brand-info #desc p {
text-align: justify;
font-size: 0.8em;
line-height: 1.2em;
color: #000;
}
#brand-info #desc .social {
display: flex;
flex-direction: columns;
justify-content: space-evenly;
margin-top: 50px;
}
#brand-info .store-slides {
grid-column: 1 / 11;
grid-row: 1 / 4;
display: grid;
grid-template-columns: repeat(10, 1fr);
grid-template-rows: repeat(10, 1fr);
background-color: #ffd400;
// width: 100vw;
}
#brand-info .store-slides .brand-stores {
grid-column: 1 / 11;
grid-row: 1 / 11;
width: 100%;
display: grid;
grid-template-columns: repeat(10, 1fr);
grid-template-rows: repeat(10, 1fr);
}
#brand-info .store-slides .brand-stores img {
width: 100%;
height: 100%;
object-fit: contain;
grid-column: 1 / 11;
grid-row: 1 / 11;
border: 1px solid black;
}
#brand-info .store-slides .brand-stores .label {
grid-column: 4 / 11;
grid-row: 6 / 7;
font-size: 0.8em;
background-color: rgba(255, 212, 0, 0.5);
display: flex;
align-items: center;
padding: 0 0 0 20px;
color: #000;
font-weight: bold;
}
#brand-info .store-slides #left {
grid-column: 1 / 2;
grid-row: 5 / 7;
transform: rotate(90deg);
align-self: center;
justify-self: center;
}
#brand-info .store-slides #right {
grid-column: 10 / 11;
grid-row: 5 / 7;
transform: rotate(-90deg);
align-self: center;
justify-self: center;
}
<div id="fp-container">
<div id="splash-bg">
</div>
<div id="brand-info">
<div id="desc">
Brand Logo
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed malesuada ex eget enim lobortis dictum pharetra vel odio. Integer vulputate, ligula at placerat accumsan, nibh lectus consectetur metus, in consectetur nulla nunc at ante. Morbi felis leo, pellentesque
sit amet enim sit amet, blandit malesuada lacus.
</p>
<div class="social">
instagram
<router-link to="/locator">facebook</router-link>
</div>
</div>
<div class="store-slides">
<div class="brand-stores">
<img src="https://images.unsplash.com/photo-1425326452142-67c31f601d2f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2982&q=80" alt="" />
<div class="label">Store Name 1</div>
</div>
<div id="left" class="arrows">left arrow</div>
<div id="right" class="arrows">right arrow</div>
</div>
</div>
</div>

how to manually position multiple divs within another div

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).

Content overflows from 100% height DIV

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>