I have created a button with background image parts on the left, center and right. I would like to position it at the center of the page, but I can't do it.
I have simplified the example with simple color backgrounds instead of images.
HTML:
<a id="button" href="#">
<div id="b-left"></div>
<div id="b-center">Content</div>
<div id="b-right"></div>
</a>
CSS:
#button {
height: 40px;
margin: 0 auto;
}
#b-left, #b-right, #b-center {
display: inline;
float: left;
height: inherit;
}
#b-left {
background-color: blue;
width: 30px;
}
#b-right {
background-color: green;
width: 30px;
}
#b-center {
background-color: yellow;
}
Here's the demo:
http://jsfiddle.net/yh6sS/4/
Thanks a lot.
Replace all divs inside your link with spans. This will make the code to be valid.
"margin: 0 auto;" property only works, when there's a fixed width, for example 100px. So it can be deleted in your case.
Use the next technique to make all buttons: http://jsfiddle.net/2GJu2/
<div class="outer">
<a href="#">
<span class="b-left"></span>
<span class="b-center">Content</span>
<span class="b-right"></span>
</a>
</div>
.outer { text-align: center; }
a {
display: inline-block; margin: 0 10px; line-height: 30px;
position: relative; }
a span { display: inline-block; }
.b-center { background: yellow; }
.b-left,
.b-right { position: absolute; top: 0; width: 10px; height: 30px; }
.b-left { left: -10px; background: red; }
.b-right { right: -10px; background: green; }
Add text-align: center to a parent element, and add display: inline-block to #button.
See: http://jsfiddle.net/thirtydot/yh6sS/7/
Related
Today, I am comming with a problem from work. First of all, the code was created some time ago and I have to correct it now. Of course I've made the sandbox easier to avoid unnecessary styles.
<div>
<a id="perfect" href="https://css-tricks.com/">
<span class="perfect">
<p>Perfect</p>
</span>
<span class="maker">Solution</span>
</a>
</div>
<div>
<a href="https://css-tricks.com/">
<span class="problem">Problem</span>
<span class="maker">Makes me cry</span>
</a>
</div>
<div>
<a href="https://css-tricks.com/">
<span class="problem">Problem</span>
<span class="maker">Makes me cry</span>
</a>
</div>
div {
display: block;
width: 150px;
height: 150px;
background: lightblue;
text-align: center;
float: left;
margin: 10px;
}
a {
display: block;
width: 150px;
height: 150px;
text-decoration: none;
color: black;
}
.problem {
display: block;
padding: 30px 10px 0;
}
.maker {
display: block;
padding: 20px 10px 0;
}
p {
padding: 0;
margin: 0;
}
p:hover {
color: red;
}
I have three tile there. First of all works what I expect, but I would like to receive the same result on the second and third tile without paragraph.
The clue is that red color appears, if I put a mouse on random place above right content. I mean all span called "problem" is on hover.
How to ensure a similar behaviour like in first tile on the others without using paragraph? Do you have some idea?
I've tried to do that using margin, but it was wrong.
Try to add this into your stylesheets:
div > a > span:hover {
color: red;
}
Here is a solution:
Your issue is that your applying padding: 30px 10px 0; to span. The link is applying itself to the entire span with its padding.
I removed padding on your span and instead applied it to the div. - You can now adjust the padding on the div instead of the span.
Additionally, I moved the #perfect id to the first div because it had a different background-color.
div {
display: block;
width: 150px;
height: 150px;
background: lightblue;
text-align: center;
float: left;
margin: 10px;
padding: 30px 10px 0;
}
a {
display: block;
width: 150px;
height: 150px;
text-decoration: none;
color: black;
}
.problem {
display: block;
}
.problem:hover {
color: red;
}
.maker {
display: block;
}
p {
padding: 0;
margin: 0;
}
p:hover {
color: red;
}
.perfect {
display: block;
}
#perfect {
background-color: pink;
}
<div id="perfect">
<a href="https://css-tricks.com/">
<span class="perfect">
<p>Perfect</p>
</span>
<span class="maker">Solution</span>
</a>
</div>
<div>
<a href="https://css-tricks.com/">
<span class="problem">Problem</span>
<span class="maker">Makes me cry</span>
</a>
</div>
<div>
<a href="https://css-tricks.com/">
<span class="problem">Problem</span>
<span class="maker">Makes me cry</span>
</a>
</div>
Just change the display for the .problem from 'block' to 'inline-block', change the padding-top to 0 and give a margin-top of 30px
.problem {
display: inline-block;
padding: 0px 10px 0;
margin-top: 30px;
}
.problem:hover {
color: red;
}
so, I have some html layout and I cannot change the html, ONLY the css. Now, I can achieve the colors I want and create that circle (see image), with border-radius. Here is the rub, each square is a span. There are no inner divs/outer divs.. just the span. Is there a way to achieve, with css, that circle and then the half background fill.
the code would be, on a base level:
<span class="day is-range is-selected" />22</span>
<span class="day is-range" />23</span>
Basically, when a user selects a date, I color it that bright reddish color, make it a circle, make the other dates backgrounds that more bourbon red color.. great... BUT the selected date with the cirle doesn't have that "bleed into" the other square look with half its span colored. Is there a way to achieve this with css and no mods to the html?
I've only been able to achieve the following:
What I want to achieve.
This is really what I am trying to achieve. The span, goes to a circle, I can do that - but somehow make half of the span have a different background color.
The CSS I am using is fairly trivial. Note, I have to use !important to override what is gen'ed.
.is-selected {
background-color: #selected-background !important;
color: #base;
border-radius: 20px;
}
.is-inRange {
background-color: #active-background !important;
color: #base;
}
A pseudo-element would seem the only option here as the HTML cannot be altered.
The specificity has be managed though:
.day {
float: left;
width: 40px;
height: 40px;
background: plum;
line-height: 40px;
text-align: center;
color: white;
}
.is-range {
background: plum;
}
.is-range.is-selected {
background-color: red;
border-radius: 50%;
position: relative;
}
.is-range.is-selected:after {
content: "";
position: absolute;
top: 0;
width: 50%;
height: 100%;
left: 50%;
background: plum;
z-index: -1;
}
<div>
<span class="day is-range is-selected">22</span>
<span class="day is-range">23</span>
<span class="day is-range">24</span>
<span class="day is-range">25</span>
</div>
.day {
float: left;
width: 3em;
height: 3em;
background: silver;
line-height: 3em;
text-align: center;
}
.is-selected {
background-color: red;
border-radius: 50%;
position: relative;
}
.is-selected:after {
content: "";
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 50%;
background: silver;
z-index: -1;
}
<span class="day is-range is-selected">22</span>
<span class="day is-range">23</span>
If both days are in one row:
.day {
float: left;
width: 3em;
height: 3em;
background: silver;
line-height: 3em;
text-align: center;
}
.is-selected {
background-color: red;
border-radius: 50%;
position: relative;
}
.is-selected + .day {
margin-left: -1.5em;
padding-left: 1.5em;
}
<span class="day is-range is-selected">22</span>
<span class="day is-range">23</span>
.left-icons is inline-block and has a height of 21px:
Note that the height of the image inside of it is 38px:
CSS Tricks says:
If the height of the containing block is not specified explicitly, and
the element is not absolutely positioned, the value of its height
computes to auto (it will be as tall as the content inside it is, or
zero if there is no content).
The height of the containing block isn't being explicitly specified. So why is my outer element smaller than the image inside of it?
HTML
<div class='tango-directive-template'>
<div class='tango level-{{ level }}'>
<span class='left-icons'>
<img
ng-show='tango.children.length > 0'
src='/assets/images/show-arrow.png'>
<span class='author'>A</span>
</span>
<textarea
ng-focus='focus = true;'
ng-blur='focus = false;'
rows='1'>{{ tango.text }}</textarea>
<p class='menu' ng-show='focus'>
<span class='glyphicon glyphicon-indent-left'></span>
<span class='glyphicon glyphicon-indent-right'></span>
<span class='glyphicon glyphicon-arrow-down'></span>
<span class='glyphicon glyphicon-arrow-right'></span.
</p>
</div>
<tango
ng-repeat='subtango in tango.children'
tango='subtango'
level='{{ +level + 1 }}'>
</tango>
</div>
CSS
.tango-directive-template {
.tango {
margin-bottom: 20px;
.left-icons {
display: inline-block;
text-align: right;
width: 67px;
img, .author {
position: relative;
bottom: 15px;
margin-right: 5px;
height: 100%;
}
img {
height: 20px;
}
.author {
border: 1px solid gray;
border-radius: 25px;
padding: 10px;
}
}
textarea {
font-size: 18px;
width: 700px;
line-height: 135%;
padding: 8px 16px;
resize: none;
border: 1px solid white;
overflow: hidden;
}
textarea:focus {
outline: none;
border: 1px solid gray;
overflow: auto; // only have scroll bar when focused
}
.menu {
width: 750px;
span {
float: right;
margin-left: 15px;
cursor: pointer;
}
}
}
#for $i from 0 through 10 {
.level-#{$i} {
position: relative;
left: #{$i*65}px;
}
}
}
Use an inline block.
span.left-icons{
display: inline-block;
}
You probably should try a clearfix method.
Look here: What methods of ‘clearfix’ can I use?
I have a div with an image and a hidden button that shows up when the image is being hovered.
However, I don't know how to position the button relative to the top and right of the image instead of the div...
And here is the fiddle of what I have so far: http://jsfiddle.net/ab3vg97t/
HTML:
<div class="placeholder-container">
<a href=<?= "index.php?page=viewPoll&id=".$currentPoll['idPoll']."&previous=My+polls"; ?>>
<img class="resize-to-fit-and-center placeholder-containter-img" src="<?= $currentPoll['image'] != '' ? UPLOADS_URL . "/" . $currentPoll['image'] : 'assets/img/default-poll.png' ?>" alt="Default poll image">
</a>
<a id="my-poll-delete-btn" href=<?="actions.php?action=deletePoll&id=".$currentPoll['idPoll']?>>
<i class="glyphicon glyphicon-remove"></i>
</a>
</div>
CSS:
.placeholder-container {
width: 200px;
height: 200px;
line-height: 200px;
text-align: center;
display: inline-block;
position: relative;
}
.placeholder-container:hover #my-poll-delete-btn {
display: flex;
}
.placeholder-containter-img {
border-radius: 10px;
}
.placeholder-containter-img:hover {
box-shadow: 0 0 1em gray;
}
#my-poll-delete-btn {
display: none;
text-decoration: none;
position: absolute;
top: 10px;
right: 10px;
}
#my-poll-delete-btn:hover {
color: red;
}
.resize-to-fit-and-center {
max-width:100%;
max-height:100%;
vertical-align: middle;
}
I am trying to create a site with just one actual page containing multiple pseudo-pages in form of divs. There are four divs and I have set the width of the wrapper in which the divs are to 200%(so that I get two rows of two divs each) and set the divs to 50% width(so that they cover the whole page of the viewport).
I have four divs named home, like, dislike and contact. I first created a tag link to the like div and it worked. But tag links to no other divs are working and shows only the second page everytime.
Here is the jsfiddle : JsFiddle of my site
What am I doing wrong?
Css:
#wrapper { max-width : 100%;
overflow : hidden;
position : relative;
}
#header { position : fixed;
float : left;
}
#logo { margin-left: 0px;
padding-top: 20px;
height: 50px;
width: 300px;
border : solid black;
background-color: red;
}
#nav {margin-top : 20px;
width : 50%;
height: 300px;
border : solid black;
}
#pages { width: 200%;
position : relative;
border : solid black;
float: left;
height : 800px;
}
#main-page, #like-page, #dislike-page, #contact-page {float:left;
position : relative;
width:50%;
height: 800px;}
div.content { margin-top: 100px;
}
div H2 {margin-left: 180px;
margin-bottom: 20px;
}
div p {margin-left: 180px;
margin-right: 50px;
}
Here is a working fiddle on what you are talking about:
http://jsfiddle.net/X4URc/3/
I used html:
<div class='container'>
<div class='navbar'>
<div align='center'> <a class='menu1 menu-item'>Item 1</a>
<a class='menu2 menu-item'>Item 2</a>
<a class='menu3 menu-item'>Item 3</a>
<a class='menu4 menu-item'>Item 4</a>
</div>
</div>
<div class='content'>
<ul class='content-container'>
<li class='contents content1'>Content 1</li>
<li class='contents content2'>Content 2</li>
<li class='contents content3'>Content 3</li>
<li class='contents content4'>Content 4</li>
</ul>
</div>
</div>
CSS:
.menu-item {
background: black;
color: white;
padding: 15px;
cursor: pointer;
}
.menu-item:hover {
background: white;
color: black;
}
.menu-item:not(.menu1) {
margin-left: -8px;
}
.navbar {
background: black;
padding: 15px;
width: 700px;
}
.container {
background: white;
width: 730px;
margin: 0 auto;
}
.content1 {
margin-left: -40px;
}
.contents {
padding-bottom: 400px;
padding-right: 668px;
height: 500px;
background: red;
list-style-type: none;
display: inline;
}
.contents:not(.content1) {
margin-left: -4px;
}
body {
background: #ccc;
}
.content {
width: 730px;
background: white;
overflow: hidden;
}
.content-container {
width: 9999999px;
height: 500px;
}
Jquery:
$('.menu1').click(function(){
$('.content1').css({'margin-left' : '-40px'});
});
$('.menu2').click(function(){
$('.content1').css({'margin-left' : '-770px'});
});
$('.menu3').click(function(){
$('.content1').css({'margin-left' : '-1500px'});
});
$('.menu4').click(function(){
$('.content1').css({'margin-left' : '-2230px'});
});
// for more add -730px every time
//If you don't want animations change .animate() to .css()
Instead of having lots of divs, I used a <ul> within a div with overflow hidden and then styled it display: inline;