Why does this behave the way it does with max-width: 0?
Note: It's not just max-width:0
, it's any width less than the text content's width.
The mixture of display: table-cell
, max-width
, and width:100%
make an interesting situation that the specifications don't explicitly cover. However, by making some observations and thinking, we can understand why we have the same behavior across all major browsers.
max-width:0
tries to make the element width 0, of course, meaning that everything should be overflow. However, the combination of display: table-cell
and width:100%
, as seen in the linked demo, override that command for the div
(maybe for the same reason why max-width
does not apply to table rows) for the most part. As such, the element displays itself like it would without the max-width:0
until the width of the div
is no longer large enough to contain all of the text content.
A thing to note before we continue is that the text itself by default has a width that is set by the characters inside of it. It's kind of a child element of the parent div
, though there are no tags.
This innate width of the text content is the reason why max-width:0
is needed to create the effect. Once the width of the div
is no longer large enough to contain all of the content, the max-width:0
property enables the width to become less than the width of the text content, but forces the text that can no longer fit to become overflow of the div
itself. Thus, since the div
now has text overflow and text-overflow: ellipsis
is set, the text overflow is ellipsed (if that's a word).
This is very useful to us because otherwise we couldn't make this effect without a lot of messy JavaScript. Use case demo
Note: This answer describes the behavior and gives some insight as to why this is the case. It doesn't cover how display:table-cell
overrides part of the max-width
's effect.
What does max-width: 0px do?
table {
table-layout: fixed;
}
This fixes the cell widths to the value they are given. So if you do width: 30%
on the cells, they won't resize based on their content and will maintain their set widths.
If only some of the cells are given a width
value, the remaining cells will share the space available. Use this functionality as a replacement to max-width
.
Using max-width: 0px
is more of a quirk than reliable behaviour.
DEMO
max-width changes its value automatically
with max-width, you tell the browser what is max-width of the element.
if the max-width of the element is bigger than 500px it will make the height of the element bigger.
with max-width, you set the maximum width and therefore it can be smaller but not bigger than 500px
Why does max-width and width display a completely different result?
The difference between width: 100% and max-width:100% is that: First,
width define the width of the specific element while max-width define the
maximum size the element is allow to have link
What's the purpose of @media (max-width:-1)
@media (max-width:-1) {
...standard css stuff here
}
I believe this is invalid condition so it will never get executed
max-width of 100px vs width of 100px , why max-width is shorter than width?
You can think of the width
property as being more strict than the max-width
property.
- Width says: "You should be this width and nothing else."
- Max-width says: "Be whatever width you want, just not bigger than this width."
You can see this with the demo you provided. The natural width of the boxes is around 50px, not bigger than 100px so max-width
doesn't really care. However, width
sees that the boxes aren't 100px and so sets them to be 100px.
Media Query min-width: 320px and max-width
I think you can map media queries one by one according to the length and style you want to use, so you don't get confused
check this video
@media screen and (max-width: 320px) {
.navbar{
background-color: lightskyblue;
}
}
@media screen and (min-width: 768px) {
.navbar{
background-color: salmon;
}
}
@media screen and (min-width: 320px) and (max-width: 767px) {
#logo img {
width: 80px;
height: 82px;
margin: 50px 0 0 20px;
}
.navbar {
width: auto;
font-size: xx-large;
background-color: red;
}
.navbar ul {
height: auto;
}
.navbar li {
float:none;
}
#boton_registro {
float: none;
}
.navbar li a {
display: block;
padding: 15px;
margin-left: 40px;
}
#imagen1 {
width: 250px;
height: 250px;
float: none;
margin: 250px auto 20px auto;
}
#presentacion {
width: auto;
height: auto;
float: none;
margin: 0 10px 10px 10px;
padding: 0px;
}
#titulo {
text-align: center;
}
#contenido_presentacion {
font-size: large;
text-align: center;
}
#video1 video {
width: 90%;
height: auto;
float: none;
margin: 0 0 20px 15px;
}
#contenedor_cita {
width: 90%;
height: auto;
float: none;
margin: 0 0px 20px 15px;
padding: 15px;
}
#contenedor_cita p {
font-size: xx-large;
font-weight: 600;
margin-top: 0px;
}
#contenedor_cita h4 {
text-align: right;
margin-top: 0px;
}
#video2 video {
width: 90%;
height: auto;
float: none;
margin: 0 10px 10px 15px;
}
.contenedor .tarjeta_principal .tarjetas {
display: inline;
}
.tarjeta_principal .tarjetas .tarjeta {
width: calc(100% / 1 - 10px);
}
footer {
width: 90%;
margin-left: 5%;
margin-bottom: 10%;
}
#copyright {
text-align: center;
padding-top: 50px;
float: none;
}
#copyright p {
font-size: 15px;
margin-bottom: 0px;
}
#copyright p a {
font-size: 16px;
}
#social {
float: none;
}
#botonera_social ul {
display: flex;
}
#botonera_social ul li {
float: none;
}
}
<header>
<div id="logo">
<img src="https://cdn-icons-png.flaticon.com/128/5068/5068966.png" alt="logo">
</div>
<nav class="navbar">
<ul>
<li><a href="index.html">Inicio</a></li>
<li><a href="pages/publicar.html">Publicar</a></li>
<li><a href="pages/adoptar.html">Adoptar</a></li>
<li id="boton_registro"><a href="pages/login.html">Ingresar / Registrarse</a></li>
</ul>
</nav>
</header>
<br>
<br>
<!-- Contenido -->
<section id="contenido_principal">
<div id="imagen1"></div>
<article id="presentacion">
<h1 id="titulo">Adopta una Mascota</h1>
<p id="contenido_presentacion">Es un website con fines de reubicación de mascotas que se encuentran en condición de calle. Queremos darle la posibilidad a estos nobles seres, de ser amados y cuidados por su familia de acogida,
la cual disfrutará de la gratitud y el amor incondicional del nuevo miembro de la familia.
Es de suma importancia concientizar a la sociedad sobre la tenencia responsable de mascotas,
que abarca: castración, cuidados médicos, alimentación y bienestar emocional. A continuación te compartimos un poco de incentivo: Las mascotas ayudan a disminuir el estrés, te mantienen activo, ayudan a tener amigos y sanan el corazón.
</p>
</article>
<div id="video1"><video src="video/video_perro.mp4" autoplay muted loop type="video/mp4"></video></div>
<article id="contenedor_cita">
<p>“la grandeza de una nación y su progreso moral pueden ser juzgados por la forma en que se trata a sus animales”
<h4>Mahatma Gandhi</h4>
</p>
</article>
<div id="video2"><video src="video/video_gato.mp4" autoplay muted loop type="video/mp4"></video></div>
</section>
<div class="contenedor">
<h3>Buscando un hogar</h3>
<input type="radio" name="dot" id="one">
<input type="radio" name="dot" id="two">
<div class="tarjeta_principal">
<div class="tarjetas">
<div class="tarjeta">
<div class="contenido">
<div class="img">
<a href="pages/detalle_paco.html"><img src="img/img1.jpg" alt="cachorro marrón"></a>
</div>
<div class="details">
<div class="nombre">Paco</div>
</div>
</div>
</div>
<div class="tarjeta">
<div class="contenido">
<div class="img">
<a href="pages/detalle_sirio.html"><img src="img/img2.jpg" alt="gato siamés"></a>
</div>
<div class="details">
<div class="nombre">Sirio</div>
</div>
</div>
</div>
<div class="tarjeta">
<div class="contenido">
<div class="img">
<a href="pages/detalle_africa.html"><img src="img/img3.jpg" alt="cachorra negra"></a>
</div>
<div class="details">
<div class="nombre">África</div>
</div>
</div>
</div>
</div>
<div class="tarjetas">
<div class="tarjeta">
<div class="contenido">
<div class="img">
<a href="pages/detalle_tom.html"><img src="img/img4.jpg" alt="gato blanco y gris"></a>
</div>
<div class="details">
<div class="nombre">Tom</div>
</div>
</div>
</div>
<div class="tarjeta">
<div class="contenido">
<div class="img">
<a href="pages/detalle_oddie.html"><img src="img/img5.jpg" alt="perro adulto"></a>
</div>
<div class="details">
<div class="nombre">Oddie</div>
</div>
</div>
</div>
<div class="tarjeta">
<div class="contenido">
<div class="img">
<a href="pages/detalle_gea.html"><img src="img/img6.jpg" alt="gata adulta"></a>
</div>
<div class="details">
<div class="nombre">Gea</div>
</div>
</div>
</div>
</div>
</div>
<div class="button">
<label for="one" class=" active one"></label>
<label for="two" class="two"></label>
</div>
</div>
</body>
<footer>
<div id="contenedor_footer">
<div id="copyright">
<p>© Copyright 2022 Adopta una Mascota // Patrocinado por <a href="https://www.royalcanin.com/ar/">Royal Canin</a>
</div>
<div id="social">
<nav id="botonera_social">
<ul>
<li><a href="https://twitter.com/"><img src="img/ico/twitter.png" alt="Twitter"></a></li>
<li><a href="https://www.facebook.com/"><img src="img/ico/facebook.png" alt="facebook"></a></li>
<li><a href="https://www.instagram.com/"><img src="img/ico/instagram.png" alt="instagram"></a></li>
</ul>
</nav>
</div>
</div><!-- fin del contenedor -->
</footer>
Hiding element with max-width:0?
this flex property is messing with what you're attempting to do here
&__list-item {
text-align: center;
display: flex;
/* flex: 1; */
//if you comment out the line above it should work
fine i guess
justify-content: center;
align-items: center;
}
Related Topics
I Need Help Making CSS for a Modern Vertical Navbar
How to Scale Svg Path to Fit The Window Size
How to Change My Gwt Listbox Style
How to Map CSS and Js File from Jsp File in Spring Mvc
Color Change of Navigation Buttons When Scrolling Up and When Scrolling Down
CSS Attribute Selector Class Starts with But Not Equals To
Widths to Use in Media Queries
With Webpack, How to Generate CSS Only, Excluding The Output.Js
Using Bootstrap Cards as a Hyperlink
Why Is My Bootstrap Column Centered Instead of Left-Aligned
How to Select from Only One Table with Web::Scraper
Tailwindcss Not Using My Custom Class Inside Breakpoints
Detect If Computer Can Support 3D Transforms Properly
What Is The 'Best Practice' Way of Creating an Icon Button with an Svg