Place icon next to centered element on hover without centering the icon - html

I'm trying to add an icon next to a centered text on hover, wo centering both text and icon. I want the icon to be placed directly after the centered element. Picture should explain it. Preferably I would like to use flexbox. This is what i currently have: http://jsfiddle.net/TmdZ3/
HTML
<div class="container">
<span>Label</span>
<i>icon</i>
</div>
CSS
.container {
height: 150px;
width: 150px;
border: 5px solid grey;
display: flex;
align-items: center;
justify-content: center;
}
i {
display: none;
}
.container:hover > i {
display: flex;
}

You'll have to make a small change to your markup and put the icon inside the span to then position it relative to its parent:
http://jsfiddle.net/TmdZ3/1/
span {
position: relative;
}
i {
display: none;
position: absolute;
top: 0;
right: -30px;
}
Note that the right: -30px; would have to be adjusted according to your icon's size and spacing to the span.

You should create a new span, make its position to be absolute, keeping relative to its immediate parent and then he would get what he is looking for on hover.
WORKING DEMO
The Code:
<div class='wrapper'>
<span>Label</span>
</div>
.wrapper{
padding:20px;
border:4px solid grey;
display:inline-block;
color:black;
font-size:22px;
font-family:verdana;
position:relative;
width:100px;
height:30px;
}
span{
padding:0 20px;
color:black;
text-decoration:none;
position:absolute;
}
Hope this helps.

What have you tried at present? Have a look at this FIDDLE which expands on the code example below.
This should be done by setting the background of the :hover css to something like:
background-image:url(myicon.png);
background-repeat:no-repeat;
background-position:right center;
The the regular link css to something like
padding:0 15px;

Related

center element directly below and in the middle of text

The problem I am trying to solve in my project:
I want to have the selected state of a link in my navbar be a small icon.
I want that icon to sit directly below and in the middle of the navbar link text.
I can't seem to figure it out.
I mimicked some of the css in the project below.
Essentially I am trying to get the text "trying ma best" centered in the middle of the parent "center the text below me relative".
All of our nav links sit inline, so I don't want to alter the a tag being inline.
https://jsfiddle.net/6Lk1uqs8/5/
here is the code in the fiddle:
<a href="#">
center the text below me relative to me
<div>
trying ma best
</div>
</a>
a {
display: inline;
}
div {
display: inline;
position: relative;
top: 15px;
right: 50px;
}
Any help is greatly appreciated.
You can use relative and absolute position to achieve the desired results.
a {
display:inline;
position:relative;
}
div {
position: absolute;
top: 15px;
width:100%; /* instead of this you can use left:0; right:0 also */
text-align:center;
}
a {
display:inline;
position:relative;
}
div {
position: absolute;
top: 15px;
width:100%;
text-align:center;
}
<a href="#">
center the text below me relative to me
<div>
centere insdff
</div>
</a>
You can do it easily with the Flexbox:
a {
display: inline-flex; /* takes only the contents width; you can also use "flex" (100% width) */
flex-direction: column; /* stacks flex-items (children) vertically */
align-items: center; /* centers them horizontally */
}
<a href="#">
center the text below me relative to me
<div>
trying ma best
</div>
</a>
not full proof but could be a good starting point. https://jsfiddle.net/p1er4t2p/
a {
display: inline-block;
border:1px solid black;
}
a:after {
visibility:hidden;
content:'center';
display: inline-block;
position: relative;
display:flex;
justify-content:center;
border:1px solid blue;
}
a:hover::after{
visibility:visible;
}
<a href="#">
center the text below me relative to me
</a>
You could do the following css
a {
display: inline;
position: relative;
}
a div {
display: inline;
position: absolute;
top: 15px;
width:100%;
text-align:center;
}
PS:div inside anchor tag is not good method Is putting a div inside an anchor ever correct?

Div aligned in the middle of another div

I am trying to make a layout with a div which have some text in the middle, and bellow that text, 4 social media icons. So I have made a div with another div inside of it, but I can't center the last one vertically...
So, how do I center div "sec10_content" in the middle of div "section10"?
I know that this question has been asked before, but I can't manage the code in my case...
Here's the code:
HTML:
<div class="section10">
<div class="sec10_content">
</div>
</div>
CSS:
.section10
{
width:100%;
height:320px;
vertical-align: middle;
background-color:#222222;
}
.sec10_content
{
width: 50%;
height: 100px;
margin: auto;
border: 1px solid white;
}
I think that adding display inline-flex would work.
.section10
{
width:100%;
height:320px;
vertical-align: middle;
background-color:#222222;
display: inline-flex;
}
The way I like is to add these 3 lines of css on inner div
position: relative;
top: 50%;
transform: translateY(-50%);
See JSFiddle.
Source: http://zerosixthree.se/vertical-align-anything-with-just-3-lines-of-css/

How to text-align CENTER when using overflow HIDDEN?

I have this:
<div style="line-height:50px;;white-space:nowrap;overflow:hidden;width:120px;height:50px;text-align:center;">
<span style="padding:20px;">aaaaaaaaaaaaaaa</span>
</div>
I removed some code to display it clean. The all code return this:
I want to horizontally text-align it to center. Keep in mind that I am also "clipping" the text, so it is not displaying all the text on purpose.
For the effect you want to achieve, you can use a combination of absolute positioning and CSS transform. The logic is to offset the inner span to the mid-point of its containing parent (therefore left: 50%), and the move it backwards by half of its own width, therefore effectively centering it within the parent (using transform: translateX(-50%)). You might want to use vendor prefixes for the CSS transform, and be aware that it is not supported in legacy browsers.
div {
background-color: #ccc;
line-height: 50px;
white-space: nowrap;
overflow: hidden;
width: 120px;
height: 50px;
position: relative;
}
span {
display: block;
padding: 0 20px;
position: absolute;
top: 0;
left: 50%;
-webkit-transform: translateX(-50%);
}
See proof-of-concept fiddle here: http://jsfiddle.net/teddyrised/VCaEQ/
That space left of the aaaaaa's is just your padding:20px on the span.
Put that padding:20px; on the containing div, and..
Make your span another div, and then put the overflow:hidden on that inside div
Live example: http://jsfiddle.net/Hebj4/
HTML
<div id="div1">
<div id="div2">aaaaaaaaaaaaaaaaaaaaaaaaa</div>
</div>
CSS
#div1 {
width:120px;
height:50px;
padding:0px 20px 0px 20px;
line-height:50px;
text-align:center;
white-space:nowrap;
background-color: #777777;
}
#div2 {
overflow:hidden;
}

Make text stay relative within div

I'm having trouble forcing the text to stay relative within its div and at the same height as the image. So when the browser is resized, it doesn't overflow. I'm doing this as I'm creating a responsive webpage. I hope I've explained this clearly. Please check out my http://jsfiddle.net/DMnhB/1/
The html is as follows:
<div id="postd"><img
src="http://www.tntmagazine.com/media/content/_master/42628/images/barack-obama.jpg">
<span>
Text Here
Text Here
Text Here
</span>
</div>
And the CSS:
#postd{
width:100%;
padding-bottom: 5%;
background-color: blue;
padding-top:6%;
border-bottom: 1px dotted #ccc;
}
#postd img{
width:40%;
}
#postd span{
float:right;margin-left:1px;
position: absolute;
background-color: red;
}
Here is a start, try the following CSS:
#postd {
width:100%;
padding-bottom: 5%;
background-color: blue;
padding-top:6%;
border-bottom: 1px dotted #ccc;
}
#postd img {
width:40%;
display: inline-block;
vertical-align: top;
}
#postd span {
display: inline-block;
margin-left:1px;
background-color: red;
}
You can see how it looks at: http://jsfiddle.net/audetwebdesign/DMnhB/2/
I used inline-blocks to fix the overflow problem and vertical-align: top to place
the top of the image inline with the top of the text block.
You need to provide some additional feedback before I make any other adjustments.

Looking to have a <hr> and <h3> together

Like the example above. I've found some helpful script with the a small img which I do like however I don't know how to get the padding about the title so the line doesn't go straight through.
h3.line {
background-attachment: scroll;
background-clip: border-box;
background-color: transparent;
background-image: url(../images/line.jpg);
background-origin: padding-box;
background-position: center;
background-repeat: repeat-x;
background-size: auto auto;
display: block;
margin-bottom: 20px;
}
Which shows this.
Any suggestion or ideas?
You can have a 1px dot image which you can place as a background on the H3. Then have a span element in between which have a background on.
CSS:
h3 {
background: url(images/dot.png) left center repeat-x;
padding: 10px;
text-align: center;
}
h3 span { background: #fff; display: inline-block; padding: 10px 15px; }
HTML:
<h3><span>About</span></h3>
You can put a <span> for example in your <h3> and make it have the same background as your <h3> but without the line so the <span> effectively overlaps the <h3>.
You can say this to your span:
span {
display: block;
margin-left: auto;
margin-right: auto;
}
to make it center. You can add width and height to it too. line-height helps place your text to the middle vertically.
If you want to spare images than you can use text-decoration: line-through; to draw a line through your text.
Here is a solution using the CSS border property instead of an image.
the html:
<h2>
<span>This is a test</span>
<div></div>
</h2>
And here is the CSS:
h2 {
text-align:center;
background-color:#EFEFEF;
line-height:26px;
position:relative;
}
span {
background-color:#EFEFEF;
padding-right:5px;
padding-left:5px;
position:relative;
z-index:2;
}
h2 > div {
border-bottom:1px solid grey;
position:relative;
z-index:1;
top:-13px; /* half the line-height of the containing element */
}
A fiddle Demonstration
The <div> is placed inside the heading element, and positioned half-way up by settings its top position to one-half the height of the heading element, which is the headings line-height. z-index is used on the span and div so that the span gets a higher stack order than the div and obscures the (border) line where there is overlap.
I just stumbled upon another way of achieving this.
h1
{
position: relative;
padding: 0 26%;
}
h1:before,
h1:after
{
width: 25%;
background-color: rgba( 0, 0, 0, .5 );
content: '';
position: absolute;
top: 0;
bottom: 0;
}
Taken from: http://osvaldas.info/blog/background-independent-css-bars