How can I vertically center a div element [duplicate] - html

This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 10 months ago.
I want to center a div vertically with CSS. I don't want tables or JavaScript, but only pure CSS. I found some solutions, but all of them are missing Internet Explorer 6 support.

You can do it in multiple ways. I'll start with grid because I love grid.
CSS Grid Layout excels at dividing a page into major regions or defining the relationship in terms of size, position, and layer, between parts of a control built from HTML primitives.
Grid layout enables an author to align elements into columns and rows.
If you want all the elements contained in an element to be vertically centered you can use on the parent element the property display: grid; and after the property align-items: center;. For example see this code:
section {
height: 90vh;
width: 100%;
background-color: blue;
display: grid;
align-items: center;
}
div {
height: 100px;
width: 100px;
background-color: brown;
}
<section>
<div></div>
</section>
if you want a specific alignment for each element, you can avoid specifying the property align-items: center; for the parent element and use the property align-self: center; directly on each element it contains. See this example:
section {
height: 90vh;
width: 100%;
background-color: blue;
display: grid;
}
div {
height: 100px;
width: 100px;
background-color: brown;
align-self: center;
}
<section>
<div></div>
</section>
another way is through the use of flexbox. The principle is more or less the same.
you can apply a rule that applies to all contained elements:
section {
height: 90vh;
width: 100%;
background-color: blue;
display: flex;
align-items: center;
}
div {
height: 100px;
width: 100px;
background-color: brown;
}
<section>
<div></div>
</section>
or specify the position of each:
section {
height: 90vh;
width: 100%;
background-color: blue;
display: flex;
}
div {
height: 100px;
width: 100px;
background-color: brown;
align-self: center;
}
<section>
<div></div>
</section>
If you want to learn more about these topics I leave you the link of the site from which I learned them: https://developer.mozilla.org/en-US/docs/Learn
I hope I have been helpful!

Related

Problems with buttons / grid in HTML/CSS

i have problems with the setup of the buttons. I'm unsure if i need a grid system or not?
I want it to look like this example:
No code yet as i am unsure of where to start, and what to start with.
If somebody can help then hanks in advanced!
You can use with flex, justify-content, align-items like example below:
.wrapped {
display: flex;
flex-direction: column;
align-items: center;
width: 500px;
}
.avatar {
width: 50px;
height: auto;
}
button {
width: 200px;
}
.div1 {
display: flex;
justify-content: center;
padding: 10px;
}
.div2 {
display: flex;
flex-direction: column;
padding: 10px;
}
.div2 button{
width: 200px;
margin: 6px;
padding: 5px;
}
.div3 {
display: flex;
padding: 10px;
margin-left: 0px;
align-items: center;
}
.div3 button {
width: 40px;
height: 30px;
margin-left: 160px;
}
.bell {
width: 30px;
padding: 10px;
flex-basis: 1000px;
}
<section class="wrapped">
<div class="div1">
<button>At campus</button>
<img class="avatar" src="https://media.istockphoto.com/vectors/user-icon-flat-isolated-on-white-background-user-symbol-vector-vector-id1300845620?k=20&m=1300845620&s=612x612&w=0&h=f4XTZDAv7NPuZbG0habSpU0sNgECM0X7nbKzTUta3n8=" />
</div>
<div class="div2">
<button>Q & A</button>
<button>Klasser</button>
<button>Grupper</button>
<button>Chat</button>
</div>
<div class="div3">
<img class="bell" src="https://www.iconpacks.net/icons/1/free-bell-icon-860-thumb.png"/>
<button>Help</button>
</div>
</section>
for sure a grid would perfectly work for your design. However,you don't explicitly need a grid to obtain that result. Css flexbox display (display:flex) would also work and maybe fit your needs. Even display: block would work.
If you need a web layout that only consists of rows or columns, then Flexbox is the best model to use. However, if you have a complex, multi-row and multi-column layout, then you'll want to use CSS Grid.
Have a look for more details: https://www.webdesignerdepot.com/2018/09/grid-vs-flexbox-which-should-you-choose/
Here is a simple set-up for your design with flexbox in mind:
Suppose your first component (logo and user profile) are inside one div. You can use display:flex and flex-direction: row to display them in one line, and justify-content: space-between so the elements fill the entire row.
Then you have 4 buttons. You can use another div and set the flex-direction to column. Change the width of the div and of the buttons as you need.
Basically, the last div would be similar to the first one.
For each div you can specify different width or height.

Is possible position multiple elements in grid cell as one element without a container wrapping them?

I'm making html layout which will be showing shop receipt, i need it to be unified so i can style design of different shops receipts only with use of css maybe java-script if it would help.
So far most likely option come css grid, because my html layout is fixed and cannot be modified in advance to adding new receipts styles. I had done grid container which includes three containers. Those containers contain divs with data from database. I can control appearance of divs with display: none and position in grid with grid-area property.
<div class="container">
<div class="wrapper">
<div class="item1">Pr: </div>
<div class="item2">Levocska</div>
<div class="item3">tel: </div>
<div class="item4">0900 000 000</div>
</div>
</div>
* {
box-sizing: border-box;
}
.container {
display: grid;
border: 1px solid #000;
width: 300px;
margin: 20px 0;
}
.wrapper {
display: grid;
grid-template-areas:
"one"
"two";
background-color: yellow;
}
.item1 {
grid-area: one;
background-color: red;
width: 125px;
text-align: right;
}
.item2 {
grid-area: one;
display: flex;
background-color: orange;
width: 168px;
justify-self: end;
}
.item3 {
grid-area: two;
background-color: red;
width: 112px;
text-align: right;
}
.item4 {
grid-area: two;
display: flex;
background-color: orange;
width: 180px;
justify-self: end;
}
Here come problem, sometimes i need place two div elements in one grid cell but then because there is no flow in grid cell they overlap. Is there any option how i can control and position those two elements as one element ? I cannot wrap them because of unification for multiple receipts styles. So far only solution seems to be to use width for element and then text-align and flex to somehow position them but for example centering of two elements cannot be done precise. here is JSFiddle with code https://jsfiddle.net/g8vwby6u/1/ Or is there different approach to solve this issue?

Stretch display flex or table inner divs along parent with centered texts

I'd like to create a responsive page with a fixed html structure so I can just adjust the css. I would like to create rows with vertically and horizontally centered texts. The divs should fully stretch across the parent div.
My HTML...
<body>
<div class="parent">
<div class="d1">
one
</div>
<div class="d2">
two
</div>
<div class="d3">
three
</div>
</div>
</body>
My CSS...
body {
background-color: lightyellow;
height: 100%;
width: 100%;
margin: 0px;
}
.parent {
background-color: lightblue;
height: 100%;
}
.d1, .d2, .d3 {
display: flex;
align-items: center;
justify-content: center;
text-align: center;
height: 100px;
}
.d2 {
background-color: lightgreen;
}
However here I am setting d1, d2 and d3 to the height of 100px and not 100% of the parent div. Example here: https://jsfiddle.net/bLf2sxq0/
My second idea was to use display: table for the parent which results in table-rows for the childs but then I end up with the same stretching issue plus the texts are not vertically centered. Here the css would be like this ...
body {
background-color: lightyellow;
height: 100%;
width: 100%;
margin: 0px;
}
.parent {
width: 100%;
height: 100%;
display: table;
background-color: lightblue;
}
.d1, .d2, .d3 {
text-align: center;
height: 100px;
}
.d2 {
background-color: lightgreen;
}
Example here: https://jsfiddle.net/qmbzkwr2/
Is there a way to stretch the divs vertically along the parent and keep the texts centered vertically and horizontally within the divs? So I would not have width 100px but something like calc(100%/3) or any other solution to do this? Or maybe by using the flex grow option? Easiest way would do it :)
Thanks for any help!
You're on the right track. Use flexbox to stretch and fill items vertically and evenly. Remember to set parent containers (e.g. body, html) to height: 100%.
From here, if you want control over some items, use flex on any individual item, like flex: 1 1 300px on class .d2 for example.
Codepen
body, html {
height: 100%;
}
.parent {
background-color: lightblue;
height: 100%;
display: flex;
flex-direction: column;
}
.d1, .d2, .d3 {
display: flex;
align-items: center;
justify-content: center;
flex: 1;
}
.d2 {
background-color: lightgreen;
}
<div class="parent">
<div class="d1">
<div class="d11">
one
</div>
</div>
<div class="d2">
two
</div>
<div class="d3">
three
</div>
</div>

How to center multiple images at the center of a page in HTML?

I want to center 4 boxes at the center of a page, i.e., they should be vertically centered and horizontally, one box should be at extreme left, one at extreme right and the left ones should be placed horizontally between the extreme ones.
I know that such a question have been asked before, but I am not getting the exact logic of the solution. Can someone please give a proper explanation for the same? Thanks a lot.
Here's the HTML code-
.cards div{
height: 200px;
width: 200px;
float: left;
text-align: center;
margin-left: 100px;
}
.card_1{
background-color: green;
}
.card_2{
background-color: blue;
}
.card_3{
background-color: yellow;
}
.card_4{
background-color: red;
}
<html>
<head>
<link rel="stylesheet" href="second.css">
</head>
<body>
<div class="cards">
<div class="card_1">
</div>
<div class="card_2">
</div>
<div class="card_3">
</div>
<div class="card_4">
</div>
</div>
</body>
</html>
Here is a solution using flexbox:
html, body {
height: 100%;
padding: 0;
margin: 0;
}
.cards {
width: 100%;
height: 100%;
align-items: center;
display: flex;
flex-direction: row;
justify-content: space-between;
}
.cards div {
height: 200px;
width: 200px;
}
.card_1 {
background-color: green;
}
.card_2 {
background-color: blue;
}
.card_3 {
background-color: yellow;
}
.card_4 {
background-color: red;
}
More information about flexbox:
A Guide To Flexbox
Flexbox Froggy
JSFiddle
Edit
The secret to this solution is
align-items: center;
display: flex;
flex-direction: row;
justify-content: space-between;
display: flex; instructs the browser to use a flexbox layout when rendering your container element. flex-direction: row; renders all children of the container in a row. align-items: center; vertically centers the children of the container. And finally justify-content: space-between; spaces each child of the container with equal space in between each.
Flexbox is a powerful layout system. I would recommend learning more about it through SO or the provided links.
Keep in mind that flexbox is supported across all major browsers but IE 11 has limited support due to several bugs.

Having trouble vertically centering an element

https://jsfiddle.net/93hjyhs8/
I'm trying to vertically center the text on the right, but having great difficulty finding a pure CSS solution that works in all browsers.
The height of the .block element should be dynamic and expand to the size of the tallest child element (currently .thing1).
Note, that if possible I would prefer to avoid hacks such as using tables, but feel free to post those kind of solutions regardless.
Also, why is the element not only at the bottom but also slightly nudged down?
.block {
width: 500px;
background: yellow;
}
.thing1 {
height: 100px;
width: 40%;
background: blue;
display: inline-block;
}
.thing2 {
background: red;
width: 60%;
vertical-align: top;
display: inline-block;
}
<div class='block'>
<span class='thing1'></span><span class='thing2'>Hello world how are you today r u alrite m8 i think u r weak m8</span>
</div>
demo - https://jsfiddle.net/victor_007/93hjyhs8/1/
add vertical-align:middle for both the inline-block elements
You could use the new awesome css flexbox,
.block {
width: 500px;
background: yellow;
display: flex;
align-items: center;
justify-content: center;
}
Demo: https://jsfiddle.net/3y41ot0b/