max-width not changing width of my divs - html

I've been following a css and html tutorial at udacity.
My code is this:
.image{
max-width: 50%;
}
.app{
display: flex;
}
.description{
color: red;
max-width: 705px;
}
<h1 class="title">My App</h1>
<div class="app">
<div class="image">image</div>
<div class="description">some long text</div>
</div> <!-- /.app -->
When I increase max-width of the image div, image div remains the same width no matter what.
But when I increase max-width of description, it changes accordingly.
Also when I change max-width to width it changes also the 460px and occupies more space than with max-width.
Why is this happening ?

max-width doesn't set the width of an element.
It tells your divs: this is how wide you can get (starting from 0, content width, or min-width)
Your divs have a max-width: 50% (.image) and max-width: 705px (.description).
If there were no content in either div, the width would be zero.
.app {
display: flex;
padding: 5px;
}
.image {
max-width: 50%;
border: 1px dashed black;
}
.description {
color: red;
max-width: 705px;
border: 1px dashed green;
}
<h1 class="title">My App</h1>
<div class="app">
<div class="image"></div>
<div class="description"></div>
</div>
<!-- /.app -->
If the content width were to be less than the max-width, the divs would take the content width.
.app {
display: flex;
padding: 5px;
border: 1px solid red;
}
.image {
max-width: 50%;
border: 1px dashed black;
}
.description {
color: red;
max-width: 705px;
border: 1px dashed green;
}
<h1 class="title">My App</h1>
<div class="app">
<div class="image">xxx</div>
<div class="description">xxxx</div>
</div>
<!-- /.app -->
If the content were to be more than the max-width, then max-width would come into play.
.app {
display: flex;
padding: 5px;
border: 1px solid red;
}
.image {
max-width: 10%;
border: 1px dashed black;
}
.description {
color: red;
max-width: 70px;
border: 1px dashed green;
}
<h1 class="title">My App</h1>
<div class="app">
<div class="image">image image image image image image image</div>
<div class="description">some long text some long text some long text some long text some long text</div>
</div>
<!-- /.app -->

Related

CSS 2 fluid 1 fixed column

I am trying to achieve this layout.
left column fixed size
right column fluid, it may have x number of elements inside, for example up to 4 divs 50px wide (this is done dynamically) so it must be max 200px wide, or if it has 3 such elements, then it must be 150px wide...
center column fluid, takes all the rest space
The closest I have comes is this:
#container {
overflow:hidden;
width: 100%;
}
#leftcol {
border: 1px solid #0f0;
float: left;
width: 80px;
}
#rightcol {
border: 1px solid #0f0;
float: right;
}
#centercol {
border: 1px solid #000;
margin-left: 80px;
}
.box{
width:50px;
height:20px;
background:red;
float:left;
}
<div id="container">
<div id="leftcol">
fixed 80px
</div>
<div id="rightcol">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
</div>
<div id="centercol">
fluid center
</div>
</div>
but center fluid is not correct width.
I can change some html if it will be easier to achieve desired effect.
You can do it with the Flexbox:
body {margin: 0}
#container {
display: flex; /* displays flex-items (children) inline */
overflow: hidden;
}
#leftcol {
border: 1px solid #0f0;
width: 80px;
}
#centercol {
flex: 1; /* takes the remaining horizontal space */
border: 1px solid #000;
}
#rightcol {
display: flex;
border: 1px solid #0f0;
max-width: 200px; /* adjust to your needs */
}
.box {
width: 50px;
height: 20px;
background: red;
}
<div id="container">
<div id="leftcol">
fixed 80px
</div>
<div id="centercol">
fluid center
</div>
<div id="rightcol">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
</div>
</div>

Vertically centralising content in an inline-block

I’m trying to get the blue text vertically in the centre of the inline-block divs. I’ve tried various variations but to no avail. The parent must stay as an inline block.
If the parent is displayed as a table and child as a table cell with vertical-align: middle then it almost works, but fails because then the child div is 100% high, and I want to add a border-top and bottom with about 10px padding, which won’t work at 100% height. Cannot use display: flex because it destroys the positioning of other elements. Line height also fails.
Can someone explain what the problem is here because I'm very confused.
.matches-container {
padding-top: 50px;
}
.match {
width: 25%;
height: 250px;
display: inline-block;
border: 1px solid yellow;
}
.match-contents {
background: blue;
border-top: 1px solid red;
border-bottom: 1px solid red;
}
.a {
background: black
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<section class="matches-container">
<article class="match a">
<div class="match-contents">central text with borders top and bottom</div>
</article>
<!-- -->
<article class="match b">
<div class="match-contents">central text with borders top and bottom</div>
</article>
</section>
Add this properties in .match-contents
.match-contents {
position:relative;
top:50%;
transform:translateY(-50%);
}
Your code is working as expected...no where you added a property to make blue box vertically center.
Here is snippet.
.matches-container {
padding-top: 50px;
}
.match {
width: 25%;
height: 250px;
display: inline-block;
border: 1px solid yellow;
}
.match-contents {
background: blue;
border-top: 1px solid red;
border-bottom: 1px solid red;
position: relative;
top: 50%;
transform: translateY(-50%);
}
.a {
background: black
}
<section class="matches-container">
<article class="match a">
<div class="match-contents">central text with borders top and bottom</div>
</article>
<!--
-->
<article class="match b">
<div class="match-contents">central text with borders top and bottom</div>
</article>
</section>
You can use display: inline-flex and align-items: center here. Demo:
.matches-container {
padding-top: 50px;
}
.match {
width: 25%;
height: 250px;
/* become inline flex-container */
display: inline-flex;
/* center items */
align-items: center;
border: 1px solid yellow;
}
.match-contents {
background: blue;
border-top: 1px solid red;
border-bottom: 1px solid red;
}
.a {
background: black
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<section class="matches-container">
<article class="match a">
<div class="match-contents">central text with borders top and bottom</div>
</article>
<!-- -->
<article class="match b">
<div class="match-contents">central text with borders top and bottom</div>
</article>
</section>

HTML/CSS - auto size the width of content in a wrapper

I have a content div with a variable length of content, horizontally.
I would like the width of this content div to auto-size depending on the length of it's content. For example, I would like to be able to remove the arbitrary "20%" value, and have the div size accordingly. Currently removing the 20% value from the width is forcing it to resize to 100% of it's container. What CSS am I missing?
Fiddle
.wrapper {
border: 2px solid green;
}
.content {
margin: 0 auto;
text-align: center;
border: 2px solid orange;
width: 20%;
}
<div class="wrapper">
<div class="content">
<span>One</span>
<span>Two</span>
<span>Three</span>
<span>Four</span>
</div>
</div>
Put text-align:center on the wrapper and set the display of the content div to inline or inline-block:
.wrapper {
border: 2px solid green;
text-align:center;
}
.content {
margin: 0 auto;
text-align: center;
border: 2px solid orange;
display: inline;
}
<div class="wrapper">
<div class="content">
<span>One</span>
<span>Two</span>
<span>Three</span>
<span>Four</span>
</div>
</div>
If you want fluid resizing behavior I'd suggest flexbox:
.wrapper {
border: 2px solid green;
display: flex;
justify-content: center;
}
.content {
margin: 0 auto;
text-align: center;
border: 2px solid orange;
/*width: 20%;*/
}
<div class="wrapper">
<div class="content">
<span>One</span>
<span>Two</span>
<span>Three</span>
<span>Four</span>
</div>
</div>
inline:
flexbox:

Css table with scalable div on the sides

I have a central div that have 4 others divs arrount it (top, right, left, bottom).
Top and Bottom divs are suposed to be fixed. They don't have to increase neither height nor width.
Right and left divs are ONLY suposed to increase its width.
How?
The "content" div (central) is a table that can have as many rows as the user wants. Then, I want to increase or decrease the height of the left and right divs depending on the height of the "content".
I want to automatically do that .
How can I do that?
I have created an example with what I have done, without success.
https://jsfiddle.net/y6ad2crg/4/
<div class="pantalla">
<div class=" pantallaSup"></div>
<div class="pantallaEsq"></div>
<div class="pantallaDre"></div>
<div class="interiorPantalla">
content<br>
aaa<br>
bbb<br>
ccc<br>
ddd<br>
eee
</div>
<div class="pantallaInf"></div>
</div>
The result I want to get is that:
What am I doing wrong?
Maybe just changing little things in css it may be done, or maybe my html is wrong designed...
change u code
https://jsfiddle.net/p7haf3oo/
.pantalla {
/*position: relative;*/
display: block;
width: 690px;
background-color: gray;
}
.pantallaSup {
height: 200px;
width: 100%;
background: orange;
}
.flex {
display: flex;
align-items: stretch;
}
.pantallaEsq {
width: 69.4px;
border-right: 4px solid black;
background: red;
display: block;
}
.pantallaDre {
width: 69.4px;
border-left: 4px solid black;
background: red;
display: block;
align-self: stretch;
min-height: 100px;
}
.pantallaInf {
height: 200px;
width: 100%;
background: orange;
}
.interiorPantalla {
position: relative;
margin: 0 auto;
border: 1px;
border-color: blue;
border-style: solid;
width: 550px;
background-color: white;
}
<div class="pantalla">
<div class="pantallaSup"></div>
<div class="flex">
<div class="pantallaEsq"></div>
<div class="interiorPantalla">
content
<br> aaa
<br> bbb
<br> ccc
<br> ddd
<br> eee
</div>
<div class="pantallaDre"></div>
</div>
<div class="pantallaInf"></div>
</div>
I believe I have coded what you're looking for -https://jsfiddle.net/Shuaso/vpmn3LLv/
I removed all the unnecessary divs and used CSS borders and background colors to achieve the same effect. You can alter the content in the "content" div and see the dynamic styles. Here's the code:
CSS:
.container {
width: 500px;
background-color: orange;
padding: 20px 0;
}
.content {
border-left: 20px solid red;
border-right: 20px solid green;
background-color: white;
}
HTML:
<div class="container">
<div class="content">
<p>test</p>
<p>test</p>
</div>
</div>

How to make full 100% height of floated element?

I have the following html markup:
.container {
border: 1px solid green;
}
.right {
border: 1px solid #000;
float: right;
width: 40px;
}
.left {
border: 1px solid red;
overflow: hidden;
}
<div class="container">
<div class="right">Right</div>
<div class="left-container">
<div class="left">
Left fluid
<br/>
multiple rows
</div>
</div>
</div>
As you can see right block looks ugly. How could I make right element fluid height 100%?
Add the rule height:100% the right div, and remove float:right. I changed it to position:absolute, so that you didn't need the container's height.
.container {
border: 1px solid green;
position: relative;
width: 100%;
}
.right {
border: 1px solid #000;
width: 40px;
height: 100%;
position: absolute;
right: 0;
}
.left {
display: block;
border: 1px solid red;
overflow: hidden;
margin-right:40px;
}
<br><br><div class="container">
<div class="right">Right</div>
<div class="left-container">
<div class="left">
Left fluid
multiple rows a really long sentence a really long sentence a really long sentence a really long sentence a really long sentence a really long sentence.
</div>
</div>
</div>
If your application will run in a modern browser, then using flexbox is a good way to go: http://jsfiddle.net/2hn9zgog/.
HTML:
<div class="container">
<div class="right">Right</div>
<div class="left">
Left fluid
<br/>multiple rows
</div>
</div>
CSS:
.container {
display: flex;
width: 100%;
outline: 1px dotted gray;
}
.right {
order: 2;
flex: 0 0 auto;
border: 1px solid green;
}
.left {
flex: 1 0 auto;
border: 1px solid red;
}
add clear: both; after floated element.
<div class="right"></div>
<div style="clear: both"></div>
Add
html, body{
height: 100%;
min-height: 100%;
}
.your-container{
height: 100%;
}