Hide some elements when text starts to overflow - html

I'm making a button that has text content and an icon, I'd like to make it so that when it starts to put content onto multiple lines it hides the icon.
Here's a code example with three cases: one where the content fits just fine and the icon looks good, one where the content can't fit and the icon is pretty lame, and a last one that's more like how I would like it to work with the icon being hidden.
<style>
.some-flex-box {
display: flex;
gap: 10px;
justify-content: space-between;
width: 300px;
}
.fun-icon {
height: 16px;
width: 16px;
}
.reactive-button img {
height: 16px;
}
</style>
<div class="some-flex-box">
<div>First box fits content</div>
<button class="reactive-button">
<img src="https://upload.wikimedia.org/wikipedia/en/thumb/e/e0/WPVG_icon_2016.svg/1024px-WPVG_icon_2016.svg.png" />
awesome
</button>
</div>
<br>
<div class="some-flex-box">
<div>Second box has lots of content and puts everything on multiple lines</div>
<button class="reactive-button">
<img src="https://upload.wikimedia.org/wikipedia/en/thumb/e/e0/WPVG_icon_2016.svg/1024px-WPVG_icon_2016.svg.png" />
uh oh
</button>
</div>
<br>
<div class="some-flex-box">
<div>If the second example hid the icon (like this) that would be super sweet</div>
<button class="reactive-button">
this works
</button>
</div>

Related

Bootstrap 4 - How to float-right an element until it moves to its own line?

I am new to Bootstrap, and have been able to find only one other person who has asked this question. They didn't get a valid answer, so I'm asking it again.
I have two button groups. On larger screen sizes, they should appear on the same line, one left aligned and one right aligned. When the screen becomes small enough, the right aligned element drops down to the next line. This is currently working as intended. What isn't currently functional is I want the right aligned element to become left aligned when this shift takes place. The issue I (and the above post) are having is that one of the elements is dynamically sized, so simply using Bootstrap breakpoints isn't an option. The shift needs to be dynamically based on the size available to the right aligned button group, not the absolute size of the window in question.
My current HTML is below.
<div class = "btn-group grouped-buttons btn-group-toggle" data-toggle="buttons" style = "margin: 25px">
<!-- Dyanimcally sized button group left aligned-->
<button *ngFor = "let type of typeList" class = "btn btn-default" type = "button" (click) = "catagoryFilter(type.name)" [ngClass] = "{'active': typeSelect == type.name}">{{type.name}}</button>
</div>
<div class = "btn-group grouped-buttons btn-group-toggle float-right" data-toggle="buttons" style = "margin-top: 25px">
<!-- Fixed size button group which should only float-right when not on its own line -->
<button class="btn btn-red" type="button" (click)="newSpeciesModal.show()"><fa-icon [icon]="addIcon"></fa-icon> New Species</button>
<button class="btn btn-blue" type="button" (click)="newSpeciesTypeModal.show()"><fa-icon [icon]="addIcon"></fa-icon> New Species Type</button>
</div>
You could put the elements into a container which has display: flex.
If you give it justify-content: space-between that will ensure the second element is to the far right when both fit on the same line.
Adding also flex-wrap: wrap ensures that if there isn't enough space the second element will move down (and to the left).
Here's a simple example:
body {
width: 100vw;
height: 100vh;
}
.container {
width: 100%;
height: 20%;
margin-bottom: 10px;
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
.container :nth-child(1) {
height: 100%;
background-color: red;
}
.container :nth-child(2) {
width: 20%;
height: 100%;
background-color: blue;
}
<div class="container">
<div style="width:30%;"></div>
<div></div>
</div>
<div class="container">
<div style="width:90%;"></div>
<div></div>
</div>

Vertically and horizontally align content whilst still being responsive

I currently have a simple one page website which displays a logo, 4 short lines of text and 2 buttons which are side by side. I'm using Bootstrap 4.
I have already tried many solutions posted on stack overflow but haven't found a fix.
I am trying to align all of the content horizontally and vertically whilst still being responsive so it is in the middle of the screen on all devices. Would be good to have no scroll however i'm guessing scroll may be required especially on smaller devices such as iPhone SE so that's ok.
The closest I have got is with the code below. This centers correctly however for example, on iPhone SE the logo and buttons are cut off at the top and bottom of the page rather than resizing like responsive should do.
css:
html, body {
height: 100%;
font-family: Arial, sans-serif !important;
overflow:auto;
}
body {
margin: 0;
}
.container {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100vh;
}
html:
<div class="container">
<div style="padding:15px;" class="row">
<div class="col text-center">
<center>
<img src="assets/img/logo.png" class="logo" />
<br>
<br>
<p style="margin-top:1rem;" class="big">text</p>
<p>text<br>text<br>text</p><p>text<br>text<br>text</p>
<p class="no-margin">PURCHASE TICKET INSTANTLY ONLINE</p>
<p class="big">OR</p>
<p>RESERVE AND PAY ON ENTRY</p>
<br>
<div class="row">
<div class="col-sm-12 text-center">
<a href="purchase">
<button style="background-color: #db0e0e !important;border:none; line-height: 130%;" class="btn btn-danger btn-md center-block">PURCHASE
<br>TICKET ONLINE</button>
</a>
<a href="purchase"><button style="background-color: #ffffff !important;color: #db0e0e !important;border:none; line-height: 130%;" class="btn btn-danger btn-md center-block">RESERVE
<br>PAY ON ENTRY</button></a>
</div>
</div>
</center>
</div>
</div>
</div>
Flex. See my jsfiddle.
The key is to center both axes with nested flex <div>'s one with a flex-direction of column and one of row, both with justify-content: center. The key "gotcha" with flex is that at least one of the first flex div has to have height: 100%, since flex elements' default height are determined by their contents, not their parent height.

How to make two words come in single line

<div>Mandatory Documents</div>
<div>
<button type="text" (click)="delete()" pButton icon="pi pi-times" label="Delete">
</button>
</div>
I have two div tags. The word Mandatory Documents is coming in two lines. I need the whole word to come in only one line and the Delete button beside to it
What CSS should I apply?
you just need to use "display: inline-block" by default div elements are block. or you could also span i guess which are inline by default. I dont know why you say don't break the two words? anyway you could add between the words so it wouldnt break
<div style="display:inline-block">Mandatory Documents</div>
<div style="display:inline-block">
<button type="text" (click)="delete()" pButton icon="pi pi-times" label="Delete">test</button>
</div>
I just don't understand why it would split up the words? can you provide a screen shot if it doesnt work just to show the full layout?
Use a non-breakable space between your words:
To keep the two div on the same line, use display: flex on their parent with flex-direction: row. Here is an example with a parent container which is too small yet the children are correctly placed on the same row.
.group {
display: flex;
flex-direction: row;
border: 1px solid black;
max-width: 100px;
}
.group > div {
border: 1px solid green;
margin: 2px;
}
<div class="group">
<div>Mandatory Documents</div>
<div><button>Button</button></div>
</div>
<br>
<div class="group">
<div>Mandatory Documents</div>
<div><button>Button</button></div>
</div>

Displaying a div on top of another without using z-index

I've created custom drop down menus that have one <div> holding the current value and acting as a <select> field and another below it that gets displayed when the top one is clicked, holding all the <option> values.
The design requires the bottom <div> to position slightly below the upper one (weird rounded corners, etc) so I used z-index to achieve that. It all works until the moment I have two drop down menus close to each other. If you click the upper one the drop down option list is displayed but it goes below the second drop down menu as well which is highly undesirable. Here's simplified version in a jsfiddle:
jsFiddle
As you can see, the first drop down menu is fine but the second one is hidden below the third one. Any ideas how can I achieve this so the second menu works as well? May be without using z-index somehow?
First, wrap each menu in an element. I used a div.container.
Since only one menu is going to be open at a time, simply changing the z-index of the .major and .minor elements in each container (on hover in my example) works correctly:
.major {
position: relative;
width: 100px;
background-color: red;
z-index: 1;
}
.minor {
position: absolute;
width: 150px;
background-color: blue;
padding-top: 10px;
margin-top: -10px;
z-index: 0;
display: none;
}
.container:hover .major {
z-index: 5;
}
.container:hover .minor {
display: block;
z-index: 4;
}
.moveup {
margin-top: -25px;
}
<div class="container">
<div class="major">
First one fine
</div>
<div class="minor">
Option part goes here Option part goes here Option part goes here
</div>
</div>
<br />
<br />
<br />
<br />
<div class="container">
<div class="major">
Second menu
</div>
<div class="minor">
Option part goes here Option part goes here Option part goes here
</div>
</div>
<div class="container">
<div class="major">
Third menu
</div>
<div class="minor">
Option part goes here Option part goes here Option part goes here
</div>
</div>
I'm not 100% sure what you mean, but if I'm not wrong, the solution should be to wrap the .major and .minor divs so they don't overlap each other, like so:
<div class="dropdown">
<div class="major">
First one fine
</div>
<div class="minor">
Option part goes here
Option part goes here
Option part goes here
</div>
</div>
See my fork of your JSFiddle: https://jsfiddle.net/jk1dn4yx/

Text top align on CSS

I have created an HTML framework to display a top banner containing an image a centered title and a clock, below it, there is a stage area to display some graphs and images.
The problem I am encountering is that when I increase the font size of the title in the banner, I am left with some white space above, and with increasing the font size, the text disappears (I have set the containing div to hide the overflow). Ideally, this spacing could be reduced with some property.
I tried searching for aligning the text to the top, but unfortunately I just cannot find it! The behaviour is present even when the text and its div are the only things in the document.
Link to the jsFiddle with the whole page here
Link to the isolated div here
Is there a property I am missing, or another trick to move the text upwards?
<div id="banner" style="width:100vw;height:10vh;overflow:hidden">
<div id="bannerLeft" style="width:15vw;height:100%;overflow:hidden;float:left;background-color:red">
img
</div>
<div id="bannerMiddle" style="width:70vw;height:100%;overflow:hidden;float:left;background-color:blue">
<p style="text-align:center;font-size: 2.5em;">Staged Title</p>
</div>
<div id="bannerRight" style="width:15vw;height:100%;overflow:hidden;float:left;background-color:green">
<p id="timeNow" style="font-size: 2.5em;text-align:center">
10:00
</p>
</div>
</div>
<div id="stage" style="width:100vw;height:85vh;overflow: hidden;background-color:yellow">
Stage is managed with JS
</div>
The problem is the <p> tag inside the div. You realy don't need it here. Remove it, and add the styling to the div element:
Staged Title
Demo:
.bannerMiddle {
width: 70vw;
height: 10vh;
overflow: hidden;
float: left;
background-color: blue;
text-align: center;
font-size: 2em;
}
<div class="bannerMiddle">
Staged Title
</div>
<br style="clear: left"><br>
<div class="bannerMiddle">
<p>Staged Title (BAD)</p>
</div>