CSS bar with controls to the right - css

Here's a nice jsFiddle illustrating the problem
I'm trying to create a titlebar element which...
is only ever 1 line tall
has controls in the top right which are always present
has a title in the top left, which gets truncated with "..." if it's too long
Does anyone know how I'd go about fixing this? I'm a bit stumped.
.item {
border: 1px solid grey;
display: inline-block;
margin-right: 1em;
width: 25%;
}
.titlebar {
background: red;
overflow: hidden;
padding: 0.5em;
text-overflow: ellipsis;
}
.title {
overflow: hidden;
white-space: nowrap;
}
i {
font-style: normal;
float: right;
}
.content {
min-height: 15em;
}
<div class="item">
<div class="titlebar">
<span class="title">My title</span>
<span class="controls">
<i>-</i><i>+</i><i>X</i>
</span>
</div>
<div class="content">
This is the most simple use-case - a nice short title, everything works tickety-boo.
</div>
</div>
<div class="item">
<div class="titlebar">
<span class="title">My title which is just right...</span>
<span class="controls">
<i>-</i><i>+</i><i>X</i>
</span>
</div>
<div class="content">
This is how it should look if the title is too long (obviously this title is just the right length, but pretend there are other words after it).
</div>
</div>
<div class="item">
<div class="titlebar">
<span class="title">My title which is really far too long for a sensible title to be</span>
<span class="controls">
<i>-</i><i>+</i><i>X</i>
</span>
</div>
<div class="content">
This is the problem: the title is way longer than can fit, and it pushes the buttons down on to a second line.
</div>
</div>

For a solution without fixed sizes change the order of the .title and .controls in the html. This makes sure that when you set float: right on the .controls that the controls are rendered first and will occur on the first line. After this you need to have overflow: hidden, text-overflow: ellipsis, white-space: nowrap and display: block on the .title
I've updated your example to reflect this.
.item {
border: 1px solid grey;
display: inline-block;
margin-right: 1em;
width: 25%;
}
.titlebar {
background: red;
overflow: hidden;
padding: 0.5em;
}
.title {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
display: block;
}
i {
font-style: normal;
float: right;
}
.content {
min-height: 15em;
}
.controls {
float: right;
}
<div class="item">
<div class="titlebar">
<span class="controls">
<i>-</i><i>+</i><i>X</i>
</span>
<span class="title">My title</span>
</div>
<div class="content">
This is the most simple use-case - a nice short title, everything works tickety-boo.
</div>
</div>
<div class="item">
<div class="titlebar">
<span class="controls">
<i>-</i><i>+</i><i>X</i>
</span>
<span class="title">My title which is just right...</span>
</div>
<div class="content">
This is how it should look if the title is too long (obviously this title is just the right length, but pretend there are other words after it).
</div>
</div>
<div class="item">
<div class="titlebar">
<span class="controls">
<i>-</i><i>+</i><i>X</i>
</span>
<span class="title">My title which is really far too long for a sensible title to be</span>
</div>
<div class="content">
This is the problem: the title is way longer than can fit, and it pushes the buttons down on to a second line.
</div>
</div>

Here's your answer.
I floated the objects and gave them a width of 70% and 30%.
enter link description here
Hope this helps you, and is what you're looking for. if not, add a comment and i'll try to help you.

Related

Push the right aligned element down when the first left element is too long

I'm trying to achieve this, where brown gets pushed down when pink is too long, and pink is dynamically sized based on its text content.
I was only able to achieve this using javascript and two templates, if pink length is over X use second template, but I'm wondering if there's a way to achieve this using CSS only?
I tried meddling with grid auto-fill/auto-fit with minmax, float, flex with wrap, but I couldn't get to the desired result with these.
.parent {
width: 170px;
}
.flex {
display: flex;
}
div {
outline: 2px solid rgba(255, 0,0, 0.3);
}
<div>
<p>scenario A</p>
<div class="parent flex">
<div>
<div class="one">A short title</div>
<div class="three">Some text here</div>
<div class="three">some more text</div>
</div>
<div>
<button>A button</button>
</div>
</div>
</div>
<div>
<p>scenario B</p>
<div class="parent">
<div class="one">Testing a very long title</div>
<div class="flex">
<div>
<div class="three">Some text here</div>
<div class="three">some more text</div>
</div>
<div>
<button>A button</button>
</div>
</div>
</div>
</div>
<p>can a component achieve both a and b with only css?
I found a solution, it requires having fixed height in the first row, and the right side button will basically overflow when needed.
Will wait a bit before accepting my own solution in case someone came with a better one.
.container {
width: 300px;
display: flex;
gap: 10px;
padding: 16px;
font-size: 14px;
font-family: Arial;
}
.text {
flex: 1;
min-width: 0;
}
.first-row {
height: 22px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
}
.image {
width: 40px;
height: 40px;
border-radius: 50%;
background-color: #444;
}
.title {
flex-grow: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
<h2>Long title</h2>
<div class="container">
<div class="image"></div>
<div class="text">
<div class="first-row">
<div class="title">Test title test title test title</div>
<button>Visit store</button>
</div>
<div class="three">in the stores</div>
<div class="three">sponsored</div>
</div>
</div>
<h2>Short title</h2>
<div class="container">
<div class="image"></div>
<div class="text">
<div class="first-row">
<div class="title">Short title</div>
<button>Visit place</button>
</div>
<div class="three">in the stores</div>
<div class="three">sponsored</div>
</div>
</div>
<h2>Very long title</h2>
<div class="container">
<div class="image"></div>
<div class="text">
<div class="first-row">
<div class="title">Test title test titleTest title test titleTest title test title</div>
<button>Visit place</button>
</div>
<div class="three">in the stores</div>
<div class="three">sponsored</div>
</div>
</div>

margin-bottom on my foreach does not align because of multiple lines due to the title length

Above is my for each result. but on each one the title length is different hence others have more lines. My problem is my css id has margin-bottom:50px; on all of them but it's annoyingly not all have the same height. How do I change my css so all will be the same margin regardless of the length of my title?
foreach($results_n as $res_n){
echo'<div class="col-md-4">
<img src="" class="mb-3" alt="news banner">
<p id="news-category">'.$res_f->category.'</p>
<h2 id="news-title">'.$res_n->title.'</h2>
<hr>
<div class="row mt-1">
<div class="col-md-6">
<p id="news-date" class="float-left">'.$res_n->date.'</p>
</div>
<div class="col-md-6">
<p id="read-more">READ MORE</p>
</div>
</div>
</div>'; }
echo'</div>';
my css in question is:
#news-title {
font-weight: bold;
font-size:15px;
margin-bottom: 50px;
}
Try to add max-height with over flow hidden.
#news-title {
font-weight: bold;
font-size:15px;
margin-bottom: 50px;
max-height: 50px;
overflow: hidden;
}

How do I get my text to align to top?

I've got a 4-col page and I'm inserting content. It works OK, but I can't get my first div to align to top in inline-blocks. I'm pretty newbie so I'm thinking I'm missing something. (Tried padding 0; in divs.)
Thanks. Milt.
<html>
<style>
html, body, #content { margin: 0px; height: 100%; width: 100%; padding: 0px;}
div.outer { background-color: lightgreen }
div.inner { display: inline-block; }
div.WebTopic { width: 190px;}
.col {
position: absolute;
top: 0px; bottom: 0px;
margin: left: 10px;
border: 1px solid black;
background-color: seashell;
}
.col-1 { left: 0px; width: 400px; top: 0px;}
.col-2 { left: 401px; width: 200px; top 0px; }
.col-3 { left: 601px; width: 400px; }
.col-4 { left: 1001px; width: 50px; }
</style>
<body>
<div id="content">
<div class="col col-1">
<div class="outer">
<div class="inner">
<div class="WebTopic">
<p>??? Why is this at the foot of the frame? Would like it at top.</p>
</div>
<p class="fanRating">HTML Fan rated #1</p>
</div>
<div class="inner">
<div class="WebTopic">
<p>CSS is used for describing the presentation of web pages. The CSS tutorial section will help you learn the essentials of CSS, so that you can fine control the style and layout of your HTML document.</p>
</div>
<p class="fanRating">CCS Fan Rated #3</p>
</div>
</div> <!-- OUTER 1 END -->
<p>Stuff here in usual block mode</p>
<!-- OUTER 2 START -->
<div class="outer">
<div class="inner">
<div class="WebTopic">
<p>HTML is a markup language that is used for creating web pages. </p>
</div>
<p class="fanRating">HTML Fan rated #1</p>
</div>
<div class="inner">
<div class="WebTopic">
<p>CSS is used for describing the presentation of web pages. Loads more words. Loads more words. Loads more words. Loads more words. Loads more words. Loads more words. Loads more words. </p>
</div>
<p class="fanRating">CCS Fan Rated #3</p>
</div>
</div> <!-- OUTER 2 END -->
</div>
<div class="col col-2">
COLUMN 2 -------------
</div>
<!-- COLUMN 3 START -->
<div class="col col-3">
<div class="outer">
<div class="inner">
<div class="WebTopic">
<p>??? Why is this at the foot of the frame? Would like it at top. Tons of text. Tons of text. Tons of text. Tons of text. Tons of text. </p>
</div>
<p class="fanRating">HTML Fan rated #1</p>
</div>
<div class="inner">
<div class="WebTopic">
<p>CSS is used for describing the presentation of web pages. The CSS tutorial section will help you learn the essentials of CSS, so that you can fine control the style and layout of your HTML document.</p>
</div>
<p class="fanRating">CCS Fan Rated #3</p>
</div>
</div>
</div>
<div class="col col-4">
COL 4
</div>
</body>
<html>
Since it's not explicitly declared, you will probably need to add vertical-align: top to the inline-block elements.
e.g.
div.inner {
display: inline-block;
vertical-align: top;
}
Here it is applied to your code:
Before: https://jsfiddle.net/hu6yosgq/1/
After: https://jsfiddle.net/hu6yosgq/2/

Twitter Bootstrap Banner - How to render

This is probably a pretty basic question, but I have a banner with an image on the left and text on the right. Under the banner is just a block of color. When the page gets smaller, my expectation is that the bits in the banner would stack (maintaining the background color for both) and the block of color (class="blue-line") would fall beneath them.
Here is the mark-up:
<section>
<div class="row header">
<div class="col-sm-6">
<img src="../images/logo.png" height="100px" />
</div>
<div class="col-sm-6 title">
<h2>Some Title Text</h2>
</div>
</div>
<div class="row">
<div class="col-sm-12 blue-line"></div>
</div>
</section>
and the css
.header {
background-color: #F2EBCC;
border: 10px solid #F2EBCC;
height: 120px;
}
.row > .title {
text-align: right;
top: 45%;
}
Thanks in advance!
JSFiddle: http://jsfiddle.net/3n6Kd/
try this:
<section>
<div class="row header">
<div class="col-sm-6">
<img src="../images/logo.png" height="100px" />
</div>
<div class="col-sm-6 title">
<h2>Some Title Text</h2>
</div>
</div>
<div class="row">
<div class="col-sm-12 blue-line"></div>
</div>
and:
.header {
background-color: #F2EBCC;
height: 120px;
}
.title {
text-align: right;
display: block;
padding: 10px;
}
.blue-line {
margin-top:10px;
height: 15px;
background-color: blue;
}
the text go under the first column not the blue-line, but it seems to appear above the blue-line so try it in your computer because some time jsfiddle.net don't show code correctly.
hope it will help you.

How to close gap between image and text?

I dont understand why I have a large gap between the second picture and the text to its right. I've attached a fiddle for the code. How do I close this gap?
http://jsfiddle.net/7Qchr/
.main {
-webkit-column-gap: 1em;
-webkit-column-rule: 2px;
-webkit-columns: 2;
}
#image {
max-width: 100%;
}
<div class="main">
<p id="text_l">
“ The best selection of cheese I've ever seen! Cannot wait for our next order!”
<p>
<img src="img/cheese1.jpg" alt="Picture of cheese" id="image">
</div>
<div class="main">
<img src="img/cheese2.jpg" alt="Picture of cheese" id="image">
<p id="text_r">
“ Wow,amazing cheese selection and fast delivery! I highly recommed you try!”
<p>
</div>
You'll have to rewrite your code a bit... Try something like this:
HTML
<div class="main">
<div>
<p id="text_l">blah</p>
</div>
<div>
<img src="cheese1.jpg" class="image">
</div>
</div>
<div class="main">
<div>
<img src="cheese2.jpg" class="image odd">
</div>
<div>
<p id="text_r">blah</p>
</div>
</div>
CSS
.main div{
width: 48%;
display: inline-block;
vertical-align: top;
}
.image {
max-width: 100%;
padding: 0 10px;
}
.image.odd {float: right;}
http://jsfiddle.net/7Qchr/6/
Updated Demo
The following CSS was added (none of the existing HTML or CSS was changed).
.main + .main {
text-align: right;
}
p {
text-align: left;
}

Resources