Overlapping multiple div using CSS? - javascript
I'm trying to create a design using multiple divs using CSS.
I'm already written code for it but don't know what is the problem with my code as my left and right side div not aligning at vertically center and all the divs are not overlapped with main yellow centered div which is I'm unable to achieve.
Note: I tried this with z-index but did not get what I want.
Output I'm getting:
Output I want to achieve:
My code :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<style>
.maind {
margin: 0px auto;
width: 90%;
padding: 10px;
height: 900px;
background-color: rgb(9, 252, 9);
position: relative;
}
.fdiv {
margin: auto;
width: 25%;
padding: 10px;
height: 100px;
background-color: rgb(10, 233, 222);
margin-top: 30px;
border: 2px solid red;
}
.sdiv {
width: 55%;
height: 600px;
background-color: #ffff00ec;
}
.tdiv {
margin: auto;
width: 25%;
padding: 10px;
height: 100px;
background-color: rgb(10, 233, 222);
border: 2px solid red;
}
p {
text-align: center;
}
.wrap {
display: flex;
flex-direction: row;
}
.wr1 {
width: 25%;
height: 100px;
background-color: rgb(10, 233, 222);
border: 2px solid red;
}
</style>
<body>
<div class="maind">
<div class="fdiv">
<p>Some content here...</p>
</div>
<div class="wrap">
<div class="wr1">
<p>Some content here..</p>
</div>
<div class="sdiv">
<p>Some content here..</p>
</div>
<div class="wr1">
Some content here...
</div>
</div>
<div class="tdiv">
<p>Some content here..</p>
</div>
</div>
</body>
</html>
Please somebody help me with the Source Code I tried almost all the related answer.
You can use Flexbox or Positioning.
Using Positioning makes it more flexible to add content to the holder element.
While Flexbox is more flexible when it's about adding and aligning boxes.
# Positioning
Description:
Create 4 elements to be the boxes.
Each .box has it's direction.
Example: <div class="box top"></div>.
Wrap all of them in div.boxes. This way you can separate the .boxes from the content (if there) in the holder,
<div class="boxes">
<div class="box top"></div>
<div class="box right"></div>
<div class="box left"></div>
<div class="box bottom"></div>
</div>
Style the the position of .wrapper so all the positioned absolute elements stays in the .wrapper.
.wrapper {position: relative;}
Finally, set the position of each box:
Example:
.box.top {
top: 0;
left: 50%;
transform: translateX(-50%);
margin-top: -40px;
}
Notes:
Don't use:
left property on .box.right.
top property on .box.bottom.
It won't set the negative margin which pushes them to edges.
In case content added to the holder (.wrapper), wrap the content in div.content and add inner space using padding. The value of padding in the code example is 40px, which it's related to the .boxes dimenstions.
The space (padding) is added to prevent overflow between content and .boxeses. And we can go further with styling the .boxes with overflow and z-index property.
For more about using negative maring and the boxes dimenstions:
Check for Notes in Flexbox
The Code:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.wrapper {
position: relative;
width: 600px;
height: 600px;
border: 1px solid;
margin: 50px auto;
}
.box {
position: absolute;
border: 1px solid;
background-color: red;
}
.box.top, .box.bottom {
width: 200px;
height: 80px;
}
.box.left, .box.right {
width: 80px;
height: 200px;
}
.box.top {
top: 0;
left: 50%;
transform: translateX(-50%);
margin-top: -40px;
}
.box.bottom {
bottom: 0;
left: 50%;
transform: translateX(-50%);
margin-bottom: -40px;
}
.box.left {
top: 50%;
left: 0;
transform: translateY(-50%);
margin-left: -40px;
}
.box.right {
right: 0;
top: 50%;
transform: translateY(-50%);
margin-right: -40px;
}
.content {
padding: 40px; /* check notes */
}
<div class="wrapper">
<div class="boxes">
<div class="box top"></div>
<div class="box right"></div>
<div class="box left"></div>
<div class="box bottom"></div>
</div>
<div class="content">
<h1>Hello World!</h1>
</div>
</div>
# Flexbox
Description:
Create 3 elements to hold the .boxes.
top: holding top box
center: holding left and right boxes
bottom: holding bottom box
In other words:
Each .box is nested (a child) in a div that has the class of the direction.
Example: <div class="top">BOX</div>.
Left and right are nested in center.
HTML:
<!-- top box -->
<div class="top">
<div class="box"></div>
</div>
<!-- left, right boxes -->
<div class="center">
<div class="left">
<div class="box"></div>
</div>
<div class="right">
<div class="box"></div>
</div>
</div>
<!-- bottom box -->
<div class="bottom">
<div class="box"></div>
</div>
Wrap all of them in a div.wrapper:
<div class="wrapper">
<!-- top box -->
<div class="top"></div>
<!-- left, right boxes -->
<div class="center"></div>
<!-- bottom box -->
<div class="bottom"></div>
</div>
The lines below will style the 3 elements and set them to their positions, .top will be centered (left right) and on top, .center will be centered from all the directions, .bottom is centered (left right) and at the bottom, by displaying the .wrapper children horizontally (flex-direction: column;) and centered (align-items: center;) with (space-between) them, using flex.
Check: A Complete Guide to Flexbox
.wrapper {
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
}
Then we do something similar with the .center element, by displaying both of left and right next to each other, centered and space-between them.
(No flex-direction property in the declaration, since the default is in a row (vertically))
.wrapper .center {
width: 100%; /* Don't delete, check notes */
display: flex;
justify-content: space-between;
align-items: center;
}
And finally, with negative margin we move the boxes to the edges.
.top .box {
margin-top: -40px;
}
.bottom .box {
margin-bottom: -40px;
}
.center .left .box {
margin-left: -40px;
}
.center .right .box {
margin-right: -40px;
}
Notes:
The left and right boxes are (width: 80px), each, which means the margin should be -40px (80 / 2 = 40) to set on center.
left: margin-left: -40px
right: margin-right: -40px
Same for top and bottom, since the dimensions are flipped.
top: margin-top: -40px
bottom: margin-bottom: -40px
This way, all the boxes are gonna be centered at the edges.
By default, when displaying with flexbox, the parent(.center) will take the width of it's content/children (fitted)! which means, width: 40px * 2, since we have 2 boxes in there. Now to make sure that the space-between value works, we should "stretch" the .center element (parent) by styling it's width to 100% which allows to the boxes to have as much as space-between, then every box is gonna be on it's position.
.wrapper .center {
width: 100%; /* Don't delete, check notes */
}
The Code:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.wrapper {
border: 1px solid;
max-width: 600px;
min-height: 600px;
margin: 60px auto;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
}
.box {
border: 1px solid;
background-color: red;
}
.wrapper .top .box,
.wrapper .bottom .box {
width: 200px;
height: 80px;
}
.wrapper .center .box {
width: 80px;
height: 200px;
}
.top .box {
margin-top: -40px;
}
.bottom .box {
margin-bottom:-40px;
}
.wrapper .center {
width: 100%; /* Don't delete, check notes */
display: flex;
justify-content: space-between;
align-items: center;
}
.center .left .box {
margin-left: -40px;
}
.center .right .box {
margin-right: -40px;
}
<div class="wrapper">
<div class="top">
<div class="box"></div>
</div>
<div class="center">
<div class="left">
<div class="box"></div>
</div>
<div class="right">
<div class="box"></div>
</div>
</div>
<div class="bottom">
<div class="box"></div>
</div>
</div>
EDIT:
As #anatolhiman mentioned in the comments:
but negative margins will create a problem by having the elements
overflowing right and left (especially on narrow screens).
A simple solution:
(same works for both examples)
wrap the HTML that we added before in another div, .container for example, and add spacing with CSS, either padding or margin works, depends on your situation.
So the question is...
Is it a space within the .container? --> padding.
Or outside of it? --> margin.
Give the .container a background-color, resize the window, and check both margin and padding to see the differences.
HTML - Update:
<div class="container">
<div class="wrapper">
</div>
</div>
CSS - Add:
/* outside space */
.container {margin: 50px;}
/* Or */
/* inside space */
.container {padding: 50px;}
You may have to edit the margin property in .wrapper for top bottom.
Extra space added (50px) to include spaces for the .boxes as well.
Remember: .wrapper{max-width: VALUE} is taking a place in this functionality, since it's max-width is X but it could be smaller. So if the property is width: and not max-width then it'll behave differently, and won't work as expected (fully responsive), unless we use #media query or JavaScript.
Maybe something like following snippet, with absolute positioning:
.maind {
margin: 0px auto;
width: 90%;
padding: 10px;
height: 900px;
background-color: rgb(9, 252, 9);
position: relative;
}
.fdiv {
margin: auto;
width: 25%;
padding: 10px;
height: 100px;
background-color: rgb(10, 233, 222);
margin-top: 30px;
border: 2px solid red;
position: absolute;
top: 0;
left: 35%;
z-index: 22;
}
.sdiv {
width: 80%;
position: absolute;
top: 10%;
left: 10%;
height: 600px;
background-color: #ffff00ec;
z-index: 12;
margin: 0 auto;
}
.tdiv {
margin: auto;
width: 25%;
padding: 10px;
height: 100px;
background-color: rgb(10, 233, 222);
border: 2px solid red;
position: absolute;
bottom: 20%;
left: 35%;
z-index: 22;
}
p {
text-align: center;
}
.wrap {
display: flex;
flex-direction: row;
}
.wr1 {
width: 25%;
height: 100px;
background-color: rgb(10, 233, 222);
border: 2px solid red;
position: absolute;
top: 35%;
left: 0;
z-index: 22;
}
.wr2 {
width: 25%;
height: 100px;
background-color: rgb(10, 233, 222);
border: 2px solid red;
position: absolute;
top: 35%;
right: 0;
z-index: 22;
}
<div class="maind">
<div class="fdiv">
<p>Top...</p>
</div>
<div class="wrap">
<div class="wr1">
<p>Left..</p>
</div>
<div class="sdiv">
<p>Somessss content here..</p>
</div>
<div class="wr2">
<p>Right...</p>
</div>
</div>
<div class="tdiv">
<p>Bottom..</p>
</div>
</div>
Related
Hi, I was stuck on centering the flex items [duplicate]
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. <body> <div>Div to be aligned vertically</div> </body> How can I center a div vertically in all major browsers, including Internet Explorer 6?
Below is the best all-around solution I could build to vertically and horizontally center a fixed-width, flexible height content box. It was tested and worked for recent versions of Firefox, Opera, Chrome, and Safari. .outer { display: table; position: absolute; top: 0; left: 0; height: 100%; width: 100%; } .middle { display: table-cell; vertical-align: middle; } .inner { margin-left: auto; margin-right: auto; width: 400px; /* Whatever width you want */ } <div class="outer"> <div class="middle"> <div class="inner"> <h1>The Content</h1> <p>Once upon a midnight dreary...</p> </div> </div> </div> View A Working Example With Dynamic Content I built in some dynamic content to test the flexibility and would love to know if anyone sees any problems with it. It should work well for centered overlays also -- lightbox, pop-up, etc.
The simplest way would be the following three lines of CSS: 1) position: relative; 2) top: 50%; 3) transform: translateY(-50%); Following is an example: div.outer-div { height: 170px; width: 300px; background-color: lightgray; } div.middle-div { position: relative; top: 50%; -webkit-transform: translateY(-50%); -ms-transform: translateY(-50%); transform: translateY(-50%); } <div class='outer-div'> <div class='middle-div'> Test text </div> </div>
One more I can't see on the list: .Center-Container { position: relative; height: 100%; } .Absolute-Center { width: 50%; height: 50%; overflow: auto; margin: auto; position: absolute; top: 0; left: 0; bottom: 0; right: 0; border: solid black; } Cross-browser (including Internet Explorer 8 - Internet Explorer 10 without hacks!) Responsive with percentages and min-/max- Centered regardless of padding (without box-sizing!) height must be declared (see Variable Height) Recommended setting overflow: auto to prevent content spillover (see Overflow) Source: Absolute Horizontal And Vertical Centering In CSS
Now the Flexbox solution is a very easy way for modern browsers, so I recommend this for you: .container { display: flex; align-items: center; justify-content: center; height: 100%; background: green; } body, html { height: 100%; } <div class="container"> <div>Div to be aligned vertically</div> </div>
Actually, you need two div's for vertical centering. The div containing the content must have a width and height. #container { position: absolute; top: 50%; margin-top: -200px; /* Half of #content height */ left: 0; width: 100%; } #content { width: 624px; margin-left: auto; margin-right: auto; height: 395px; border: 1px solid #000000; } <div id="container"> <div id="content"> <h1>Centered div</h1> </div> </div> Here is the result.
Edit 2020: only use this if you need to support old browsers like Internet Explorer 8 (which you should refuse to do 😉). If not, use Flexbox. This is the simplest method I found and I use it all the time (jsFiddle demo here). Thank Chris Coyier from CSS Tricks for this article. html, body{ height: 100%; margin: 0; } .v-wrap{ height: 100%; white-space: nowrap; text-align: center; } .v-wrap:before{ content: ""; display: inline-block; vertical-align: middle; width: 0; /* adjust for white space between pseudo element and next sibling */ margin-right: -.25em; /* stretch line height */ height: 100%; } .v-box{ display: inline-block; vertical-align: middle; white-space: normal; } <div class="v-wrap"> <article class="v-box"> <p>This is how I've been doing it for some time</p> </article> </div> Support starts with Internet Explorer 8.
After a lot of research I finally found the ultimate solution. It works even for floated elements. View Source .element { position: relative; top: 50%; transform: translateY(-50%); /* or try 50% */ }
Use the CSS Flexbox align-items property to achieve this. html, body { height: 100%; } body { display: flex; align-items: center; } <div>This is centered vertically</div>
To center the div on a page, check the fiddle link. #vh { margin: auto; position: absolute; top: 0; left: 0; bottom: 0; right: 0; } .box{ border-radius: 15px; box-shadow: 0 0 8px rgba(0, 0, 0, 0.4); padding: 25px; width: 100px; height: 100px; background: white; } <div id="vh" class="box">Div to be aligned vertically</div> Another option is to use flex box, check the fiddle link. .vh { background-color: #ddd; height: 400px; align-items: center; display: flex; } .vh > div { width: 100%; text-align: center; vertical-align: middle; } <div class="vh"> <div>Div to be aligned vertically</div> </div> Another option is to use a CSS 3 transform: #vh { position: absolute; top: 50%; left: 50%; /*transform: translateX(-50%) translateY(-50%);*/ transform: translate(-50%, -50%); } .box{ border-radius: 15px; box-shadow: 0 0 8px rgba(0, 0, 0, 0.4); padding: 25px; width: 100px; height: 100px; background: white; } <div id="vh" class="box">Div to be aligned vertically</div>
The easiest solution is below: .outer-div{ width: 100%; height: 200px; display: flex; border:1px solid #000; } .inner-div{ margin: auto; text-align: center; border: 1px solid red; } <div class="outer-div"> <div class="inner-div"> Hey there! </div> </div>
There are multiple ways to achieve this. Using flex property of CSS. Solution #1 .parent { width: 400px; height:200px; background: blue; display: flex; align-items: center; justify-content:center; } .child { width: 75px; height: 75px; background: yellow; } <div class="parent"> <div class="child"></div> </div> or by using display: flex; and margin: auto; Solution #2 .parent { width: 400px; height:200px; background: blue; display: flex; } .child { width: 75px; height: 75px; background: yellow; margin:auto; } <div class="parent"> <div class="child"></div> </div> show text center Solution #3 .parent { width: 400px; height: 200px; background: yellow; display: flex; align-items: center; justify-content:center; } <div class="parent">Center</div> Using percentage(%) height and width. Solution #4 .parent { position: absolute; height:100%; width:100%; background: blue; display: flex; align-items: center; justify-content:center; } .child { width: 75px; height: 75px; background: yellow; } <div class="parent"> <div class="child"></div> </div>
Unfortunately — but not surprisingly — the solution is more complicated than one would wish it to be. Also unfortunately, you'll need to use additional divs around the div you want vertically centered. For standards-compliant browsers like Mozilla, Opera, Safari, etc. you need to set the outer div to be displayed as a table and the inner div to be displayed as a table-cell — which can then be vertically centered. For Internet Explorer, you need to position the inner div absolutely within the outer div and then specify the top as 50%. The following pages explain this technique well and provide some code samples too: Vertical Centering in CSS Vertical Centering in CSS with Unknown Height (Internet Explorer 7 compatible) (Archived article courtesy of the Wayback Machine) There is also a technique to do the vertical centering using JavaScript. Vertical alignment of content with JavaScript & CSS demonstrates it.
If someone cares for Internet Explorer 10 (and later) only, use Flexbox: .parent { width: 500px; height: 500px; background: yellow; display: -webkit-flex; display: -ms-flexbox; display: flex; -webkit-justify-content: center; -ms-flex-pack: center; justify-content: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; } .centered { width: 100px; height: 100px; background: blue; } <div class="parent"> <div class="centered"></div> </div> Flexbox support: http://caniuse.com/flexbox
A modern way to center an element vertically would be to use flexbox. You need a parent to decide the height and a child to center. The example below will center a div to the center within your browser. What's important (in my example) is to set height: 100% to body and html and then min-height: 100% to your container. body, html { background: #F5F5F5; box-sizing: border-box; height: 100%; margin: 0; } #center_container { align-items: center; display: flex; min-height: 100%; } #center { background: white; margin: 0 auto; padding: 10px; text-align: center; width: 200px; } <div id='center_container'> <div id='center'>I am center.</div> </div>
.center { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); /* (x, y) => position */ -ms-transform: translate(-50%, -50%); /* IE 9 */ -webkit-transform: translate(-50%, -50%); /* Chrome, Safari, Opera */ } .vertical { position: absolute; top: 50%; //left: 0; transform: translate(0, -50%); /* (x, y) => position */ } .horizontal { position: absolute; //top: 0; left: 50%; transform: translate(-50%, 0); /* (x, y) => position */ } div { padding: 1em; background-color: grey; color: white; } <body> <div class="vertical">Vertically left</div> <div class="horizontal">Horizontal top</div> <div class="center">Vertically Horizontal</div> </body> Related: Center a Image
Centering only vertically If you don't care about Internet Explorer 6 and 7, you can use a technique that involves two containers. The outer container: should have display: table; The inner container: should have display: table-cell; should have vertical-align: middle; The content box: should have display: inline-block; You can add any content you want to the content box without caring about its width or height! Demo: body { margin: 0; } .outer-container { position: absolute; display: table; width: 100%; /* This could be ANY width */ height: 100%; /* This could be ANY height */ background: #ccc; } .inner-container { display: table-cell; vertical-align: middle; } .centered-content { display: inline-block; background: #fff; padding: 20px; border: 1px solid #000; } <div class="outer-container"> <div class="inner-container"> <div class="centered-content"> Malcolm in the Middle </div> </div> </div> See also this Fiddle! Centering horizontally and vertically If you want to center both horizontally and vertically, you also need the following. The inner container: should have text-align: center; The content box: should re-adjust the horizontal text-alignment to for example text-align: left; or text-align: right;, unless you want text to be centered Demo: body { margin: 0; } .outer-container { position: absolute; display: table; width: 100%; /* This could be ANY width */ height: 100%; /* This could be ANY height */ background: #ccc; } .inner-container { display: table-cell; vertical-align: middle; text-align: center; } .centered-content { display: inline-block; text-align: left; background: #fff; padding: 20px; border: 1px solid #000; } <div class="outer-container"> <div class="inner-container"> <div class="centered-content"> Malcolm in the Middle </div> </div> </div> See also this Fiddle!
It can be done in two ways body{ left: 50%; top:50%; transform: translate(-50%, -50%); height: 100%; width: 100%; } OR Using flex body { height:100% width:100% display: flex; justify-content: center; align-items: center; } align-items:center; makes the content vertically center justify-content: center;makes the content horizontally center
This is always where I go when I have to come back to this issue. For those who don't want to make the jump: Specify the parent container as position:relative or position:absolute. Specify a fixed height on the child container. Set position:absolute and top:50% on the child container to move the top down to the middle of the parent. Set margin-top:-yy where yy is half the height of the child container to offset the item up. An example of this in code: <style type="text/css"> #myoutercontainer {position:relative} #myinnercontainer {position:absolute; top:50%; height:10em; margin-top:-5em} </style> ... <div id="myoutercontainer"> <div id="myinnercontainer"> <p>Hey look! I'm vertically centered!</p> <p>How sweet is this?!</p> </div> </div>
I just wrote this CSS and to know more, please go through: This article with vertical align anything with just 3 lines of CSS. .element { position: relative; top: 50%; transform: perspective(1px) translateY(-50%); }
For newcomers, please try: display: flex; align-items: center; justify-content: center;
The three lines of code using transform works practically on modern browsers and Internet Explorer: .element{ position: relative; top: 50%; transform: translateY(-50%); -moz-transform: translateY(-50%); -webkit-transform: translateY(-50%); -ms-transform: translateY(-50%); } I am adding this answer since I found some incompleteness in the previous version of this answer (and Stack Overflow won't allow me to simply comment). 'position' relative messes up the styling if the current div is in the body and has no container div. However 'fixed' seems to work, but it obviously fixes the content in the center of the viewport Also I used this styling for centering some overlay divs and found that in Mozilla all elements inside this transformed div had lost their bottom borders. Possibly a rendering issue. But adding just the minimal padding to some of them rendered it correctly. Chrome and Internet Explorer (surprisingly) rendered the boxes without any need for padding
CSS Grid body, html { margin: 0; } body { display: grid; min-height: 100vh; align-items: center; } <div>Div to be aligned vertically</div>
.center{ display: grid; place-items: center; }
The answer from Billbad only works with a fixed width of the .inner div. This solution works for a dynamic width by adding the attribute text-align: center to the .outer div. .outer { position: absolute; display: table; width: 100%; height: 100%; text-align: center; } .middle { display: table-cell; vertical-align: middle; } .inner { text-align: center; display: inline-block; width: auto; } <div class="outer"> <div class="middle"> <div class="inner"> Content </div> </div> </div>
Just do it: Add the class at your div: .modal { margin: auto; position: absolute; top: 0; right: 0; left: 0; bottom: 0; height: 240px; } And read this article for an explanation. Note: Height is necessary.
I did it with this (change width, height, margin-top and margin-left accordingly): .wrapper { width: 960px; height: 590px; position: absolute; top: 50%; left: 50%; margin-top: -295px; margin-left: -480px; } <div class="wrapper"> -- Content -- </div>
Not answering for browser compatibility but to also mention the new Grid and the not so new Flexbox feature. Grid From: Mozilla - Grid Documentation - Align Div Vertically Browser Support: Grid Browser Support CSS: .wrapper { display: grid; grid-template-columns: repeat(4, 1fr); grid-gap: 10px; grid-auto-rows: 200px; grid-template-areas: ". a a ." ". a a ."; } .item1 { grid-area: a; align-self: center; justify-self: center; } HTML: <div class="wrapper"> <div class="item1">Item 1</div> </div> Flexbox Browser Support: Flexbox Browser Support CSS: display: -webkit-box; display: -moz-box; display: -ms-flexbox; display: -webkit-flex; display: flex; align-items: center; justify-content: center;
I think a solid solution for all browsers without using Flexbox - "align-items: center;" is a combination of display: table and vertical-align: middle;. CSS .vertically-center { display: table; width: 100%; /* Optional */ height: 100%; /* Optional */ } .vertically-center > div { display: table-cell; vertical-align: middle; } HTML <div class="vertically-center"> <div> <div style="border: 1px solid black;">some text</div> </div> </div> ‣Demo: https://jsfiddle.net/6m640rpp/
Especially for parent divs with relative (unknown) height, the centering in the unknown solution works great for me. There are some really nice code examples in the article. It was tested in Chrome, Firefox, Opera, and Internet Explorer. /* This parent can be any width and height */ .block { text-align: center; } /* The ghost, nudged to maintain perfect centering */ .block:before { content: ''; display: inline-block; height: 100%; vertical-align: middle; margin-right: -0.25em; /* Adjusts for spacing */ } /* The element to be centered, can also be of any width and height */ .centered { display: inline-block; vertical-align: middle; width: 300px; } <div style="width: 400px; height: 200px;"> <div class="block" style="height: 90%; width: 100%"> <div class="centered"> <h1>Some text</h1> <p>Any other text..."</p> </div> </div> </div>
There is a trick I found out recently: You need to use top 50%, and then you do a translateY(-50%). .outer-div { position: relative; height: 150px; width: 150px; background-color: red; } .centered-div { position: absolute; top: 50%; -webkit-transform: translateY(-50%); -ms-transform: translateY(-50%); transform: translateY(-50%); background-color: white; } <div class='outer-div'> <div class='centered-div'> Test text </div> </div>
positioning div after it has been rotated
I would like to make a div with 3 texts, rotate it and place it on a fixed spot on the page. I would like it to perfectly fit the size of the screen, So I created a div which height and width were the opposite of what I need then I rotated it. The thing is that I'm having problems positioning it correctly on the spot I want. (top:0 right:40px) This is my code where I tried to move the div around. .page { width: 100%; heigth: 100%; background-color: #fefefe; } .green-band { height: 90px; width: 100vh; padding: 0 30px; background-color: green; display: flex; justify-content: space-between; align-items: center; transform: rotate(-90deg); position: fixed; top: 0; right: 40px; } <div class="page"></div> <div class="green-band"> <div class="text-inside">Text 1</div> <div class="text-inside">Text 2</div> <div class="text-inside">Text 3</div> </div> Can you help me figuring out how to correctly position the div?
.page { width: 100%; heigth: 100%; background-color: #fefefe; } .green-band { height: 90px; width: 100vh; padding: 0 30px; background-color: green; display: flex; justify-content: space-between; align-items: center; transform: translateX(calc(100% - 40px)) rotate(-90deg); transform-origin: bottom left; position: fixed; bottom: 0; right: 0; } <div class="page"></div> <div class="green-band"> <div class="text-inside">Text 1</div> <div class="text-inside">Text 2</div> <div class="text-inside">Text 3</div> </div>
Inherit padding width without position absolute? Is it possible?
Not sure, but I am currently not able to figure out. I'm trying to center the inner div (blue transparent one) from the parent (with the red background) inside the background. As an example, they're technically in each other perfectly at the first example in the snippet. At the second example however I've added padding: 5px; to both of them to the red and blue one. To the blue one because I wanted to inherit the width some how. https://jsfiddle.net/L8enbcy3/ .box-1-1 { width: 50px; height: 50px; background: red; position: relative; display: block; } .box-1-2 { width: 50px; height: 50px; background: #0000ffb0; position: relative; } .box1 { width: 50px; height: 50px; background: red; padding: 5px; position: relative; display: block; } .box2 { width: 50px; height: 50px; background: #0000ffb0; padding: 5px; } <div class="box-1-1"> <div class="box-1-2"></div> </div> <pre> </pre> <div class="box1"> <div class="box2"></div> </div> What I'm trying is to get "box2" centered into "box1" like example 1 but with its padding, so that's covered by blue. without having to position: absolute it, if possible. What I'm thinking I have to do is to create and invisibile box absolute it, "center it with top: 0 and left: 0 when the parent has position: relative. Then as I mentioned with it being absolute it would go to the corners of the parents padding too and then in the absolute box, I would create a relative one with display: table and put in all the content. My question now though is, is there another way to do that?
Solution 1: transform: translate() You could use transform: translate() with variables to achieve what you want, without weird margins (next solution). Here's some MDN about translate(). :root { --padding: 5px; } .box1 { height: 50px; width: 50px; background-color: red; padding: var(--padding); } .box2 { height: calc(50px + var(--padding)*2); width: calc(50px + var(--padding)*2); background-color: #0000ffb0; transform: translate(calc(0px - var(--padding)), calc(0px - var(--padding))) } <div class="box1"> <div class="box2"></div> </div> As you can see, the box is brought up and left with translate, and the height is lengthened by adding the needed padding to it. Thisachieves the desired cover effect. Solution 2: Positive padding, negative margin You could also use positive paddings and negative margins. Info below code snippet. :root { --padding: 5px; } .box1 { height: 50px; width: 50px; background-color: red; padding: var(--padding); position: relative; display: block; } .box2 { height: 50px; width: 50px; background-color: #0000ffb0; padding: var(--padding); margin: calc(0px - var(--padding)); } <div class="box1"> <div class="box2"></div> </div> What's happening here is following the CSS box model, found on MDN and w3schools. We're simply pushing out with margin and sucking in with padding. Then, as per request in the comments, --padding is a CSS variable that stores the amount of padding that you want. Hope I helped! Cheers, Bobbay
You can add a negative margin if you insist on keeping the padding in place. .box-1-1 { width: 50px; height: 50px; background: red; position: relative; display: block; } .box-1-2 { width: 50px; height: 50px; background: #0000ffb0; position: relative; } .box1 { width: 50px; height: 50px; background: red; padding: 5px; position: relative; display: block; } .box2 { width: 50px; height: 50px; background: #0000ffb0; padding: 5px; margin: -5px; } <div class="box-1-1"> <div class="box-1-2"></div> </div> <pre> </pre> <div class="box1"> <div class="box2"></div> </div>
To center box2 within box1 without absolute position, you can use following css: .box1 { display: flex; align-items: center; justify-content: center; }
Edit: example 2 illustrate your need. just remove padding from both boxes and settop: 0; and 'left: 0' with position: relative on box 2. I hope this is the required solution to center box 2 you need to make its dimension less than box 1. consider the extra pixels added with padding. so the inner box width and height should be 10px less than the outer box. Example: .box1 { width: 50px; height: 50px; background: red; padding: 5px; position: relative; display: block; } .box2 { width: 40px; height: 40px; background: #0000ffb0; padding: 5px; } .box1-1 { border: 2px solid yellow; width: 50px; height: 50px; background: red; position: relative; display: block; } .box2-2 { position:relative; top: 0px; left: 0px; width: 50px; height: 50px; background: #0000ffb0; } <div class="box1"> <div class="box2"></div> </div> <br> <div class="box1-1"> <div class="box2-2"></div> </div>
Particle js hiding web elements below it
I am using particle js as a background image.Now <div id="particles-js"></div> <div class="text"> <h1>Particles Background</h1> </div> I have to set position attribute of .text as absolute. Otherwise the section remains hidden. I don't seem to understand why others become hidden. I can't use absolute as it will break my code. Below is the css. Only if I set .text as position:absolute it will display #particles-js { position: relative; width: 100%; height: 100%; background: grey; } .text { position: relative; top: 50%; right: 50%; } <div id="particles-js"></div> <div class="text"> <h1>Particles Background</h1> </div>
You are facing this issue possibly because of heighr z-index value for #particle-js You can do it by either making position: absolute; for #particle-js and/or increasing the z-index for .text To understand more about positions please check this link
You are using divs which by default have layout but with no contents have no size. You also position the right of one element so the text is off screen. You can then fix that by right align of the text in the div. Here I put two examples to help understand the differences, one with no content as you have and one with a right aligned text. I put some borders on just so you have a visual of the elements. #mycontainer{border:solid lime 1px;} #particles-js { position: relative; width: 100%; height: 100%; background: grey; border: solid 1px blue; } .text { position: relative; top: 50%; right: 50%; border: solid 1px red; } <div id="mycontainer"> <div id="particles-js">cheese </div> <div class="text"> <h1>Particles Background</h1> </div> </div> Second example #mycontainer { border: solid lime 1px; width: 100%; height: 100%; } #particles-js { position: relative; width: 100%; height: 100%; background: grey; border: solid 1px blue; } .text { position: relative; top: 50%; right: 50%; border: solid 1px red; text-align:right; } <div id="mycontainer"> <div id="particles-js"> </div> <div class="text"> <h1>Particles Background</h1> </div> </div>
How to increase size of something in class, but not other elements in class?
Let's say I have four images inside a div. they all have a width of 5.5% [_o__o__o__o_] I want to use javascript to change the target that is moused over (hovered on), and have it look like this: [_o__O__o__o_] so I made the width of the target increase however it also pushes the other elements to the side instead of staying where they are so it's more like: [_o___O___o__o_] I don't know how to make the other elements stay exactly where they are instead of being pushed. The issue is that YES I am successfully able to alter the width. BUT changing the width of one element pushes the surrounding elements to the respective right and left. jsbin: https://jsbin.com/zujutamazo/edit?html,css,js,output
You can use flexbox for this one: .wrapper { display: flex; display: -webkit-flex; display: -moz-flex; width: 400px; background-color: red; } .item { position: relative; width: 25%; height: 200px; } .circle { position: absolute; bottom: 20px; left: 50%; -webkit-transform: translate(-50%, -50%); background: white; width: 20px; height: 20px; border-radius: 50%; transition: all .3s; } .item1 { background-color: blue; } .item2 { background-color: red; } .item3 { background-color: orange; } .item4 { background-color: yellow; } .item:hover .circle{ background-color: black; height: 40px; width: 40px; } <div class="wrapper"> <div class="item item1"> <div class="circle"></div> </div> <div class="item item2"> <div class="circle"></div> </div> <div class="item item3"> <div class="circle"></div> </div> <div class="item item4"> <div class="circle"></div> </div> </div>
As I was explaining, you need to set a higher z-index to "be above" the non-hovered boxes. And set negative left-right margins, equivalent to the additional width from hovering to prevent everything from moving around. Below is a working example, with percentages. body { width: 300px; height: 100px; } .myClass { width: 20%; height: 50%; position: relative; z-index: 1; float: left; } .myClass:hover { width: 30%; height: 70%; z-index: 10; margin: 0 -5%; } body .myClass:nth-child(1) { background-color: red; } body .myClass:nth-child(2) { background-color: green; } body .myClass:nth-child(3) { background-color: blue; } body .myClass:nth-child(4) { background-color: yellow; } <html> <head> </head> <body> <div class="myClass"></div> <div class="myClass"></div> <div class="myClass"></div> <div class="myClass"></div> </body> </html>