Center label of any size under a button in CSS - html

I've got a button with a label absolutely positioned underneath.
I want the label to not wrap to a new line but to be rendered all on one line. I know i can do that with width: max-content or with overflow: visible.
But at the same time i want the label to be centered with the button, which is what i can't find a way to do unless i wrap the label to a new line.
Any way i can achieve this?
button {
position: relative;
width: 40px;
height: 40px;
background: #E30202;
color: black;
border-radius: 14px;
}
span {
position: absolute;
top: calc(45px);
left: 0;
font-size: 12px;
color: black;
width: 100%;
pointer-events: none;
/* width: max-content; */
}
<button>
<span>Label Line</span>
</button>

So your text is centered. There is just not enough width in the container for the text to fall on only one line. You can use the psuedo-element :after to add the centered text. But you'll notice the example 1 in the snippet still doesn't seem centered. The width of the button is doubled on the second example with the same styles showing the centered text.
Another solution would be to add a .wrapper and use display: flex; with flex-flow: column; and justify-content: center; to make sure the text is always centered with the button.
button {
position: relative;
width: 40px;
height: 40px;
background: #E30202;
color: black;
border-radius: 14px;
margin: auto;
}
button.pseudo:after {
content: "Label Line";
position: relative;
top: calc(100% + 3px);
}
.btn-wrapper:nth-child(2)>button {
width: 80px;
}
.btn-wrapper {
display: flex;
flex-flow: column;
justify-content: center;
align-items: center;
margin-top: 3em;
}
<!-- example 1 -->
<div class="btn-wrapper">
<button class="pseudo">
</button>
</div>
<!-- example 2 -->
<div class="btn-wrapper">
<button class="pseudo">
</button>
</div>
<!-- example 3 -->
<div class="btn-wrapper">
<button>btn</button>
<span>Label Line</span>
</div>

Related

CSS position icon at top right of div, goes behind div

I got a quick question. I want an icon at the top right of the div, and the bottom right. I think I am doing it well, but for some reason it goes behind the div.
Image of what happens, you can see the green and red icon not going above it, but behind
This is the code I wrote:
HTML:
<div v-if="showMenu" class="wrapper">
<!-- Begin karakters onderaan -->
<div v-if="karakters" class="karakters-wrapper">
<span v-for="karakter in karakters" :key="karakter">
<div v-if="karakter !== undefined" class="karakter-box">
<p>{{karakter.charinfo.firstname}} {{karakter.charinfo.lastname}}</p>
<i class="fas fa-play-circle icoon-speel"></i>
<i class="fas fa-trash icoon-verwijder"></i>
</div>
<div v-else class="karakter-box">
<p>Leeg karakter slot</p>
</div>
</span>
</div>
<div v-else>
<h1 style="color:#fff;font-size:48px;">GEDULD!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</h1>
</div>
<!-- Einde karakters onderaan -->
</div>
^^ Don't mind the v-if etc. that's from VueJS. Anyways, the div with class "karakter-box" is the div where I want an icon at the top right of.
CSS:
width: 100%;
height: 100%;
display: flex;
justify-content: flex-end;
align-items: center;
flex-direction: column;
}
.karakters-wrapper {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
padding: 10px;
}
/* Karakter boxjes */
.karakter-box {
position: relative;
display: flex;
justify-content: center;
align-items: center;
width: 300px;
height: 100px;
background-color: rgba(0, 0, 0, 0.7);
border-top: 8px solid var(--white);
padding: 5px;
margin: 5px;
color: var(--white);
transition: .25s ease-in-out;
}
.karakter-box:hover {
border-top: 8px solid var(--groen);
background-color: rgba(0, 0, 0, 0.9);
}
.icoon-speel {
position: absolute;
top: -10px;
right: -10px;
font-size: 2rem;
background-color: var(--groen);
color: var(--white);
padding: 10px;
border-radius: 100%;
}
.icoon-verwijder {
position: absolute;
bottom: -10px;
right: -10px;
font-size: 2rem;
background-color: var(--rood);
color: var(--white);
padding: 10px;
border-radius: 100%;
}```
I'm not sure why but it looks like there is an overflow: hidden karakter-box class.
even though i don't see it in the code sample you provided.
When i tested the code the sample this issue did not occur
Try Checking for css overflow on karakter-box
I suggest use another for the background and set its width to device width.
use this css code to get positions to right corners
Position: absolute;
and use this at css to get front of div
z-index:1;
index 1 is go front of styles and 2 3 4 going backward as ordered.
set the z-index of div to be -1
div{
z-index: -1;
}
and that of icons to be 1
i{
z-index: 1;
}
i will make the div behind your icons, so you can interact with your icons.

Any way for leader dots to extend from inside the text?

I think it's best to explain with the image above, is there a way to make the dots extend all the way to the volume? Whatever I try the text on the left is inside a block with a set width so the dots are contained inside of it and I can't use a white background to only cover the text area.
Codepen
.two-line-menu-item {
width: 100%;
text-align: left;
}
.two-line-menu-item__title {
color: #4ca099;
}
.two-line-menu-item-info {
display: flex;
justify-content: space-between;
align-items: flex-end;
}
.two-line-menu-item-info__type-vol {
max-width: 70%;
padding-right: 0.33rem;
background-color: white;
}
.two-line-menu-item-info__price {
padding-left: 0.33rem;
background-color: white;
}
.dots {
position: relative;
z-index: -1;
top: -9px;
height: 3px;
border-top: 3px dotted;
}
<div class="two-line-menu-item">
<div class="title-text two-line-menu-item__title">Cappy Juices</div>
<div class="two-line-menu-item-info">
<span class="two-line-menu-item-info__type-vol">orange, apple, apricot, peach, black currant, smoothie 0.2 l</span>
<span class="two-line-menu-item-info__price">16 kn</span>
</div>
<div class="dots"></div>
</div>

Styling Inline Troubles

I am trying to style - on one line - a group of buttons on the left, a navigation list in the middle, and a group of buttons on the right. I have grouped the navigation buttons for when the layout should change. Question is how to get the navigation structure on the same line in the center of the parent element?
I have made both overarching div elements into "display: inline" elements, and also the navigation.
Do I need to use absolute positioning of the nav for this to work? Why?
How can I center the nav horizontally?
My HTML:
// you need to have font smoothing on to avoid blurred and jagged lines
html{
background-color: rgb(235, 235, 235);
}
#main_body{
width: 1200px;
margin: auto;
background-color: white;
}
// first line header styling
#header_topnav{
width: 96%;
height: fit-content;
}
// button div, navigation and button div
.topnav_button{
width: 45px;
height: 35px;
margin-top: 18px;
margin-bottom: 10px;
}
// make div inline element to get it on one line
#left_buttons{
display: inline;
position: relative;
}
// make div inline element to get it on one line
// no other way than to go with left or margin
#right_buttons{
display: inline;
position: relative;
margin-left: 87.5%;
// left: 86.5%;
}
// vertical centering
ul {
margin: auto;
}
// for horizontal nav menu
li {
display: inline;
float: left;
margin-left: 10px;
}
// display block so clickable area is larger
li a {
display: block;
}
// how do you center this horizontally?
// do I have to make position absolute? can't I keep it in between the button sections?
#top_navmenu {
position: absolute;
display: inline;
margin-top: 28px;
}
<div id="main_body">
<header>
<section id="header_topnav">
<div id="left_buttons">
<button class="topnav_button"></button>
<button class="topnav_button"></button>
</div>
<nav id="top_navmenu">
<ul>
<li><a>F+</a></li>
<li><a>POLITIK</a></li>
</ul>
</nav>
<div id="right_buttons">
<button class="topnav_button"></button>
</div>
</section>
</header>
</div>
You don't need to use position: absolute. You can achieve this with flexbox.
Just add display:flex on the wrapper ( #header_topnav ) and flex-grow: 1 on the middle menu. This way the menu will occupy all the available space between the left and right buttons.
Also when sharing code here on SO please be sure you share valid code.
HTML code is missing some closing tags
// is not a valid comment in CSS. Use /* comment */ instead.
See below
html {
background-color: rgb(235, 235, 235);
}
#main_body {
width: 100%;
margin: auto;
background-color: white;
}
#header_topnav {
height: fit-content;
display: flex;
flex-wrap: wrap;
flex-direction: row;
}
.topnav_button {
width: 45px;
height: 35px;
margin-top: 18px;
margin-bottom: 10px;
}
#left_buttons {
display: inline;
position: relative;
}
#right_buttons {
position: relative;
}
ul {
margin: auto;
display: flex;
flex-direction: row;
}
li {
margin-left: 10px;
}
li a {
display: block;
}
#top_navmenu {
margin-top: 28px;
flex-grow: 1;
display: flex;
align-items: center;
}
<div id="main_body">
<header>
<section id="header_topnav">
<div id="left_buttons">
<button class="topnav_button"></button>
<button class="topnav_button"></button>
</div>
<nav id="top_navmenu">
<ul>
<li><a>F+</a></li>
<li><a>POLITIK</a></li>
</ul>
</nav>
<div id="right_buttons">
<button class="topnav_button"></button>
</div>
</section>
</header>
</div>

How to center text in a div, which contains an Icon

I got the following question, with a pic to illustrate:
I got a container, containing an icon and text. The Icon is always on the left, there can be more icons which are all fixed on the left. Now I want the text to be in the centre of the container. If the text gets too long, it should NOT overlap the icons, but it should expand to the right instead.
If the text is too long to fit in the container next to the icon, finally I just ellipse it.
But how do I do this? Is there a css solution?
Thanks in advance!
You can have the text taking the entire width of the container parent, but having left and right padding in a way that it will have enough space for the icon.
Then you can have the icon overlaying on top of the textbox.
The text box can align text in the center and clip text if overflow.
I made a simple pen to illustrate the idea. I hope this help
Codepen example
Here's the code.
HTML
<div class="container">
<i class="fa fa-map"></i>
<div class="textbox"><span class='centerized-text'>This is a centered text and it is very long</span></div>
</div>
CSS
.container {
background-color: #FAFBFD;
width: 15rem;
height: auto;
padding: 0.5rem;
border-radius: 4px;
}
i {
color: teal;
position: absolute;
}
.textbox {
padding: 0 1.3rem;
text-align: center;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
body {
padding: 2rem;
background-color: #dedede;
}
(The style for body is just for your visual pleasure)
Here a code
#container {
display: flex;
flex-direction: row;
padding: 20px;
border: 2px solid skyblue;
}
#icon {
width: 24px;
height: 24px;
border-radius: 12px;
background-color: skyblue;
}
#text {
flex: 1;
text-align: center
}
<div id="container">
<span id="icon"></span>
<span id="text">Lorem Ipsum</span>
</div>
To have text centered in the block
#container {
position: relative;
padding: 20px;
border: 2px solid skyblue;
text-align: center;
}
#icon {
width: 24px;
height: 24px;
border-radius: 12px;
background-color: skyblue;
position: absolute;
left: 20px;
top: 0;
bottom: 0;
margin: auto;
}
<div id="container">
<span id="icon"></span>
<span id="text">Lorem Ipsum</span>
</div>
In a container use, two span tag and First span will contain your image tab and another has text in it.
You can use align-text-center to center your text.

Show text on hovering a div under a flex-container

I have to create an icon navigation bar with webfont-icons on the top of the page, that is tiled 3 Sections:
upper-left: Icons are aligning on the left side
middle-center: Icons are aligning in the middle of the site
upper right: Icons are aligning on the right side
This part i got to work. With the following HTML ...
<div id="topNavigation">
<div id="topNavigationLeft">
<div class="iconButton makeAnIcon" data-icon="🙈"><span class="tooltiptext">The funky monkey</span></div>
<div class="iconButton makeAnIcon" data-icon="😎"><span class="tooltiptext">The cool smiley</span></div>
</div>
<div id="topNavigationMiddle">
<div class="iconButton makeAnIcon" data-icon="🚹"><span class="tooltiptext">Man rest room</span></div>
<div class="iconButton makeAnIcon" data-icon="🚺"><span class="tooltiptext">Woman rest room</span></div>
<div class="iconButton makeAnIcon" data-icon="🚾"><span class="tooltiptext">Water Closet</span></div>
</div>
<div id="topNavigationRight">
<div class="iconButton makeAnIcon" data-icon="🚻"><span class="tooltiptext">Mixed up rest room</span></div>
<div class="iconButton makeAnIcon" data-icon="🚼"><span class="tooltiptext">Baby room</span></div>
<div class="iconButton makeAnIcon" data-icon="🚽"><span class="tooltiptext">Toilet</span></div>
</div>
</div>
This is the CSS i had used:
.iconButton {
float: left;
margin-left: 10px;
margin-right: 10px;
}
.iconButton:hover {
color: #ff0000;
}
.iconButton .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
/* Position the tooltip */
position: absolute;
z-index: 1;
}
.iconButton:hover .tooltiptext {
visibility: visible;
}
.makeAnIcon:before
{
font-family: 'Arial';
content: attr(data-icon);
font-size:60px;
}
#topNavigation {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: space-between;
align-items: center;
position: absolute;
width: 100%;
height: 80px;
top: 0px;
left: 0px;
margin: 0px;
padding: 0px;
border-bottom: 1px solid;
}
#topNavigationLeft {
display: flex;
justify-content: flex-start;
flex-wrap: nowrap;
height: 100%;
/* Debug Color */
/* background-color: rgba(255, 0, 0, 0.2); */
}
#topNavigationMiddle {
display: flex;
justify-content: center;
flex-wrap: nowrap;
height: 100%;
/* Debug Color */
/* background-color: #711e82; */
}
#topNavigationRight {
display: flex;
justify-content: flex-end;
flex-wrap: nowrap;
height: 100%;
/* Debug Color */
/* background-color: rgba(255, 0, 0, 0.2); */
}
See my fiddle: https://jsfiddle.net/schludi/yrgaf6p9/
Now i have to show a text UNDER the Icons on hover.
My problem is, that it is under the flex-container i have used and it should not affect further elements that will be added under the "topnavigation"-div.
When i am on the upper right side, the Text should appear left-aligned to the icon, that no scroll bars will be generated because the span element is too big. How can i do this?
First off, for ToolTips i would highly recommend using a plugin. You'll run into issues where the tooltip goes off the screen (either x or y) and you can't detect that at all with CSS.
However, lets answer your question.
So if you've got a div that's appearing underneath another element, there's one nice css property that will solve this for you! I see you've already used it. What you can do is add z-index to your element that you want on top. The higher the index, the higher the element will be visually.
.iconButton .tooltiptext {
display:none;
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
/* Position the tooltip */
position: absolute;
z-index: 9999; /* This is extreme, don't always default to 9999 */
}
Just make sure the z-index of the element you want on top is higher than the other elements. If it's still not on top, then the chances are the parent is lower than the other element that you want on top, so on your flex-container make sure that the z-index is lower than the parent of .tooltiptext
For your specific case, the following would keep the last tooltip within the bounds of the page. It's not very dynamic, though.
#topNavigationRight .iconButton:last-of-type .tooltiptext {
right: 0;
}