In the middle of the page is where I need the textboxes
horizontal plus vertical center is troublesome.
first center horizontally by making your parent display: flex
and include two bracketting children that stretch flex: 1 as well as your centered element (this allows it to be a perfect third, if you want it to instead be bigger, remove flex: 1 for the center child and put a width in percentage instead (pixel works but will not scale))
then center vertically by adding a margin top where you calculate leftover size :
.centeredchild {
background-color: red;
height: 20px;
flex: 1;
text-align: center;
margin-top: calc(50vh - 10px);
}
.centeringaid {
flex: 1;
}
.parent {
width: 100%;
height: 100vh;
display: flex;
background-color: lightblue;
}
<div class="parent">
<div class="centeringaid"></div>
<div class="centeredchild">hi</div>
<div class="centeringaid"></div>
</div>
there's also the more common method : https://www.w3schools.com/howto/howto_css_center-vertical.asp
this website, by the way is full of usefull tidbits.
Refer below code snippet to align your multiple text boxes in the middle of the page. (Consider page height to be 400px)
.main-div {
height: 400px;
position: relative;
}
.sub-div {
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
position: absolute;
top: 50%;
left: 50%;
}
input[type="text"] {
margin:10px auto;
border: 1px solid gray;
}
<div class="main-div">
<div class="sub-div">
<input type="text" />
<input type="text" />
<input type="text" />
</div>
</div>
You use this code:
CSS:
.outercontainer {
height: 200px;
position: relative;
border: 3px solid green;
}
.innercontainer {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
HTML:
<div class="outercontainer">
<div class="innercontainer">
<p>This sentance is it the middle!</p>
</div>
</div>
Related
Is there a way to push the content of an element to the right using pseudo elements to get the result in the picture below ?
Right now, this is the html code:
<div class="center" style="background-color: cyan;">
<div class="outside-right">
Content
</div>
</div>
And these are the CSS classes:
.center {
position: absolute;
top:50%;
left:50%;
transform: translate(-50%, -50%);
}
.outside-right{
transform: translateX(100%);
}
The problem is with CSS animations when changing the transform property of an element, it will override the class .outside-right transform and the element will snap back to the center of its parent before starting the animation.
So I have tried changing the .outside-right class, by putting the ::before pseudo element with width:100% so it pushes the content of that element. But nothing seems to make it work.
I have tried:
CSS:
.outside-right-using-pseudo-element::before {
content: '';
display: inline-block;
background-color: red;
width: 100%;
}
And
.outside-right-using-pseudo-element::before {
content: '';
float: left;
background-color: red;
width: 100%;
}
HTML
<div class="center" style="background-color: cyan;">
<div class="outside-right-using-pseudo-element">
Content
</div>
</div>
Is there any possible way that make it work ?
You are almost good, add white-space:nowrap; to avoid having the element in the next line:
.outside-right-using-pseudo-element {
white-space: nowrap;
}
.outside-right-using-pseudo-element::before {
content: '';
display: inline-block;
background-color: red;
width: 100%;
transition: 1s;
}
.outside-right-using-pseudo-element:hover::before {
width: 0%;
}
.center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div class="center" style="background-color: cyan;">
<div class="outside-right-using-pseudo-element">
Content
</div>
</div>
Another idea using flexbox:
.outside-right-using-pseudo-element {
display: flex;
white-space:nowrap;
}
.outside-right-using-pseudo-element::before {
content: '';
background-color: red;
width: 100%;
flex-shrink: 0; /* this will do the trick */
align-self: flex-start;
transition: 1s;
}
.outside-right-using-pseudo-element:hover::before {
width: 0%;
}
.center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div class="center" style="background-color: cyan;">
<div class="outside-right-using-pseudo-element">
Content
</div>
</div>
And you can easily have the other direction:
.outside-right-using-pseudo-element,
.outside-left-using-pseudo-element{
display: flex;
white-space:nowrap;
}
.outside-left-using-pseudo-element,
.outside-bottom-using-pseudo-element{
justify-content:flex-end;
}
.outside-right-using-pseudo-element::before,
.outside-left-using-pseudo-element::after{
content: '';
background-color: red;
width: 100%;
flex-shrink: 0; /* this will do the trick */
align-self: flex-start;
transition: 1s;
}
.outside-right-using-pseudo-element:hover::before,
.outside-left-using-pseudo-element:hover::after {
width: 0%;
}
.center {
display:table;
margin:80px auto;
}
<div class="center" style="background-color: cyan;">
<div class="outside-left-using-pseudo-element">
Content
</div>
</div>
<div class="center" style="background-color: cyan;">
<div class="outside-right-using-pseudo-element">
Content content
</div>
</div>
.outside-right-using-pseudo-element {
position:relative;
padding-left:110px
}
.outside-right-using-pseudo-element:before{
content: '';
position: absolute;
width: 100px;
left: 0;
height: 100%;
background: cyan;
}
<div class="center">
<div class="outside-right-using-pseudo-element">
Content
</div>
</div>
I am trying to create a graoh to show positive and negative value by percentage so if value will be negative it will be red bar if value will be positive it will run red bar so the thing is that I am having issue when I set width to 46 or 50% the bar is showing to be of full size as it shouldn't be can anyone help me out with this please
.box {
position: relative;
width: 400px;
height: 30px;
background-color: #333
}
.bar_red {
background-color: #d40216 !important;
left: 50%;
width: 13%;
max-width: 180px;
}
.bar_green {
right: 50%;
}
.bar_green,
.bar_red {
width: 42%;
height: 20px;
background-color: #88c500;
position: absolute;
top: 50%;
transform: translateY(-50%);
-webkit-transform: translateY(-50%);
-moz-transform: translateY(-50%);
}
<div class="box">
<div class="bar_red" style="width: 50%;"></div>
<div class="bar_green" style="width: 50%;"></div>
</div>
https://jsfiddle.net/vck8wchh/
First of all your bars are pulled 50%. see example below
.bar_green {
right: 50%;
}
.bar_red {
left: 50%;
}
So this means that if you fill in 50% or higher in your <div style="50%"> it will be full width. Go to your fiddle and for example replace your HTML with the following:
<div class="box">
<div class="bar_red" style="width: 10%;"></div>
<div class="bar_green" style="width: 30%;"></div>
</div>
You'll see that they won't be fully filled. new jsfiddle
May I suggest a simpler solution? In my snippet the green bar is 100% wide, while the red bar gets a percentage width, is right-aligned and covers the green one using a higher z-index. So you only have to set the percentage of the red bar.
.box {
position: relative;
width: 390px;
height: 20px;
background-color: #333;
border: 5px solid #333;
}
.bar_green,
.bar_red {
height: 20px;
position: absolute;
}
.bar_green {
left: 0;
width: 100%;
background-color: #88c500;
z-index: 1;
}
.bar_red {
background-color: #d40216 !important;
right: 0;
width: 42%;
z-index: 2;
}
<div class="box">
<div class="bar_red"></div>
<div class="bar_green"></div>
</div>
I have a container div for the main content but am trying to have a sidebar float to the left of it. For example (http://www.bureautonic.com/en/) the menu button.
This is the code
.main-wrapper {
position: relative;
top: 50%;
height: 500px;
}
.container {
position: relative;
height: 100%;
}
.body {
height: 100%;
}
.slider {
display: block;
width: 940px;
height: 500px;
margin-right: auto;
margin-left: auto;
float: none;
}
.img {
position: absolute;
width: 100%;
height: 100%;
max-height: 100%;
}
.tagline {
position: absolute;
left: 50%;
top: 50%;
z-index: 1;
display: block;
width: 332px;
margin-right: auto;
margin-left: auto;
padding: 1em 3em;
border: 1px solid white;
-webkit-transform: translate(-50%, 0px) translate(0px, -50%);
-ms-transform: translate(-50%, 0px) translate(0px, -50%);
transform: translate(-50%, 0px) translate(0px, -50%);
font-family: 'Josefin Sans', sans-serif;
color: white;
text-align: center;
text-decoration: none;
text-transform: none;
}
.header {
margin-top: 33px;
margin-bottom: -61px;
}
.brand {
font-family: Cardo, sans-serif;
text-align: center;
}
<body class="body">
<div class="w-section container">
<div class="w-container header">
<h1 class="brand">The One And Only</h1>
</div>
<div class="w-container main-wrapper">
<div data-animation="outin" data-duration="500" data-infinite="1" data-easing="ease-in-cubic" data-hide-arrows="1" class="w-slider slider">
<div class="w-slider-mask">
<div class="w-slide slide">
<div class="tagline">
<h1>Marc Cain</h1>
<h3>F/W 2015-16</h3>
</div>
<img width="846" src="http://uploads.webflow.com/567a26541a69a693654038a1/567b15da06a9675444fc740d_marc_cain_campaign.jpg" class="img">
</div>
</div>
<div class="w-slider-arrow-left">
<div class="w-icon-slider-left"></div>
</div>
<div class="w-slider-arrow-right">
<div class="w-icon-slider-right"></div>
</div>
<div class="w-slider-nav"></div>
</div>
</div>
</div>
</body>
I'm using webflow and uploaded the site for you guys http://the-one-and-only.webflow.io/
I originally tried making another absolute div with a set width and 100% height, but the menu button wasn't relative to the main container. Any help would be appreciated.
Give this a look, it mimics what http://www.bureautonic.com/en/ has for their menu
$(function() {
$('#menu-container').click(
function() {
ToggleMenu();
}
);
});
function ToggleMenu() {
var $menu = $('#menu');
var newleft = +$menu.css('left').replace('px', '') <= -150 ? '0' : '-300px';
$('#menu').css('left', newleft);
}
#menu,
#content {
width: 300px;
height: 200px;
}
#menu-container {
display: inline-block;
height: 200px;
width: 30px;
background-color: yellow;
position: relative;
}
#menu {
display: inline-block;
position: absolute;
}
#content {
display: inline-block;
position: relative;
overflow: hidden;
background-color: red;
}
#menu {
transition: left 1s;
left: -300px;
background-color: orange;
}
#menu-label {
-moz-transform: translateX(-50%) translateY(-50%) rotate(-90deg);
-webkit-transform: translateX(-50%) translateY(-50%) rotate(-90deg);
transform: translateX(-50%) translateY(-50%) rotate(-90deg);
position: absolute;
top: 50%;
text-align: center;
width: 200px;
left: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id="menu-container">
<div id="menu-label">
This is my Menu Label
</div>
</div>
<div id="content">
This is the content
<div id="menu">
Menu
</div>
</div>
For the sliding menu
The basic concept is a parent div with position:relative and overflow:hidden , and a child div with position:absolute, beginning with a negative left equal to the width of the div
I used the css transition property for the smooth slide effect
Edit:
For the left aligned & rotated menu label
This effect is created with a combination of several properties.
My code block has been updated with the appropriate css.
See here http://jsfiddle.net/CCMyf/79/ (not my fiddle) for alterations
to the css if you need to have a dynamic height
If you want to float a menu to left of the main content, you need to firstly create the menu element that you want to be the menu (obviously), then float it to the left with float: left. e.g.
HTML
<div class="floated-menu">
Menu
</div>
CSS
.floated-menu {
float: left;
width: 50px;
height: 600px;
background-color: #ccc;
}
Then you have to float the main content container as well. .e.g
.container {
float: left;
position: relative;
width: 800px;
height: 100%;
}
I could be wrong, but I believe if you don't float both the items, the normal (non-floated context) behaviour of the container divs display: block; property kicks in and it will move down the page to the next "line". Which is weird because all items next to something thats floated should lose their display block behaviour and sit next to the floated item - i.e. float was originally intended to make block type headings and paragraphs sit next to pictures like in a magazine or newspaper, but yep, welcome to the world of CSS - you fill find many nonsensical things like this.
Also, the combined width of both floated elements border box (the widest and largest of the boxes that an element is contained in) cannot be wider than their parent element - other wise the second element will drop down to the next line - which actually does make sense. I have reduced the sizes for you in my demo, but you will have to manage that as you build your page.
You also need to remember that, by default the browser uses the
"content-box" box-sizing property. from the docs
content-box
This is the default style as specified by the CSS standard. The width
and height properties are measured including only the content, but not
the padding, border or margin. Note: Padding, border & margin will be
outside of the box e.g. IF .box {width: 350px}; THEN you apply
{border: 10px solid black;} RESULT {rendered in the browser} .box
{width: 370px;}
Here is a demo - http://codepen.io/anon/pen/QyKyVV?editors=110
I was able to center align it properly when the content height and width were known, but if the popup content can vary in height and width, I cant think of any way to do this. Is there any?
Im doing this so that I can have a generic popup component, which will accept content of any height or width, starting from one line to screen max width/height.
Note: No javascript. Im looking for pure CSS methods.
The modern and bulletproof way to achieve this in late 2021 is with either flexbox or grid:
<-- HTML -->
<section class="parent flex"><div>Flex</div></section><-- OR -->
<section class="parent grid"><div>Grid</div></section>
<-- CSS -->
<style>
.parent.flex {
display: flex;
align-items: center;
justify-content: center;
}
.parent.grid {
display: grid;
place-content: center; /* shorthand for justify-content and align-content */
}
.parent {
width: 100vw; /* whatever */
height: 50vh; /* whatever */
}
</style>
Previous answer (Aug 2015):
There are a bunch of ways to do this, the easiest is using transforms:
// HTML
<div id="parent"><div id="child"></div></div>
// CSS
#parent {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
#child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%); // needs browser-prefixes
}
Working dynamic example below. Alternative solutions here: http://codepen.io/shshaw/full/gEiDt
function padding(val) {
document.querySelector("h1").style.padding = val + "em";
}
function text(val) {
document.querySelector("h1").innerText = val;
}
.container {
top: 0;
left: 0;
background: rgba(0, 0, 0, 0.9);
position: absolute;
height: 100%;
width: 100%;
overflow: scroll;
}
h1 {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: white;
padding: 1em;
}
input,
textarea {
position: relative;
}
<div class="container">
<h1>the middle.</h1>
</div>
<textarea type="text" min=1 max=5 step=.1 oninput="text(this.value)">the middle.</textarea>
<input type="range" min=1 max=5 step=.1 value=1 oninput="padding(this.value)">
Note: The above won't work in IE8.
Try this :
Demo: http://jsfiddle.net/GCu2D/834/
CSS:
html, body {
height:100%;
width:100%;
}
#popup {
position:fixed;
top:0;
left:0;
right:0;
bottom:0;
text-align:center;
}
#popup>div.wrapper {
position: absolute;
top: 50%;
left:0;
right:0;
transform: translateY(-50%);
display:block;
text-align:center
}
#popup .content {
display:inline-block;
border:1px solid blue //For visual feedback.
max-width:50%; //In case you want to restrict the div width
}
HTML:
<div id="popup">
<div class="wrapper">
<div class="content">Hello this is a sample content</div>
</div>
</div>
Replace the ID with a class in case you want multiple dialogs
For vertical alignment have you looked at this?
position: relative;
top: 50%;
transform: translateY(-50%);
For horizontal alignment simply apply text-align: center to the containing element.
You can check with the below link.
Fiddle
.reveal-modal {
background:#e1e1e1;
margin: 0 auto;
width:160px;
position:relative;
z-index:41;
top: 25%;
padding:30px;
-webkit-box-shadow:0 0 10px rgba(0,0,0,0.4);
-moz-box-shadow:0 0 10px rgba(0,0,0,0.4);
box-shadow:0 0 10px rgba(0,0,0,0.4);
}
I have a colored div that has been rotated 45 degrees and was wondering if there is way to crop the edges of it so that it fits within a certain boundry. (eg: a 45 degree line through a square that is cut off where it touches the square)
Here is the code:
#parent {
width: 200px;
height: 200px;
border: 1px solid black;
}
#child {
-ms-transform: rotate(45deg);
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
.red_stripe {
width: 500px;
height: 50px;
background-color: red;
position:absolute;
}
#gap {
height:100px;
}
<div id = "parent">
<div id = "child">
<div class = "red_stripe"></div>
<div id = "gap"></div>
<div class = "red_stripe"></div>
</div>
</div>
I have recreated this in JSFIDDLE: http://jsfiddle.net/RBlair/s9qusfvv/2/
So what should happen is that the red bar should be cut off where it meets the black border on the right and along the bottom sides (I am not worried about it exceeding the boundary at the top or left, just the right side and the bottom)
Add overflow:hidden to #parent
#parent {
width: 200px;
height: 200px;
border: 1px solid black;
overflow: hidden;
}
#child {
-ms-transform: rotate(45deg);
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
.red_stripe {
width: 500px;
height: 50px;
background-color: red;
}
#gap {
height: 100px;
}
<div id="parent">
<div id="child">
<div class="red_stripe">
</div>
<div id="gap">
</div>
<div class="red_stripe">
</div>
</div>
</div>
Let's reduce the HTML required
Pseudo element background
Use overflow: hidden, but create the lines with a ::before pseudo element and no extra HTML.
We can use:
inner box shadow to create the lines (useful as it does not take up space like a border)
position: absolute to position the :before along with a percentage height, width, left and right
position: relative on the div so that the pseudo element is positioned in relation to it
Complete Example
div {
width: 200px;
height: 200px;
border: 1px solid black;
position: relative;
overflow: hidden;
}
div::before {
content: '';
display: block;
position: absolute;
top: 0;
left: -50%;
box-shadow: inset 0 0 0 50px #F00;
height: 100%;
width: 200%;
transform: rotate(45deg);
z-index: -1;
}
<div class="box">
</div>