I'm trying to overlay an icon on top of an element's border. My current solution involves absolute positioning. I can hack it to fit as close to center as possible by using something like left: 40% but as soon as I resize the window, it moves out of the center.
Here's a JSFiddle showing what I've got so far. You'll see that if you resize the window, the icon moves out of center. https://jsfiddle.net/83on2jr9/
Is there an easier approach to this?
You could use margin:0 auto; with position:absolute; - providing that you have some other values set:
.landing-section2 .landing-icon {
position: absolute;
top:-16px;
right:0;
bottom:0;
left:0;
width:50px;
height:50px;
margin:0 auto;
}
JSFiddle
You can use calc in the .landing-section2 .landing-icon class :
left: calc(50% - 32px);
JSFiddle
Use a CSS transform. This is responsive and works for any size element and doesn't require any magic number for widths and margins.
.landing-section2 .landing-icon {
color: #357ca3;
font-size: 3em;
background: #2c2c2c;
z-index: 1000;
position: absolute;
padding-left: 10px;
padding-right: 10px;
left: 50%;
top: 0;
transform:translate(-50%,-50%);
}
JSfiddle Demo
Support is IE9 and up CanIUse.com
I find that when using absolute positioning, it's easier to use it as included in the JSFiddle I updated below. Basically, I wrap the "icon" in a span and attain much greater control.
.landing-section2 .landing-icon {
color: #357ca3;
font-size: 3em;
z-index: 1000;
position: absolute;
top: -28px;
left: 0;
width: 100%;
text-align: center;
}
.landing-icon span {
display: inline-block;
padding: 8px;
background: #2c2c2c;
}
Here is the updated Fiddle with working code: https://jsfiddle.net/83on2jr9/7/
I think, put 'margin-left: -32px' is easy way to move it to center without changing many other options.
also, it moves dynamically.
you can use display and margin too without position :) https://jsfiddle.net/83on2jr9/10/
.landing-section2 {
padding: 50px;
background-color: #2c2c2c;
text-align: center;
}
.landing-section2 .col-sm-4 > div {
border: 1px solid #357ca3;
padding: 20px;
position: relative;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
margin-bottom:2em;
}
.landing-section2 h3 {
color: white;
margin-top: 30px;
}
.landing-section2 p {
color: #ccc;
}
.landing-section2 .landing-icon {
color: #357ca3;
font-size: 3em;
background: #2c2c2c;
display:table;
margin:-1em auto 0;
padding:0 5px;
}
<div class='landing-section2'>
<div class='container'>
<div class='row'>
<div class='col-sm-4 landing-section2-pillar'>
<div>
<div class='landing-icon'>#</div>
<h3>
Section 1
</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas nunc nulla, fringilla et auctor et, congue sit amet nibh. Aenean vel est ante. Suspendisse quis tortor laoreet ligula vehicula commodo. Morbi suscipit, neque id vulputate mollis, orci sapien aliquam sem, ac laoreet ex nisi id leo.</p>
</div>
</div>
<div class='col-sm-4 landing-section2-pillar'>
<div>
<div class='landing-icon'>#</div>
<h3>
Section 2
</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas nunc nulla, fringilla et auctor et, congue sit amet nibh. Aenean vel est ante. Suspendisse quis tortor laoreet ligula vehicula commodo. Morbi suscipit, neque id vulputate mollis, orci sapien aliquam sem, ac laoreet ex nisi id leo.</p>
</div>
</div>
<div class='col-sm-4 landing-section2-pillar'>
<div>
<div class='landing-icon'>#</div>
<h3>
Section 3
</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas nunc nulla, fringilla et auctor et, congue sit amet nibh. Aenean vel est ante. Suspendisse quis tortor laoreet ligula vehicula commodo. Morbi suscipit, neque id vulputate mollis, orci sapien aliquam sem, ac laoreet ex nisi id leo.</p>
</div>
</div>
</div>
</div>
</div>
Related
I'm trying to set a background color for a h3 which is at the bottom of a paragraph however the color is covering both the heading and the paragraph text when I just want it to cover the h3. Not sure why its oversizing like that rather than just wrapping around the h3.
This is how I've done it -
section.council {
height: 350px;
max-width: 100%;
position: relative;
background-color: #F0F0F0;
}
.council h2 {
text-align: left;
margin-top: 30px;
line-height: 5px;
font-size: 20px;
color: #000000;
}
.council p {
font-size: 10px;
color: black;
float: left;
margin-top: 20px;
margin-right: 50px;
}
.readmore h3 {
color: #FFFFFF;
font-size: 15px;
text-align: center;
background-color: #00BFFF;
}
<div class="row">
<h4>LATEST CASE STUDY</h4>
<div class="six columns">
<h2>Wakefield Council</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vel ex nisl. Vestibulum vitae ultricies nisl. Praesent sodales, leo at pellentesque pellentesque, nunc erat dapibus nunc, ut congue libero lorem in orci.
<br> Suspendisse potenti. Quisque facilisis mauris in vestibulum tempor. Suspendisse nec venenatis nisi. Phasellus sodales viverra ante quis efficitur. Pellentesque quis orci mi.
</p>
<div class="readmore">
<h3>READ MORE</h3>
</div>
</div>
</div>
Is your problem just the fact that h3 as a block element by default goes over the whole width …?
Well then just make it inline or inline-block, and move text-align to the parent element to center it:
section.council {
height: 350px;
max-width: 100%;
position: relative;
background-color: #F0F0F0;
}
.council h2 {
text-align: left;
margin-top: 30px;
line-height: 5px;
font-size: 20px;
color: #000000;
}
.council p {
font-size: 10px;
color: black;
float: left;
margin-top: 20px;
margin-right: 50px;
}
.readmore {
text-align:center;
}
.readmore h3 {
display: inline-block;
color: #FFFFFF;
font-size: 15px;
background-color: #00BFFF;
}
<div class="row">
<h4>LATEST CASE STUDY</h4>
<div class="six columns">
<h2>Wakefield Council</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vel ex nisl. Vestibulum vitae ultricies nisl. Praesent sodales, leo at pellentesque pellentesque, nunc erat dapibus nunc, ut congue libero lorem in orci.
<br> Suspendisse potenti. Quisque facilisis mauris in vestibulum tempor. Suspendisse nec venenatis nisi. Phasellus sodales viverra ante quis efficitur. Pellentesque quis orci mi.
</p>
<div class="readmore">
<h3>READ MORE</h3>
</div>
</div>
</div>
If you want it to look more button-y, then add a bit of padding as well.
I want to have two stacked divs on one side, and then have a single column on the other side with the same height as the left divs.
Kind of like this:
I have the two divs and a side bar, but the two divs won't stack.
Here is what I have so far Fiddle
#import url(https://fonts.googleapis.com/css?family=Oxygen);
body {
background-color: #222;
}
.description h1 {
text-align: left;
padding: 20px;
}
#wrapper {
text-align: center;
}
.description,
.sidebar,
.demo-container {
display: inline-block;
vertical-align: top;
}
.description {
background: #eee;
width: 50%;
font-family: "Oxygen";
font-size: 14px;
color: #000;
line-height: 1.2;
}
.sidebar {
background: #eee;
width: 15%;
height: 575px;
}
.demo-container {
background: #eee;
width: 50%;
font-family: "Oxygen";
font-size: 14px;
color: #000;
line-height: 1.2;
}
<div id='wrapper'>
<div class="demo-container">
<h1>Title</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam pretium lorem nec tortor elementum.</p>
</div>
<div class="description">
<h1>Title</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam pretium lorem nec tortor elementum.</p>
</div>
<div class="sidebar">
</div>
</div>
you are complicating a lot, here is a basic demo of what you want using flexbox
*,
*::before,
*::after {
box-sizing: border-box
}
body {
margin: 0
}
.flex {
display: flex;
flex-basis: 100%
}
.fl {
flex: 1;
display: flex;
flex-direction: column;
margin: 0 5px;
justify-content: space-between
}
.flex-item {
border: 1px solid black
}
.flex-item:not(:first-of-type) {
margin: 10px 0 0
}
.sidebar {
border: 1px solid black;
}
<div class="flex">
<div class="fl">
<div class="flex-item">
<h1>Title</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam pretium lorem nec tortor elementum, et aliquam erat feugiat. Duis interdum enim vitae justo cursus pulvinar eu ac nulla. Donec consectetur vehicula turpis. Nunc laoreet tincidunt elit</p>
</div>
<div class="flex-item">
<h1>Title</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam pretium lorem nec tortor elementum, et aliquam erat feugiat. Duis interdum enim vitae justo cursus pulvinar eu ac nulla. Donec consectetur vehicula turpis. Nunc laoreet tincidunt elit
ultrices elementum. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Curabitur augue magna, posuere id tortor vel, condimentum consectetur lacus. Pellentesque dui est, ornare vitae semper et, dapibus ut lacus.
Etiam sed porta dui. Phasellus non nisl eget dolor commodo imperdiet.</p>
</div>
</div>
<div class="fl sidebar"></div>
</div>
Just put <div class="sidebar"></div> before the other two divs, then float them all right. See fiddle https://jsfiddle.net/y71tkmtw/1/
.description,
.sidebar, .demo-container {
float: right;
margin: 40px;
}
Just add another <div> surrounding the 2 divs on the left-hand side, with float:left. Add float:right to the sidebar.
.left-container
{
width: 85%;
float:left;
}
.sidebar {
background: #eee;
width: 15%;
height: 575px;
float:right;
}
Fiddle: https://jsfiddle.net/dncgytef/2/
I have a main div with CSS:
.border {
border-radius: 25px;
border: 8px solid #2C3E50;
padding: 50px;
width: 500px;
height: 500px;
margin: 0 auto;
display: flex;
justify-content: center;
align-items: center;
}
And two buttons, side by side, inside the div, with CSS to make it animate on hover/press. How would I make the buttons to be at the bottom of the div, no matter of what the content is in the div? Position: absolute, doesn't work for me.
Try position: absolute or relative and then alter the bottom and right properties.
Also, have you checked to make sure that no other styles are overwriting your preferred styles? Chrome's developer tools are an awesome way to find this out.
For html:
<div id="wrapper">
<button></button>
</div>
You can apply style:
#wrapper {
position: relative;
}
#wrapper > button {
position: absolute;
bottom: 0;
}
To place the <button> element at the bottom of the wrapper <div>. Take note that you must declare position: relative or position: absolute on the containing element ( the wrapper ) for position: absolute to place a child element (the button) with respect to its bottom.
Try this
.border {
border-radius: 25px;
border: 8px solid #2C3E50;
padding: 50px;
width: 500px;
height: 500px;
margin: 0 auto;
position: relative;
}
.inner-border-normal {
float: left;
}
.inner-border-bottom {
position: absolute;
bottom: 50px;
}
.button {
width: 100px;
height: 32px;
border: 1px solid #CCCCCC;
border-radius: 4px;
background: #F2F2F2;
cursor: pointer;
}
<div class="border">
<div class="inner-border-normal">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin id erat interdum ex dapibus sodales nec id nibh. Sed egestas bibendum erat quis ultricies. Ut auctor iaculis odio quis tempor. In maximus nisi et ex consectetur scelerisque. Nam et nunc sit amet massa faucibus commodo. Etiam at tortor nisl. Integer at justo sit amet ex ornare rutrum a sed felis. Mauris eu risus dolor. Curabitur non ullamcorper lacus. Curabitur ultricies aliquet ex, non maximus erat convallis eget. Nulla pretium ex quis commodo tempus. Nunc non vestibulum neque, vitae venenatis lorem. Nunc eget maximus ante. Sed aliquam ac arcu sed pharetra.</div>
<div class="inner-border-bottom">
<input type="button" class="button" value="Press">
<input type="button" class="button" value="Press">
</div>
</div>
I have this fixed right side bar layout working perfectly for me for a long time, it works in most of the browsers and devices too.
But the recent chrome update to 45 which happened few days ago, broke the layout by adding a horizontal scrollbar.
There are different ways to achieve the fixed right side bar layout, but this layout needs to extend the background color of main and side columns to the browser width extent with fixed max-width container(marked in red) and with shadow between columns.
And this below code was the best way I could achieve it.
Now all I need is no scrollbar in Chrome 45, I tried different ways to avoid it but none works. I know this wont be a easy fix, but any help on this would be appreciated.
http://jsfiddle.net/chetanjk/ptuxn2dq/
HTML
<div class="container" style="background:#000; color:#fff; text-align:center">
------page content max width for reference ----
</div>
<div class="page-cols">
<div class="container ">
<div class="cols-wrap">
<section class="main-col">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse ipsum sapien, tincidunt ac augue sodales, consequat sodales elit. Nunc pharetra eget velit sed pharetra.
</section>
<aside class="aside-col">
<div class="pack">
Sed luctus nisl ut ipsum scelerisque semper. Nullam euismod eros vitae odio viverra tristique. Nam pulvinar massa at diam congue, vitae fringilla neque varius. In molestie quis neque luctus facilisis.
Vestibulum sit amet mi ut odio condimentum dictum vel a metus. Morbi ultrices enim ut accumsan lacinia. Praesent augue purus, bibendum in odio in, pharetra consectetur mi. Vivamus ac arcu dignissim, placerat ipsum eu, tempor magna. Integer nec ipsum dui. Quisque at diam est. Aliquam ut placerat ligula, eu venenatis turpis. Sed nec eros vel ante ornare eleifend. Suspendisse aliquam nulla consectetur tellus molestie efficitur.
</div>
</aside>
</div>
</div>
</div>
CSS
body{
font-family: arial;
font-size: 14px;
color: #333;
line-height: 1.5;
overflow-y: scroll;
margin:0;
padding: 0;
}
*, *::before, *::after {
box-sizing: border-box;
}
.container:after,
.cols-wrap:after,
.page-cols:after{
clear: both;
content: "";
display:table;
}
.container{
margin: 0 auto;
max-width: 1200px;
min-width: 300px;
padding:0 10px;
position: relative;
}
.page-cols{
background-color: #999;
}
.cols-wrap{
width: 100%;
position: relative;
background-color: #ccc; /*this can be #fff too to match body bg*/
box-shadow: 10px 10px 10px -10px #000;
right: 320px;
}
.cols-wrap .main-col{
float: left;
left: 320px;
position: relative;
width: 100%;
padding-right: 340px;
}
.cols-wrap .aside-col{
float: right;
position: relative;
width: 320px;
margin-right: -320px;
padding-left: 20px
}
In the follwing example the text goes out of the box. And when I reduce the size of the borowser the size of the boxes shring resposively but the text becomes mixed and unorganized. How can solve this?
<html>
<head>
<meta charset="utf-8">
<title>This is an email template</title>
<style>
body {
background-color: rgba(79, 183, 227, 0.4);
direction: rtl;
}
body * {
font-family: Tahoma;
}
a:link {
text-decoration: none;
margin-right: 25px;
color: #46B1F9;
}
#wrap {
background-color: #e0f2f6;
margin: auto;
width: 75%;
padding: 15px;
border: 1px solid grey;
}
.item {
border: 1px solid #95A5A6;
border-radius: 5px;
margin-bottom: 25px;
width: 60%;
display: inline-block;
}
.item p {
font-size: 1em;
}
.item img {
float: left;
width: 30%;
}
.item .notice {
text-align: center;
float: right;
padding-top: 25px;
padding-right: 25px;
width: 50%;
height: 1em;
}
/*clearfixes*/
.clearfix:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
.clearfix {
display: inline-block;
}
/* Hides from IE-mac \*/
* html .clearfix {
height: 1%;
}
.clearfix {
display: block;
}
/* End hide from IE-mac */
</style>
</head>
<body>
<div id="wrap">
<div style="padding:15px;">
<div class="item clearfix">
<div class="notice">
<p><strong>Lorem ipsum</strong>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam rhoncus sollicitudin aliquet. Fusce dolor leo, egestas non nisi in, aliquam ullamcorper diam. Quisque placerat tortor in porta egestas. Aenean et elementum purus. Nunc eget nulla blandit, volutpat libero non, finibus purus. Vivamus vitae tellus at risus commodo varius.</p>
</div>
<img src="http://s14.postimg.org/wqzq39iht/image.jpg" alt="" />
</div>
<div class="item clearfix">
<div class="notice">
<p>
<strong>اLorem ipsum</strong> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam rhoncus sollicitudin aliquet. Fusce dolor leo, egestas non nisi in, aliquam ullamcorper diam. Quisque placerat tortor in porta egestas. Aenean et elementum purus. Nunc eget nulla blandit, volutpat libero non, finibus purus. Vivamus vitae tellus at risus commodo varius.</p>
</div>
<img src="http://s10.postimg.org/y4kk17q21/image.jpg" alt="" />
</div>
<div class="item clearfix">
<div class="notice">
<p><strong>Lorem ipsum</strong>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam rhoncus sollicitudin aliquet. Fusce dolor leo, egestas non nisi in, aliquam ullamcorper diam. Quisque placerat tortor in porta egestas. Aenean et elementum purus. Nunc eget nulla blandit, volutpat libero non, finibus purus. Vivamus vitae tellus at risus commodo varius.</p>
</div>
<img src="http://s3.postimg.org/xca6ju1kj/image.jpg" alt="" />
</div>
</div>
</div>
</div>
</div>
</body>
</html>
If you are trying to expand the block by content, removing height from .item .notice should fix the issue.
In all cases your text will overflow the box , so you should add overflow:scroll to notice class
Depends on what you are trying to do.
If the boxes must be a fixed height there are couple of different strategies.
The easiest thing to do is to turn off the height restriction to the notice class. However, this will reflow your document and push everything down.
On the other hand, if you want to keep the current layout, I cannot provide you a unilateral decision as the padding, height and overflow will conflict with each other on this element.