Please, see this fiddle: http://jsfiddle.net/xg6SJ/2/
Why text jumps?
Why borders on hover expands menu's div?
*, *:before, *:after
{
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
div
{
height: 100px;
width: 960px;
margin: 0 auto;
background-color: #eee;
}
ul
{
list-style-type: none;
margin: 0;
padding: 0;
}
li
{
display: block;
float: left;
box-sizing:border-box;
}
li > a
{
text-decoration: none;
font-size: 20px;
display: table-cell;
height: 100px;
vertical-align: middle;
padding: 0 18px;
}
li:hover
{
border-top: 2px red solid;
border-bottom: 2px red solid;
background-color: #ddd;
}
<div>
<ul>
<li>sdfdf</li>
<li>sdfdf</li>
<li>sdfdf</li>
<li>sdfdf</li>
</ul>
</div>
The li doesn't have a fixed height. If you set the height of the li to 100px the border is put inside the element on hover.
To prevent the text from jumping you can remove the additional height added by the borders from the a like this:
li:hover > a {
height: 96px; /* 100 - (border-top + border-bottom) */
}
Or you can add a transparent border and a fixed height to the li (demo).
li {
...
border-top: 2px transparent solid;
border-bottom: 2px transparent solid;
height: 100px;
}
Because the border is added to the div on hover only. So on hovering, the div's height is expanded. If you added border-top: 2px grey to the li (in 'unhovered' state) , you won't have that jumping effect anymore.
Check the updated fiddle: http://jsfiddle.net/xg6SJ/3/
li
{
display: block;
float: left;
box-sizing:content-box;
}
Because, border-box ... The width and height properties include the padding and border, but not the margin. 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 border, margin, or padding.
You are adding 2px to top on bottom on hover which where not their before.
Or add the two pixels of border to li and have the border be the same as the background until you hover.
li
{
display: block;
float: left;
border-top: 2px #eee; solid;
}
Related
How could I make the effect of below picture with HTML, CSS using the the bootstrap framework?
I need two adjacent divs with trapezoid shape (or separated by a diagonal line). Both need to have a border.
You can do this by drawing a shape in CSS.
You can draw such a triangle in CSS by playing with different borders (top, right, bottom left) of an element that has zero width.
Example: https://css-tricks.com/snippets/css/css-triangle/
In the example below I use the pseudo element :after for this effect:
/* Apply styles to both DIVs */
.container > div {
width: 50%;
float:left;
font-weight: bold;
padding-left: 10px;
/* include padding in the height/width */
box-sizing: border-box;
margin: 0;
}
.container {
/* One way to make the DIV height extend to full heihgt of `float:left` DIVs inside it. Not the only way */
clear: both;
}
.container div:first-child {
background: #66ff66;
/* The triangle will be position:absolute, so it requires a `position:relative` parent */
position: relative;
/* We are drawing a full rectangle later, so we hide the rest of it */
overflow: hidden;
}
.container div:last-child {
background: #ff6666;
}
.container div:first-child:after {
position: absolute;
display: block;
content: ' ';
padding: inherit;
box-sizing: border-box;
/* Change below units (you can use px not just em)
to make the line become at different angles */
border-top: 1.3em solid transparent;
border-bottom: 1.3em solid transparent;
border-right: 1.3em solid #ff6666;
right: 0;
top: 0;
}
<div class="container">
<div>div١</div>
<div>div٢</div>
</div>
Update
But as you indicated in the comment, you wanted a different answer that uses div2 for the triangle, so here you are:
/* Apply styles to both DIVs */
.container > div {
width: 50%;
float:left;
font-weight: bold;
/* include padding in the height/width */
box-sizing: border-box;
margin: 0;
}
.container {
/* One way to make the DIV height extend to full heihgt of `float:left` DIVs inside it. Not the only way */
clear: both;
}
.container div:first-child {
background: #66ff66;
padding-left: 10px;
}
.container div:last-child {
background: #ff6666;
position: relative;
padding-left: 1.3em;
}
.container div:last-child:before {
position: absolute;
content: '';.
width: 0;
height: 0;
box-sizing: border-box;
/* Change below units (you can use px not just em)
to make the line become at different angles */
border-top: 1.3em solid #66ff66;
border-bottom: 1.3em solid transparent;
border-right: 1.3em solid transparent;
top: 0;
left: 0;
}
<div class="container">
<div>div١</div>
<div>div٢</div>
</div>
Update 2
The picture you showed in comments also included real borders. This requires changing the approach. The new approach still uses :before, but adds border to it, and rotates it 45 degrees.
The idea is based on an example from: https://kilianvalkhof.com/2017/design/sloped-edges-with-consistent-angle-in-css/
To imagine it:
Here's the code:
/* Apply styles to both DIVs */
.container > div {
width: 50%;
float:left;
font-weight: bold;
/* include padding in the height/width */
box-sizing: border-box;
margin: 0;
}
.container {
/* One way to make the DIV height extend to full heihgt of `float:left` DIVs inside it. Not the only way */
clear: both;
}
.container div:first-child {
background: #66ff66;
padding-left: 10px;
border: 1px solid;
border-right: none;
}
/*
The following assumes diemnsions 1.3em * 1.3em
Your real case can change the number
*/
.container div:last-child {
background: #ff6666;
position: relative;
border: 1px solid;
border-left: none;
padding-left: calc(1.5 * 1.3em);
overflow: hidden;
}
.container div:last-child:before {
position: absolute;
content: '';
width: calc(2 * 1.3em);
height: calc(2 * 1.3em);
box-sizing: border-box;
background: #66ff66;
border: 1px solid ;
transform:rotate(45deg);
margin-top: -1.3em;
margin-left: -1.3em;
left: 0;
top: 0;
}
<div class="container">
<div>div١</div>
<div>div٢</div>
</div>
just use border-right like following code snippet and see result :
.parent{
width: 100%;
display: flex;
background-color: #01579b;
}
.div1 {
width: 30%;
border-bottom: 100px solid #000;
border-right: 50px solid transparent;
}
.div2 {
width: 70%;
height: 100px;
}
<div class="parent">
<div class="div1"></div>
<div class="div2"></div>
</div>
I want to have 3 elements in line.. a round icon for the step number, the title and an optional icon on the far right, separated by a vertical line.
The issue is with the bottom border. Because the bottom border "enlarges" the element, there is a gap in the bottom of the border between the vertical border and the bottom border... (and in fact, in my production code, the border bottom on hover DOES line up with the border-bottom of the container, however, the vertical border still has a gap at the bottom equal to the thickness of the border-bottom of the icon-button.
See the fiddle below:
http://codepen.io/anon/pen/wdvVBX
HTML
<div class="step-container">
<h2 class="step-title">
<div class="round-icon">
3
</div>
<span class="title">Migration</span>
</h2>
<div class="icon-button">
<i class="fa fa-print"></i>
</div>
</div>
CSS
* { margin: 0; padding: 0;}
.step {
}
.step-container {
display: block;
box-sizing: border-box;
border-bottom: 1px solid #ddd;
}
.step-title {
display: inline-block;
padding: 2rem 0 2rem 2rem;
}
.round-icon {
display: inline-block;
background-color: black;
color: white;
width: 2rem;
height: 2reml;
line-height: 2rem;
font-size: 1rem;
border-radius: 50%;
text-align: center;
}
.title {
margin-left: 1rem;
display: inline-block;
vertical-align: middle;
}
.icon-button {
float: right;
padding: 2rem;
border-left: 1px solid #ddd;
font-size: 1.5rem;
height: 100%;
}
.icon-button:hover {
border-bottom: 4px solid black;
}
You can use an ::after pseudo element instead of border.
http://codepen.io/meecrobe/pen/pPoMZo
I resolved it like this: display flex on step-container and margin-left: auto for the icon-button with the height:100% and float:right removed
I'm trying to enlarge my list-item borders when hovered, but when I do, it shifts the other icons down.
My thought was to use position: absolute on the parent and position: relative on the li or image, but the other list-items/images are still being affected.
jsFiddle: http://jsfiddle.net/2e07Lv9y/2/
HTML:
<div class="container">
<ul class="social">
<li><img src="http://placehold.it/36x36"/></li>
<li><img src="http://placehold.it/36x36"/></li>
<li><img src="http://placehold.it/36x36"/></li>
<li><img src="http://placehold.it/36x36"/></li>
<li><img src="http://placehold.it/36x36"/></li>
</ul>
</div>
CSS:
.container {
background-color: rgb(54, 129, 245);
height: 75px;
}
.social {
width: 50%;
margin: 3%;
position: absolute;
right: 0;
}
li {
list-style: none;
display: inline-block;
margin: 0 1%;
}
img {
border: 2px white solid;
}
li:hover {
cursor: pointer;
border: 5px white solid;
position: relative;
}
What am I missing?
You could use outline instead of border. Outlinke behaves similar to border, but it is not part of the elements dimensions.
see: http://www.w3schools.com/css/css_outline.asp
li:hover {
cursor: pointer;
outline: 5px white solid;
position: relative;
}
Fiddle: http://jsfiddle.net/2e07Lv9y/7/
You are adding a border 5px wide when you hover.
You need to add a border 5px wide when not hover so that they are only changing the background colour. Something along the lines of:
li {
list-style: none;
display: inline-block;
margin: 0 1%;
border:5px solid rgb(54, 129, 245);
}
Updated DEMO
You may need to adjust the height, position, of the li
Problem is that on :hover li that display inline block, have more width including border. if you won't shifts down you need change .social width to more than 50% or place elements via text-align: right.
Add
padding: 5px;
to the normal li
and
padding: 0px;
to the li:hover
This is css for an unordered-list with the id "leftmenu"
#leftmenu ul li{
list-style:none;
padding:15px 0 8px 0;
border-bottom:1px dashed white;
float:left;
clear:both;
The problem is the border only goes as far as the text go. see:http://imgur.com/dhx2OKk
I want it to be like that border under "Links"
The problem is that your list-items should be displayed as regular block items. These would always scale to the full width of any container. For a <li> element that is actually the default behavior.
By setting float: left; to the <li> items, alter this behavior. The following code would achieve what you are after (also check the JS fiddle)
* {
margin: 0;
padding: 0;
}
h4 {
border-bottom: 1px solid black;
}
.menu {
width: 200px;
}
.menu>ul {
list-style-type: none;
}
.menu>ul>li {
margin-top:10px;
border-bottom: 1px dotted black;
}
http://jsfiddle.net/fhckxene/
edit: for fun play round with the jsfiddle, for example by adding float: left; or display: inline-block; to the <li> style.
I have this situation in which I'm using
height: 100%
on a parent, and in the parent I have this header which is 34px and a container which is 100% again.
For some reason the container (ordered list) is bigger than the parent
Here is a jsfiddle
And here is the css
* {
height: 100%;
width: 100%;
margin: 0;
box-sizing: border-box;
}
section {
padding: 10px 20px 20px 10px;
border: 2px solid black;
}
header {
height: 30px;
background-color: blue;
}
ol {
list-style-type: none;
border: 1px dashed #d2d4d8;
box-sizing: border-box;
background-color: yellow;
padding: 0;
}
li {
display: inline-block;
box-sizing: border-box;
background-color: green;
width: 30%;
border: 1px solid blue;
font-size: 0;
}
Any suggestions why the ordered list is outside the parent section element ?
It's setting the height of the ol to 100% of the parent height, not 100% of the parent minus the 30px for the header. I've gotten frustrated at this before, because in my head I want 100% to mean "Fill to the parent" but it means literally 100%. If you can do css3 stuff, you could change your css to this:
ol { height: calc(100% - 30px); }
You could also do some positioning stuff, but that always gets gross. Here is an untested idea of it:
section { position: relative; }
ol { position: absolute; top: 30px; bottom: 0; }
It doesn't help that your mixing percentages and fixed sizes with your padding. When you do that use box-sizing:border-box; so that the percentage based width and height will take into account the padding and margins and not just add them on the end.