center horizontally and vertically paragraph inside div - css

how i can center horizontally and vertically the paragraph inside the div , i came to this result
.Benefits {
min-height: 20%;
width: 100%;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
}
<section class="Benefits">
<div>
<h1>Lorem ipsum </h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Omnis nemo quas dolor optio autem! Veniam, dolore neque nam harum quae excepturi distinctio optio explicabo ipsum laudantium nesciunt et autem a.</p>
</div>
<div>
<h1>Lorem ipsum n</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Omnis nemo quas dolor optio autem! Veniam, dolore neque nam harum quae excepturi distinctio optio explicabo ipsum laudantium nesciunt et autem a.</p>
</div>
<div>
<h1>Lorem ipsum </h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Omnis nemo quas dolor optio autem! Veniam, dolore neque nam harum quae excepturi distinctio optio explicabo ipsum laudantium nesciunt et autem a.</p>
</div>
</section>

.Benefits div {
min-height: 20%;
margin: auto;
width: 50%;
}
<section class="Benefits">
<div>
<h1>Lorem ipsum </h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Omnis nemo quas dolor optio autem! Veniam, dolore neque nam harum quae excepturi distinctio optio explicabo ipsum laudantium nesciunt et autem a.</p>
</div>
<div>
<h1>Lorem ipsum n</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Omnis nemo quas dolor optio autem! Veniam, dolore neque nam harum quae excepturi distinctio optio explicabo ipsum laudantium nesciunt et autem a.</p>
</div>
<div>
<h1>Lorem ipsum </h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Omnis nemo quas dolor optio autem! Veniam, dolore neque nam harum quae excepturi distinctio optio explicabo ipsum laudantium nesciunt et autem a.</p>
</div>
</section>

I have noted in my CSS what's going on here:
I think you're best using two cases of display: flex. One horizontally, one vertically.
.Benefits {
width: 100%;
max-width: 1248px;
margin: 0 auto; /* centers container */
padding: 10px; /* this combined with the margin in the divs will make the margins appear consistent */
box-sizing: border-box;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-wrap: wrap;
flex-wrap: wrap;
flex-direction: row; /* sort into rows */
align-items: center;
justify-content: space-between;
text-align: center;
}
.Benefits div {
width: calc((100% / 3) - 20px); /* calculate width for maximum accuracy | minus margin */
margin: 10px; /* space between boxes */
padding: 20px; /* space inside boxes */
box-sizing: border-box; /* calculates border and padding in width */
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-wrap: wrap;
flex-wrap: wrap;
flex-direction: column; /* sort into columns */
align-items: center;
justify-content: center;
flex-grow: 1; /* forces singular div that falls beneath to be full width */
background-color: rgba(0,0,0,0.25);
}
#media screen and (min-width: 640px) and (max-width: 1023px) {
.Benefits div {
width: calc((100% / 2) - 20px); /* calculate width for maximum accuracy | minus margin */
}
}
#media screen and (max-width: 639px) {
.Benefits div {
width: 100%; /* calculate width for maximum accuracy | minus margin */
}
}
.Benefits div h1 {
margin-bottom: 20px;
}
<section class="Benefits">
<div>
<h1>Lorem ipsum </h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Omnis nemo quas dolor optio autem! Veniam, dolore neque nam harum quae excepturi distinctio optio explicabo ipsum laudantium nesciunt et autem a.</p>
</div>
<div>
<h1>Lorem ipsum n</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Omnis nemo quas dolor optio autem! Veniam, dolore neque nam harum quae excepturi distinctio optio explicabo ipsum laudantium nesciunt et autem a.</p>
</div>
<div>
<h1>Lorem ipsum </h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Omnis nemo quas dolor optio autem! Veniam, dolore neque nam harum quae excepturi distinctio optio explicabo ipsum laudantium nesciunt et autem a.</p>
</div>
</section>

Related

How to resize grid cells to fit their contents? I have spent couple days now trying to figure this out now

I'm trying to resize my grid cells to fit their contents but when i gave them heigh induvidually it distored the whol layout. I have a grid container that has two container inside. the first one is a grid container and the second one is a flex box
take a look at my css maybe I got the layout wrong `.
mycontainer {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
padding: 10px;
}
.mycontainer > div {
background-color: gray;
border: 1px red solid;
}
.boxe1 {
grid-column: 1 / span 2;
}
.seconContainer > div {
background-color: gray;
border: 1px red solid;
}
.boxe1 {
height: 435px;
}
.boxe5 {
height: 535px;
}
.seconContainer {
display: flex;
flex-direction: column;
gap: 10px;
padding: 10px;
}
.bigcontainer {
display: grid;
grid-template-columns: 1fr 1fr;
padding: 25px;
}
#media only screen and (max-width: 800px) {
.bigcontainer {
background-color: blue;
display: flex;
flex-direction: column;
}
}
here is my HTML
<div class="bigcontainer">
<div class="mycontainer">
<div class="boxe1">
<p><br/></p>
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit.<br/>
Aliquid sit provident doloremque vitae deserunt<br/> rerum placeat
neque tenetur, atque
unde. Dolores consectetur,<br/> assumenda animi aliquid incidunt
enim nam natus sunt.</p><p><br/></p> <p>Lorem ipsum dolor sit, amet consectetur adipisicing elit.<br/>
Aliquid sit provident doloremque vitae deserunt<br/> rerum placeat
neque tenetur, atque
unde. Dolores consectetur,<br/> assumenda animi aliquid incidunt
enim nam natus sunt.</p><p><br/></p>
</div>
<div class="boxe2">
<img src="textimages/box3.jpg" alt="" class="img-fluid"/>
</div>
<div class="boxe3">
<img src="textimages/box3.jpg" alt="" class="img-fluid"/>
</div>
</div>
<div class="seconContainer">
<div class="boxe4">
<img src="textimages/box4.jpg" alt="" class="img-fluid" />
</div>
<div class="boxe5">
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit.<br/>
Aliquid sit provident doloremque vitae deserunt<br/> rerum placeat
neque tenetur, atque
unde. Dolores consectetur,<br/> assumenda animi aliquid incidunt
enim nam natus sunt.</p><p><br/></p> <p>Lorem ipsum dolor sit, amet consectetur adipisicing elit.<br/>
Aliquid sit provident doloremque vitae deserunt<br/> rerum placeat
neque tenetur, atque
unde. Dolores consectetur,<br/> assumenda animi aliquid incidunt
enim nam natus sunt.</p><p><br/></p>
</div>
</div>
`
this is what I'm trying to achieve
This might help you and i used sample images
I used flex box for responsive instead of grid.
* {
box-sizing: border-box;
}
body{
overflow:hidden;
overflow-y:scroll;
}
.bigcontainer {
display: flex;
flex-wrap: wrap;
width: 98vw;
height: 100vh;
font-family: 'sans';
}
.bigcontainer::after {
content: '';
background: #f6f2ea;
width: 60%;
height: inherit;
position: absolute;
z-index: -1;
right: 0;
}
.bigcontainer > div {
width: 50%;
height: 50%;
padding: 15px;
}
.box2 img {
width: 100%;
height: 100%;
border-radius: 5px;
}
.box3 {
display: flex;
gap: 10px;
}
.box3 img {
width: 50%;
border-radius: 5px;
}
#media screen and (max-width: 768px) {
.bigcontainer > div {
width:100%;
height: max-content;
flex: 0 0 100%;
}
.box2 {
order: 4
}
.box3 {
flex-wrap: wrap;
}
.box3 img {
width: 100%;
}
.bigcontainer::after {
display: none;
}
}
<div class="bigcontainer">
<div class="box1">
<h2>Title</h2>
<p>Lorem apsum dolor sit, amet consectetur adipisicing elit.<br/>
Aliquid sit provident doloremque vitae deserunt<br/> rerum placeat
neque tenetur, atque
unde. Dolores consectetur,<br/> assumenda animi aliquid incidunt
enim nam natus sunt.</p>
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit.<br/>
Aliquid sit provident doloremque vitae deserunt<br/> rerum placeat
neque tenetur, atque
unde. Dolores consectetur,<br/> assumenda animi aliquid incidunt
enim nam natus sunt.</p>
</div>
<div class="box2">
<img src="https://static.asianpaints.com/content/dam/sleek/kitchen-landing/TheRomantic%20Inspiration.jpg" alt="" class="img-fluid" />
</div>
<div class="box3">
<img src="https://cdn.onekindesign.com/wp-content/uploads/2013/09/Small-Kitchen-Ideas-01-1-Kindesign.jpg" alt="" class="img-fluid"/>
<img src="https://cdn.onekindesign.com/wp-content/uploads/2013/09/Small-Kitchen-Ideas-01-1-Kindesign.jpg" alt="" class="img-fluid"/>
</div>
<div class="box4">
<h2>Title</h2>
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit.<br/>
Aliquid sit provident doloremque vitae deserunt<br/> rerum placeat
neque tenetur, atque
unde. Dolores consectetur,<br/> assumenda animi aliquid incidunt
enim nam natus sunt.</p>
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit.<br/>
Aliquid sit provident doloremque vitae deserunt<br/> rerum placeat
neque tenetur, atque
unde. Dolores consectetur,<br/> assumenda animi aliquid incidunt
enim nam natus sunt.</p>
</div>

Centering multiple items in flexbox not working as expected [duplicate]

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 2 years ago.
I am trying to vertically content within a div with flexbox, so that it always remains centered.
However it is not working as expected. Interested to see where I'm going wrong.
Just to confirm, the 'left div' should display the contents in regular vertical order, not horizontally as is currently happening.
section {
display:flex;
}
div {
padding:12px;
width:400px;
height:300px;
display:flex;
align-items:center;
}
.left {background:#bada55}
.right {background:red}
<section>
<div class="left">
<h2>Very long title</h2>
<p>Caption goes here</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque at magni est facilis error! Perspiciatis ab tempore qui mollitia, nihil itaque praesentium deleniti hic asperiores soluta, rem natus. Quidem, necessitatibus?</p>
</div>
<div class="right">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsa distinctio, labore, quod ex blanditiis totam esse eligendi consequatur inventore reprehenderit temporibus architecto molestias ratione, ullam deserunt tempora nesciunt corrupti! Tempora!</p>
</div>
</section>
Link to codepen
You can use flex-direction on .left to make it center vertically:
section {
display: flex;
}
div {
padding: 12px;
width: 400px;
height: 300px;
display: flex;
}
.left {
background: #bada55;
flex-direction: column;
justify-content: center;
}
.right {
background: red;
align-items: center;
}
<section>
<div class="left">
<h2>Very long title</h2>
<p>Caption goes here</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque at magni est facilis error! Perspiciatis ab tempore qui mollitia, nihil itaque praesentium deleniti hic asperiores soluta, rem natus. Quidem, necessitatibus?</p>
</div>
<div class="right">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsa distinctio, labore, quod ex blanditiis totam esse eligendi consequatur inventore reprehenderit temporibus architecto molestias ratione, ullam deserunt tempora nesciunt corrupti! Tempora!</p>
</div>
</section>
EDIT based on your comment, I believe this works:
section {
display: flex;
align-items: center;
height: 300px;
}
div {
width: 400px;
padding: 12px;
}
div.right {
display: flex;
align-items: center;
}
<section>
<div class="left">
<h2>Very long title</h2>
<p>Caption goes here</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque at magni est facilis error! Perspiciatis ab tempore qui mollitia, nihil itaque praesentium deleniti hic asperiores soluta, rem natus. Quidem, necessitatibus?</p>
</div>
<div class="right">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsa distinctio, labore, quod ex blanditiis totam esse eligendi consequatur inventore reprehenderit temporibus architecto molestias ratione, ullam deserunt tempora nesciunt corrupti! Tempora!</p>
</div>
</section>

when overflow: auto, padding-bottom is not seen

<div class="container">
<div class="content">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Pariatur amet, vitae fuga provident et quae aut minus voluptate quidem maiores at recusandae sit deleniti quia dolore, illum reiciendis! Hic, optio Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fugit accusantium, obcaecati dicta unde repellat illo maxime! Magni officiis, culpa nihil, sequi aliquid vel voluptas quidem laboriosam, omnis nam fuga veniam.</div>
</div>
.content {
width: 200px;
height: 150px;
padding: 20px;
background: yellow;
overflow: scroll;
box-sizing: border-box;
}
http://jsfiddle.net/v6yjLdnp/
Why doesn't the padding-bottom work in this case when scrolling? And how do I make this possible?
I suppose you mean the padding at the bottom of the scrolled content. This seems to be a browser issue - see the comments. But with the following code it should work properly in all browsers.
Transfer some of the settings to the container, then it works as desired:
body {
margin: 0;
}
.container {
width: 200px;
height: 150px;
overflow: scroll;
}
.content {
padding: 20px;
background: yellow;
}
<div class="container">
<div class="content">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Pariatur amet, vitae fuga provident et quae aut minus voluptate quidem maiores at recusandae sit deleniti quia dolore, illum reiciendis! Hic, optio Lorem ipsum dolor sit amet, consectetur adipisicing
elit. Fugit accusantium, obcaecati dicta unde repellat illo maxime! Magni officiis, culpa nihil, sequi aliquid vel voluptas quidem laboriosam, omnis nam fuga veniam.</div>
</div>
The problem is probably be caused by the scroll bars.
This is a workaround with using pseudo elements in css, but it might solve your problem:
.content::after { content: ''; display: block; width: 100%; height: 20px; }
EDIT: Remove the padding bottom of the content element to make it work in all browsers:
.content { padding-bottom: 0px; }

Center text in column/vertically, CSS Grid

I have a simple grid (https://jsfiddle.net/3f5oLjxu/1/), how can I get the links on the left side of the grid to center vertically, instead of being positioned at the top.
css:
.grid {
display: grid;
grid-template-columns: 1fr 3fr;
margin: 0px 12%;
}
.grid>* {
border: 1px solid lightgray;
padding: 15px;
}
<div class="grid">
<nav>
<ul>
<li>Home</li>
<li>Interior Painting</li>
<li>Exterior Painting</li>
<li>Deck Painting</li>
<li>Power Wash</li>
<li>Wallpaper Remvoal</li>
<li>Contact</li>
</ul>
</nav>
<section>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Placeat dignissimos error expedita debitis, eligendi a, dolorum velit, doloremque est repellat perferendis consectetur non similique mollitia maiores officiis totam voluptatibus libero.Lorem ipsum
dolor sit amet, consectetur adipisicing elit. Distinctio non provident sunt, voluptatem omnis. Porro nisi architecto expedita quae odit distinctio sint illo, tempora possimus dolore at, reprehenderit corrupti voluptatibus.
</section>
</div>
<!-- close grid -->
I have tried all sorts of combinations of justify-content, justify-items, align-self, margin: auto;, turning the nav into a flex item, the ul into a flex item, etc... Thanks in advance for any help.
You could do this using flexbox:
https://yoksel.github.io/flex-cheatsheet/
It is easy to learn and is very common tool in nowadays websites.
Make .grid a flex item with flex-basis: 100%; and flex-flow: row nowrap;
Make .nav a flex container, and align it's items in the center with align-items: center;
OR if you just want to use flex on your nav element and nowhere else, doing
nav{
display: flex;
align-items: center;
}
would be totally sufficient, If you don't want your whole layout to have a flexy manner
.grid {
display: flex;
flex-flow: row nowrap;
flex-basis: 100%;
margin: 0px 12%;
}
.nav{
display: flex;
align-items: center;
}
.grid>* {
border: 1px solid lightgray;
padding: 15px;
}
<div class="grid">
<div class="nav">
<nav>
<ul>
<li>Home</li>
<li>Interior Painting</li>
<li>Exterior Painting</li>
<li>Deck Painting</li>
<li>Power Wash</li>
<li>Wallpaper Remvoal</li>
<li>Contact</li>
</ul>
</nav>
</div>
<section>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Placeat dignissimos error expedita debitis, eligendi a, dolorum velit, doloremque est repellat perferendis consectetur non similique mollitia maiores officiis totam voluptatibus libero.Lorem ipsum
dolor sit amet, consectetur adipisicing elit. Distinctio non provident sunt, voluptatem omnis. Porro nisi architecto expedita quae odit distinctio sint illo, tempora possimus dolore at, reprehenderit corrupti voluptatibus.
dignissimos error expedita debitis, eligendi a, dolorum velit, doloremque est repellat perferendis consectetur non similique mollitia maiores officiis totam voluptatibus libero.Lorem ipsum
dolor sit amet, consectetur adipisicing elit. Distinctio non provident sunt, voluptatem omnis. Porro nisi architecto expedita quae odit distinctio sint illo, tempora possimus dolore at, reprehenderit corrupti voluptatibus.
</section>
</div>
<!-- close grid -->
To change the position of a child of a grid, you can use the *-self property. Vertical will be your column access, use nav {align-self: center;} to vertically center the nav in this scenario.
You can use the flexbox layout module. It seems like you were on the track there to begin with. Just attach this to the nav style.
nav {
display: flex;
alignt-items: center;
}

Flexbox - Same height columns inside row

Is it possible to make the columns inside each row the same height as each other? For example to make both the blue headings the same size and so on
Apologies if this has been asked before but I couldn't see an answer to this when searching.
<div class="row">
<div class="column">
<h2>heading</h2>
<div class="block-1">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Debitis, nesciunt.</div>
<div class="block-2">Lorem ipsum dolor sit amet.</div>
</div>
<div class="column">
<h2>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quod, maiores!</h2>
<div class="block-1">Lorem ipsum</div>
<div class="block-2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. In quam praesentium suscipit laudantium illo voluptatibus eligendi, est exercitationem commodi reiciendis.</div>
</div>
</div>
.row {
display: flex;
}
.column {
display: flex;
flex-direction: column;
* {
flex: 1;
}
}
https://jsfiddle.net/vdLaq7t1/
You can do it with align-items: stretch (which is the default value for align-items), but you should redesign your HTML structure.
.column {
background: silver;
}
h2 {
background: cornflowerblue;
margin: 0;
}
.block-1 {
background: tomato;
}
.block-2 {
background: brown;
}
.row {
display: flex;
/* Not required because it's already the default value */
align-items: stretch;
}
.row > *{
flex: 1;
}
<div class="row">
<h2>heading</h2>
<h2>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quod, maiores!</h2>
</div>
<div class="row">
<div class="block-1">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Debitis, nesciunt.</div>
<div class="block-1">Lorem ipsum</div>
</div>
<div class="row">
<div class="block-2">Lorem ipsum dolor sit amet.</div>
<div class="block-2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. In quam praesentium suscipit laudantium illo voluptatibus eligendi, est exercitationem commodi reiciendis.</div>
</div>
Flexbox works in both X and Y axis. When you use flex-direction: column you are changing the main axis from X to Y. You can manage the main axis with the justify-content property, and the secondary axis with the align-items property.
The thing is that align-items work with the stretch value, but justify-content cannot.
So if you want the height to be the same (Y axis), you need to stretch it with align-items: stretch, but align-items only works in the secondary axis, so the main axis needs to be the X axis in this case, and that means that you cannot use flex-direction: column in order to make them the same height.
It can be done with many technics.
For example, you can place 6 columns in 1 row.
.row {
display: flex;
flex-wrap: wrap;
}
.column {
flex-basis: 70%;
}
.column.sm {
flex-basis: 30%;
}
.blue {
background: blue;
}
.silver {
background: silver;
}
.cornflowerblue {
background: cornflowerblue;
}
<div class="row">
<div class="column blue sm">heading</div>
<div class="column blue">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quod, maiores!</div>
<div class="column silver sm"><div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Inventore quaerat, facilis qui placeat! Voluptatem reprehenderit similique rerum officia iste error ab, animi nobis quaerat culpa possimus, nisi laboriosam aliquid hic.</div>
<div>Tempora eius eaque harum, temporibus sequi porro, minima quia, necessitatibus amet nisi unde reiciendis iure ipsa, facilis rerum qui dolores doloribus sed voluptatum! Dicta at, qui, exercitationem molestiae voluptas natus.</div></div>
<div class="column silver">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iusto nemo ipsum voluptates mollitia eius enim, esse voluptatibus eaque doloremque vel asperiores quos unde similique rerum perspiciatis iure, ipsam eum soluta.</div>
<div class="column cornflowerblue sm">lorem</div>
<div class="column cornflowerblue"><div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore enim recusandae veniam optio delectus doloremque maiores quidem, impedit vel reprehenderit quam vitae, fugit atque assumenda molestiae debitis laboriosam blanditiis fugiat.</div>
<div>Excepturi ea minima accusantium delectus totam quae fugiat, ex eos inventore deleniti odit, commodi eveniet, eum ullam consectetur ipsa quasi odio similique. Doloribus tempore accusantium soluta, id deserunt maxime accusamus.</div></div>
</div>

Resources