Iยดve made a transparent "button" and place it on a fullwidth background image
I use the class in an <a>
I would like the button to be centered responsively on the image. I got it done horizontally but not exactly vertically. (My idea is to make a one page type a page)
Here is the html:
<div class="container">
<div class="nav">nav area</div>
<div class="bg-image">
<a class="button-transp abs" href="#">Butt text<br>second line<br>third</a>
<img src="bg_top.jpg">
<h1>This is centered text.</h1>
</div>
<div class="main">main area</div>
</div>
the CSS
.abs {
position: absolute;
z-index: 2;
margin: 25% auto 0;
left: 0;
right: 0;
}
.button-transp {
display: inline-block;
min-width: 170px;
max-width: 500px;
width: 30%;
padding: 8px;
color: #fff;
background-color: transparent;
border: 2px solid #fff;
text-align: center;
outline: none;
}
I use margin: 25% auto 0; to get it something near right..
You wanted the button to stay center responsively to the image, which, in this case you should use background image. Then position your button to be 50% from the top, 50% from the left and pull it back -1/2 of its height to the top and -1/2 of its width to the left. Doing this to enforce the button to always stay in the center of the bg-image.
<div class="container">
<div class="nav">nav area</div>
<div class="bg-image">
<a class="button-transp abs" href="#">Butt text<br>second line<br>third</a>
<h1>This is centered text.</h1>
</div>
<div class="main">main area</div>
</div>
.abs {
position: absolute;
z-index: 2;
top: 50%;
margin-top: -37px; /** the button height is 74, so pull it half to the top the make the gap top and bottom equal */
left: 50%;
margin-left: -95px; /** again, full button width / 2 */
}
.button-transp {
display: inline-block;
min-width: 170px;
max-width: 500px;
width: 30%;
padding: 8px;
color: #fff;
background-color: transparent;
border: 2px solid #fff;
text-align: center;
outline: none;
}
.bg-image {
background: url(http://www.online-image-editor.com//styles/2014/images/example_image.png) no-repeat 0 0;
width: 475px;
height: 360px;
position: relative;
}
See fiddle
I've abbreviated the structure but this option will work for any size button and will always center both horizontally & vertically.
CSS3 transforms are basically IE9 and up (suitably prefixed as required)
.bg-image {
height: 200px;
background: orange;
position: relative; /* positioning contexr */
}
.button-transp {
width: 30%;
white-space:nowrap;
padding: 8px;
color: #fff;
background-color: transparent;
border: 2px solid #fff;
text-align: center;
outline: none;
position: absolute;
z-index: 2;
top:50%;
left: 50%;
transform:translate(-50%,-50%);
}
<div class="container">
<div class="bg-image">
<a class="button-transp abs" href="#">Butt text<br/>second line<br/>third</a>
</div>
</div>
Related
I have a requirement to make a banner that is 50% of the width of the "content wrapper" and that content wrapper is dynamically sized to a max width (so it can be, in theory, 0px-1330px). The content wrapper is the dark gray box here. It contains the content of the site. Within that content is a banner that comes out from the side of window. The window in this exactly has a boundary of the black outline.
How with HTML/CSS can I make the content of the purple container fit within the dark "content wrapper" even tho it is 50% of the window and the content lines up with left side of the wrapper when I don't know the specific width.
I've tried all kinds of math but I can't quite get it. I'm using CSS vars for the site max width and I can use var or calc to make this work but just no combo is working.
Does this fit your question?
* {
margin: 0;
padding: 0;
list-style: none;
}
.wrap {
width: 100%;
max-width: 500px;
height: 100vh;
margin: auto;
background-color: #aaa;
}
.banner {
width: 50%;
background-color: #a0f;
padding: 20px 20px 20px 0;
position: relative;
box-sizing: border-box;
}
.banner::before {
content: '';
position: absolute;
background-color: #a0f;
top: 0;
left: calc(100% - 50vw);
height: 100%;
width: calc(50vw - 100%);
}
<div class="wrap">
<h1>Content Wrapper</h1>
<div class="banner-wrap">
<div class="banner">
<h2>The title here</h2>
<p>Some other text here</p>
</div>
</div>
</div>
* {
padding: 0px;
margin: 0px;
box-sizing: border-box;
}
body {
overflow: hidden;
}
.window {
width: 100%;
height: 100vh;
border: 2px black solid;
background-color: lightgrey;
display: grid;
align-items: center;
justify-items: center;
}
.wrapper {
height: 75%;
width: 80%;
background-color: darkgrey;
padding:20px 0px;
}
.content {
padding: 20px 0px;
position: relative;
width:50%;
}
p{
position: relative;
z-index:2;
color:white;
}
.banner-container {
height: 100%;
width: 100%;
position: absolute;
top: 0px;
clip-path:inset( -100vw 0vw -100vw -100vw );
}
.banner {
height: 100%;
width: 150vw;
position: absolute;
top: 0px;
left: -50vw;
background-color: purple;
}
<div class="window">
<div class="wrapper">
<div class="content">
<p><b>Some text here</b></p>
<p>Some text here</p>
<div class="banner-container">
<div class="banner">
</div>
</div>
</div>
</div>
</div>
I'm trying to place two images on top of each other, with both of the images horizontally and vertically centered inside their container.
One of the images will be have its opacity animated to reveal the image underneath.
The images are both the same size, but I don't know the size of the images beforehand. I also would like to do this in just pure CSS and HTML.
Here is what I ended up with.
.data-box{
border: 2px solid #d4d4d4;
border-radius: 3px;
display: flex;
height: 120px;
margin: 5px;
margin-bottom: 10px;
margin-right: 10px;
padding: 0;
position: relative;
width: 120px;
}
.logo {
align-items: center;
display: flex;
justify-content: center;
margin: auto;
position: relative;
}
.data-name {
position: absolute;
width: 100%;
height: 23px;
bottom: 0px;
right: 0px;
background: rgba(200, 200, 200, 0.3);
}
span {
position: absolute;
width: 100%;
bottom: 2px;
text-align: center;
}
img {
position: absolute;
}
<div class="data-box">
<div class="logo">
<img class="grayscale-image" src="https://placeholdit.imgix.net/~text?txtsize=8&txt=65%C3%9765&w=65&h=65" alt="">
<img class="color-image" src="https://placeholdit.imgix.net/~text?txtsize=8&txt=65%C3%9765&w=65&h=65" alt="">
</div>
<div class="data-name"><span>Flickr</span></div>
</div>
I made the images position: absolute so they would leave the normal flow of the browser and render directly on top of each other instead of next to each other.
This works correctly in Chrome, but in Firefox and Safari the image's top left corner is horizontally and vertically centered:
How can I horizontally and vertically center these images while still having them render directly on top of each other?
Solution
Add this to your code:
img {
position: absolute;
top: 0;
left: 0;
transform: translate(-50%, -50%);
}
.data-box {
border: 2px solid #d4d4d4;
border-radius: 3px;
display: flex;
height: 120px;
margin: 5px;
margin-bottom: 10px;
margin-right: 10px;
padding: 0;
position: relative;
width: 120px;
}
.logo {
align-items: center;
display: flex;
justify-content: center;
margin: auto;
position: relative;
}
.data-name {
position: absolute;
width: 100%;
height: 23px;
bottom: 0px;
right: 0px;
background: rgba(200, 200, 200, 0.3);
}
span {
position: absolute;
width: 100%;
bottom: 2px;
text-align: center;
}
img {
position: absolute;
top: 0;
left: 0;
transform: translate(-50%, -50%);
}
<div class="data-box">
<div class="logo">
<img class="grayscale-image" src="https://placeholdit.imgix.net/~text?txtsize=8&txt=65%C3%9765&w=65&h=65" alt="">
<img class="color-image" src="https://placeholdit.imgix.net/~text?txtsize=8&txt=65%C3%9765&w=65&h=65" alt="">
</div>
<div class="data-name"><span>Flickr</span>
</div>
</div>
Explanation
Although setting an element to position: absolute removes it from the normal flow, it doesn't actually position it anywhere.
The CSS offset properties (top, bottom, left and right) have an initial value of auto, which keeps an absolutely positioned element where it normally would be if it were in the document flow. As you can see, browser behavior will vary when the offsets aren't defined.
For an explanation of how the code above works, see this post: Element will not stay centered, especially when re-sizing screen
I don't think you need flexbox at all:
.data-box {position:relative; display:inline-block;}
.logo {position:relative;}
.color-image {position:absolute; top:0; left:0; bottom:0; right:0; opacity:0.5;}
.data-name {position:absolute; left:0; right:0; bottom:5px; text-align:center;}
<div class="data-box">
<div class="logo">
<img class="grayscale-image" src="https://placeholdit.imgix.net/~text?txtsize=8&txt=65%C3%9765&w=65&h=65" alt="">
<img class="color-image" src="https://placeholdit.imgix.net/~text?txtsize=8&txt=65%C3%9765&w=65&h=65" alt="">
</div>
<div class="data-name"><span>Flickr</span></div>
</div>
Could you set the img in a div, and have the behind image set as the background of the div?
It's not the most elegant solution but this works:
img {
position: absolute;
transform: translate(-50%, -50%);
}
I'll try to get straight to the point.
Here's an example that centralizes two images inside a parent.
<html>
<head>
<title>Exemple</title>
<style type="text/css">
.parent{
margin: auto auto;
width: 500px;
height: 500px;
border: 3px solid #ccc;
}
.child1, .child2{
width: 50%;
height: 50%;
margin: 25%;
background-color: rgb(226,26,60);
}
.child1{
opacity:0.5;
}
</style>
</head>
<body>
<div class="parent">
<img class="child1" src="https://placeholdit.imgix.net/~text?txtsize=8&txt=65%C3%9765&w=65&h=65" alt="">
<img class="child2" src="https://placeholdit.imgix.net/~text?txtsize=8&txt=65%C3%9765&w=65&h=65" alt="">
</div>
</body>
</html>
Use margin with percentage to align both images in the middle of the
parent div. Here I set the with and height to 50%, which means there's
50% left. That's why you set the margin to 25%, so he puts it in the
middle of the parent.
Good luck
I really need your help,
I can't seem to figure out as to why my div #text spills out past my container div? It should fit nicely inside its container?
Here is the CSS markup:
height: 100px;
width: 500px;
bottom: 50%;
right: 50%;
position: absolute;
display: none;
}
#container {
background: #FFF;
left: 50%;
padding: 10px;
top: 50%;
margin: 0;
padding: 0;
height: 100%;
border: 2px solid rgb(100,139,170);
height: 100%;
position: relative;
}
.topbar {
cursor: pointer;
color: white;
background: rgb(100,139,170);
padding: 4px;
font-weight: bold;
}
#text {
height: 100%;
border: 1px solid red;
}
HTML:
<div id="wrapper">
<div id="container">
<div style="float:left;" class="topbar">Custom Dialog Box</div><div class="topbar" style="text-align: right;">Close</div>
<div id="text"><p>test</p></div>
</div>
</div>
Here is a snapshot of the problem:
The height of #text is 100% which means it gets the height of the containing block, in this case #container. Both the height of #text as well as the #container are 500px. But #text is being pushed down by it's sibling .topbar, causing it to overflow.
To solve this you can use the css property overflow:auto as suggested by Jarred Farrish in the comments
Because #test {height:100%;} it will look for it's parent's height, all the way to #wrapper which is set to height:100px, so #test will get the same height, plus the borders, and the #container doesn't have enough space to hold it (due to the extra blue bar), so it overflows.
I also noticed the layout can be done simpler as follows.
#wrapper {
height: 100px;
width: 500px;
bottom: 50%;
right: 50%;
margin-bottom: -50px; /*half height*/
margin-right: -250px; /*half width*/
position: absolute;
/* display: none; */
}
#container {
background: #FFF;
border: 2px solid rgb(100, 139, 170);
}
.topbar {
cursor: pointer;
color: white;
background: rgb(100, 139, 170);
padding: 4px;
font-weight: bold;
}
#text {
border: 1px solid red;
}
<div id="wrapper">
<div id="container">
<div style="float:left;" class="topbar">Custom Dialog Box</div>
<div class="topbar" style="text-align: right;">Close</div>
<div id="text">
<p>test</p>
</div>
</div>
</div>
You are taking the height of the #container but remember that there is also sort of a header at the top of the container so the text height should be < 100% because you have to substract the height of the dialog header.
Amir got point, the way you can "fix" this is to add padding to content, so you got safe space.
CodePen Sample
<div id="wrapper">
<div id="container">
<div style="float:left;" class="topbar">Custom Dialog Box</div><div class="topbar" style="text-align: right;">Close</div>
<div id="text"><p>test</p></div>
</div>
#wrapper{
height: 100px;
width: 500px;
bottom: 50%;
right: 50%;
margin-right: -250px;
position: absolute;
border: 1px solid yellow;
}
#container {
background: #FFF;
left: 0%;
padding-bottom: 30px;
top: 0%;
margin: 0;
height: 100%;
border: 2px solid rgb(100,139,170);
position: relative;
}
.topbar {
cursor: pointer;
color: white;
background: rgb(100,139,170);
padding: 4px;
font-weight: bold;
border: 1px solid green;
}
#text {
height: 100%;
border: 1px solid red;
}
I also fixed positioning for you.
I'm having trouble with a simple layout for a navigation bar. The icons of the bar are supposed to be both horizontally and vertically centred in their cell.
http://jsfiddle.net/digorydoo/j2v5m7gr/
I just can't figure out what's wrong with my layout.
HTML:
<div class="outer-frame">
<div class="nav-frame">
<div class="nav-cell">
<div class="nav-icon">๐ </div>
</div>
<div class="nav-cell">
<div class="nav-icon">๐</div>
</div>
<div class="nav-cell">
<div class="nav-icon">๐ซ</div>
</div>
</div>
</div>
CSS:
/* box around everything */
.outer-frame {
position: relative;
/* origin for absolute pos'ed children */
margin-left: auto;
margin-right: auto;
margin-bottom: 12pt;
width: 200px;
height: 190px;
border: 1px solid #f0f0f0;
background-color: #fafafa;
}
/* grey area to the left */
.nav-frame {
position: absolute;
left: 0px;
top: 0px;
box-sizing: border-box;
width: 36px;
height: 100%;
background-color: grey;
}
/* the outer container of the icon */
.nav-cell {
position: relative;
display: block;
box-sizing: border-box;
width: 36px;
height: 38px;
background-color: yellow;
margin-top: 4px;
}
/* the inner container of the icon */
.nav-icon {
display: block;
background-color: white;
border: 1px solid orange;
width: 8px;
height: 8px;
margin:auto;
position:absolute;
top:0;
right:0;
bottom:0;
left:0;
}
This is one of the ways to center it vertically and horizontally, you need to position it absolutely, set the margin to auto and all four sides to zero (or an offset, but you need to define it):
position: absolute;
margin: auto;
left: 0;
right: 0;
top: 0;
bottom: 0;
See it here: http://jsfiddle.net/j2v5m7gr/7/
The icons of the bar are supposed to be both horizontally and vertically centred in their cell.
Here you go: http://jsfiddle.net/j2v5m7gr/10/ The same approach from above.
I'm trying to create a progress bar and i have a problem aligning div inside a div.
css:
.outer {
width: 20px;
height: 190px;
border: 2px solid #ccc;
overflow: hidden;
position: relative;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
display:inline-block;
}
.inner {
width: 100%;
overflow: hidden;
position: absolute;
border-top-width: 0;
background-image: url('/images/progressBar2Red.png');
background-size: 20px;
bottom: 0;
height: 0%;
display:inline-block;
}
.progress{
display: inline-block;
align-items:center;
}
html:
<div class="progress">
<label class="progNum">20</label><br />
<div class="outer">
<div class="inner"></div>
</div>
</div>
For some reason the inner div is not exactly in the middle of the outer div. This is how it looks:
How can i put the inner div exactly in the middle of the outer div?
You made this .inner element of position: absolute. Just add left: 0; and right: 0; to the .inner CSS rules.
Divs are of 100% width by default, never set a div to a 100% width unless you absolutely need it... for some reason.
EDIT
Ok I actually do not understand what DOES NOT work for you. Check this JSFiddle. I think the problem is your background.
Give the inner margin: 0 auto;
JSfiddle Demo
HTML
<div class="progress">
<label class="progNum">20</label>
<div class="outer">
<div class="inner"></div>
</div>
</div>
CSS
.outer {
height: 190px;
border: 2px solid #ccc;
overflow: hidden;
position: relative;
border-radius: 4px;
}
.inner {
width: 100%;
position: absolute;
border-top-width: 0;
background-color: red;
bottom: 0;
width: 100%;
height:20px; /* or anything else you want */
border-radius: 4px;
}
.progress{
display: inline-block;
width: 20px; /* sets width of the whole bar - everything else can be 100% */
}
.progNum {
text-align: center;
display: block;
}