1

I have a <h2> tag above a <table> and a @media (max-width: 1500px) in my css file.

When the screen is small, the table overflows from its div, popping out further than the elements above. This is fine.

My problem is that Id like the <h2> above it to match the width of the table.

Please see the image for clarification.

html

<div id="notifications">
    <h2 class='notifications_heading'>Notifications</h2>
    <table class='notifications_table'>
        {% for notification in queryset %}
        <tr>
            <td>{{ notification.actor }}</td>
            <td>{{ notification.verb }}</td>
            <td>{{ notification.timestamp|date }}</td>
        </tr>
        {% endfor %}
    </table>
</div>

css

#notifications {
    float: right;
    width: 500px;
    position: relative;
    margin-right: -840px;
}

@media (max-width: 1500px) {

    #notifications {
        float: right;
        width: 260px;
        margin-right: -290px;
        margin-top: 290px;
    }
}

.notifications_heading {
    background: #79aec8;
}

.notifications_table {
    width: 100%;
  }

https://i.stack.imgur.com/FVKXN.jpg

Thank you.

horse
  • 479
  • 7
  • 25
  • an `h2` should not be a direct child of a `table`. your markup is invalid – zgood Jun 09 '20 at 22:25
  • Woops. Originally I had the h2 outside of the table and moved it in there when I was testing out different stuff. Ive moved it out again now but the problem remains – horse Jun 09 '20 at 22:27
  • 1
    You also have a extra `
    ` hiding at the end of this line:: `
    `
    – Ben Jun 09 '20 at 23:36
  • 1
    I think your `margin-right` on #notifications is messing up your layout. – Ben Jun 09 '20 at 23:53
  • Hey Ben Thanks for picking up on the extra div, that was also leftover from my experimenting. I removed the ```margin-right``` but unfortunately the ```

    ``` still didnt match the table when I resized the window. Thanks a lot for your suggestion though :)

    – horse Jun 11 '20 at 08:10

2 Answers2

0

This is the method I ended up going with (it may be more verbose than it could be, but I cant easily shorten it). It aligns nicely with the 'Recent actions' panel in Django Admin. Courtesy to Peter Josling Set div to have its siblings width.

<div id="notifications">
    <div class="wrapper">
        <div class="child">
            <div>
                <h2 class='notifications_heading'>Notifications</h2>
            </div>
        </div>
        <div>
            {% load notifications_tags %}

            <table class='notifications_table'>
                {% for notification in queryset %}
                <tr>
                    <td>{{ notification.actor }}</td>
                    <td>{{ notification.verb }}</td>
                        <td>{{ notification.timestamp|date }}</td>
                </tr>
            {% endfor %}
            </table>
        </div>
    </div> 
</div>
#notifications {
    float: right;
    width: 500px;
    position: relative;
    margin-right: -840px;
}

.notifications_table {
    width: 500px;
}

@media (max-width: 1500px) {

    #notifications {
        float: right;
        width: 260px;
        margin-right: -290px;
        margin-top: 290px;
    }

    .notifications_table {
        width: 100%;
    }
}

@media (max-width: 767px) {

    #notifications {
        width: 100%;
        margin: 0;
        padding-top: 30px;
    }

    .notifications_table {
        width: 100%;
    }

    .wrapper {
        width: 100%;
    }
}

/*  make <h2> adjust to fit table beneath */
.child {
    display: flex;
}

.child div {
    flex-grow: 1;
    width: 0;
}

.wrapper {
    display: inline-block;
}
horse
  • 479
  • 7
  • 25
0

There is no need for you to worry or make your code more complex at all... First of all your media query is working fine and so is the table, now the silly mistake which is stopping h2 from following the same nature is that you haven't specified its nature under the media query section of CSS...

Kindly add this in the h2 of media query section of your CSS:

.notification-heading {
  width: 100vw; 
}
Edric
  • 24,639
  • 13
  • 81
  • 91
Om_16
  • 696
  • 4
  • 13
  • Hello. Thank you very much. I think in most cases your answer would work perfectly, however my problem was very unusual. When resized to <767px the table moved into a div which was smaller than the table (the table would flow outside of the div). Therefore if I set ```.notification-heading{width: 100%;}``` the heading would adjust to fit the outer div, meaning it wouldnt expand to the entire size of the table. If you would like an image I can show you. Thanks very much for your help though :) – horse Jun 11 '20 at 10:03
  • I have edited few things according to the problem, so kindly check the edit section also because it might solve the problem – Om_16 Jun 11 '20 at 10:09